from header_common import *
from header_operations import *
from module_constants import *
from header_parties import *
from header_skills import *
from header_mission_templates import *
from header_items import *
from header_triggers import *
from header_terrain_types import *
from header_music import *
from header_map_icons import *
from header_presentations import *
from ID_animations import *
from multi_scripts import *
from module_items import items


####################################################################################################################
# scripts is a list of script records.
# Each script record contns the following two fields:
# 1) Script id: The prefix "script_" will be inserted when referencing scripts.
# 2) Operation block: This must be a valid operation block. See header_operations.py for reference.
####################################################################################################################


edited_native_scripts = [
  
  #script_game_start:
  # This script is called when a new game is started
  # INPUT: none
  ("game_start",
    [
      
      #VC variable init
      (assign, "$game_started_with_content_update", 1), #add-ons
      (assign, "$game_started_in_version", vc_version),
      (assign, "$g_spouse_embarazada", 0), #childs
      (assign, "$g_player_minister", -1),
      (assign,"$g_nino_varon",0),
      (assign,"$g_nina_chica",0),
      (assign, "$first_time", first_time_check_lairs|first_time_check_l_lairs|first_time_check_l2_lairs|first_time_food_store|first_time_fix_ports|first_time_fix_home_center), #avoid applying unnecessary fixes from game_load trigger
      (assign, "$g_player_faith", 1), #religion player chief, cristiano por defecto
      (assign, "$g_campaign_death", 0),
      (assign, "$g_player_coffers", 0), #simple bank system
      (assign, "$g_player_conversions", 0),
      (assign, "$sabe_leer", 0), #no sabe leer de no ser que lo cambie character creation chief
      (assign, "$player_trait", 0), #player traits chief
      (assign, "$player_side", 0), #mainquest side part chief
      (assign, "$kennemer_revenge", 0), #kennemer and solveig mainquest
      (assign, "$premio_minister", 0), #premio a ministro
      (assign, "$control_tax", 0),
      (assign, "$faith_conversion", 0), #control conversions for minister
      (assign, "$tax_rate", 0), #normal tax rate
      (assign, "$g_trainerlair_training_center2", -1),
      (assign, "$g_trainerlair_training_center", -1),
      (assign, "$choose_duel_troop", 0), #duel troops troops rebelion party rebelion
      (assign,"$g_player_prisoner_released", 0),
      (assign, "$druid_on", 0), #druids stone circle last druid
      (assign, "$rumors_inquired", 0), #rumors
      (assign, "$event_oneuse", 0), #random events juices juicios
      (assign, "$special1", 0), #
      #(assign, "$farmstead_boss_tax", 0), #farmstead
      #(assign, "$farmstead_boss_sell", 0), #farmstead
      #(assign, "$village_elder_tax", 0), #village extortion
      (assign, "$first_time variable", 0), #no conversation repit in 90 hours
      (assign, "$g_lord_hide", 0),  #hide lords in campaign
      (assign, "$g_random_eventnorepit", 0),  #no repit some random events
      (assign, "$g_no_some_options", 0),  #usable, always finish in 0
      (assign, "$FormAI_autorotate", 1),
      (assign, "$current_color", 0xE12126),
      
      ##difficult settings
      (assign, "$difficulty_type", camp_d1),
      (assign, "$g_multiplayer_allow_shield_bash", 1),
      (assign, "$stamina_on", 0),
      (assign, "$g_armor_penalties", 0),
      (assign, "$moralep_on", 0),
      (assign, "$insanedamage_on", 0),
      (assign, "$recruitment_on", 0), #recruitment easy on
      (assign, "$easy_levelling", 0),
      (assign, "$easy_wage", 0),
      (assign, "$bandit_quantity_option", 1),	#0 = 2/3, 1 = 1, 2 = 3/2
      (options_set_damage_to_player, 0),	#0 = 1/4, 1 = 1/2, 2 = 1/1
      (options_set_damage_to_friends, 0),	#0 = 1/2, 1 = 3/4, 2 = 1/1
      (options_set_combat_ai, 2),		#0 = good, 1 = average, 2 = poor
      (options_set_campaign_ai, 2),	#0 = good, 1 = average, 2 = poor
      (options_set_combat_speed, 2),	#0 = slowest, 1 = slower, 2 = normal, 3 = faster, 4 = fastest
      (options_set_battle_size, 0),	#To stop complains of people with slow pc
      (assign, "$goredec_on", 0), #gore off
      (assign, "$dog_companion", 0),
      (assign, "$ambient_music_in_battle", 0),
      
      #for debug log
      (str_clear, s1),
      (troop_set_plural_name, "trp_pseudo_troop_01", s1),#economy
      (troop_set_plural_name, "trp_pseudo_troop_02", s1),#player army
      (troop_set_plural_name, "trp_pseudo_troop_03", s1),#bandits
      (store_random_in_range, "$savegame_id", 1, 999999),
      (assign,"$num_selled_goods_in_week", 0),
      (assign, "$selled_goods_in_week_for", 0),
      (assign, "$num_selled_items_in_week", 0),
      (assign, "$selled_items_in_week_for", 0),
      (assign, "$num_buyed_goods_in_week", 0),
      (assign, "$buyed_goods_in_week_for", 0),
      (assign, "$num_buyed_items_in_week", 0),
      (assign, "$buyed_items_in_week_for", 0),
      (assign, "$g_sgfix", 0),
      (assign, "$arboleda_conrey", 0),
      (assign, "$continue_storyline", 0),
      
      #scene quest
      (assign, "$on_map", 0),
      (assign, "$g_odin_cave_visited", 0),
      (assign, "$g_roman_ruins_visited", 0),
      (assign, "$g_hadrianwall_visited", 0),
      (assign,"$circle_mystic",0),
      (assign,"$troll_bridge",0),
      (assign,"$roman_fort_riddles",0),
      (assign,"$farmland_ambush",0),
      (assign,"$battle_stones",0),
      (assign,"$battle_type",0), #chief
      #camp system
      (assign, "$send_scouts", 0),
      (assign, "$foragers_a", 0),
      (assign, "$fortified_camp", 0),
      (assign,"$chest_broken",0), #camp chest
      (assign,"$player_ambushed",0), #ambush chief
      
      #companions conversations camp chief
      (try_for_range,":current_npc",companions_begin,companions_end),
        (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 0),
        (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversed, 0),
      (try_end),
      (assign,"$romance_target",-1), #romance variable
      
      ###sandbox campaign special scenes
      (try_begin),
        (neq, "$campaign_type", camp_storyline), #no in storyline mode chief
        (enable_party, "p_odin_cave"), #odin cave
        (enable_party, "p_boar_grove"),
      (try_end),
      ###
      
      #messenger system
      (assign,"$message_target",-1), #lord
      (assign,"$message_type",-1), #type
      (assign,"$message_distance",-1), #distance
      (assign,"$message_place",-1), #place
      (assign, "$message_target_type", -1),  # 1 player's kingdom lord , 2 no player's kingdom lord , 3 King of other kingdom
      (assign,"$message_sent",0), #message ok
      
      #saqueo para centers. Siege warfare
      (try_for_range, ":center_no", centers_begin, centers_end),
        (party_set_slot,":center_no",recruit_permission_need, 1),
        (party_set_slot,":center_no",slot_center_blockaded,0),
        (party_set_slot,":center_no",slot_center_blockaded_time,0),
        (party_set_slot, ":center_no", slot_center_mantlets_placed, 0),
        (party_set_slot,":center_no",slot_center_ladder_time,0),
        (party_set_slot,":center_no",slot_center_latrines,0),
        (party_set_slot,":center_no",slot_center_infiltration_type,0),
        (party_set_slot, ":center_no", slot_center_enslaved, 0),
        (party_set_slot, ":center_no", slot_center_accumulated_tariffs, 0),
        (party_set_slot, ":center_no", slot_center_accumulated_rents, 0),
        (party_set_slot, ":center_no", slot_center_tax_rate, 0),
        (party_set_slot, ":center_no", slot_center_last_spotted_enemy, -1),
        (party_set_slot, ":center_no", slot_center_is_besieged_by, -1),
        (party_set_slot, ":center_no", slot_center_last_taken_by_troop, -1),
        (party_set_slot, ":center_no", slot_center_racket, 0),
      (try_end),
      # Setup special places
      (try_for_range, ":center", special_places_begin, special_places_end),
        (party_set_slot, ":center", slot_center_fee_paid, 0),
      (try_end),
      (try_for_range, ":center", "p_quarry1", "p_hadrian_wall1"),
        (party_set_slot, ":center", slot_center_inventory, 0),
      (try_end),
      (try_for_range, ":center", "p_monasterio1", "p_yourlair"),
        (party_set_slot, ":center", slot_center_inventory, 0),
      (try_end),
      (try_for_range, ":farm", "p_farmsteadsp1", "p_hadrian_wall1"),
        (party_set_slot,  ":farm",slot_center_player_cattle, 0),
      (try_end),
      (try_for_range, ":center", villages_begin, villages_end),
        (party_set_slot, ":center", slot_center_fee_paid, 0),
      (try_end),
      # End set special places
      ###recrutar sailors to old captain in port chief
      (try_for_range, ":center_no", towns_begin, towns_end),
        (party_slot_eq,":center_no",slot_town_port, 1),
        (party_set_slot, ":center_no", slot_center_sailors_troop_amount, 1),
        #	(try_for_range, ":center_no", centers_begin, centers_end),
        #		(party_set_slot, ":center_no", slot_center_sailors_troop_amount, 1),
      (try_end),
      
      ####chief variables acaba
      
      (faction_set_slot, "fac_player_supporters_faction", slot_faction_state, sfs_inactive),
      (assign, "$g_player_luck", 200),
      (troop_set_slot, "trp_player", slot_troop_occupation, slto_kingdom_hero),
      (store_random_in_range, ":starting_training_ground", training_grounds_begin, training_grounds_end),
      (party_relocate_near_party, "p_main_party", ":starting_training_ground", 3),
      (str_store_troop_name, s5, "trp_player"),
      (party_set_name, "p_main_party", s5),
      (call_script, "script_update_party_creation_random_limits"),
      (assign, "$g_player_party_icon", -1),
      
      #Warband changes begin -- set this early
      (try_for_range, ":npc", 0, kingdom_ladies_end),
        (this_or_next|eq, ":npc", "trp_player"),
        (is_between, ":npc", active_npcs_begin, kingdom_ladies_end),
        (troop_set_slot, ":npc", slot_troop_father, -1),
        (troop_set_slot, ":npc", slot_troop_mother, -1),
        (troop_set_slot, ":npc", slot_troop_guardian, -1),
        (troop_set_slot, ":npc", slot_troop_spouse, -1),
        (troop_set_slot, ":npc", slot_troop_betrothed, -1),
        (troop_set_slot, ":npc", slot_troop_prisoner_of_party, -1),
        (troop_set_slot, ":npc", slot_lady_last_suitor, -1),
        (troop_set_slot, ":npc", slot_troop_stance_on_faction_issue, -1),
        (troop_set_slot, ":npc", slot_troop_refused, 0),
        (troop_set_slot, ":npc", slot_troop_tortured, 0),
        (troop_set_slot, ":npc", slot_troop_robbed, 0),
        (troop_set_slot, ":npc", slot_troop_temp, 0),
        (troop_set_slot, ":npc", slot_troop_mission_object, -1),
        (troop_set_slot, ":npc", slot_troop_message_sent, 0),
        (troop_set_slot, ":npc", slot_troop_marriage_time, 0),
        (store_random_in_range, ":decision_seed", 0, 10000),
        (troop_set_slot, ":npc", slot_troop_set_decision_seed, ":decision_seed"),	#currently not used
        (troop_set_slot, ":npc", slot_troop_temp_decision_seed, ":decision_seed"),	#currently not used, holds for at least 24 hours
      (try_end),
      
      (assign, "$g_lord_long_term_count", 0),
      
      (call_script, "script_initialize_banner_info"),
      (call_script, "script_initialize_item_info"),
      (call_script, "script_initialize_aristocracy"),
      (call_script, "script_initialize_npcs"),
      (assign, "$disable_npc_complaints", 0),
      #NPC companion changes end
      
      # Setting random feast time
      (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
        (store_random_in_range, ":last_feast_time", 0, 312), #240 + 72
        (val_mul, ":last_feast_time", -1),
        (faction_set_slot, ":faction_no", slot_faction_last_feast_start_time, ":last_feast_time"),
      (try_end),
      
      # Setting the random town sequence:
      (store_sub, ":num_towns", towns_end, towns_begin),
      (assign, ":num_iterations", ":num_towns"),
      (try_for_range, ":cur_town_no", 0, ":num_towns"),
        (troop_set_slot, "trp_random_town_sequence", ":cur_town_no", -1),
      (try_end),
      (assign, ":cur_town_no", 0),
      (try_for_range, ":unused", 0, ":num_iterations"),
        (store_random_in_range, ":random_no", 0, ":num_towns"),
        (assign, ":is_unique", 1),
        (try_for_range, ":cur_town_no_2", 0, ":num_towns"),
          (troop_slot_eq, "trp_random_town_sequence", ":cur_town_no_2", ":random_no"),
          (assign, ":is_unique", 0),
        (try_end),
        (try_begin),
          (eq, ":is_unique", 1),
          (troop_set_slot, "trp_random_town_sequence", ":cur_town_no", ":random_no"),
          (val_add, ":cur_town_no", 1),
        (else_try),
          (val_add, ":num_iterations", 1),
        (try_end),
      (try_end),
      
      # Cultures:
      (faction_set_slot, "fac_culture_norse",  slot_faction_tier_1_troop, "trp_norse_slave"),
      (faction_set_slot, "fac_culture_norse",  slot_faction_tier_2_troop, "trp_norse_level0_landed"),
      (faction_set_slot, "fac_culture_norse",  slot_faction_tier_3_troop, "trp_norse_level1_landed"),
      (faction_set_slot, "fac_culture_norse",  slot_faction_tier_4_troop, "trp_norse_level2_landed"),
      (faction_set_slot, "fac_culture_norse",  slot_faction_tier_5_troop, "trp_norse_level3_landed"),
      
      (faction_set_slot, "fac_culture_saxon", slot_faction_tier_1_troop, "trp_saxon_slave"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_tier_2_troop, "trp_saxon_level0_landed"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_tier_3_troop, "trp_saxon_level1_landed"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_tier_4_troop, "trp_saxon_level2_landed"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_tier_5_troop, "trp_saxon_level3_landed"),
      
      (faction_set_slot, "fac_culture_angle", slot_faction_tier_1_troop, "trp_angle_slave"),
      (faction_set_slot, "fac_culture_angle", slot_faction_tier_2_troop, "trp_angle_level0_landed"),
      (faction_set_slot, "fac_culture_angle", slot_faction_tier_3_troop, "trp_angle_level1_landed"),
      (faction_set_slot, "fac_culture_angle", slot_faction_tier_4_troop, "trp_angle_level2_landed"),
      (faction_set_slot, "fac_culture_angle", slot_faction_tier_5_troop, "trp_angle_level3_landed"),
      
      (faction_set_slot, "fac_culture_welsh",  slot_faction_tier_1_troop, "trp_briton_slave"),
      (faction_set_slot, "fac_culture_welsh",  slot_faction_tier_2_troop, "trp_briton_level0_landed"),
      (faction_set_slot, "fac_culture_welsh",  slot_faction_tier_3_troop, "trp_briton_level1_landed"),
      (faction_set_slot, "fac_culture_welsh",  slot_faction_tier_4_troop, "trp_briton_marksman"),
      (faction_set_slot, "fac_culture_welsh",  slot_faction_tier_5_troop, "trp_briton_level2_landed"),
      
      (faction_set_slot, "fac_culture_irish", slot_faction_tier_1_troop, "trp_irish_slave"),
      (faction_set_slot, "fac_culture_irish", slot_faction_tier_2_troop, "trp_irish_level0_landed"),
      (faction_set_slot, "fac_culture_irish", slot_faction_tier_3_troop, "trp_irish_level1_landed"),
      (faction_set_slot, "fac_culture_irish", slot_faction_tier_4_troop, "trp_irish_level2_landed"),
      (faction_set_slot, "fac_culture_irish", slot_faction_tier_5_troop, "trp_irish_level3_landed"),
      
      (faction_set_slot, "fac_culture_scotch", slot_faction_tier_1_troop, "trp_scotch_peasant"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_tier_2_troop, "trp_scotch_level0_landed"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_tier_3_troop, "trp_scotch_level1_landed"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_tier_4_troop, "trp_scotch_level1_skirmisher"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_tier_5_troop, "trp_scotch_level2_landed"),
      
      (faction_set_slot, "fac_culture_norse", slot_faction_town_walker_male_troop, "trp_town_walker_7"),
      (faction_set_slot, "fac_culture_norse", slot_faction_town_walker_female_troop, "trp_town_walker_8"),
      (faction_set_slot, "fac_culture_norse", slot_faction_village_walker_male_troop, "trp_village_walker_7"),
      (faction_set_slot, "fac_culture_norse", slot_faction_village_walker_female_troop, "trp_village_walker_8"),
      (faction_set_slot, "fac_culture_norse", slot_faction_town_spy_male_troop, "trp_spy_walker_1"),
      (faction_set_slot, "fac_culture_norse", slot_faction_town_spy_female_troop, "trp_spy_walker_2"),
      
      (faction_set_slot, "fac_culture_saxon", slot_faction_town_walker_male_troop, "trp_town_walker_5"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_town_walker_female_troop, "trp_town_walker_6"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_village_walker_male_troop, "trp_village_walker_5"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_village_walker_female_troop, "trp_village_walker_6"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_town_spy_male_troop, "trp_spy_walker_1"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_town_spy_female_troop, "trp_spy_walker_2"),
      
      (faction_set_slot, "fac_culture_angle", slot_faction_town_walker_male_troop, "trp_town_walker_5"),
      (faction_set_slot, "fac_culture_angle", slot_faction_town_walker_female_troop, "trp_town_walker_6"),
      (faction_set_slot, "fac_culture_angle", slot_faction_village_walker_male_troop, "trp_village_walker_5"),
      (faction_set_slot, "fac_culture_angle", slot_faction_village_walker_female_troop, "trp_village_walker_6"),
      (faction_set_slot, "fac_culture_angle", slot_faction_town_spy_male_troop, "trp_spy_walker_1"),
      (faction_set_slot, "fac_culture_angle", slot_faction_town_spy_female_troop, "trp_spy_walker_2"),
      
      (faction_set_slot, "fac_culture_welsh", slot_faction_town_walker_male_troop, "trp_town_walker_1"),
      (faction_set_slot, "fac_culture_welsh", slot_faction_town_walker_female_troop, "trp_town_walker_2"),
      (faction_set_slot, "fac_culture_welsh", slot_faction_village_walker_male_troop, "trp_village_walker_1"),
      (faction_set_slot, "fac_culture_welsh", slot_faction_village_walker_female_troop, "trp_village_walker_2"),
      (faction_set_slot, "fac_culture_welsh", slot_faction_town_spy_male_troop, "trp_spy_walker_1"),
      (faction_set_slot, "fac_culture_welsh", slot_faction_town_spy_female_troop, "trp_spy_walker_2"),
      
      (faction_set_slot, "fac_culture_irish", slot_faction_town_walker_male_troop, "trp_town_walker_3"),
      (faction_set_slot, "fac_culture_irish", slot_faction_town_walker_female_troop, "trp_town_walker_4"),
      (faction_set_slot, "fac_culture_irish", slot_faction_village_walker_male_troop, "trp_village_walker_3"),
      (faction_set_slot, "fac_culture_irish", slot_faction_village_walker_female_troop, "trp_village_walker_4"),
      (faction_set_slot, "fac_culture_irish", slot_faction_town_spy_male_troop, "trp_spy_walker_1"),
      (faction_set_slot, "fac_culture_irish", slot_faction_town_spy_female_troop, "trp_spy_walker_2"),
      
      (faction_set_slot, "fac_culture_scotch", slot_faction_town_walker_male_troop, "trp_town_walker_3"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_town_walker_female_troop, "trp_town_walker_4"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_village_walker_male_troop, "trp_village_walker_3"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_village_walker_female_troop, "trp_village_walker_4"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_town_spy_male_troop, "trp_spy_walker_1"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_town_spy_female_troop, "trp_spy_walker_2"),
      #chief acaba
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (assign, reg3, "$cheat_mode"),
        (display_message, "@{!}DEBUG : Completed faction troop assignments, cheat mode: {reg3}"),
      (try_end),
      
      # Factions:
      (faction_set_slot, "fac_kingdom_1",  slot_faction_culture, "fac_culture_norse"),
      (faction_set_slot, "fac_kingdom_1",  slot_faction_leader, "trp_kingdom_1_lord"),
      (troop_set_slot, "trp_kingdom_1_lord", slot_troop_renown, 1200),
      
      (faction_set_slot, "fac_kingdom_2",  slot_faction_culture, "fac_culture_norse"),
      (faction_set_slot, "fac_kingdom_2",  slot_faction_leader, "trp_kingdom_2_lord"),
      (troop_set_slot, "trp_kingdom_2_lord", slot_troop_renown, 1300),
      
      (faction_set_slot, "fac_kingdom_3",  slot_faction_culture, "fac_culture_norse"),
      (faction_set_slot, "fac_kingdom_3",  slot_faction_leader, "trp_kingdom_3_lord"),
      (troop_set_slot, "trp_kingdom_3_lord", slot_troop_renown, 1300),
      
      (faction_set_slot, "fac_kingdom_4",  slot_faction_culture, "fac_culture_norse"),
      (faction_set_slot, "fac_kingdom_4",  slot_faction_leader, "trp_kingdom_4_lord"),
      (troop_set_slot, "trp_kingdom_4_lord", slot_troop_renown, 1000),
      
      (faction_set_slot, "fac_kingdom_5",  slot_faction_culture, "fac_culture_saxon"),
      (faction_set_slot, "fac_kingdom_5",  slot_faction_leader, "trp_kingdom_5_lord"),
      (troop_set_slot, "trp_kingdom_5_lord", slot_troop_renown, 1800),
      
      (faction_set_slot, "fac_kingdom_6",  slot_faction_culture, "fac_culture_angle"),
      (faction_set_slot, "fac_kingdom_6",  slot_faction_leader, "trp_kingdom_6_lord"),
      (troop_set_slot, "trp_kingdom_6_lord", slot_troop_renown, 1000),
      
      (faction_set_slot, "fac_kingdom_7",  slot_faction_culture, "fac_culture_angle"),
      (faction_set_slot, "fac_kingdom_7",  slot_faction_leader, "trp_kingdom_7_lord"),
      (troop_set_slot, "trp_kingdom_7_lord", slot_troop_renown, 1200),
      
      (faction_set_slot, "fac_kingdom_8",  slot_faction_culture, "fac_culture_norse"),
      (faction_set_slot, "fac_kingdom_8",  slot_faction_leader, "trp_kingdom_8_lord"),
      (troop_set_slot, "trp_kingdom_8_lord", slot_troop_renown, 1600),
      
      (faction_set_slot, "fac_kingdom_9",  slot_faction_culture, "fac_culture_welsh"),
      (faction_set_slot, "fac_kingdom_9",  slot_faction_leader, "trp_kingdom_9_lord"),
      (troop_set_slot, "trp_kingdom_9_lord", slot_troop_renown, 1400),
      
      (faction_set_slot, "fac_kingdom_10",  slot_faction_culture, "fac_culture_welsh"),
      (faction_set_slot, "fac_kingdom_10",  slot_faction_leader, "trp_kingdom_10_lord"),
      (troop_set_slot, "trp_kingdom_10_lord", slot_troop_renown, 1000),
      
      (faction_set_slot, "fac_kingdom_11",  slot_faction_culture, "fac_culture_welsh"),
      (faction_set_slot, "fac_kingdom_11",  slot_faction_leader, "trp_kingdom_11_lord"),
      (troop_set_slot, "trp_kingdom_11_lord", slot_troop_renown, 1200),
      
      (faction_set_slot, "fac_kingdom_12",  slot_faction_culture, "fac_culture_welsh"),
      (faction_set_slot, "fac_kingdom_12",  slot_faction_leader, "trp_kingdom_12_lord"),
      (troop_set_slot, "trp_kingdom_12_lord", slot_troop_renown, 1200),
      
      (faction_set_slot, "fac_kingdom_13",  slot_faction_culture, "fac_culture_welsh"),
      (faction_set_slot, "fac_kingdom_13",  slot_faction_leader, "trp_kingdom_13_lord"),
      (troop_set_slot, "trp_kingdom_13_lord", slot_troop_renown, 1100),
      
      (faction_set_slot, "fac_kingdom_14",  slot_faction_culture, "fac_culture_irish"),
      (faction_set_slot, "fac_kingdom_14",  slot_faction_leader, "trp_kingdom_14_lord"),
      (troop_set_slot, "trp_kingdom_14_lord", slot_troop_renown, 1100),
      
      (faction_set_slot, "fac_kingdom_15",  slot_faction_culture, "fac_culture_irish"),
      (faction_set_slot, "fac_kingdom_15",  slot_faction_leader, "trp_kingdom_15_lord"),
      (troop_set_slot, "trp_kingdom_15_lord", slot_troop_renown, 1200),
      
      (faction_set_slot, "fac_kingdom_16",  slot_faction_culture, "fac_culture_irish"),
      (faction_set_slot, "fac_kingdom_16",  slot_faction_leader, "trp_kingdom_16_lord"),
      (troop_set_slot, "trp_kingdom_16_lord", slot_troop_renown, 1200),
      
      (faction_set_slot, "fac_kingdom_17",  slot_faction_culture, "fac_culture_irish"),
      (faction_set_slot, "fac_kingdom_17",  slot_faction_leader, "trp_kingdom_17_lord"),
      (troop_set_slot, "trp_kingdom_17_lord", slot_troop_renown, 1200),
      
      (faction_set_slot, "fac_kingdom_18",  slot_faction_culture, "fac_culture_irish"),
      (faction_set_slot, "fac_kingdom_18",  slot_faction_leader, "trp_kingdom_18_lord"),
      (troop_set_slot, "trp_kingdom_18_lord", slot_troop_renown, 1400),
      
      (faction_set_slot, "fac_kingdom_19",  slot_faction_culture, "fac_culture_irish"),
      (faction_set_slot, "fac_kingdom_19",  slot_faction_leader, "trp_kingdom_19_lord"),
      (troop_set_slot, "trp_kingdom_19_lord", slot_troop_renown, 1100),
      
      (faction_set_slot, "fac_kingdom_20",  slot_faction_culture, "fac_culture_scotch"),
      (faction_set_slot, "fac_kingdom_20",  slot_faction_leader, "trp_kingdom_20_lord"),
      (troop_set_slot, "trp_kingdom_20_lord", slot_troop_renown, 1200),
      
      (faction_set_slot, "fac_kingdom_21",  slot_faction_culture, "fac_culture_irish"),
      (faction_set_slot, "fac_kingdom_21",  slot_faction_leader, "trp_kingdom_21_lord"),
      (troop_set_slot, "trp_kingdom_21_lord", slot_troop_renown, 1400),
      
      (faction_set_slot, "fac_adventurers",  slot_faction_leader, -1),
      (faction_set_slot, "fac_adventurers", slot_faction_marshal, -1),
      
      (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
        (faction_set_slot, ":faction_no", slot_faction_marshal, -1),
      (try_end),
      (faction_set_slot, "fac_player_supporters_faction", slot_faction_marshal, "trp_player"),
      (call_script, "script_initialize_faction_troop_types"),
      #Farmsteads
      (try_for_range, ":farm", "p_farmsteadsp1", "p_hadrian_wall1"),
        (store_random_in_range, ":rand", 120, 250),
        (party_set_slot, ":farm", slot_center_head_cattle, ":rand"),
      (try_end),
      
      # Towns:
      (try_for_range, ":item_no", trade_goods_begin, trade_goods_end),
        (store_sub, ":offset", ":item_no", trade_goods_begin),
        (val_add, ":offset", slot_town_trade_good_prices_begin),
        (try_for_range, ":center_no", centers_begin, centers_end),
          (party_set_slot, ":center_no", ":offset", average_price_factor), #1000
        (try_end),
      (try_end),
      
      #version check
      (try_begin),
        (assign, reg0, 0),
        (get_operation_set_version, reg0),
        (ge, reg0, warband_version),
        
        (call_script, "script_initialize_trade_routes"),
        (call_script, "script_initialize_town_arena_info"),
        #start some tournaments
        (try_for_range, ":town_no", towns_begin, towns_end),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", 20),
          (store_random_in_range, ":random_days", 12, 15),
          (party_set_slot, ":town_no", slot_town_has_tournament, ":random_days"),
        (try_end),
        
        #Give centers to factions first, to ensure more equal distributions
        #chief da ciudades
        (call_script, "script_give_center_to_faction_aux", "p_town_1", "fac_kingdom_5"),
        (call_script, "script_give_center_to_faction_aux", "p_town_2", "fac_kingdom_5"),
        (call_script, "script_give_center_to_faction_aux", "p_town_3", "fac_kingdom_8"),
        (call_script, "script_give_center_to_faction_aux", "p_town_4", "fac_kingdom_1"),
        (call_script, "script_give_center_to_faction_aux", "p_town_5", "fac_kingdom_2"),
        (call_script, "script_give_center_to_faction_aux", "p_town_6", "fac_kingdom_11"),
        (call_script, "script_give_center_to_faction_aux", "p_town_7", "fac_kingdom_3"),
        (call_script, "script_give_center_to_faction_aux", "p_town_8", "fac_kingdom_7"),
        (call_script, "script_give_center_to_faction_aux", "p_town_9", "fac_kingdom_13"),
        (call_script, "script_give_center_to_faction_aux", "p_town_10", "fac_kingdom_8"),
        (call_script, "script_give_center_to_faction_aux", "p_town_11", "fac_kingdom_4"),
        (call_script, "script_give_center_to_faction_aux", "p_town_12", "fac_kingdom_7"),
        (call_script, "script_give_center_to_faction_aux", "p_town_13", "fac_kingdom_9"),
        (call_script, "script_give_center_to_faction_aux", "p_town_14", "fac_kingdom_6"),
        (call_script, "script_give_center_to_faction_aux", "p_town_15", "fac_kingdom_20"),
        (call_script, "script_give_center_to_faction_aux", "p_town_16", "fac_kingdom_5"),
        (call_script, "script_give_center_to_faction_aux", "p_town_17", "fac_kingdom_12"),
        (call_script, "script_give_center_to_faction_aux", "p_town_18", "fac_kingdom_21"),
        (call_script, "script_give_center_to_faction_aux", "p_town_19", "fac_kingdom_20"),
        (call_script, "script_give_center_to_faction_aux", "p_town_20", "fac_kingdom_14"),
        (call_script, "script_give_center_to_faction_aux", "p_town_21", "fac_kingdom_15"),
        (call_script, "script_give_center_to_faction_aux", "p_town_22", "fac_kingdom_9"),
        
        (call_script, "script_give_center_to_faction_aux", "p_town_23", "fac_kingdom_7"),
        (call_script, "script_give_center_to_faction_aux", "p_town_24", "fac_kingdom_16"),
        (call_script, "script_give_center_to_faction_aux", "p_town_25", "fac_kingdom_18"),
        (call_script, "script_give_center_to_faction_aux", "p_town_26", "fac_kingdom_10"),
        (call_script, "script_give_center_to_faction_aux", "p_town_27", "fac_kingdom_8"),
        (call_script, "script_give_center_to_faction_aux", "p_town_28", "fac_kingdom_17"),
        (call_script, "script_give_center_to_faction_aux", "p_town_29", "fac_kingdom_19"),
        #chief da castillos
        #northumbria
        (call_script, "script_give_center_to_faction_aux", "p_castle_1", "fac_kingdom_8"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_2", "fac_kingdom_8"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_3", "fac_kingdom_8"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_4", "fac_kingdom_8"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_5", "fac_kingdom_8"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_6", "fac_kingdom_8"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_7", "fac_kingdom_8"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_8", "fac_kingdom_8"),
        #mercia
        (call_script, "script_give_center_to_faction_aux", "p_castle_9", "fac_kingdom_7"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_10", "fac_kingdom_7"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_11", "fac_kingdom_7"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_12", "fac_kingdom_7"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_13", "fac_kingdom_7"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_14", "fac_kingdom_7"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_15", "fac_kingdom_7"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_16", "fac_kingdom_7"),
        #east engle
        (call_script, "script_give_center_to_faction_aux", "p_castle_17", "fac_kingdom_6"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_18", "fac_kingdom_6"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_19", "fac_kingdom_6"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_20", "fac_kingdom_6"),
        #wessex
        (call_script, "script_give_center_to_faction_aux", "p_castle_21", "fac_kingdom_5"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_22", "fac_kingdom_5"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_23", "fac_kingdom_5"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_24", "fac_kingdom_5"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_25", "fac_kingdom_5"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_26", "fac_kingdom_5"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_27", "fac_kingdom_5"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_28", "fac_kingdom_5"),
        #Cornubia
        (call_script, "script_give_center_to_faction_aux", "p_castle_29", "fac_kingdom_12"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_30", "fac_kingdom_12"),
        #alt clut
        (call_script, "script_give_center_to_faction_aux", "p_castle_31", "fac_kingdom_11"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_32", "fac_kingdom_11"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_33", "fac_kingdom_11"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_34", "fac_kingdom_11"),
        #Alban
        (call_script, "script_give_center_to_faction_aux", "p_castle_35", "fac_kingdom_20"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_36", "fac_kingdom_20"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_37", "fac_kingdom_20"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_38", "fac_kingdom_20"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_39", "fac_kingdom_20"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_40", "fac_kingdom_20"),
        #gwynedd
        (call_script, "script_give_center_to_faction_aux", "p_castle_41", "fac_kingdom_9"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_42", "fac_kingdom_9"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_43", "fac_kingdom_9"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_44", "fac_kingdom_9"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_45", "fac_kingdom_9"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_46", "fac_kingdom_9"),
        #Glywyssing
        (call_script, "script_give_center_to_faction_aux", "p_castle_47", "fac_kingdom_13"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_48", "fac_kingdom_13"),
        #denmark
        (call_script, "script_give_center_to_faction_aux", "p_castle_49", "fac_kingdom_1"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_50", "fac_kingdom_1"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_51", "fac_kingdom_1"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_52", "fac_kingdom_1"),
        #norwey
        (call_script, "script_give_center_to_faction_aux", "p_castle_53", "fac_kingdom_2"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_54", "fac_kingdom_2"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_55", "fac_kingdom_2"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_56", "fac_kingdom_2"),
        #Laithind or isles
        (call_script, "script_give_center_to_faction_aux", "p_castle_57", "fac_kingdom_3"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_58", "fac_kingdom_3"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_59", "fac_kingdom_3"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_60", "fac_kingdom_3"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_61", "fac_kingdom_3"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_62", "fac_kingdom_3"),
        #frisia
        (call_script, "script_give_center_to_faction_aux", "p_castle_63", "fac_kingdom_4"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_64", "fac_kingdom_4"),
        #Uladh
        (call_script, "script_give_center_to_faction_aux", "p_castle_65", "fac_kingdom_14"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_66", "fac_kingdom_14"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_67", "fac_kingdom_14"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_68", "fac_kingdom_14"),
        #Aileach
        (call_script, "script_give_center_to_faction_aux", "p_castle_69", "fac_kingdom_18"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_70", "fac_kingdom_18"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_71", "fac_kingdom_18"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_72", "fac_kingdom_18"),
        #Meath
        (call_script, "script_give_center_to_faction_aux", "p_castle_73", "fac_kingdom_19"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_74", "fac_kingdom_19"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_75", "fac_kingdom_19"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_76", "fac_kingdom_19"),
        #Connachta
        (call_script, "script_give_center_to_faction_aux", "p_castle_77", "fac_kingdom_17"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_78", "fac_kingdom_17"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_79", "fac_kingdom_17"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_80", "fac_kingdom_17"),
        #Mumain
        (call_script, "script_give_center_to_faction_aux", "p_castle_81", "fac_kingdom_16"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_82", "fac_kingdom_16"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_83", "fac_kingdom_16"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_84", "fac_kingdom_16"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_85", "fac_kingdom_16"),
        #Laigin
        (call_script, "script_give_center_to_faction_aux", "p_castle_86", "fac_kingdom_15"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_87", "fac_kingdom_15"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_88", "fac_kingdom_15"),
        (call_script, "script_give_center_to_faction_aux", "p_castle_89", "fac_kingdom_15"),
        #Osrige
        (call_script, "script_give_center_to_faction_aux", "p_castle_90", "fac_kingdom_21"),
        #(call_script, "script_give_center_to_faction_aux", "p_castle_91", "fac_kingdom_21"),
        
        #village products -- at some point we might make it so that the villages supply raw materials to towns, and the towns produce manufactured goods
        #village products designate the raw materials produced in the vicinity
        #right now, just doing a test for grain produced in the swadian heartland
        
        
        # fill_village_bound_centers
        
        #pass 0: critical villages #phaiak
        (call_script, "script_give_village_to_center", "p_village_94", "p_town_2"),
        (call_script, "script_give_village_to_center", "p_village_95", "p_town_2"),
        (call_script, "script_give_village_to_center", "p_village_96", "p_town_3"),
        (call_script, "script_give_village_to_center", "p_village_97", "p_town_3"),
        (call_script, "script_give_village_to_center", "p_village_102", "p_town_6"),
        (call_script, "script_give_village_to_center", "p_village_103", "p_town_6"),
        (call_script, "script_give_village_to_center", "p_village_104", "p_town_7"),
        (call_script, "script_give_village_to_center", "p_village_105", "p_town_7"),
        (call_script, "script_give_village_to_center", "p_village_148", "p_town_8"),
        (call_script, "script_give_village_to_center", "p_village_149", "p_town_8"),
        (call_script, "script_give_village_to_center", "p_village_106", "p_town_9"),
        (call_script, "script_give_village_to_center", "p_village_107", "p_town_9"),
        (call_script, "script_give_village_to_center", "p_village_114", "p_town_13"),
        (call_script, "script_give_village_to_center", "p_village_115", "p_town_13"),
        (call_script, "script_give_village_to_center", "p_village_116", "p_town_14"),
        (call_script, "script_give_village_to_center", "p_village_117", "p_town_14"),
        (call_script, "script_give_village_to_center", "p_village_118", "p_town_15"),
        (call_script, "script_give_village_to_center", "p_village_119", "p_town_15"),
        (call_script, "script_give_village_to_center", "p_village_120", "p_town_16"),
        (call_script, "script_give_village_to_center", "p_village_121", "p_town_16"),
        (call_script, "script_give_village_to_center", "p_village_126", "p_town_19"),
        (call_script, "script_give_village_to_center", "p_village_127", "p_town_19"),
        (call_script, "script_give_village_to_center", "p_village_128", "p_town_20"),
        (call_script, "script_give_village_to_center", "p_village_129", "p_town_20"),
        (call_script, "script_give_village_to_center", "p_village_130", "p_town_21"),
        (call_script, "script_give_village_to_center", "p_village_131", "p_town_21"),
        (call_script, "script_give_village_to_center", "p_village_132", "p_town_22"),
        (call_script, "script_give_village_to_center", "p_village_134", "p_town_23"),
        (call_script, "script_give_village_to_center", "p_village_135", "p_town_23"),
        (call_script, "script_give_village_to_center", "p_village_138", "p_town_25"),
        (call_script, "script_give_village_to_center", "p_village_139", "p_town_25"),
        (call_script, "script_give_village_to_center", "p_village_146", "p_town_29"),
        (call_script, "script_give_village_to_center", "p_village_147", "p_town_29"),
        
        
        #pass 1: Give one village to each castle  #phaiak edited
        (try_for_range, ":counter", 0, 90),
          (store_add, ":cur_center", castles_begin, ":counter"),
          (store_add, ":cur_village", villages_begin, ":counter"),
          (party_set_slot, ":cur_village", slot_village_bound_center, ":cur_center"),
          (store_faction_of_party, ":center_faction", ":cur_center"),
          (call_script, "script_give_center_to_faction_aux", ":cur_village", ":center_faction"),
        (try_end),
        
        
        #pass 2: Give other villages to closest town.
        (try_for_range, ":cur_village", villages_begin, villages_end),
          (neg|party_slot_ge, ":cur_village", slot_village_bound_center, 1), #skip villages which are already bound.
          (assign, ":min_dist", 999999),
          (assign, ":min_dist_town", -1),
          (try_for_range, ":cur_town", walled_centers_begin, walled_centers_end),
            (store_distance_to_party_from_party, ":cur_dist", ":cur_village", ":cur_town"),
            (lt, ":cur_dist", ":min_dist"),
            (assign, ":min_dist", ":cur_dist"),
            (assign, ":min_dist_town", ":cur_town"),
          (try_end),
          (party_set_slot, ":cur_village", slot_village_bound_center, ":min_dist_town"),
          (store_faction_of_party, ":town_faction", ":min_dist_town"),
          (call_script, "script_give_center_to_faction_aux", ":cur_village", ":town_faction"),
        (try_end),
        
        
        # Towns (loop)
        (try_for_range, ":town_no", towns_begin, towns_end),
          (store_sub, ":offset", ":town_no", towns_begin),
          (party_set_slot,":town_no", slot_party_type, spt_town),
          #(store_add, ":cur_object_no", "trp_town_1_seneschal", ":offset"),
          #(party_set_slot,":town_no", slot_town_seneschal, ":cur_object_no"),
          (store_add, ":cur_object_no", "scn_town_1_center", ":offset"),
          (party_set_slot,":town_no", slot_town_center, ":cur_object_no"),
          (store_add, ":cur_object_no", "scn_town_1_castle", ":offset"),
          (party_set_slot,":town_no", slot_town_castle, ":cur_object_no"),
          (store_add, ":cur_object_no", "scn_town_1_prison", ":offset"),
          (party_set_slot,":town_no", slot_town_prison, ":cur_object_no"),
          (store_add, ":cur_object_no", "scn_town_1_walls", ":offset"),
          (party_set_slot,":town_no", slot_town_walls, ":cur_object_no"),
          (store_add, ":cur_object_no", "scn_town_1_tavern", ":offset"),
          (party_set_slot,":town_no", slot_town_tavern, ":cur_object_no"),
          (store_add, ":cur_object_no", "scn_town_1_store", ":offset"),
          (party_set_slot,":town_no", slot_town_store, ":cur_object_no"),
          (store_add, ":cur_object_no", "scn_town_1_arena", ":offset"),
          (party_set_slot,":town_no", slot_town_arena, ":cur_object_no"),
          (store_add, ":cur_object_no", "scn_town_1_alley", ":offset"),
          (party_set_slot,":town_no", slot_town_alley, ":cur_object_no"),
          (store_add, ":cur_object_no", "trp_town_1_mayor", ":offset"),
          (party_set_slot,":town_no", slot_town_elder, ":cur_object_no"),
          (store_add, ":cur_object_no", "trp_town_1_tavernkeeper", ":offset"),
          (party_set_slot,":town_no", slot_town_tavernkeeper, ":cur_object_no"),
          (store_add, ":cur_object_no", "trp_town_1_weaponsmith", ":offset"),
          (party_set_slot,":town_no", slot_town_weaponsmith, ":cur_object_no"),
          (store_add, ":cur_object_no", "trp_town_1_armorer", ":offset"),
          (party_set_slot,":town_no", slot_town_armorer, ":cur_object_no"),
          (store_add, ":cur_object_no", "trp_town_1_merchant", ":offset"),
          (party_set_slot,":town_no", slot_town_merchant, ":cur_object_no"),
          (store_add, ":cur_object_no", "trp_town_1_horse_merchant", ":offset"),
          (party_set_slot,":town_no", slot_town_horse_merchant, ":cur_object_no"),
          (store_add, ":cur_object_no", "scn_town_1_center", ":offset"),
          (party_set_slot,":town_no", slot_town_center, ":cur_object_no"),
          (party_set_slot,":town_no", slot_town_reinforcement_party_template, "pt_center_reinforcements"),
          (store_add, ":cur_object_no", "trp_town_1_healer", ":offset"),
          (party_set_slot,":town_no", slot_town_healer, ":cur_object_no"),
          (store_add, ":cur_object_no", "trp_town_1_shipwright", ":offset"),
          (party_set_slot,":town_no", slot_town_shipwright, ":cur_object_no"),
        (try_end),
        
      (try_end),  #version check
      
      #chief sea battles buy ship barcos ciudades con puerto
      (party_set_slot,"p_town_3",slot_town_port, 1),                 ##Set Eidynburh as a port
      (party_set_slot,"p_town_4",slot_town_port, 1),                 ##Set Ribe as a port
      (party_set_slot,"p_town_5",slot_town_port, 1),                 ##Set Tunsberg as a port
      (party_set_slot,"p_town_6",slot_town_port, 1),                 ##Set Dun Breatann as a port	(Phaiak after advice from Adorno)
      (party_set_slot,"p_town_7",slot_town_port, 1),                 ##Set Dubh_linn as a port
      (party_set_slot,"p_town_9",slot_town_port, 1),                 ##Set Caer_Dyf as a port
      (party_set_slot,"p_town_11",slot_town_port, 1),                 ##Set Dorestad as a port	 (Phaiak)
      (party_set_slot,"p_town_12",slot_town_port, 1),                 ##Set Lundenwic as a port
      
      (party_set_slot,"p_town_13",slot_town_port, 1),                 ##Set Bebbanburh as a port
      (party_set_slot,"p_town_14",slot_town_port, 1),                 ##Set Dun Keltair as a port
      (party_set_slot,"p_town_19",slot_town_port, 1),                 ##Set Aileach as a port
      (party_set_slot,"p_town_20",slot_town_port, 1),                 ##Set Duin Foither as a port
      (party_set_slot,"p_town_25",slot_town_port, 1),                 ##Set Din Cado as a port
      (party_set_slot,"p_town_27",slot_town_port, 1),                 ##Set Caer Manaw as a port
      #(party_set_slot,"p_castle_52",slot_town_port, 1),                 ##Set heidabyr as a port	  (Phaiak)
      #chief buy ship end
      # JuJu70 slavemarket setup
      (try_for_range, ":town", towns_begin, towns_end),
        (party_set_slot, ":town", slot_center_has_slavemarket, 0),
      (try_end),
      
      (party_set_slot,"p_town_4",slot_center_has_slavemarket, 1),
      (party_set_slot,"p_town_7",slot_center_has_slavemarket, 1),
      # JuJu70 end
      # Castles
      (try_for_range, ":castle_no", castles_begin, castles_end),
        (store_sub, ":offset", ":castle_no", castles_begin),
        (store_add, ":senechal_troop_no", "trp_castle_1_seneschal", ":offset"),
        (party_set_slot,":castle_no", slot_town_seneschal, ":senechal_troop_no"),
        (val_mul, ":offset", 3),
        
        
        (store_add, ":exterior_scene_no", "scn_castle_1_exterior", ":offset"),
        (party_set_slot,":castle_no", slot_castle_exterior, ":exterior_scene_no"),
        (store_add, ":interior_scene_no", "scn_castle_1_interior", ":offset"),
        (party_set_slot,":castle_no", slot_town_castle, ":interior_scene_no"),
        (store_add, ":interior_scene_no", "scn_castle_1_prison", ":offset"),
        (party_set_slot,":castle_no", slot_town_prison, ":interior_scene_no"),
        
        (party_set_slot,":castle_no", slot_town_reinforcement_party_template, "pt_center_reinforcements"),
        (party_set_slot,":castle_no", slot_party_type, spt_castle),
        (party_set_slot,":castle_no", slot_center_is_besieged_by, -1),
      (try_end),
      
      # Monasteries
      (try_for_range, ":monastery", "p_monasterio1", "p_yourlair"),
        (store_mod, ":mod", ":monastery", 2),
        (try_begin),
          (eq, ":mod", 0),
          (party_set_slot, ":monastery", slot_castle_exterior, "scn_monasterio2"),
          (party_set_slot, ":monastery", slot_town_castle, "scn_monasterio2_normal_interior"),
        (else_try),
          (party_set_slot, ":monastery", slot_castle_exterior, "scn_monasterio1_costa"),
          (party_set_slot, ":monastery", slot_town_castle, "scn_monasterio1_normal_interior"),
        (end_try),
      (end_try),
      (party_set_slot, "p_paganholysites1", slot_castle_exterior, "scn_pagan_holy_site_norway"), #norwey
      (party_set_slot, "p_paganholysites2", slot_castle_exterior, "scn_pagan_holy_site_danmark"), #denmark
      # (party_set_slot, "p_monasterio6", slot_castle_exterior, "scn_monasterio1_costa"),
      
      
      ### Set which castles need to be attacked with siege towers.
      ##      (party_set_slot,"p_town_13", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_town_16", slot_center_siege_with_belfry, 1),
      ##
      ##      (party_set_slot,"p_castle_1", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_2", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_4", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_7", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_8", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_9", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_11", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_13", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_21", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_25", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_34", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_35", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_38", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_40", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_41", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_42", slot_center_siege_with_belfry, 1),
      ##      (party_set_slot,"p_castle_43", slot_center_siege_with_belfry, 1),
      
      # Villages characters
      (try_for_range, ":village_no", villages_begin, villages_end),
        (store_sub, ":offset", ":village_no", villages_begin),
        
        (store_add, ":exterior_scene_no", "scn_village_1", ":offset"),
        (party_set_slot,":village_no", slot_castle_exterior, ":exterior_scene_no"),
        
        (store_add, ":store_troop_no", "trp_village_1_elder", ":offset"),
        (party_set_slot,":village_no", slot_town_elder, ":store_troop_no"),
        
        (party_set_slot,":village_no", slot_party_type, spt_village),
        (party_set_slot,":village_no", slot_village_raided_by, -1),
        
        (call_script, "script_refresh_village_defenders", ":village_no"),
        (call_script, "script_refresh_village_defenders", ":village_no"),
        (call_script, "script_refresh_village_defenders", ":village_no"),
        (call_script, "script_refresh_village_defenders", ":village_no"),
      (try_end),
      
      # (try_for_range, ":center_no", centers_begin, centers_end),
      # (party_set_slot, ":center_no", slot_center_last_spotted_enemy, -1),
      # (party_set_slot, ":center_no", slot_center_is_besieged_by, -1),
      # (party_set_slot, ":center_no", slot_center_last_taken_by_troop, -1),
      # (try_end),
      
      # Troops:
      # Reassign divisions MOTO chief
      (try_for_range, ":troop_no", soldiers_begin, soldiers_end),
        (call_script, "script_troop_default_division", ":troop_no", 0),
        (troop_get_class, ":division", ":troop_no"),
        (neq, ":division", reg0),
        (troop_set_class, ":troop_no", reg0),
      (try_end),
      
      # Assign banners and renown.
      # We assume there are enough banners for all kingdom heroes.
      
      #faction banners
      (faction_set_slot, "fac_kingdom_1", slot_faction_banner, "mesh_banner_kingdom_g"),
      (faction_set_slot, "fac_kingdom_2", slot_faction_banner, "mesh_banner_kingdom_a"),
      (faction_set_slot, "fac_kingdom_3", slot_faction_banner, "mesh_banner_kingdom_s"),
      (faction_set_slot, "fac_kingdom_4", slot_faction_banner, "mesh_banner_kingdom_j"),
      (faction_set_slot, "fac_kingdom_5", slot_faction_banner, "mesh_banner_kingdom_e"),
      (faction_set_slot, "fac_kingdom_6", slot_faction_banner, "mesh_banner_kingdom_f"),
      (faction_set_slot, "fac_kingdom_7", slot_faction_banner, "mesh_banner_kingdom_p"),
      (faction_set_slot, "fac_kingdom_8", slot_faction_banner, "mesh_banner_kingdom_b"),
      (faction_set_slot, "fac_kingdom_9", slot_faction_banner, "mesh_banner_kingdom_h"),
      (faction_set_slot, "fac_kingdom_10", slot_faction_banner, "mesh_banner_kingdom_l"),
      (faction_set_slot, "fac_kingdom_11", slot_faction_banner, "mesh_banner_kingdom_k"),
      (faction_set_slot, "fac_kingdom_12", slot_faction_banner, "mesh_banner_kingdom_m"),
      (faction_set_slot, "fac_kingdom_13", slot_faction_banner, "mesh_banner_kingdom_t"),
      (faction_set_slot, "fac_kingdom_14", slot_faction_banner, "mesh_banner_kingdom_ll"),
      (faction_set_slot, "fac_kingdom_15", slot_faction_banner, "mesh_banner_kingdom_n"),
      (faction_set_slot, "fac_kingdom_16", slot_faction_banner, "mesh_banner_kingdom_o"),
      (faction_set_slot, "fac_kingdom_17", slot_faction_banner, "mesh_banner_kingdom_q"),
      (faction_set_slot, "fac_kingdom_18", slot_faction_banner, "mesh_banner_kingdom_r"),
      (faction_set_slot, "fac_kingdom_19", slot_faction_banner, "mesh_banner_kingdom_i"),
      (faction_set_slot, "fac_kingdom_20", slot_faction_banner, "mesh_banner_kingdom_c"),
      (faction_set_slot, "fac_kingdom_21", slot_faction_banner, "mesh_banner_kingdom_d"),
      
      (try_for_range, ":cur_faction", npc_kingdoms_begin, npc_kingdoms_end),
        (faction_get_slot, ":cur_faction_king", ":cur_faction", slot_faction_leader),
        (faction_get_slot, ":cur_faction_banner", ":cur_faction", slot_faction_banner),
        (val_sub, ":cur_faction_banner", banner_meshes_begin),
        (val_add, ":cur_faction_banner", banner_scene_props_begin),
        (troop_set_slot, ":cur_faction_king", slot_troop_banner_scene_prop, ":cur_faction_banner"),
      (try_end),
      ##      (assign, ":num_khergit_lords_assigned", 0),
      ##      (assign, ":num_sarranid_lords_assigned", 0),
      ##      (assign, ":num_other_lords_assigned", 0),
      
      (try_for_range, ":kingdom_hero", active_npcs_begin, active_npcs_end),
        (this_or_next|troop_slot_eq, ":kingdom_hero", slot_troop_occupation, slto_kingdom_hero),
        (troop_slot_eq, ":kingdom_hero", slot_troop_occupation, slto_inactive_pretender),
        (troop_set_slot, ":troop_no", slot_troop_conv, 0),
        (store_character_level, ":level", ":kingdom_hero"),
        (store_mul, ":renown", ":level", ":level"),
        (val_div, ":renown", 4), #for top lord, is about 400
        
        (troop_get_slot, ":age", ":kingdom_hero", slot_troop_age),
        (store_mul, ":age_addition", ":age", ":age"),
        (val_div, ":age_addition", 8), #for top lord, is about 400
        (val_add, ":renown", ":age_addition"),
        
        (store_troop_faction, ":kingdom_hero_faction", ":kingdom_hero"),
        (neq,":kingdom_hero_faction", "fac_adventurers"),
        (try_begin),
          (faction_slot_eq, ":kingdom_hero_faction", slot_faction_leader, ":kingdom_hero"),
          (store_random_in_range, ":random_renown", 350, 500), #aumentado chief renombre
        (else_try),
          (store_random_in_range, ":random_renown", 100, 300), #aumentado chief renombre
        (try_end),
        (val_add, ":renown", ":random_renown"),
        
        (troop_set_slot, ":kingdom_hero", slot_troop_renown, ":renown"),
      (try_end),
      
      #empieza asignacion individual de banners chief
      #Danmark
      (troop_set_slot, "trp_knight_1_1", slot_troop_banner_scene_prop, "spr_banner_viking01"),
      (troop_set_slot, "trp_knight_1_2", slot_troop_banner_scene_prop, "spr_banner_viking02"),
      (troop_set_slot, "trp_knight_1_3", slot_troop_banner_scene_prop, "spr_banner_viking03"),
      (troop_set_slot, "trp_knight_1_4", slot_troop_banner_scene_prop, "spr_banner_viking04"),
      (troop_set_slot, "trp_knight_1_5", slot_troop_banner_scene_prop, "spr_banner_viking05"),
      (troop_set_slot, "trp_knight_1_6", slot_troop_banner_scene_prop, "spr_banner_viking06"),
      
      #Northvegr
      (troop_set_slot, "trp_knight_2_1", slot_troop_banner_scene_prop, "spr_banner_viking07"),
      (troop_set_slot, "trp_knight_2_2", slot_troop_banner_scene_prop, "spr_banner_viking08"),
      (troop_set_slot, "trp_knight_2_3", slot_troop_banner_scene_prop, "spr_banner_viking09"),
      (troop_set_slot, "trp_knight_2_4", slot_troop_banner_scene_prop, "spr_banner_viking10"),
      (troop_set_slot, "trp_knight_2_5", slot_troop_banner_scene_prop, "spr_banner_viking11"),
      
      #Laithlind
      (troop_set_slot, "trp_knight_3_1", slot_troop_banner_scene_prop, "spr_banner_viking12"),
      (troop_set_slot, "trp_knight_3_2", slot_troop_banner_scene_prop, "spr_banner_viking13"),
      (troop_set_slot, "trp_knight_3_3", slot_troop_banner_scene_prop, "spr_banner_viking14"),
      (troop_set_slot, "trp_knight_3_4", slot_troop_banner_scene_prop, "spr_banner_viking15"),
      (troop_set_slot, "trp_knight_3_5", slot_troop_banner_scene_prop, "spr_banner_viking16"),
      (troop_set_slot, "trp_knight_3_6", slot_troop_banner_scene_prop, "spr_banner_viking17"),
      (troop_set_slot, "trp_knight_3_7", slot_troop_banner_scene_prop, "spr_banner_viking18"),
      (troop_set_slot, "trp_knight_3_8", slot_troop_banner_scene_prop, "spr_banner_viking19"),
      
      #Friese
      (troop_set_slot, "trp_knight_4_1", slot_troop_banner_scene_prop, "spr_banner_viking20"),
      (troop_set_slot, "trp_knight_4_2", slot_troop_banner_scene_prop, "spr_banner_viking21"),
      (troop_set_slot, "trp_knight_4_3", slot_troop_banner_scene_prop, "spr_banner_viking201"),
      
      #wessex
      (troop_set_slot, "trp_knight_5_1", slot_troop_banner_scene_prop, "spr_banner_anglosaxon01"),
      (troop_set_slot, "trp_knight_5_2", slot_troop_banner_scene_prop, "spr_banner_anglosaxon02"),
      (troop_set_slot, "trp_knight_5_3", slot_troop_banner_scene_prop, "spr_banner_anglosaxon03"),
      (troop_set_slot, "trp_knight_5_4", slot_troop_banner_scene_prop, "spr_banner_anglosaxon04"),
      (troop_set_slot, "trp_knight_5_5", slot_troop_banner_scene_prop, "spr_banner_anglosaxon05"),
      (troop_set_slot, "trp_knight_5_6", slot_troop_banner_scene_prop, "spr_banner_anglosaxon06"),
      (troop_set_slot, "trp_knight_5_7", slot_troop_banner_scene_prop, "spr_banner_anglosaxon07"),
      (troop_set_slot, "trp_knight_5_8", slot_troop_banner_scene_prop, "spr_banner_anglosaxon08"),
      (troop_set_slot, "trp_knight_5_9", slot_troop_banner_scene_prop, "spr_banner_anglosaxon09"),
      (troop_set_slot, "trp_knight_5_10", slot_troop_banner_scene_prop, "spr_banner_anglosaxon10"),
      (troop_set_slot, "trp_knight_5_11", slot_troop_banner_scene_prop, "spr_banner_anglosaxon11"),
      
      #East Engle
      (troop_set_slot, "trp_knight_6_1", slot_troop_banner_scene_prop, "spr_banner_anglosaxon12"),
      (troop_set_slot, "trp_knight_6_2", slot_troop_banner_scene_prop, "spr_banner_anglosaxon13"),
      (troop_set_slot, "trp_knight_6_3", slot_troop_banner_scene_prop, "spr_banner_anglosaxon14"),
      (troop_set_slot, "trp_knight_6_4", slot_troop_banner_scene_prop, "spr_banner_anglosaxon15"),
      (troop_set_slot, "trp_knight_6_5", slot_troop_banner_scene_prop, "spr_banner_anglosaxon16"),
      
      #Mierce
      (troop_set_slot, "trp_knight_7_1", slot_troop_banner_scene_prop, "spr_banner_anglosaxon17"),
      (troop_set_slot, "trp_knight_7_2", slot_troop_banner_scene_prop, "spr_banner_anglosaxon18"),
      (troop_set_slot, "trp_knight_7_3", slot_troop_banner_scene_prop, "spr_banner_anglosaxon19"),
      (troop_set_slot, "trp_knight_7_4", slot_troop_banner_scene_prop, "spr_banner_anglosaxon20"),
      (troop_set_slot, "trp_knight_7_5", slot_troop_banner_scene_prop, "spr_banner_anglosaxon21"),
      (troop_set_slot, "trp_knight_7_6", slot_troop_banner_scene_prop, "spr_banner_anglosaxon201"),
      (troop_set_slot, "trp_knight_7_7", slot_troop_banner_scene_prop, "spr_banner_anglosaxon202"),
      (troop_set_slot, "trp_knight_7_8", slot_troop_banner_scene_prop, "spr_banner_anglosaxon203"),
      (troop_set_slot, "trp_knight_7_9", slot_troop_banner_scene_prop, "spr_banner_anglosaxon204"),
      
      #Northanhymbre
      (troop_set_slot, "trp_knight_8_1", slot_troop_banner_scene_prop, "spr_banner_viking202"),
      (troop_set_slot, "trp_knight_8_2", slot_troop_banner_scene_prop, "spr_banner_viking203"),
      (troop_set_slot, "trp_knight_8_3", slot_troop_banner_scene_prop, "spr_banner_viking204"),
      (troop_set_slot, "trp_knight_8_4", slot_troop_banner_scene_prop, "spr_banner_viking205"),
      (troop_set_slot, "trp_knight_8_5", slot_troop_banner_scene_prop, "spr_banner_viking206"),
      (troop_set_slot, "trp_knight_8_6", slot_troop_banner_scene_prop, "spr_banner_viking207"),
      (troop_set_slot, "trp_knight_8_7", slot_troop_banner_scene_prop, "spr_banner_viking208"),
      (troop_set_slot, "trp_knight_8_8", slot_troop_banner_scene_prop, "spr_banner_viking209"),
      (troop_set_slot, "trp_knight_8_9", slot_troop_banner_scene_prop, "spr_banner_viking210"),
      (troop_set_slot, "trp_knight_8_10", slot_troop_banner_scene_prop, "spr_banner_viking211"),
      (troop_set_slot, "trp_knight_8_11", slot_troop_banner_scene_prop, "spr_banner_viking212"),
      (troop_set_slot, "trp_knight_8_12", slot_troop_banner_scene_prop, "spr_banner_viking213"),
      (troop_set_slot, "trp_knight_8_13", slot_troop_banner_scene_prop, "spr_banner_viking214"),
      (troop_set_slot, "trp_knight_8_14", slot_troop_banner_scene_prop, "spr_banner_viking215"),
      (troop_set_slot, "trp_knight_8_15", slot_troop_banner_scene_prop, "spr_banner_viking216"),
      
      #Gwynedd
      (troop_set_slot, "trp_knight_9_1", slot_troop_banner_scene_prop, "spr_banner_briton01"),
      (troop_set_slot, "trp_knight_9_2", slot_troop_banner_scene_prop, "spr_banner_briton02"),
      (troop_set_slot, "trp_knight_9_3", slot_troop_banner_scene_prop, "spr_banner_briton03"),
      (troop_set_slot, "trp_knight_9_4", slot_troop_banner_scene_prop, "spr_banner_briton04"),
      (troop_set_slot, "trp_knight_9_5", slot_troop_banner_scene_prop, "spr_banner_briton05"),
      (troop_set_slot, "trp_knight_9_6", slot_troop_banner_scene_prop, "spr_banner_briton06"),
      (troop_set_slot, "trp_knight_9_7", slot_troop_banner_scene_prop, "spr_banner_briton07"),
      (troop_set_slot, "trp_knight_9_8", slot_troop_banner_scene_prop, "spr_banner_briton08"),
      (troop_set_slot, "trp_knight_9_9", slot_troop_banner_scene_prop, "spr_banner_briton09"),
      
      #Brycheiniog
      (troop_set_slot, "trp_knight_10_1", slot_troop_banner_scene_prop, "spr_banner_briton10"),
      
      #Alt Clut
      (troop_set_slot, "trp_knight_11_1", slot_troop_banner_scene_prop, "spr_banner_briton11"),
      (troop_set_slot, "trp_knight_11_2", slot_troop_banner_scene_prop, "spr_banner_briton12"),
      (troop_set_slot, "trp_knight_11_3", slot_troop_banner_scene_prop, "spr_banner_briton13"),
      (troop_set_slot, "trp_knight_11_4", slot_troop_banner_scene_prop, "spr_banner_briton14"),
      (troop_set_slot, "trp_knight_11_5", slot_troop_banner_scene_prop, "spr_banner_briton15"),
      (troop_set_slot, "trp_knight_11_6", slot_troop_banner_scene_prop, "spr_banner_briton16"),
      (troop_set_slot, "trp_knight_11_7", slot_troop_banner_scene_prop, "spr_banner_briton17"),
      (troop_set_slot, "trp_knight_11_8", slot_troop_banner_scene_prop, "spr_banner_briton18"),
      
      #Cornubia
      (troop_set_slot, "trp_knight_12_1", slot_troop_banner_scene_prop, "spr_banner_briton19"),
      (troop_set_slot, "trp_knight_12_2", slot_troop_banner_scene_prop, "spr_banner_briton20"),
      (troop_set_slot, "trp_knight_12_3", slot_troop_banner_scene_prop, "spr_banner_briton21"),
      
      #Glywyssing
      (troop_set_slot, "trp_knight_13_1", slot_troop_banner_scene_prop, "spr_banner_briton201"),
      (troop_set_slot, "trp_knight_13_2", slot_troop_banner_scene_prop, "spr_banner_briton202"),
      (troop_set_slot, "trp_knight_13_3", slot_troop_banner_scene_prop, "spr_banner_briton203"),
      
      #Uladh
      (troop_set_slot, "trp_knight_14_1", slot_troop_banner_scene_prop, "spr_banner_irish01"),
      (troop_set_slot, "trp_knight_14_2", slot_troop_banner_scene_prop, "spr_banner_irish02"),
      (troop_set_slot, "trp_knight_14_3", slot_troop_banner_scene_prop, "spr_banner_irish03"),
      (troop_set_slot, "trp_knight_14_4", slot_troop_banner_scene_prop, "spr_banner_irish04"),
      (troop_set_slot, "trp_knight_14_5", slot_troop_banner_scene_prop, "spr_banner_irish05"),
      
      #Laigin
      (troop_set_slot, "trp_knight_15_1", slot_troop_banner_scene_prop, "spr_banner_irish06"),
      (troop_set_slot, "trp_knight_15_2", slot_troop_banner_scene_prop, "spr_banner_irish07"),
      (troop_set_slot, "trp_knight_15_3", slot_troop_banner_scene_prop, "spr_banner_irish08"),
      (troop_set_slot, "trp_knight_15_4", slot_troop_banner_scene_prop, "spr_banner_irish09"),
      (troop_set_slot, "trp_knight_15_5", slot_troop_banner_scene_prop, "spr_banner_irish10"),
      (troop_set_slot, "trp_knight_15_6", slot_troop_banner_scene_prop, "spr_banner_irish11"),
      
      #Mumain
      (troop_set_slot, "trp_knight_16_1", slot_troop_banner_scene_prop, "spr_banner_irish12"),
      (troop_set_slot, "trp_knight_16_2", slot_troop_banner_scene_prop, "spr_banner_irish13"),
      (troop_set_slot, "trp_knight_16_3", slot_troop_banner_scene_prop, "spr_banner_irish14"),
      (troop_set_slot, "trp_knight_16_4", slot_troop_banner_scene_prop, "spr_banner_irish15"),
      (troop_set_slot, "trp_knight_16_5", slot_troop_banner_scene_prop, "spr_banner_irish16"),
      (troop_set_slot, "trp_knight_16_6", slot_troop_banner_scene_prop, "spr_banner_irish17"),
      
      #Connachta
      (troop_set_slot, "trp_knight_17_1", slot_troop_banner_scene_prop, "spr_banner_irish18"),
      (troop_set_slot, "trp_knight_17_2", slot_troop_banner_scene_prop, "spr_banner_irish19"),
      (troop_set_slot, "trp_knight_17_3", slot_troop_banner_scene_prop, "spr_banner_irish20"),
      (troop_set_slot, "trp_knight_17_4", slot_troop_banner_scene_prop, "spr_banner_irish21"),
      (troop_set_slot, "trp_knight_17_5", slot_troop_banner_scene_prop, "spr_banner_irish201"),
      (troop_set_slot, "trp_knight_17_6", slot_troop_banner_scene_prop, "spr_banner_irish202"),
      
      #Aileach
      (troop_set_slot, "trp_knight_18_1", slot_troop_banner_scene_prop, "spr_banner_irish203"),
      (troop_set_slot, "trp_knight_18_2", slot_troop_banner_scene_prop, "spr_banner_irish204"),
      (troop_set_slot, "trp_knight_18_3", slot_troop_banner_scene_prop, "spr_banner_irish205"),
      (troop_set_slot, "trp_knight_18_4", slot_troop_banner_scene_prop, "spr_banner_irish206"),
      (troop_set_slot, "trp_knight_18_5", slot_troop_banner_scene_prop, "spr_banner_irish207"),
      (troop_set_slot, "trp_knight_18_6", slot_troop_banner_scene_prop, "spr_banner_irish208"),
      (troop_set_slot, "trp_knight_18_7", slot_troop_banner_scene_prop, "spr_banner_irish209"),
      
      #Meath
      (troop_set_slot, "trp_knight_19_1", slot_troop_banner_scene_prop, "spr_banner_irish210"),
      (troop_set_slot, "trp_knight_19_2", slot_troop_banner_scene_prop, "spr_banner_irish211"),
      (troop_set_slot, "trp_knight_19_3", slot_troop_banner_scene_prop, "spr_banner_irish212"),
      (troop_set_slot, "trp_knight_19_4", slot_troop_banner_scene_prop, "spr_banner_irish213"),
      (troop_set_slot, "trp_knight_19_5", slot_troop_banner_scene_prop, "spr_banner_extra01"),
      
      #Alban
      (troop_set_slot, "trp_knight_20_1", slot_troop_banner_scene_prop, "spr_banner_pict01"),
      (troop_set_slot, "trp_knight_20_2", slot_troop_banner_scene_prop, "spr_banner_pict02"),
      (troop_set_slot, "trp_knight_20_3", slot_troop_banner_scene_prop, "spr_banner_pict03"),
      (troop_set_slot, "trp_knight_20_4", slot_troop_banner_scene_prop, "spr_banner_pict04"),
      (troop_set_slot, "trp_knight_20_5", slot_troop_banner_scene_prop, "spr_banner_pict05"),
      (troop_set_slot, "trp_knight_20_6", slot_troop_banner_scene_prop, "spr_banner_pict06"),
      (troop_set_slot, "trp_knight_20_7", slot_troop_banner_scene_prop, "spr_banner_pict07"),
      (troop_set_slot, "trp_knight_20_8", slot_troop_banner_scene_prop, "spr_banner_pict08"),
      
      #Osrige
      (troop_set_slot, "trp_knight_21_1", slot_troop_banner_scene_prop, "spr_banner_extra02"),
      (troop_set_slot, "trp_knight_21_2", slot_troop_banner_scene_prop, "spr_banner_extra03"),
      (troop_set_slot, "trp_knight_21_3", slot_troop_banner_scene_prop, "spr_banner_extra04"),
      
      #pretenders
      (troop_set_slot, "trp_kingdom_1_pretender", slot_troop_banner_scene_prop, "spr_banner_extra05"),
      (troop_set_slot, "trp_kingdom_2_pretender", slot_troop_banner_scene_prop, "spr_banner_extra06"),
      (troop_set_slot, "trp_kingdom_3_pretender", slot_troop_banner_scene_prop, "spr_banner_extra07"),
      (troop_set_slot, "trp_kingdom_4_pretender", slot_troop_banner_scene_prop, "spr_banner_extra08"),
      (troop_set_slot, "trp_kingdom_5_pretender", slot_troop_banner_scene_prop, "spr_banner_extra09"),
      (troop_set_slot, "trp_kingdom_6_pretender", slot_troop_banner_scene_prop, "spr_banner_extra10"),
      ##      (troop_set_slot, "trp_kingdom_7_pretender", slot_troop_banner_scene_prop, "spr_banner_extra11"),
      ##      (troop_set_slot, "trp_kingdom_8_pretender", slot_troop_banner_scene_prop, "spr_banner_extra12"),
      ##      (troop_set_slot, "trp_kingdom_9_pretender", slot_troop_banner_scene_prop, "spr_banner_extra13"),
      ##      (troop_set_slot, "trp_kingdom_10_pretender", slot_troop_banner_scene_prop, "spr_banner_extra14"),
      ##      (troop_set_slot, "trp_kingdom_11_pretender", slot_troop_banner_scene_prop, "spr_banner_extra15"),
      ####chief banners asignados personalmente acaba
      
      (try_for_range, ":troop_no", "trp_player", "trp_merchants_end"),
        (add_troop_note_tableau_mesh, ":troop_no", "tableau_troop_note_mesh"),
      (try_end),
      
      (try_for_range, ":center_no", centers_begin, centers_end),
        (add_party_note_tableau_mesh, ":center_no", "tableau_center_note_mesh"),
      (try_end),
      
      (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
        (is_between, ":faction_no", "fac_kingdom_1", kingdoms_end), #Excluding player kingdom
        (add_faction_note_tableau_mesh, ":faction_no", "tableau_faction_note_mesh"),
      (else_try),
        (add_faction_note_tableau_mesh, ":faction_no", "tableau_faction_note_mesh_banner"),
      (try_end),
      
      #Now give towns to great lords
      (call_script, "script_give_center_to_lord", "p_town_1",  "trp_knight_5_2", 0),
      (call_script, "script_give_center_to_lord", "p_town_2",  "trp_kingdom_5_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_3",  "trp_kingdom_8_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_4",  "trp_kingdom_1_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_5",  "trp_kingdom_2_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_6",  "trp_kingdom_11_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_7",  "trp_kingdom_3_lord", 0),
      
      (call_script, "script_give_center_to_lord", "p_town_8",  "trp_knight_7_1", 0),
      (call_script, "script_give_center_to_lord", "p_town_9",  "trp_kingdom_13_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_10", "trp_knight_8_1", 0),
      (call_script, "script_give_center_to_lord", "p_town_11", "trp_kingdom_4_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_12", "trp_knight_7_4", 0),
      (call_script, "script_give_center_to_lord", "p_town_13", "trp_kingdom_9_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_14", "trp_kingdom_6_lord", 0),
      
      (call_script, "script_give_center_to_lord", "p_town_15", "trp_kingdom_20_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_16", "trp_knight_5_1", 0),
      (call_script, "script_give_center_to_lord", "p_town_17", "trp_kingdom_12_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_18", "trp_kingdom_21_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_19", "trp_knight_20_2", 0),
      (call_script, "script_give_center_to_lord", "p_town_20",  "trp_kingdom_14_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_21",  "trp_kingdom_15_lord", 0),
      
      (call_script, "script_give_center_to_lord", "p_town_22",  "trp_knight_9_3", 0),
      (call_script, "script_give_center_to_lord", "p_town_23",  "trp_kingdom_7_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_24",  "trp_kingdom_16_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_25",  "trp_kingdom_18_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_26",  "trp_kingdom_10_lord", 0),
      
      (call_script, "script_give_center_to_lord", "p_town_27",  "trp_knight_8_3", 0),
      (call_script, "script_give_center_to_lord", "p_town_28",  "trp_kingdom_17_lord", 0),
      (call_script, "script_give_center_to_lord", "p_town_29", "trp_kingdom_19_lord", 0),
      
      # Give family castles to certain nobles.
      (call_script, "script_give_center_to_lord", "p_castle_10", "trp_knight_7_2", 0), #
      (call_script, "script_give_center_to_lord", "p_castle_13", "trp_knight_7_3", 0), #Ha
      (call_script, "script_give_center_to_lord", "p_castle_14", "trp_knight_7_8", 0), #Nee
      (call_script, "script_give_center_to_lord", "p_castle_15", "trp_knight_7_5", 0), #Ae
      
      (call_script, "script_give_center_to_lord", "p_castle_22", "trp_knight_5_4", 0), #H
      (call_script, "script_give_center_to_lord", "p_castle_25", "trp_knight_5_3", 0), #A
      (call_script, "script_give_center_to_lord", "p_castle_26", "trp_knight_5_7", 0), #As
      (call_script, "script_give_center_to_lord", "p_castle_28", "trp_knight_5_6", 0), #Ha
      
      (call_script, "script_give_center_to_lord", "p_castle_41", "trp_knight_9_2", 0), #H
      (call_script, "script_give_center_to_lord", "p_castle_44", "trp_knight_9_4", 0), #As
      (call_script, "script_give_center_to_lord", "p_castle_45", "trp_knight_9_1", 0), #A
      
      (call_script, "script_give_center_to_lord", "p_castle_54", "trp_knight_2_2", 0), #A
      (call_script, "script_give_center_to_lord", "p_castle_55", "trp_knight_2_3", 0), #H
      
      (call_script, "script_give_center_to_lord", "p_castle_59", "trp_knight_3_5", 0), #H
      (call_script, "script_give_center_to_lord", "p_castle_61", "trp_knight_3_4", 0), #A
      
      (call_script, "script_give_center_to_lord", "p_castle_65", "trp_knight_14_1", 0), #As
      (call_script, "script_give_center_to_lord", "p_castle_67", "trp_knight_14_2", 0), #As
      
      (call_script, "script_give_center_to_lord", "p_castle_70", "trp_knight_18_7", 0), #As
      (call_script, "script_give_center_to_lord", "p_castle_71", "trp_knight_18_5", 0), #As
      
      (call_script, "script_give_center_to_lord", "p_castle_82", "trp_knight_16_5", 0), #A
      (call_script, "script_give_center_to_lord", "p_castle_83", "trp_knight_16_3", 0), #H
      (call_script, "script_give_center_to_lord", "p_castle_84", "trp_knight_16_4", 0), #As
      
      (call_script, "script_give_center_to_lord", "p_castle_87", "trp_knight_15_3", 0), #A
      
      (call_script, "script_give_center_to_lord", "p_castle_63", "trp_knight_4_3", 0), #Kennemer to for hrodulfr haraldsson Mainquest chief.
      
      (call_script, "script_assign_lords_to_empty_centers"),
      
      #set original factions
      (try_for_range, ":center_no", centers_begin, centers_end),
        (store_faction_of_party, ":original_faction", ":center_no"),
        (faction_get_slot, ":culture", ":original_faction", slot_faction_culture),
        (party_set_slot, ":center_no", slot_center_culture,  ":culture"),
        (party_set_slot, ":center_no", slot_center_original_faction,  ":original_faction"),
        (party_set_slot, ":center_no", slot_center_ex_faction,  ":original_faction"),
      (try_end),
      
      #set territorial disputes/outstanding border issues
      (party_set_slot, "p_castle_10", slot_center_ex_faction, "fac_kingdom_8"), #
      (party_set_slot, "p_castle_20", slot_center_ex_faction, "fac_kingdom_8"), #
      (party_set_slot, "p_castle_24", slot_center_ex_faction, "fac_kingdom_8"), #
      (party_set_slot, "p_castle_57", slot_center_ex_faction, "fac_kingdom_11"), #
      (party_set_slot, "p_castle_28", slot_center_ex_faction, "fac_kingdom_12"), #
      (party_set_slot, "p_castle_58", slot_center_ex_faction, "fac_kingdom_20"), #
      
      (call_script, "script_update_village_market_towns"),
      (call_script, "script_find_neighbors"),	#MOTO chief
      
      #this should come after assignment of territorial grievances
      # (try_for_range, ":unused", 0, 70),
      (try_for_range, ":unused", 0, 38),	#MOTO chief chill diplomacy: model 38 days (length of a truce -2 days; see randomization next)
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "@{!}DEBUG -- initial war/peace check begins"),
        (try_end),
        (call_script, "script_randomly_start_war_peace_new", 0),
      (try_end),
      
      #MOTO randomize truces
      (try_for_range, ":kingdom_a", kingdoms_begin, kingdoms_end),
        (store_add, ":already_done", ":kingdom_a", 1),	#hit every relationship just ONCE
        (try_for_range, ":kingdom_b", ":already_done", kingdoms_end),
          (store_add, ":truce_slot", ":kingdom_a", slot_faction_truce_days_with_factions_begin),
          (val_sub, ":truce_slot", kingdoms_begin),
          (faction_get_slot, ":truce_days", ":kingdom_b", ":truce_slot"),
          (ge, ":truce_days", truce_time),
          (store_random_in_range, reg0, 1, truce_time),
          (val_sub, ":truce_days", reg0),
          (val_add, ":truce_days", 1),	#leave a minimum of 2 days
          (faction_set_slot, ":kingdom_b", ":truce_slot", ":truce_days"),
          
          (store_add, ":truce_slot", ":kingdom_b", slot_faction_truce_days_with_factions_begin),
          (val_sub, ":truce_slot", kingdoms_begin),
          (faction_set_slot, ":kingdom_a", ":truce_slot", ":truce_days"),
        (try_end),
      (try_end),
      #MOTO  chief randomize truces end
      
      #Initialize walkers
      (try_for_range, ":center_no", centers_begin, centers_end),
        ##        (this_or_next|party_slot_eq, ":center_no", slot_party_type, spt_town), #puest off para castle walkers chief
        ##        (this_or_next|party_slot_eq, ":center_no", slot_party_type, spt_castle), #chief para walkers in castillos castle walkers
        ##                     (party_slot_eq, ":center_no", slot_party_type, spt_village),
        (try_for_range, ":walker_no", 0, num_town_walkers),
          (call_script, "script_center_set_walker_to_type", ":center_no", ":walker_no", walkert_default),
        (try_end),
      (try_end),
      
      
      #This needs to be after market towns
      (call_script, "script_initialize_economic_information"),
      
      (try_for_range, ":village_no", villages_begin, villages_end),
        (call_script, "script_refresh_village_merchant_inventory", ":village_no"),
      (try_end),
      
      (try_for_range, ":troop_id", original_kingdom_heroes_begin, active_npcs_end),
        (try_begin),
          (store_troop_faction, ":faction_id", ":troop_id"),
          (is_between, ":faction_id", kingdoms_begin, kingdoms_end),
          (troop_set_slot, ":troop_id", slot_troop_original_faction, ":faction_id"),
          (try_begin),
            (is_between, ":troop_id", pretenders_begin, pretenders_end),
            (faction_set_slot, ":faction_id", slot_faction_has_rebellion_chance, 1),
          (try_end),
        (try_end),
        #        (assign, ":initial_wealth", 6000), #chief COMMENTED OUT
        #chief Random wealth for lords
        (store_random_in_range,":random_gold",7000,16000),
        (assign, ":initial_wealth", ":random_gold"),
        (store_div,":travel_money",":initial_wealth",10),
        (troop_add_gold,":troop_id",":travel_money"),
        (val_sub,":initial_wealth",":travel_money"),
        (val_abs,":initial_wealth"),
        #Chief acaba
        (try_begin),
          (store_troop_faction, ":faction", ":troop_id"),
          (faction_slot_eq, ":faction", slot_faction_leader, ":troop_id"),
          (assign, ":initial_wealth", 30000), #cambiado chief
        (try_end),
        (troop_set_slot, ":troop_id", slot_troop_wealth, ":initial_wealth"),
      (try_end),
      
      (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),#add town garrisons
        #Add initial center wealth
        (assign, ":initial_wealth", 2000), #chief cambia para guarnicion
        (try_begin),
          (is_between, ":center_no", towns_begin, towns_end),
          (val_mul, ":initial_wealth", 2),
        (try_end),
        (party_set_slot, ":center_no", slot_town_wealth, ":initial_wealth"),
        
        (assign, ":garrison_strength", 15), #chief change to 10
        (try_begin),
          (party_slot_eq, ":center_no", slot_party_type, spt_town),
          (assign, ":garrison_strength", 25), #chief change to 20
        (try_end),
        (try_for_range, ":unused", 0, ":garrison_strength"),
          (call_script, "script_cf_reinforce_party", ":center_no"),
        (try_end),
        ## ADD some XP initially
        (store_div, ":xp_rounds", ":garrison_strength", 5),
        (val_add, ":xp_rounds", 2),
        
        (options_get_campaign_ai, ":reduce_campaign_ai"),  #moto chief
        #chief hace cambios
        (try_begin), #hard
          (eq, ":reduce_campaign_ai", 0),
          (assign, ":xp_addition_for_centers", 8500),
        (else_try), #moderate
          (eq, ":reduce_campaign_ai", 1),
          (assign, ":xp_addition_for_centers", 6000),
        (else_try), #easy
          (eq, ":reduce_campaign_ai", 2),
          (assign, ":xp_addition_for_centers", 3500),
        (try_end),
        #chief cambia acaba
        (try_for_range, ":unused", 0, ":xp_rounds"),
          (party_upgrade_with_xp, ":center_no", ":xp_addition_for_centers", 0),
        (try_end),
        
        #Normal distribution of town food stores
        (call_script, "script_center_get_food_store_limit", ":center_no"),
        (assign, ":food_store_limit", reg0),
        (store_random_in_range, ":distribution", 0, 67),
        (store_random_in_range, reg0, 0, 67),
        (val_add, ":distribution", reg0),
        (try_begin),
          (gt, ":distribution", 66),
          (store_sub, reg0, ":distribution", 66),
          (store_sub, ":distribution", 66, reg0),
        (try_end),
        (val_add, ":distribution", 34), #conquered centers get this much automatically
        (val_mul, ":food_store_limit", ":distribution"),
        (val_div, ":food_store_limit", 100),
        (party_set_slot, ":center_no", slot_party_food_store, ":food_store_limit"),
        
        #create lord parties
        (party_get_slot, ":center_lord", ":center_no", slot_town_lord),
        (ge, ":center_lord", 1),
        (troop_slot_eq, ":center_lord", slot_troop_leaded_party, 0),
        (assign, "$g_there_is_no_avaliable_centers", 0),
        (call_script, "script_create_kingdom_hero_party", ":center_lord", ":center_no"),
        (assign, ":lords_party", "$pout_party"),
        (party_attach_to_party, ":lords_party", ":center_no"),
        (party_set_slot, ":center_no", slot_town_player_odds, 1000),
      (try_end),
      
      #More pre-Warband family structures removed here
      
      #Warband changes begin - set companions relations
      (try_for_range, ":companion", companions_begin, companions_end),
        (try_for_range, ":other_companion", companions_begin, companions_end),
          (neq, ":other_companion", ":companion"),
          (neg|troop_slot_eq, ":companion", slot_troop_personalityclash_object, ":other_companion"),
          (neg|troop_slot_eq, ":companion", slot_troop_personalityclash2_object, ":other_companion"),
          (call_script, "script_troop_change_relation_with_troop", ":companion", ":other_companion", 7), #companions have a starting relation of 14, unless they are rivals
        (try_end),
      (try_end),
      
      #Warband changes continue -  sets relations in the same faction
      (try_for_range, ":lord", original_kingdom_heroes_begin, active_npcs_end),
        (troop_slot_eq, ":lord", slot_troop_occupation, slto_kingdom_hero),
        (troop_get_slot, ":lord_faction", ":lord", slot_troop_original_faction),
        
        (try_for_range, ":other_hero", original_kingdom_heroes_begin, active_npcs_end),
          (this_or_next|troop_slot_eq, ":other_hero", slot_troop_occupation, slto_kingdom_hero),
          (troop_slot_eq, ":other_hero", slot_troop_occupation, slto_inactive_pretender),
          (troop_get_slot, ":other_hero_faction", ":other_hero", slot_troop_original_faction),
          (eq, ":other_hero_faction", ":lord_faction"),
          (call_script, "script_troop_get_family_relation_to_troop", ":lord", ":other_hero"),
          (call_script, "script_troop_change_relation_with_troop", ":lord", ":other_hero", reg0),
          
          (store_random_in_range, ":random", 0, 11), #this will be scored twice between two kingdom heroes, so starting relation will average 10. Between lords and pretenders it will average 7.5
          (call_script, "script_troop_change_relation_with_troop", ":lord", ":other_hero", ":random"),
        (try_end),
      (try_end),
      
      #do about 5 years' worth of political history (assuming 3 random checks a day)
      (try_for_range, ":unused", 0, 5000),
        (call_script, "script_cf_random_political_event"),
      (try_end),
      (assign, "$total_random_quarrel_changes", 0),
      (assign, "$total_relation_adds", 0),
      (assign, "$total_relation_subs", 0),
      
      (try_for_range, ":kingdom", kingdoms_begin, kingdoms_end),
        (call_script, "script_evaluate_realm_stability", ":kingdom"),
      (try_end),
      #Warband changes end
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (assign, reg3, "$cheat_mode"),
        (display_message, "@{!}DEBUG : Completed political events, cheat mode: {reg3}"),
      (try_end),
      
      #assign love interests to unmarried male lords
      (try_for_range, ":cur_troop", lords_begin, lords_end),
        (troop_slot_eq, ":cur_troop", slot_troop_spouse, -1),
        (neg|is_between, ":cur_troop", kings_begin, kings_end),
        (neg|is_between, ":cur_troop", pretenders_begin, pretenders_end),
        
        (call_script, "script_assign_troop_love_interests", ":cur_troop"),
      (try_end),
      
      (store_random_in_range, "$romantic_attraction_seed", 0, 5),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (assign, reg3, "$romantic_attraction_seed"),
        (display_message, "@{!}DEBUG : Assigned love interests. Attraction seed: {reg3}"),
      (try_end),
      
      (party_set_slot, "p_clyde_coast_spawn_point", slot_party_bandit_type, "pt_steppe_bandits"), #Northmenn
      (party_set_slot, "p_clyde_coast_spawn_point", slot_party_spawn_target_spawns, 12),
      (party_set_slot, "p_clyde_coast_spawn_point", slot_party_spawn_radius, 30),
      (party_set_slot, "p_clyde_coast_spawn_point", slot_party_spawn_flags, spsf_coastal),
      
      (party_set_slot, "p_fortiu_spawn_point", slot_party_bandit_type, "pt_steppe_bandits"), #Northmenn
      (party_set_slot, "p_fortiu_spawn_point", slot_party_spawn_target_spawns, 12),
      (party_set_slot, "p_fortiu_spawn_point", slot_party_spawn_radius, 40),
      (party_set_slot, "p_fortiu_spawn_point", slot_party_spawn_flags, spsf_coastal),
      
      (party_set_slot, "p_wales_spawn_point", slot_party_bandit_type, "pt_taiga_bandits"), #Vikingarnir
      (party_set_slot, "p_wales_spawn_point", slot_party_spawn_target_spawns, 9),
      (party_set_slot, "p_wales_spawn_point", slot_party_spawn_radius, 40),
      (party_set_slot, "p_wales_spawn_point", slot_party_spawn_flags, spsf_coastal),
      
      (party_set_slot, "p_limerick_spawn_point", slot_party_bandit_type, "pt_taiga_bandits"), #Vikingarnir
      (party_set_slot, "p_limerick_spawn_point", slot_party_spawn_target_spawns, 10),
      (party_set_slot, "p_limerick_spawn_point", slot_party_spawn_radius, 70),
      (party_set_slot, "p_limerick_spawn_point", slot_party_spawn_flags, spsf_coastal),
      
      (party_set_slot, "p_northumbria_spawn_point", slot_party_bandit_type, "pt_forest_bandits"), #Robbers
      (party_set_slot, "p_northumbria_spawn_point", slot_party_spawn_target_spawns, 12),
      (party_set_slot, "p_northumbria_spawn_point", slot_party_spawn_radius, 40),
      
      (party_set_slot, "p_mierce_spawn_point", slot_party_bandit_type, "pt_forest_bandits"), #Robbers
      (party_set_slot, "p_mierce_spawn_point", slot_party_spawn_target_spawns, 12),
      (party_set_slot, "p_mierce_spawn_point", slot_party_spawn_radius, 40),
      
      (party_set_slot, "p_crafu_spawn_point", slot_party_bandit_type, "pt_mountain_bandits"), #Renegades
      (party_set_slot, "p_crafu_spawn_point", slot_party_spawn_target_spawns, 12),
      (party_set_slot, "p_crafu_spawn_point", slot_party_spawn_radius, 40),
      
      (party_set_slot, "p_alban_spawn_point", slot_party_bandit_type, "pt_mountain_bandits"), #Renegades
      (party_set_slot, "p_alban_spawn_point", slot_party_spawn_target_spawns, 12),
      (party_set_slot, "p_alban_spawn_point", slot_party_spawn_radius, 30),
      
      (party_set_slot, "p_aileach_spawn_point", slot_party_bandit_type, "pt_fianna"),
      (party_set_slot, "p_aileach_spawn_point", slot_party_spawn_target_spawns, 8),
      (party_set_slot, "p_aileach_spawn_point", slot_party_spawn_radius, 30),
      
      (party_set_slot, "p_sussex_spawn_point", slot_party_bandit_type, "pt_sea_raiders"), #Masterless Fighters
      (party_set_slot, "p_sussex_spawn_point", slot_party_spawn_target_spawns, 12),
      (party_set_slot, "p_sussex_spawn_point", slot_party_spawn_radius, 35),
      
      (party_set_slot, "p_engla_coast_spawn_point", slot_party_bandit_type, "pt_sea_raiders2"),  #Danish Elite Vikingarnir
      (party_set_slot, "p_engla_coast_spawn_point", slot_party_spawn_target_spawns, 12),
      (party_set_slot, "p_engla_coast_spawn_point", slot_party_spawn_radius, 40),
      (party_set_slot, "p_engla_coast_spawn_point", slot_party_spawn_flags, spsf_coastal),
      
      (party_set_slot, "p_cornish_coast_spawn_point", slot_party_bandit_type, "pt_sea_raiders2"),  #Danish Elite Vikingarnir
      (party_set_slot, "p_cornish_coast_spawn_point", slot_party_spawn_target_spawns, 7),
      (party_set_slot, "p_cornish_coast_spawn_point", slot_party_spawn_radius, 60),
      (party_set_slot, "p_cornish_coast_spawn_point", slot_party_spawn_flags, spsf_coastal),
      
      (party_set_slot, "p_leinster_spawn_point", slot_party_bandit_type, "pt_desert_bandits"), #Veteran Renegades
      (party_set_slot, "p_leinster_spawn_point", slot_party_spawn_target_spawns, 6),
      (party_set_slot, "p_leinster_spawn_point", slot_party_spawn_radius, 40),
      
      (party_set_slot, "p_norway_spawn_point", slot_party_bandit_type, "pt_desert_bandits"), #Veteran Renegades
      (party_set_slot, "p_norway_spawn_point", slot_party_spawn_target_spawns, 8),
      (party_set_slot, "p_norway_spawn_point", slot_party_spawn_radius, 45),
      
      (party_set_slot, "p_frisia_spawn_point", slot_party_bandit_type, "pt_frank_looters_1"), #Thieving Franks
      (party_set_slot, "p_frisia_spawn_point", slot_party_spawn_target_spawns, 6),
      (party_set_slot, "p_frisia_spawn_point", slot_party_spawn_radius, 10),
      
      (party_set_slot, "p_denmark_spawn_point", slot_party_bandit_type, "pt_frank_looters_2"), #Raiding Franks
      (party_set_slot, "p_denmark_spawn_point", slot_party_spawn_target_spawns, 6),
      (party_set_slot, "p_denmark_spawn_point", slot_party_spawn_radius, 25),
      
      (party_set_slot, "p_caitness_priest_spawn_point", slot_party_bandit_type, "pt_paganos_party"),
      (party_set_slot, "p_caitness_priest_spawn_point", slot_party_spawn_target_spawns, 8),
      (party_set_slot, "p_caitness_priest_spawn_point", slot_party_spawn_radius, 50),
      
      (party_set_slot, "p_northumbria_priest_spawn_point", slot_party_bandit_type, "pt_paganos_party"),
      (party_set_slot, "p_northumbria_priest_spawn_point", slot_party_spawn_target_spawns, 8),
      (party_set_slot, "p_northumbria_priest_spawn_point", slot_party_spawn_radius, 70),
      
      (party_set_slot, "p_norway_priest_spawn_point", slot_party_bandit_type, "pt_paganos_party"),
      (party_set_slot, "p_norway_priest_spawn_point", slot_party_spawn_target_spawns, 6),
      (party_set_slot, "p_norway_priest_spawn_point", slot_party_spawn_radius, 50),
      
      (party_set_slot, "p_denmark_priest_spawn_point", slot_party_bandit_type, "pt_paganos_party"),
      (party_set_slot, "p_denmark_priest_spawn_point", slot_party_spawn_target_spawns, 6),
      (party_set_slot, "p_denmark_priest_spawn_point", slot_party_spawn_radius, 45),
      
      (party_set_slot, "p_channel_spawn_point", slot_party_bandit_type, "pt_sea_raiders_ships2"), #Danish Vikingair
      (party_set_slot, "p_channel_spawn_point", slot_party_spawn_target_spawns, 8),
      (party_set_slot, "p_channel_spawn_point", slot_party_spawn_radius, 10),
      (party_set_slot, "p_channel_spawn_point", slot_party_spawn_flags, spsf_seaborne),
      (party_set_slot, "p_channel_spawn_point", slot_party_on_water, 1),
      
      (party_set_slot, "p_bight_spawn_point", slot_party_bandit_type, "pt_sea_raiders_ships"),  #Frankish Raiders
      (party_set_slot, "p_bight_spawn_point", slot_party_spawn_target_spawns, 8),
      (party_set_slot, "p_bight_spawn_point", slot_party_spawn_radius, 25),
      (party_set_slot, "p_bight_spawn_point", slot_party_spawn_flags, spsf_seaborne),
      (party_set_slot, "p_bight_spawn_point", slot_party_on_water, 1),
      
      (party_set_slot, "p_irish_sea_spawn_point", slot_party_bandit_type, "pt_sea_raiders_ships3"), #Vikingr
      (party_set_slot, "p_irish_sea_spawn_point", slot_party_spawn_target_spawns, 8),
      (party_set_slot, "p_irish_sea_spawn_point", slot_party_spawn_radius, 10),
      (party_set_slot, "p_irish_sea_spawn_point", slot_party_spawn_flags, spsf_seaborne),
      (party_set_slot, "p_irish_sea_spawn_point", slot_party_on_water, 1),
      
      (party_set_slot, "p_forth_firth_spawn_point", slot_party_bandit_type, "pt_sea_raiders_ships4"), #Norwegian Vikingr
      (party_set_slot, "p_forth_firth_spawn_point", slot_party_spawn_target_spawns, 6),
      (party_set_slot, "p_forth_firth_spawn_point", slot_party_spawn_radius, 25),
      (party_set_slot, "p_forth_firth_spawn_point", slot_party_spawn_flags, spsf_seaborne),
      (party_set_slot, "p_forth_firth_spawn_point", slot_party_on_water, 1),
      
      (party_set_slot, "p_skagerrak_spawn_point", slot_party_bandit_type, "pt_sea_raiders_ships5"), #Swedish Vikingr
      (party_set_slot, "p_skagerrak_spawn_point", slot_party_spawn_target_spawns, 6),
      (party_set_slot, "p_skagerrak_spawn_point", slot_party_spawn_radius, 25),
      (party_set_slot, "p_skagerrak_spawn_point", slot_party_spawn_flags, spsf_seaborne),
      (party_set_slot, "p_skagerrak_spawn_point", slot_party_on_water, 1),
      
      (party_set_slot, "p_firth_clyde_spawn_point", slot_party_bandit_type, "pt_sea_raiders_ships6"), #Raiders
      (party_set_slot, "p_firth_clyde_spawn_point", slot_party_spawn_target_spawns, 8),
      (party_set_slot, "p_firth_clyde_spawn_point", slot_party_spawn_radius, 30),
      (party_set_slot, "p_firth_clyde_spawn_point", slot_party_spawn_flags, spsf_seaborne),
      (party_set_slot, "p_firth_clyde_spawn_point", slot_party_on_water, 1),
      
      (party_template_set_slot, "pt_steppe_bandits", slot_party_template_lair_type, "pt_steppe_bandit_lair"),
      (party_template_set_slot, "pt_taiga_bandits", slot_party_template_lair_type, "pt_taiga_bandit_lair"),
      (party_template_set_slot, "pt_mountain_bandits", slot_party_template_lair_type, "pt_mountain_bandit_lair"),
      (party_template_set_slot, "pt_forest_bandits", slot_party_template_lair_type, "pt_forest_bandit_lair"),
      (party_template_set_slot, "pt_sea_raiders", slot_party_template_lair_type, "pt_sea_raider_lair"),
      (party_template_set_slot, "pt_sea_raiders2", slot_party_template_lair_type, "pt_sea_raider_lair2"),
      (party_template_set_slot, "pt_desert_bandits", slot_party_template_lair_type, "pt_desert_bandit_lair"),
      (party_template_set_slot, "pt_frank_looters_1", slot_party_template_lair_type, "pt_sea_raider_lair"),
      (party_template_set_slot, "pt_frank_looters_2", slot_party_template_lair_type, "pt_sea_raider_lair"),
      (party_template_set_slot, "pt_fianna", slot_party_template_lair_type, "pt_mountain_bandit_lair"),
      
      #we need to spawn more bandits in warband, because map is bigger. MOVED to mnu_start_game_4
      #(try_for_range, ":unused", 0, 7),
      # (call_script, "script_update_party_creation_random_limits"),
      # (call_script, "script_spawn_bandits"),
      #(try_end),
      
      #(set_spawn_radius, 50),
      #(try_for_range, ":unused", 0, 25),
      #  (spawn_around_party, "p_main_party", "pt_looters"),
      #(try_end),
      
      #we are adding looter parties around each village with 1/5 probability. DONE in script_spawn_bandits now
      # (set_spawn_radius, 5),
      # (try_for_range, ":cur_village", villages_begin, villages_end),
      # #(neq, ":cur_village", "p_village_150"),	#Phaiak: Do we want this? Player is week at beginning and cannot fight against looters
      # (store_random_in_range, ":random_value", 0, 5),
      # (eq, ":random_value", 0),
      # (spawn_around_party, ":cur_village", "pt_looters"),
      # (try_end),
      
      (call_script, "script_update_mercenary_units_of_towns"),
      (call_script, "script_update_companion_candidates_in_taverns"),
      (call_script, "script_update_ransom_brokers"),
      (call_script, "script_update_tavern_travellers"),
      (call_script, "script_update_tavern_minstrels"),
      (call_script, "script_update_booksellers"),
      
      (try_for_range, ":village_no", villages_begin, villages_end),
        (call_script, "script_update_volunteer_troops_in_village", ":village_no"),
      (try_end),
      
      #refugees in monasteries
      (try_for_range,":monastery","p_monasterio1","p_yourlair"),
        (party_set_slot,":monastery",slot_center_volunteer_troop_type,0),#can recruit
      (try_end),
      #holy pagan sites berserkers or pagan holy refuges
      (try_for_range,":hof","p_paganholysites1","p_reserved_1"),
        (party_set_slot,":hof",slot_center_volunteer_troop_type,0),#can recruit
      (try_end),
      
      (try_for_range, ":cur_kingdom", kingdoms_begin, kingdoms_end),
        (call_script, "script_update_faction_notes", ":cur_kingdom"),
        (store_random_in_range, ":random_no", -60, 0),
        (faction_set_slot, ":faction_no", slot_faction_last_offensive_concluded, ":random_no"),
      (try_end),
      
      (try_for_range, ":cur_troop", original_kingdom_heroes_begin, active_npcs_end),
        (call_script, "script_update_troop_notes", ":cur_troop"),
      (try_end),
      
      (try_for_range, ":cur_center", centers_begin, centers_end),
        (call_script, "script_update_center_notes", ":cur_center"),
      (try_end),
      
      (call_script, "script_update_troop_notes", "trp_player"),
      
      #Place kingdom ladies
      (try_for_range, ":troop_id", kingdom_ladies_begin, kingdom_ladies_end),
        (call_script, "script_get_kingdom_lady_social_determinants", ":troop_id"),
        (troop_set_slot, ":troop_id", slot_troop_cur_center, reg1),
      (try_end),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (assign, reg3, "$cheat_mode"),
        (display_message, "@{!}DEBUG : Located kingdom ladies, cheat mode: {reg3}"),
      (try_end),
      
      (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
        (call_script, "script_faction_recalculate_strength", ":faction_no"),
      (try_end),
      
      # #chief cambiado MOTO just use fac_culture name
      # (faction_set_slot, "fac_kingdom_1", slot_faction_adjective, "str_kingdom_1_adjective"),
      # (faction_set_slot, "fac_kingdom_2", slot_faction_adjective, "str_kingdom_2_adjective"),
      # (faction_set_slot, "fac_kingdom_3", slot_faction_adjective, "str_kingdom_3_adjective"),
      # (faction_set_slot, "fac_kingdom_4", slot_faction_adjective, "str_kingdom_4_adjective"),
      # (faction_set_slot, "fac_kingdom_5", slot_faction_adjective, "str_kingdom_5_adjective"),
      # (faction_set_slot, "fac_kingdom_6", slot_faction_adjective, "str_kingdom_6_adjective"),
      # (faction_set_slot, "fac_kingdom_7", slot_faction_adjective, "str_kingdom_7_adjective"),
      # (faction_set_slot, "fac_kingdom_8", slot_faction_adjective, "str_kingdom_8_adjective"),
      # (faction_set_slot, "fac_kingdom_9", slot_faction_adjective, "str_kingdom_9_adjective"),
      # (faction_set_slot, "fac_kingdom_10", slot_faction_adjective, "str_kingdom_10_adjective"),
      # (faction_set_slot, "fac_kingdom_11", slot_faction_adjective, "str_kingdom_11_adjective"),
      # (faction_set_slot, "fac_kingdom_12", slot_faction_adjective, "str_kingdom_12_adjective"),
      # (faction_set_slot, "fac_kingdom_13", slot_faction_adjective, "str_kingdom_13_adjective"),
      # (faction_set_slot, "fac_kingdom_14", slot_faction_adjective, "str_kingdom_14_adjective"),
      # (faction_set_slot, "fac_kingdom_15", slot_faction_adjective, "str_kingdom_15_adjective"),
      # (faction_set_slot, "fac_kingdom_16", slot_faction_adjective, "str_kingdom_16_adjective"),
      # (faction_set_slot, "fac_kingdom_17", slot_faction_adjective, "str_kingdom_17_adjective"),
      # (faction_set_slot, "fac_kingdom_18", slot_faction_adjective, "str_kingdom_18_adjective"),
      # (faction_set_slot, "fac_kingdom_19", slot_faction_adjective, "str_kingdom_19_adjective"),
      # (faction_set_slot, "fac_kingdom_20", slot_faction_adjective, "str_kingdom_20_adjective"),
      # (faction_set_slot, "fac_kingdom_21", slot_faction_adjective, "str_kingdom_21_adjective"),
      # #chief cambiado acaba
      
      #chief religion variables
      #emplazamientos paganos: facciones denmark, norwey and part of Isles Kingdom
      (party_set_slot,"p_town_4",slot_center_religion, 2),                 ##Set ribe como pagana
      (party_set_slot,"p_town_5",slot_center_religion, 2),                 ##Set Tunsberg como pagana
      (party_set_slot,"p_town_7",slot_center_religion, 2),                 ##Set Dubh linn como pagana
      (party_set_slot,"p_town_10",slot_center_religion, 2),                 ##Set Jorvik como pagana
      
      ##      (party_set_slot,"p_castle_49",center_religion_pagana, 1),                 ##Set ribe como pagana
      ##      (party_set_slot,"p_castle_50",center_religion_pagana, 1),                 ##Set ribe como pagana
      ##      (party_set_slot,"p_castle_51",center_religion_pagana, 1),                 ##Set ribe como pagana
      ##      (party_set_slot,"p_castle_52",center_religion_pagana, 1),                 ##Set ribe como pagana
      ##      (party_set_slot,"p_castle_53",center_religion_pagana, 1),                 ##Set ribe como pagana
      ##      (party_set_slot,"p_castle_54",center_religion_pagana, 1),                 ##Set ribe como pagana
      ##      (party_set_slot,"p_castle_55",center_religion_pagana, 1),                 ##Set ribe como pagana
      ##      (party_set_slot,"p_castle_56",center_religion_pagana, 1),                 ##Set ribe como pagana
      ##      (party_set_slot,"p_castle_60",center_religion_pagana, 1),                 ##Set ribe como pagana
      ##      (party_set_slot,"p_castle_61",center_religion_pagana, 1),                 ##Set ribe como pagana
      ##      (party_set_slot,"p_castle_62",center_religion_pagana, 1),                 ##Set ribe como pagana
      
      (party_set_slot,"p_village_5",slot_center_religion, 2),                 ##Set aldea como pagana  #denises ham northumbria
      (party_set_slot,"p_village_49",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_50",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_51",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_52",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_53",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_54",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_55",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_56",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_58",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_59",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_60",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_61",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_62",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_98",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_99",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_100",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_101",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_104",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_105",slot_center_religion, 2),                 ##Set aldea como pagana
      (party_set_slot,"p_village_108",slot_center_religion, 2),                 ##Set aldea como pagana   #jorvik
      (party_set_slot,"p_village_109",slot_center_religion, 2),                 ##Set aldea como pagana  #jorvik
      (party_set_slot,"p_castle_49",slot_center_religion, 2),
      (party_set_slot,"p_castle_50",slot_center_religion, 2),
      (party_set_slot,"p_castle_51",slot_center_religion, 2),
      (party_set_slot,"p_castle_52",slot_center_religion, 2),
      (party_set_slot,"p_castle_53",slot_center_religion, 2),
      (party_set_slot,"p_castle_54",slot_center_religion, 2),
      (party_set_slot,"p_castle_55",slot_center_religion, 2),
      (party_set_slot,"p_castle_56",slot_center_religion, 2),
      (party_set_slot,"p_castle_58",slot_center_religion, 2),
      (party_set_slot,"p_castle_59",slot_center_religion, 2),
      (party_set_slot,"p_castle_60",slot_center_religion, 2),
      (party_set_slot,"p_castle_61",slot_center_religion, 2),
      (party_set_slot,"p_castle_62",slot_center_religion, 2),
      #gente cristiana
      (try_for_range, ":center_no", centers_begin, centers_end),
        (try_begin),
          (neg|party_slot_eq, ":center_no", slot_center_religion, 2), #skip villages which are pagan.
          (party_set_slot,":center_no",slot_center_religion, 1), #el resto son cristianos
          (store_random_in_range, ":rand", 51, 90), #valores de fe
          (party_set_slot, ":center_no", slot_center_faithratio, ":rand"),
        (else_try),
          (party_slot_eq, ":center_no", slot_center_religion, 2), #anade fe a las villas paganas
          (store_random_in_range, ":rand", 5, 40),	# ratio is christian/pagan
          (party_set_slot, ":center_no", slot_center_faithratio, ":rand"),
        (try_end),
      (try_end),
      ## setup Friese, Denmark
      (store_random_in_range, ":rand", 50, 60), #valores de fe
      (party_set_slot, "p_town_11", slot_center_faithratio, ":rand"),
      (store_random_in_range, ":rand", 45, 60), #valores de fe
      (party_set_slot, "p_castle_63", slot_center_faithratio, ":rand"),
      (store_random_in_range, ":rand", 45, 60), #valores de fe
      (party_set_slot, "p_castle_64", slot_center_faithratio, ":rand"),
      (store_random_in_range, ":rand", 5, 10), #valores de fe
      (party_set_slot, "p_town_4", slot_center_faithratio, ":rand"),
      (store_random_in_range, ":rand", 5, 15), #valores de fe
      (party_set_slot, "p_town_5", slot_center_faithratio, ":rand"),
      (store_random_in_range, ":rand", 5, 15), #valores de fe
      (party_set_slot, "p_castle_50", slot_center_faithratio, ":rand"),
      (store_random_in_range, ":rand", 5, 15), #valores de fe
      (party_set_slot, "p_castle_56", slot_center_faithratio, ":rand"),
      ## end Friese
      #reinos paganos y cristianos
      (try_for_range, ":faction_no", npc_kingdoms_begin, npc_kingdoms_end), #Excluding player kingdom
        (this_or_next|is_between, ":faction_no", "fac_kingdom_1", "fac_kingdom_5"),
        (eq, ":faction_no", "fac_kingdom_8"),
        (faction_set_slot, ":faction_no", slot_faction_religion, cb3_pagan),
      (else_try),
        (faction_set_slot, ":faction_no", slot_faction_religion, cb3_christian),
      (try_end),
      #JuJu70 -religion setup
      (try_for_range, ":cur_troop", original_kingdom_heroes_begin,heroes_end),
        (store_faction_of_troop, ":faction_no", ":cur_troop"),
        (try_begin),
          (faction_slot_eq, ":faction_no", slot_faction_religion, cb3_christian),
          (troop_set_slot, ":cur_troop", slot_troop_religion, 1), # christian
        (else_try),
          (troop_set_slot, ":cur_troop", slot_troop_religion, 2),	#pagan
        (try_end),
      (try_end),
      (troop_set_slot, "trp_knight_8_1", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_knight_8_3", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_kingdom_8_lady_2", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_kingdom_8_lady_9", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_knight_8_5", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_knight_8_6", slot_troop_religion, 1), # christian
      #chief acaba
      
      
      ##      (assign, "$players_kingdom", "fac_kingdom_1"),
      ##      (call_script, "script_give_center_to_lord", "p_town_7", "trp_player", 0),
      ##      (call_script, "script_give_center_to_lord", "p_town_16", "trp_player", 0),
      ####      (call_script, "script_give_center_to_lord", "p_castle_10", "trp_player", 0),
      ##      (assign, "$g_castle_requested_by_player", "p_castle_10"),
      (party_set_slot, "p_main_party", slot_party_unrested_morale_penalty, 0),  #motomataru chief morale addition
      (call_script, "script_get_player_party_morale_values"),
      (party_set_morale, "p_main_party", 50),
      
      (troop_set_note_available, "trp_player", 1),
      
      (try_for_range, ":troop_no", kings_begin, kings_end),
        (troop_set_note_available, ":troop_no", 1),
      (try_end),
      
      (try_for_range, ":troop_no", lords_begin, lords_end),
        (troop_set_note_available, ":troop_no", 1),
      (try_end),
      
      (try_for_range, ":troop_no", kingdom_ladies_begin, kingdom_ladies_end),
        (troop_set_note_available, ":troop_no", 1),
      (try_end),
      (troop_set_note_available, "trp_knight_1_1_wife", 0),
      
      (try_for_range, ":troop_no", pretenders_begin, pretenders_end),
        (troop_set_note_available, ":troop_no", 1),
      (try_end),
      
      #Lady and companion notes become available as you meet/recruit them
      
      (try_for_range, ":faction_no", npc_kingdoms_begin, npc_kingdoms_end),
        (faction_set_note_available, ":faction_no", 1),
      (try_end),
      
      (try_for_range, ":party_no", centers_begin, centers_end),
        (party_set_note_available, ":party_no", 1),
      (try_end),
      
      ### Phaiak begin
      (call_script, "script_give_ships_to_towns"), 		# give ships to ports
      (assign, "$beaufort", 6),							# set beginning wind
      (assign, "$g_vc_menu_turned_off", 0),				# toggle new vc_menu on
      (assign, "$wounded_today", 0),
      (assign, "$total_wounds_ever", 0),
      
      # map ports and chimney smoke
      (assign, "$g_number_of_map_ports", 0),
      (assign, ":offset", 0),
      (try_for_range, ":cur_center", walled_centers_begin, walled_centers_end),
        (try_begin),#map ports
          (party_slot_eq, ":cur_center",slot_town_port, 1),
          #1. port party
          (set_spawn_radius, 0),
          (spawn_around_party, ":cur_center", "pt_port"),
          (assign, ":curr_port", reg0),
          (str_store_party_name, s1, ":cur_center"),
          (party_set_name, ":curr_port", "@Port of {s1}"),
          (party_set_slot, ":cur_center", slot_party_port_party, ":curr_port"),
          (party_set_slot, ":curr_port", slot_party_port_party, ":cur_center"),
          (party_get_position, pos1, ":cur_center"),
          (call_script, "script_get_next_water_position", 1),
          (party_set_position, ":curr_port", pos2),
          (store_faction_of_party, ":cur_faction", ":cur_center"),
          (party_set_faction, ":curr_port", ":cur_faction"),
          (party_set_slot, ":curr_port", slot_party_home_center, ":cur_center"),
          (party_set_flags, ":curr_port", pf_no_label, 1),
          (party_set_slot, ":curr_port", slot_party_on_water, 1),	#new 22.12.14
          (val_add, "$g_number_of_map_ports", 1),
          #2. coastal assault scene
          (store_add, ":cur_coastal_scene", "scn_town_3_coastal", ":offset"),
          (party_set_slot,":cur_center", slot_party_coastal_assault_scene, ":cur_coastal_scene"),
          (val_add, ":offset", 1),
        (try_end),
        
        # adjust position of map ports that would otherwise cause bugs
        (set_fixed_point_multiplier, 100),
        (party_get_slot, ":cur_port", "p_town_4", slot_party_port_party),
        (position_set_x, pos1, 22600),
        (position_set_y, pos1, -14150),
        (party_set_position, ":cur_port", pos1),
        
        #jetty system	(jettys are the ports for islands without port town)
        (call_script, "script_add_jetty_system"),
        
        (try_begin),#smoke on chimneys
          (get_party_ai_object, ":degree", ":cur_center"),
          (neq, ":degree", 0),
          (party_set_ai_object, ":cur_center", 0),
          (party_get_position, pos2, ":cur_center"),
          (position_get_rotation_around_z, ":wrong_rotation", pos2),
          (val_mul, ":wrong_rotation", -1),
          (position_rotate_z, pos2, ":wrong_rotation"),
          (position_rotate_z, pos2, ":degree"),
          (position_rotate_z, pos2, 180),
          (party_get_icon, ":icon", ":cur_center"),
          (try_begin),
            (eq, ":icon", "icon_town_port"),
            (call_script, "script_spawn_chimney_smoke", ":cur_center", -215, -81),
            (call_script, "script_spawn_chimney_smoke", ":cur_center", -87, -44),
            (call_script, "script_spawn_chimney_smoke", ":cur_center", 0, 25),
            (call_script, "script_spawn_chimney_smoke", ":cur_center", 87, 2),
            (call_script, "script_spawn_chimney_smoke", ":cur_center", 171, 7),
            (call_script, "script_spawn_chimney_smoke", ":cur_center", 345, -162),
          (else_try),
            (eq, ":icon", "icon_town_walled"),
            (call_script, "script_spawn_chimney_smoke", ":cur_center", -104, -128),
            (call_script, "script_spawn_chimney_smoke", ":cur_center", -35, -5),
            (call_script, "script_spawn_chimney_smoke", ":cur_center", 61, -19),
          (else_try),
            (eq, ":icon", "icon_fort1"),
            (call_script, "script_spawn_chimney_smoke", ":cur_center", -112, -83),
          (else_try),
            (eq, ":icon", "icon_fort2"),
            (call_script, "script_spawn_chimney_smoke", ":cur_center",  -10, -48),
          (end_try),
        (try_end),
      (try_end),
      
      #ferry system
      (try_for_range, ":cur_ferry_station", "p_ferry_1a", "p_jetty_1"), #ferry system
        (set_spawn_radius, 0),
        (spawn_around_party, ":cur_ferry_station", "pt_ferry_port"),
        (assign, ":curr_port", reg0),
        (party_set_slot, ":cur_ferry_station", slot_party_port_party, ":curr_port"),
        (party_set_slot, ":curr_port", slot_party_port_party, ":cur_ferry_station"),
        (party_get_position, pos1, ":cur_ferry_station"),
        (call_script, "script_get_next_water_position", 1),
        (party_set_position, ":curr_port", pos2),
      (try_end),
      
      # adjust position of map ports that would otherwise cause bugs
      (set_fixed_point_multiplier, 100),
      #Kennemer ferry
      (party_get_slot, ":cur_port", "p_ferry_1a", slot_party_port_party),
      (position_set_x, pos1, 19230),
      (position_set_y, pos1, -23108),
      (party_set_position, ":cur_port", pos1),
      (party_get_slot, ":cur_port", "p_ferry_1b", slot_party_port_party),
      (position_set_x, pos1, 19061),
      (position_set_y, pos1, -23304),
      (party_set_position, ":cur_port", pos1),
      #Caer Dyf ferry
      (party_get_slot, ":cur_port", "p_ferry_4a", slot_party_port_party),
      (position_set_x, pos1, -5871),
      (position_set_y, pos1, -16927),
      (party_set_position, ":cur_port", pos1),
      (party_get_slot, ":cur_port", "p_ferry_4b", slot_party_port_party),
      (position_set_x, pos1, -5370),
      (position_set_y, pos1, -16727),
      (party_set_position, ":cur_port", pos1),
      
      # bug prevent
      # (try_for_range, ":curr_town", towns_begin, towns_end),				# For some reason I have to delete this slots because they are filled after game start
      # (party_set_slot, ":curr_town", slot_party_shipyard_ship_type, 0),
      # (party_set_slot, ":curr_town", slot_party_shipyard_ship_time, 0),
      # (end_try),
      
      #icons
      (try_for_parties, ":party"),
        (gt, ":party", "p_spawn_points_end"),
        (party_is_active, ":party"),
        (call_script, "script_update_party_icon", ":party"),
      (try_end),
      
      #season shader initiation
      (set_fixed_point_multiplier, 1),
      (try_begin),
        (is_between, "$g_cur_month", 3, 6), # spring
        (set_shader_param_float, "@vSeason", shader_spring),
        (assign, "$shader_season", shader_spring),
      (else_try),
        (is_between, "$g_cur_month", 6, 9), # summer
        (set_shader_param_float, "@vSeason", shader_summer),
        (assign, "$shader_season", shader_summer),
      (else_try),
        (is_between, "$g_cur_month", 9, 12), # autumn
        (set_shader_param_float, "@vSeason", shader_autumn),
        (assign, "$shader_season", shader_autumn),
      (else_try),
        #(is_between, "$g_cur_month", 12, 3), # winter
        (set_shader_param_float, "@vSeason", shader_winter),
        (assign, "$shader_season", shader_winter),
      (end_try),
      #(call_script, "script_change_season_icons"),	#icons !!!!
      
      #wind shader initiation
      (set_fixed_point_multiplier,1),
      (assign, "$WindStrength_variable", 2), #value in range of 0 (no wind) to 3 (extreme wind)
      (assign, "$WindDirection_variable", 1),  #value in degrees of 0 (north), 90 (east), 180 (south), 270 (west), 359 (almost north)
      (set_shader_param_float, "@vWindStrength", "$WindStrength_variable"),
      (set_shader_param_float, "@vWindDirection", "$WindDirection_variable"),
      
      ### Phaiak end
      #JuJu70 bribery for bandits for now
      (try_for_parties, ":party_no"),
        (party_get_template_id, ":party_template", ":party_no"),
        (this_or_next|eq, ":party_template", "pt_looters"),
        (is_between, ":party_template", "pt_steppe_bandits","pt_merchant_caravan"),
        (party_set_slot, ":party_no", slot_party_bribed, 0),
      (try_end),
      #JuJu70 end
      
      (try_begin),
        # VC-2377 SET GOAL presentation
        (quest_set_slot, "qst_vc_menu", slot_set_goal_type, goal_empty),
        (assign, "$goal_type", goal_empty),
        (assign, "$set_goal_new_game_trigger", 0),
        (quest_set_slot, "qst_vc_menu", slot_set_goal_completed_raider, goal_not_complete),
        (quest_set_slot, "qst_vc_menu", slot_set_goal_completed_warrior, goal_not_complete),
        (quest_set_slot, "qst_vc_menu", slot_set_goal_completed_lord, goal_not_complete),
        (quest_set_slot, "qst_vc_menu", slot_set_goal_completed_king, goal_not_complete),
        (quest_set_slot, "qst_vc_menu", slot_set_goal_completed_expand, goal_not_complete),
      (try_end),
  ]),
  
  #script_game_get_use_string
  # This script is called from the game engine for getting using information text
  # INPUT: used_scene_prop_id
  # OUTPUT: s0
  ("game_get_use_string",
    [
      (store_script_param, ":instance_id", 1),
      
      (prop_instance_get_scene_prop_kind, ":scene_prop_id", ":instance_id"),
      
      (try_begin),
        (this_or_next|eq, ":scene_prop_id", "spr_winch_b"),
        (eq, ":scene_prop_id", "spr_winch"),
        (assign, ":effected_object", "spr_portcullis"),
      (else_try),
        (this_or_next|eq, ":scene_prop_id", "spr_mp_stones_refill"),
        (this_or_next|eq, ":scene_prop_id", "spr_door_destructible"),
        (this_or_next|eq, ":scene_prop_id", "spr_castle_f_door_b"),
        (this_or_next|eq, ":scene_prop_id", "spr_castle_e_sally_door_a"),
        (this_or_next|eq, ":scene_prop_id", "spr_castle_f_sally_door_a"),
        (this_or_next|eq, ":scene_prop_id", "spr_earth_sally_gate_left"),
        (this_or_next|eq, ":scene_prop_id", "spr_earth_sally_gate_right"),
        (this_or_next|eq, ":scene_prop_id", "spr_viking_keep_destroy_sally_door_left"),
        (this_or_next|eq, ":scene_prop_id", "spr_viking_keep_destroy_sally_door_right"),
        (this_or_next|eq, ":scene_prop_id", "spr_castle_f_door_a"),
        (this_or_next|eq, ":scene_prop_id", "spr_siege_ladder_move_6m"),
        (this_or_next|eq, ":scene_prop_id", "spr_siege_ladder_move_8m"),
        (this_or_next|eq, ":scene_prop_id", "spr_siege_ladder_move_10m"),
        (this_or_next|eq, ":scene_prop_id", "spr_siege_ladder_move_12m"),
        (eq, ":scene_prop_id", "spr_siege_ladder_move_14m"),
        (assign, ":effected_object", ":scene_prop_id"),
      (try_end),
      
      (scene_prop_get_slot, ":item_situation", ":instance_id", scene_prop_open_or_close_slot),
      
      (try_begin), #opening/closing portcullis
        (eq, ":effected_object", "spr_portcullis"),
        
        (try_begin),
          (eq, ":item_situation", 0),
          (str_store_string, s0, "str_open_gate"),
        (else_try),
          (str_store_string, s0, "str_close_gate"),
        (try_end),
      (else_try), #opening/closing door
        (this_or_next|eq, ":effected_object", "spr_door_destructible"),
        (this_or_next|eq, ":effected_object", "spr_castle_f_door_b"),
        (this_or_next|eq, ":effected_object", "spr_castle_e_sally_door_a"),
        (this_or_next|eq, ":effected_object", "spr_castle_f_sally_door_a"),
        (this_or_next|eq, ":effected_object", "spr_earth_sally_gate_left"),
        (this_or_next|eq, ":effected_object", "spr_earth_sally_gate_right"),
        (this_or_next|eq, ":effected_object", "spr_viking_keep_destroy_sally_door_left"),
        (this_or_next|eq, ":effected_object", "spr_viking_keep_destroy_sally_door_right"),
        (eq, ":effected_object", "spr_castle_f_door_a"),
        
        (try_begin),
          (eq, ":item_situation", 0),
          (str_store_string, s0, "str_open_door"),
        (else_try),
          (str_store_string, s0, "str_close_door"),
        (try_end),
      (else_try),
        (eq, ":scene_prop_id", "spr_inv_supply_box"),
        (try_begin),
          (gt,"$g_multiplayer_invasion_wave_number",1),
          (store_sub,":round","$g_multiplayer_invasion_wave_number",1),
          (val_max,":round",0),
          (val_mod,":round","$g_multiplayer_invasion_respawn_rounds"),
          (eq,":round",0),
          (str_store_string, s0, "@Refill Supplies"),
        (else_try),
          (str_clear,s0),
        (try_end),
      (else_try),
        (eq, ":scene_prop_id", "spr_mp_stones_refill"),
        (str_store_string, s0, "@Refill Stones"),
      (else_try), #raising/dropping ladder
        (try_begin),
          (eq, ":item_situation", 0),
          (str_store_string, s0, "str_raise_ladder"),
        (else_try),
          (str_store_string, s0, "str_drop_ladder"),
        (try_end),
      (try_end),
  ]),
  
  #script_game_quick_start
  # This script is called from the game engine for initializing the global variables for tutorial, multiplayer and custom battle modes.
  # INPUT:
  # none
  # OUTPUT:
  # none
  ("game_quick_start",
    [
      #init sounds and weather for scenes
      (call_script,"script_multi_set_scene_slots"),
      (call_script, "script_initialize_banner_info"),
      
      #for quick battle mode
      (assign, "$g_is_quick_battle", 0),
      (assign, "$g_quick_battle_game_type", 0),
      (assign, "$g_quick_battle_troop", quick_battle_troops_begin),
      (assign, "$g_quick_battle_map", quick_battle_scenes_begin),
      (assign, "$g_quick_battle_team_1_faction", "fac_culture_saxon"),
      (assign, "$g_quick_battle_team_2_faction", "fac_culture_norse"),
      (assign, "$g_quick_battle_army_1_size", 25),
      (assign, "$g_quick_battle_army_2_size", 25),
      
      (faction_set_slot, "fac_outlaws", slot_faction_quick_battle_tier_1_infantry, "trp_bandit"),
      (faction_set_slot, "fac_outlaws", slot_faction_quick_battle_tier_2_infantry, "trp_mountain_bandit"),
      (faction_set_slot, "fac_outlaws", slot_faction_quick_battle_tier_1_archer, "trp_forest_bandit"),
      (faction_set_slot, "fac_outlaws", slot_faction_quick_battle_tier_2_archer, "trp_brigand"),
      (faction_set_slot, "fac_outlaws", slot_faction_quick_battle_tier_1_cavalry, "trp_mercenary_horseman"),
      (faction_set_slot, "fac_outlaws", slot_faction_quick_battle_tier_2_cavalry, -1),
      (faction_set_slot, "fac_culture_norse", slot_faction_quick_battle_tier_1_infantry, "trp_norse_level0_landed"),
      (faction_set_slot, "fac_culture_norse", slot_faction_quick_battle_tier_2_infantry, "trp_norse_level2_companion"),
      (faction_set_slot, "fac_culture_norse", slot_faction_quick_battle_tier_1_archer, "trp_norse_slave"),
      (faction_set_slot, "fac_culture_norse", slot_faction_quick_battle_tier_2_archer, "trp_norse_elitearcher"),
      (faction_set_slot, "fac_culture_norse", slot_faction_quick_battle_tier_1_cavalry, "trp_frisian_cav"),
      (faction_set_slot, "fac_culture_norse", slot_faction_quick_battle_tier_2_cavalry, -1),
      (faction_set_slot, "fac_culture_welsh", slot_faction_quick_battle_tier_1_infantry, "trp_briton_level2_landed"),
      (faction_set_slot, "fac_culture_welsh", slot_faction_quick_battle_tier_2_infantry, "trp_briton_level3_landed"),
      (faction_set_slot, "fac_culture_welsh", slot_faction_quick_battle_tier_1_archer, "trp_briton_marksman"),
      (faction_set_slot, "fac_culture_welsh", slot_faction_quick_battle_tier_2_archer, -1),
      (faction_set_slot, "fac_culture_welsh", slot_faction_quick_battle_tier_1_cavalry, "trp_briton_horseman"),
      (faction_set_slot, "fac_culture_welsh", slot_faction_quick_battle_tier_2_cavalry, -1),
      (faction_set_slot, "fac_culture_saxon", slot_faction_quick_battle_tier_1_infantry, "trp_saxon_level2_landed"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_quick_battle_tier_2_infantry, "trp_saxon_level3_landed"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_quick_battle_tier_1_archer, "trp_saxon_slave"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_quick_battle_tier_2_archer, "trp_saxon_bowman"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_quick_battle_tier_1_cavalry, "trp_saxon_horseman"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_quick_battle_tier_2_cavalry, -1),
      (faction_set_slot, "fac_culture_scotch", slot_faction_quick_battle_tier_1_infantry, "trp_scotch_level1_landed"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_quick_battle_tier_2_infantry, "trp_scotch_level1_companion"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_quick_battle_tier_1_archer, "trp_scotch_bowman"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_quick_battle_tier_2_archer, "trp_scotch_level1_skirmisher"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_quick_battle_tier_1_cavalry, "trp_scotch_horseman"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_quick_battle_tier_2_cavalry, "trp_scotch_knight"),
      (faction_set_slot, "fac_culture_angle", slot_faction_quick_battle_tier_1_infantry, "trp_angle_level0_companion"),
      (faction_set_slot, "fac_culture_angle", slot_faction_quick_battle_tier_2_infantry, "trp_angle_level2_companion"),
      (faction_set_slot, "fac_culture_angle", slot_faction_quick_battle_tier_1_archer, "trp_angle_slave"),
      (faction_set_slot, "fac_culture_angle", slot_faction_quick_battle_tier_2_archer, "trp_angle_bowman"),
      (faction_set_slot, "fac_culture_angle", slot_faction_quick_battle_tier_1_cavalry, "trp_angle_horseman"),
      (faction_set_slot, "fac_culture_angle", slot_faction_quick_battle_tier_2_cavalry, -1),
      (faction_set_slot, "fac_culture_irish", slot_faction_quick_battle_tier_1_infantry, "trp_irish_level2_landed"),
      (faction_set_slot, "fac_culture_irish", slot_faction_quick_battle_tier_2_infantry, "trp_irish_level1_companion"),
      (faction_set_slot, "fac_culture_irish", slot_faction_quick_battle_tier_1_archer, "trp_irish_slave"),
      (faction_set_slot, "fac_culture_irish", slot_faction_quick_battle_tier_2_archer, "trp_irish_level0_skirmisher"),
      (faction_set_slot, "fac_culture_irish", slot_faction_quick_battle_tier_1_cavalry, "trp_irish_horseman"),
      (faction_set_slot, "fac_culture_irish", slot_faction_quick_battle_tier_2_cavalry, "trp_irish_knight"),
      
      #for multiplayer mode
      (assign, "$g_multiplayer_selected_map", multiplayer_scenes_begin),
      (assign, "$g_multiplayer_respawn_period", 5),
      (assign, "$g_multiplayer_round_max_seconds", 900), #chief capitan cambia para que sean 900 seg -15 m- en vez de 300, que es muy corto
      (assign, "$g_multiplayer_game_max_minutes", 60), #chief capitan cambia para que sean 60 m en vez de 30
      (assign, "$g_multiplayer_game_max_points", 300),
      
      (server_get_renaming_server_allowed, "$g_multiplayer_renaming_server_allowed"),
      (server_get_changing_game_type_allowed, "$g_multiplayer_changing_game_type_allowed"),
      (assign, "$g_multiplayer_point_gained_from_flags", 100),
      (assign, "$g_multiplayer_point_gained_from_capturing_flag", 5),
      (assign, "$g_multiplayer_game_type", 0),
      (assign, "$g_multiplayer_team_1_faction", "fac_culture_norse"),
      (assign, "$g_multiplayer_team_2_faction", "fac_culture_saxon"),
      (assign, "$g_multiplayer_next_team_1_faction", "$g_multiplayer_team_1_faction"),
      (assign, "$g_multiplayer_next_team_2_faction", "$g_multiplayer_team_2_faction"),
      (assign, "$g_multiplayer_num_bots_team_1", 0),
      (assign, "$g_multiplayer_num_bots_team_2", 0),
      (assign, "$g_multiplayer_number_of_respawn_count", 0),
      (assign, "$g_multiplayer_num_bots_voteable", 50),
      (assign, "$g_multiplayer_max_num_bots", 101),
      (assign, "$g_multiplayer_factions_voteable", 1),
      (assign, "$g_multiplayer_maps_voteable", 1),
      (assign, "$g_multiplayer_kick_voteable", 1),
      (assign, "$g_multiplayer_ban_voteable", 1),
      (assign, "$g_multiplayer_gametype_voteable", 0),
      (assign, "$g_multiplayer_valid_vote_ratio", 51), #more than 50 percent
      (assign, "$g_multiplayer_auto_team_balance_limit", 3), #auto balance when difference is more than 2
      (assign, "$g_multiplayer_player_respawn_as_bot", 1),
      (assign, "$g_multiplayer_stats_chart_opened_manually", 0),
      (assign, "$g_multiplayer_mission_end_screen", 0),
      (assign, "$g_multiplayer_ready_for_spawning_agent", 1),
      (assign, "$g_multiplayer_welcome_message_shown", 0),
      (assign, "$g_multiplayer_allow_player_banners", 1),
      (assign, "$g_multiplayer_force_default_armor", 1),
      (assign, "$g_multiplayer_disallow_ranged_weapons", 0),
      (assign, "$g_multiplayer_allow_shield_bash", 0),
      (assign, "$g_multiplayer_allow_fall_from_horse", 1),
      (assign, "$g_multiplayer_allow_shieldwalls", 1),
      (assign, "$g_multiplayer_remove_stray_horses", 150),
      (assign, "$g_multiplayer_god_mode", 0),
      (assign, "$g_multiplayer_troop_classes", 1),
      (assign, "$g_multiplayer_poll_cooldown", multiplayer_poll_cooldown),
      (assign, "$g_multiplayer_poll_cooldown_counter", 0),
      
      (assign, "$g_multiplayer_initial_gold_multiplier", 100),
      (assign, "$g_multiplayer_battle_earnings_multiplier", 100),
      (assign, "$g_multiplayer_round_earnings_multiplier", 100),
      
      #faction banners
      (faction_set_slot, "fac_culture_norse", slot_faction_banner, "mesh_banner_kingdom_g"),
      (faction_set_slot, "fac_culture_saxon", slot_faction_banner, "mesh_banner_kingdom_e"),
      (faction_set_slot, "fac_culture_angle", slot_faction_banner, "mesh_banner_kingdom_f"),
      (faction_set_slot, "fac_culture_welsh", slot_faction_banner, "mesh_banner_kingdom_h"),
      (faction_set_slot, "fac_culture_irish", slot_faction_banner, "mesh_banner_kingdom_i"),
      (faction_set_slot, "fac_culture_scotch", slot_faction_banner, "mesh_banner_kingdom_c"),
      
      ##      #alternate descriptions
      ##      (troop_set_slot, "trp_norse_slave", slot_troop_multi_desc, "str_norse_slave"),
      ##      (troop_set_slot, "trp_norse_level0_landed", slot_troop_multi_desc, "str_norse_level0_landed"),
      ##      (troop_set_slot, "trp_norse_bowman", slot_troop_multi_desc, "str_norse_bowman"),
      ##      (troop_set_slot, "trp_norse_level0_companion", slot_troop_multi_desc, "str_norse_level0_companion"),
      ##      (troop_set_slot, "trp_norse_level1_landed", slot_troop_multi_desc, "str_norse_level1_landed"),
      ##      (troop_set_slot, "trp_norse_level2_landed", slot_troop_multi_desc, "str_norse_level2_landed"),
      ##      (troop_set_slot, "trp_norse_elitearcher", slot_troop_multi_desc, "str_norse_elitearcher"),
      ##      (troop_set_slot, "trp_norse_level3_landed", slot_troop_multi_desc, "str_norse_level3_landed"),
      ##      (troop_set_slot, "trp_norse_level1_companion", slot_troop_multi_desc, "str_norse_level1_companion"),
      ##      (troop_set_slot, "trp_norse_level2_companion", slot_troop_multi_desc, "str_norse_level2_companion"),
      ##      (troop_set_slot, "trp_briton_slave", slot_troop_multi_desc, "str_briton_slave"),
      ##      (troop_set_slot, "trp_briton_level0_landed", slot_troop_multi_desc, "str_briton_level0_landed"),
      ##      (troop_set_slot, "trp_briton_level1_landed", slot_troop_multi_desc, "str_briton_level1_landed"),
      ##      (troop_set_slot, "trp_briton_level2_landed", slot_troop_multi_desc, "str_briton_level2_landed"),
      ##      (troop_set_slot, "trp_briton_level3_landed", slot_troop_multi_desc, "str_briton_level3_landed"),
      ##      (troop_set_slot, "trp_briton_level0_companion", slot_troop_multi_desc, "str_briton_level0_companion"),
      ##      (troop_set_slot, "trp_briton_level1_companion", slot_troop_multi_desc, "str_briton_level1_companion"),
      ##      (troop_set_slot, "trp_briton_level2_companion", slot_troop_multi_desc, "str_briton_level2_companion"),
      ##      (troop_set_slot, "trp_briton_bowman", slot_troop_multi_desc, "str_briton_bowman"),
      ##      (troop_set_slot, "trp_briton_marksman", slot_troop_multi_desc, "str_briton_marksman"),
      ##      (troop_set_slot, "trp_briton_horseman", slot_troop_multi_desc, "str_briton_horseman"),
      ##      (troop_set_slot, "trp_saxon_slave", slot_troop_multi_desc, "str_saxon_slave"),
      ##      (troop_set_slot, "trp_saxon_level0_landed", slot_troop_multi_desc, "str_saxon_level0_landed"),
      ##      (troop_set_slot, "trp_saxon_bowman", slot_troop_multi_desc, "str_saxon_bowman"),
      ##      (troop_set_slot, "trp_saxon_level0_companion", slot_troop_multi_desc, "str_saxon_level0_companion"),
      ##      (troop_set_slot, "trp_saxon_level1_landed", slot_troop_multi_desc, "str_saxon_level1_landed"),
      ##      (troop_set_slot, "trp_saxon_level2_landed", slot_troop_multi_desc, "str_saxon_level2_landed"),
      ##      (troop_set_slot, "trp_saxon_horseman", slot_troop_multi_desc, "str_saxon_horseman"),
      ##      (troop_set_slot, "trp_saxon_level3_landed", slot_troop_multi_desc, "str_saxon_level3_landed"),
      ##      (troop_set_slot, "trp_saxon_level1_companion", slot_troop_multi_desc, "str_saxon_level1_companion"),
      ##      (troop_set_slot, "trp_saxon_level2_companion", slot_troop_multi_desc, "str_saxon_level2_companion"),
      ##      (troop_set_slot, "trp_scotch_peasant", slot_troop_multi_desc, "str_scotch_peasant"),
      ##      (troop_set_slot, "trp_scotch_bowman", slot_troop_multi_desc, "str_scotch_bowman"),
      ##      (troop_set_slot, "trp_scotch_level0_landed", slot_troop_multi_desc, "str_scotch_level0_landed"),
      ##      (troop_set_slot, "trp_scotch_level0_skirmisher", slot_troop_multi_desc, "str_scotch_level0_skirmisher"),
      ##      (troop_set_slot, "trp_scotch_level1_skirmisher", slot_troop_multi_desc, "str_scotch_level1_skirmisher"),
      ##      (troop_set_slot, "trp_scotch_level1_landed", slot_troop_multi_desc, "str_scotch_level1_landed"),
      ##      (troop_set_slot, "trp_scotch_level2_landed", slot_troop_multi_desc, "str_scotch_level2_landed"),
      ##      (troop_set_slot, "trp_scotch_level0_companion", slot_troop_multi_desc, "str_scotch_level0_companion"),
      ##      (troop_set_slot, "trp_scotch_level1_companion", slot_troop_multi_desc, "str_scotch_level1_companion"),
      ##      (troop_set_slot, "trp_scotch_level2_companion", slot_troop_multi_desc, "str_scotch_level2_companion"),
      ##      (troop_set_slot, "trp_scotch_horseman", slot_troop_multi_desc, "str_scotch_horseman"),
      ##      (troop_set_slot, "trp_scotch_knight", slot_troop_multi_desc, "str_scotch_knight"),
      ##      (troop_set_slot, "trp_angle_slave", slot_troop_multi_desc, "str_angle_slave"),
      ##      (troop_set_slot, "trp_angle_level0_landed", slot_troop_multi_desc, "str_angle_level0_landed"),
      ##      (troop_set_slot, "trp_angle_level0_companion", slot_troop_multi_desc, "str_angle_level0_companion"),
      ##      (troop_set_slot, "trp_angle_level1_landed", slot_troop_multi_desc, "str_angle_level1_landed"),
      ##      (troop_set_slot, "trp_angle_level2_landed", slot_troop_multi_desc, "str_angle_level2_landed"),
      ##      (troop_set_slot, "trp_angle_horseman", slot_troop_multi_desc, "str_angle_horseman"),
      ##      (troop_set_slot, "trp_angle_bowman", slot_troop_multi_desc, "str_angle_bowman"),
      ##      (troop_set_slot, "trp_angle_level3_landed", slot_troop_multi_desc, "str_angle_level3_landed"),
      ##      (troop_set_slot, "trp_angle_level1_companion", slot_troop_multi_desc, "str_angle_level1_companion"),
      ##      (troop_set_slot, "trp_angle_level2_companion", slot_troop_multi_desc, "str_angle_level2_companion"),
      ##      (troop_set_slot, "trp_irish_slave", slot_troop_multi_desc, "str_irish_slave"),
      ##      (troop_set_slot, "trp_irish_bowman", slot_troop_multi_desc, "str_irish_bowman"),
      ##      (troop_set_slot, "trp_irish_level0_skirmisher", slot_troop_multi_desc, "str_irish_level0_skirmisher"),
      ##      (troop_set_slot, "trp_irish_level1_skirmisher", slot_troop_multi_desc, "str_irish_level1_skirmisher"),
      ##      (troop_set_slot, "trp_irish_level0_landed", slot_troop_multi_desc, "str_irish_level0_landed"),
      ##      (troop_set_slot, "trp_irish_level1_landed", slot_troop_multi_desc, "str_irish_level1_landed"),
      ##      (troop_set_slot, "trp_irish_level0_companion", slot_troop_multi_desc, "str_irish_level0_companion"),
      ##      (troop_set_slot, "trp_irish_level1_companion", slot_troop_multi_desc, "str_irish_level1_companion"),
      ##      (troop_set_slot, "trp_irish_level2_landed", slot_troop_multi_desc, "str_irish_level2_landed"),
      ##      (troop_set_slot, "trp_irish_level3_landed", slot_troop_multi_desc, "str_irish_level3_landed"),
      ##      (troop_set_slot, "trp_irish_horseman", slot_troop_multi_desc, "str_irish_horseman"),
      ##      (troop_set_slot, "trp_irish_knight", slot_troop_multi_desc, "str_irish_knight"),
      
      (try_for_range, ":cur_item", all_items_begin, all_items_end),
        (try_for_range, ":cur_faction", mp_factions_begin, mp_factions_end),
          (store_sub, ":faction_index", ":cur_faction", mp_factions_begin),
          (val_add, ":faction_index", slot_item_multiplayer_faction_price_multipliers_begin),
          (item_set_slot, ":cur_item", ":faction_index", 100), #100 is the default price multiplier
        (try_end),
      (try_end),
      (store_sub, ":norse_price_slot", "fac_culture_norse", mp_factions_begin),
      (val_add, ":norse_price_slot", slot_item_multiplayer_faction_price_multipliers_begin),
      (store_sub, ":saxon_price_slot", "fac_culture_saxon", mp_factions_begin),
      (val_add, ":saxon_price_slot", slot_item_multiplayer_faction_price_multipliers_begin),
      (store_sub, ":angle_price_slot", "fac_culture_angle", mp_factions_begin),
      (val_add, ":angle_price_slot", slot_item_multiplayer_faction_price_multipliers_begin),
      (store_sub, ":welsh_price_slot", "fac_culture_welsh", mp_factions_begin),
      (val_add, ":welsh_price_slot", slot_item_multiplayer_faction_price_multipliers_begin),
      (store_sub, ":irish_price_slot", "fac_culture_irish", mp_factions_begin),
      (val_add, ":irish_price_slot", slot_item_multiplayer_faction_price_multipliers_begin),
      (store_sub, ":scotch_price_slot", "fac_culture_scotch", mp_factions_begin),
      (val_add, ":scotch_price_slot", slot_item_multiplayer_faction_price_multipliers_begin),
      
      (item_set_slot, "itm_horn_multi", ":norse_price_slot", 35),
      (item_set_slot, "itm_horn_multi", ":saxon_price_slot", 35),
      (item_set_slot, "itm_horn_multi", ":angle_price_slot", 35),
      (item_set_slot, "itm_horn_multi", ":welsh_price_slot", 35),
      (item_set_slot, "itm_horn_multi", ":irish_price_slot", 35),
      (item_set_slot, "itm_horn_multi", ":scotch_price_slot", 35),
      
      # (try_for_range, ":slot", slot_item_multiplayer_faction_price_multipliers_begin, slot_item_multiplayer_item_class),
      # (item_set_slot, "itm_light_spear1", ":slot", 100),
      # (item_set_slot, "itm_light_spear2", ":slot", 110),
      # (item_set_slot, "itm_war_spear1", ":slot", 130),
      # (item_set_slot, "itm_war_spear2", ":slot", 120),
      # (item_set_slot, "itm_heavy_spear1", ":slot", 140),
      # (item_set_slot, "itm_heavy_spear2", ":slot", 130),
      # (item_set_slot, "itm_long_light_spear1", ":slot", 110),
      # (item_set_slot, "itm_long_light_spear2", ":slot", 100),
      # (item_set_slot, "itm_long_war_spear1", ":slot", 130),
      # (item_set_slot, "itm_long_war_spear2", ":slot", 140),
      # (item_set_slot, "itm_long_heavy_spear1", ":slot", 150),
      # (item_set_slot, "itm_long_heavy_spear2", ":slot", 140),
      # (try_end),
      (try_for_range, ":slot", slot_item_multiplayer_faction_price_multipliers_begin, slot_item_multiplayer_item_class),
        (item_set_slot, "itm_light_spear1", ":slot", 90),
        (item_set_slot, "itm_light_spear2", ":slot", 100),
        (item_set_slot, "itm_war_spear1", ":slot", 145),
        (item_set_slot, "itm_war_spear2", ":slot", 135),
        (item_set_slot, "itm_heavy_spear1", ":slot", 165),
        (item_set_slot, "itm_heavy_spear2", ":slot", 155),
        (item_set_slot, "itm_long_light_spear1", ":slot", 130),
        (item_set_slot, "itm_long_light_spear2", ":slot", 120),
        (item_set_slot, "itm_long_war_spear1", ":slot", 170),
        (item_set_slot, "itm_long_war_spear2", ":slot", 180),
        (item_set_slot, "itm_long_heavy_spear1", ":slot", 210),
        (item_set_slot, "itm_long_heavy_spear2", ":slot", 180),
      (try_end),
      
      
      (item_set_slot, "itm_knife", ":norse_price_slot", 50),
      (item_set_slot, "itm_spatha_8", ":norse_price_slot", 20),
      (item_set_slot, "itm_sword", ":norse_price_slot", 22),
      (item_set_slot, "itm_sword_5", ":norse_price_slot", 22),
      (item_set_slot, "itm_sword_6", ":norse_price_slot", 22),
      (item_set_slot, "itm_sword_7", ":norse_price_slot", 22),
      (item_set_slot, "itm_sword_8", ":norse_price_slot", 22),
      (item_set_slot, "itm_noble_sword_4", ":norse_price_slot", 22),
      (item_set_slot, "itm_new_sword3", ":norse_price_slot", 20),
      (item_set_slot, "itm_new_sword4", ":norse_price_slot", 20),
      (try_for_range,":item","itm_light_spear1","itm_new_mace"),
        (item_get_slot,":price_mod",":item",":norse_price_slot"),
        (val_mul,":price_mod",70),
        (val_div,":price_mod",100),
        (item_set_slot,":item",":norse_price_slot",":price_mod"),
      (try_end),
      (item_set_slot, "itm_carbatinae_1", ":norse_price_slot", 50),
      (item_set_slot, "itm_carbatinae_vc1v", ":norse_price_slot", 60),
      (item_set_slot, "itm_carbatinae_1v", ":norse_price_slot", 95),
      (item_set_slot, "itm_btunic_3", ":norse_price_slot", 40),
      (item_set_slot, "itm_btunic_8", ":norse_price_slot", 40),
      (item_set_slot, "itm_btunic_15", ":norse_price_slot", 70),
      (item_set_slot, "itm_gambeson13", ":norse_price_slot", 25),
      (item_set_slot, "itm_gambeson25", ":norse_price_slot", 25),
      (item_set_slot, "itm_byrnie17", ":norse_price_slot", 50),
      (item_set_slot, "itm_byrnie34", ":norse_price_slot", 50), #with fur
      (item_set_slot, "itm_javelin", ":norse_price_slot", 200),
      (item_set_slot, "itm_javelin_skirmishes", ":norse_price_slot", 200),
      (item_set_slot, "itm_phrygian1", ":norse_price_slot", 10),
      (item_set_slot, "itm_phrygian7", ":norse_price_slot", 25),
      (item_set_slot, "itm_phrygian14", ":norse_price_slot", 25),
      (item_set_slot, "itm_spangenhelm_13", ":norse_price_slot", 10),
      (item_set_slot, "itm_spangenhelm_17", ":norse_price_slot", 10),
      (item_set_slot, "itm_viking_elitehelm1", ":norse_price_slot", 15),
      (item_set_slot, "itm_viking_elitehelm7", ":norse_price_slot", 15),
      (item_set_slot, "itm_viking_helm", ":norse_price_slot", 15),
      (item_set_slot, "itm_viking_helm4", ":norse_price_slot", 15),
      (item_set_slot, "itm_viking_helm8", ":norse_price_slot", 25),
      (item_set_slot, "itm_viking_helm12", ":norse_price_slot", 30),
      (item_set_slot, "itm_common_pony2", ":norse_price_slot", 23),
      (item_set_slot, "itm_common_pony", ":norse_price_slot", 23),
      
      (item_set_slot, "itm_spatha", ":saxon_price_slot", 22),
      (item_set_slot, "itm_noble_swordv4", ":saxon_price_slot", 22),
      (item_set_slot, "itm_noble_swordv3", ":saxon_price_slot", 18),
      (item_set_slot, "itm_noble_sword", ":saxon_price_slot", 20),
      (item_set_slot, "itm_axe_2", ":saxon_price_slot", 120),
      (try_for_range,":item","itm_light_spear1","itm_new_mace"),
        (item_get_slot,":price_mod",":item",":saxon_price_slot"),
        (val_mul,":price_mod",70),
        (val_div,":price_mod",100),
        (item_set_slot,":item",":saxon_price_slot",":price_mod"),
      (try_end),
      (item_set_slot, "itm_carbatinae_vc2", ":saxon_price_slot", 30),
      (item_set_slot, "itm_carbatinae_2", ":saxon_price_slot", 40),
      (item_set_slot, "itm_carbatinae_vc2s", ":saxon_price_slot", 40),
      (item_set_slot, "itm_carbatinae_2s", ":saxon_price_slot", 70),
      (item_set_slot, "itm_carbatinae_12qs", ":saxon_price_slot", 90),
      (item_set_slot, "itm_bl_tunic04", ":saxon_price_slot", 30),
      (item_set_slot, "itm_bl_tunic06", ":saxon_price_slot", 30),
      (item_set_slot, "itm_bl_tunic02", ":saxon_price_slot", 60),
      (item_set_slot, "itm_gambeson3", ":saxon_price_slot", 20),
      (item_set_slot, "itm_gambeson32", ":saxon_price_slot", 20),
      (item_set_slot, "itm_mail_shirt_2", ":saxon_price_slot", 50),
      (item_set_slot, "itm_byrnie21", ":saxon_price_slot", 50),
      (item_set_slot, "itm_byrnie22", ":saxon_price_slot", 50),
      (item_set_slot, "itm_javelin", ":saxon_price_slot", 200),
      (item_set_slot, "itm_javelin_skirmishes", ":saxon_price_slot", 200),
      (item_set_slot, "itm_phrygian2", ":saxon_price_slot", 10),
      (item_set_slot, "itm_phrygian8", ":saxon_price_slot", 25),
      (item_set_slot, "itm_phrygian15", ":saxon_price_slot", 25),
      (item_set_slot, "itm_spangenhelm_1", ":saxon_price_slot", 10),
      (item_set_slot, "itm_spangenhelm_6", ":saxon_price_slot", 10),
      (item_set_slot, "itm_spangenhelm_9", ":saxon_price_slot", 10),
      (item_set_slot, "itm_spangenhelm_39", ":saxon_price_slot", 10),
      
      (item_set_slot, "itm_spangenhelm_13", ":saxon_price_slot", 10),
      (item_set_slot, "itm_spangenhelm_17", ":saxon_price_slot", 12),
      (item_set_slot, "itm_spangenhelm_18", ":saxon_price_slot", 12),
      (item_set_slot, "itm_angle_helmet1", ":saxon_price_slot", 15),
      (item_set_slot, "itm_angle_helmet2", ":saxon_price_slot", 15),
      (item_set_slot, "itm_angle_helmet5", ":saxon_price_slot", 17),
      (item_set_slot, "itm_angle_helmet6", ":saxon_price_slot", 25),
      (item_set_slot, "itm_angle_helmet4", ":saxon_price_slot", 27),
      (item_set_slot, "itm_common_pony2", ":saxon_price_slot", 23),
      (item_set_slot, "itm_common_pony", ":saxon_price_slot", 23),
      
      (item_set_slot, "itm_noble_swordv4", ":angle_price_slot", 22),
      (item_set_slot, "itm_spatha", ":angle_price_slot", 22),
      (item_set_slot, "itm_spatha_7", ":angle_price_slot", 22),
      (item_set_slot, "itm_noble_swordv3", ":angle_price_slot", 18),
      (item_set_slot, "itm_noble_sword", ":angle_price_slot", 20),
      (item_set_slot, "itm_noble_sword_2", ":angle_price_slot", 20),
      (item_set_slot, "itm_noble_sword_3", ":angle_price_slot", 20),
      (item_set_slot, "itm_axe_2", ":angle_price_slot", 120),
      (try_for_range,":item","itm_light_spear1","itm_new_mace"),
        (item_get_slot,":price_mod",":item",":angle_price_slot"),
        (val_mul,":price_mod",70),
        (val_div,":price_mod",100),
        (item_set_slot,":item",":angle_price_slot",":price_mod"),
      (try_end),
      (item_set_slot, "itm_carbatinae_vc2", ":angle_price_slot", 30),
      (item_set_slot, "itm_carbatinae_2", ":angle_price_slot", 40),
      (item_set_slot, "itm_carbatinae_vc2s", ":angle_price_slot", 40),
      (item_set_slot, "itm_carbatinae_2s", ":angle_price_slot", 70),
      (item_set_slot, "itm_carbatinae_12qs", ":angle_price_slot", 90),
      (item_set_slot, "itm_ptunic_2", ":angle_price_slot", 30),
      (item_set_slot, "itm_ptunic_1", ":angle_price_slot", 60),
      (item_set_slot, "itm_ptunic_3", ":angle_price_slot", 60),
      (item_set_slot, "itm_bl_tunic02", ":angle_price_slot", 60),
      (item_set_slot, "itm_gambeson39", ":angle_price_slot", 20),
      (item_set_slot, "itm_gambeson32", ":angle_price_slot", 20),
      (item_set_slot, "itm_gambeson42", ":angle_price_slot", 28),
      (item_set_slot, "itm_gambeson10", ":angle_price_slot", 25),
      (item_set_slot, "itm_byrnie28", ":angle_price_slot", 25),
      (item_set_slot, "itm_byrnie30", ":angle_price_slot", 25),
      (item_set_slot, "itm_mail_shirt_2", ":angle_price_slot", 50),
      (item_set_slot, "itm_javelin", ":angle_price_slot", 200),
      (item_set_slot, "itm_javelin_skirmishes", ":angle_price_slot", 200),
      (item_set_slot, "itm_phrygian2", ":angle_price_slot", 10),
      (item_set_slot, "itm_phrygian8", ":angle_price_slot", 25),
      (item_set_slot, "itm_phrygian15", ":angle_price_slot", 25),
      (item_set_slot, "itm_spangenhelm_2", ":angle_price_slot", 10),
      (item_set_slot, "itm_spangenhelm_8", ":angle_price_slot", 10),
      (item_set_slot, "itm_spangenhelm_10", ":angle_price_slot", 10),
      (item_set_slot, "itm_spangenhelm_14", ":angle_price_slot", 10),
      (item_set_slot, "itm_spangenhelm_16", ":angle_price_slot", 10),
      (item_set_slot, "itm_spangenhelm_22", ":angle_price_slot", 10),
      (item_set_slot, "itm_spangenhelm_40", ":angle_price_slot", 10),
      (item_set_slot, "itm_spangenhelm_13", ":angle_price_slot", 10),
      (item_set_slot, "itm_spangenhelm_18", ":angle_price_slot", 12),
      (item_set_slot, "itm_angle_helmet1", ":angle_price_slot", 15),
      (item_set_slot, "itm_angle_helmet2", ":angle_price_slot", 15),
      (item_set_slot, "itm_angle_helmet5", ":angle_price_slot", 17),
      (item_set_slot, "itm_angle_helmet6", ":angle_price_slot", 25),
      (item_set_slot, "itm_angle_helmet4", ":angle_price_slot", 27),
      (item_set_slot, "itm_common_pony2", ":angle_price_slot", 23),
      (item_set_slot, "itm_common_pony", ":angle_price_slot", 23),
      
      (item_set_slot, "itm_knife2", ":welsh_price_slot", 35),
      (item_set_slot, "itm_knife4", ":welsh_price_slot", 35),
      (item_set_slot, "itm_old_swordv", ":welsh_price_slot", 20),
      (item_set_slot, "itm_old_swordv2", ":welsh_price_slot", 20),
      (item_set_slot, "itm_old_swordv3", ":welsh_price_slot", 20),
      (item_set_slot, "itm_old_swordv4", ":welsh_price_slot", 20),
      (item_set_slot, "itm_old_swordv5", ":welsh_price_slot", 20),
      (item_set_slot, "itm_noble_sword_7", ":welsh_price_slot", 20),
      (item_set_slot, "itm_noble_sword_8", ":welsh_price_slot", 20),
      (item_set_slot, "itm_noble_sword_9", ":welsh_price_slot", 20),
      (item_set_slot, "itm_noble_sword_10", ":welsh_price_slot", 20),
      (item_set_slot, "itm_noble_sword_12", ":welsh_price_slot", 20),
      (item_set_slot, "itm_noble_swordv3", ":welsh_price_slot", 18),
      (try_for_range,":item","itm_light_spear1","itm_new_mace"),
        (item_get_slot,":price_mod",":item",":welsh_price_slot"),
        (val_mul,":price_mod",75),
        (val_div,":price_mod",100),
        (item_set_slot,":item",":welsh_price_slot",":price_mod"),
      (try_end),
      (item_set_slot, "itm_carbatinae_2s", ":welsh_price_slot", 60),
      (item_set_slot, "itm_carbatinae_vc2s", ":welsh_price_slot", 0),
      (item_set_slot, "itm_carbatinae_13qs", ":welsh_price_slot", 90),
      (item_set_slot, "itm_carbatinae_14qs", ":welsh_price_slot", 50),
      (item_set_slot, "itm_briton_tunic3", ":welsh_price_slot", 0),
      (item_set_slot, "itm_briton_tunic6", ":welsh_price_slot", 0),
      (item_set_slot, "itm_briton_tunic8", ":welsh_price_slot", 0),
      (item_set_slot, "itm_briton_tunic10", ":welsh_price_slot", 0),
      (item_set_slot, "itm_briton_tunic14", ":welsh_price_slot", 0),
      (item_set_slot, "itm_gambeson42", ":welsh_price_slot", 30),
      (item_set_slot, "itm_gambeson40", ":welsh_price_slot", 17),
      (item_set_slot, "itm_byrnie6", ":welsh_price_slot", 25),
      (item_set_slot, "itm_byrnie15", ":welsh_price_slot", 25),
      (item_set_slot, "itm_sling3", ":welsh_price_slot", 300),
      (item_set_slot, "itm_javelin", ":welsh_price_slot", 170),
      (item_set_slot, "itm_javelin_skirmishes", ":welsh_price_slot", 170),
      (item_set_slot, "itm_arrows", ":welsh_price_slot", 145),
      (item_set_slot, "itm_phrygian5", ":welsh_price_slot", 20),
      (item_set_slot, "itm_phrygian6", ":welsh_price_slot", 20),
      (item_set_slot, "itm_phrygian16", ":welsh_price_slot", 50),
      (item_set_slot, "itm_briton_helm4", ":welsh_price_slot", 22),
      (item_set_slot, "itm_briton_helm8", ":welsh_price_slot", 25),
      (item_set_slot, "itm_briton_helm29", ":welsh_price_slot", 25),
      (item_set_slot, "itm_briton_helm23", ":welsh_price_slot", 30),
      (item_set_slot, "itm_briton_helm18", ":welsh_price_slot", 30),
      (item_set_slot, "itm_briton_helm39", ":welsh_price_slot", 30),
      (item_set_slot, "itm_common_pony", ":welsh_price_slot", 20),
      (item_set_slot, "itm_common_pony2", ":welsh_price_slot", 20),
      (item_set_slot, "itm_common_horse", ":welsh_price_slot", 20),
      (item_set_slot, "itm_common_horse2", ":welsh_price_slot", 20),
      
      (item_set_slot, "itm_knife3", ":irish_price_slot", 40),
      (item_set_slot, "itm_knife5", ":irish_price_slot", 40),
      (item_set_slot, "itm_irish_long_sword3", ":irish_price_slot", 26),
      (item_set_slot, "itm_irish_short_sword1", ":irish_price_slot", 26),
      (item_set_slot, "itm_irish_long_sword2", ":irish_price_slot", 22),
      (item_set_slot, "itm_championsword2", ":irish_price_slot", 30),
      (try_for_range,":item","itm_light_spear1","itm_new_mace"),
        (item_get_slot,":price_mod",":item",":irish_price_slot"),
        (val_mul,":price_mod",70),
        (val_div,":price_mod",100),
        (item_set_slot,":item",":irish_price_slot",":price_mod"),
      (try_end),
      (item_set_slot, "itm_gaelshoes_2", ":irish_price_slot", 0),
      (item_set_slot, "itm_gaelshoes_3", ":irish_price_slot", 30),
      (item_set_slot, "itm_gaelshoes_1", ":irish_price_slot", 30),
      (item_set_slot, "itm_carbatinae_12q", ":irish_price_slot", 90),
      (item_set_slot, "itm_briton_tunic26", ":irish_price_slot", 20),
      (item_set_slot, "itm_briton_tunic27", ":irish_price_slot", 20),
      (item_set_slot, "itm_gael_tunic_03", ":irish_price_slot", 0),
      (item_set_slot, "itm_brat3", ":irish_price_slot", 0),
      (item_set_slot, "itm_gael_hoodtunic_11", ":irish_price_slot", 60),
      (item_set_slot, "itm_gambeson13gael", ":irish_price_slot", 30),
      (item_set_slot, "itm_gambeson16gael", ":irish_price_slot", 32),
      (item_set_slot, "itm_byrnie38", ":irish_price_slot", 28),
      (item_set_slot, "itm_javelin_skirmishesel", ":irish_price_slot", 65),
      (item_set_slot, "itm_javelin_skirmishes", ":irish_price_slot", 65),
      (item_set_slot, "itm_long_bow", ":irish_price_slot", 112),
      (item_set_slot, "itm_arrows", ":irish_price_slot", 120),
      (item_set_slot, "itm_phrygian4", ":irish_price_slot", 15),
      (item_set_slot, "itm_phrygian9", ":irish_price_slot", 30),
      (item_set_slot, "itm_phrygian14", ":irish_price_slot", 40),
      (item_set_slot, "itm_angle_helmet2", ":irish_price_slot", 12),
      (item_set_slot, "itm_angle_helmet4", ":irish_price_slot", 13),
      (item_set_slot, "itm_angle_helmet6", ":irish_price_slot", 17),
      (item_set_slot, "itm_angle_helmet5", ":irish_price_slot", 15),
      (item_set_slot, "itm_common_pony", ":irish_price_slot", 20),
      (item_set_slot, "itm_common_pony2", ":irish_price_slot", 20),
      (item_set_slot, "itm_common_horse", ":irish_price_slot", 20),
      (item_set_slot, "itm_common_horse2", ":irish_price_slot", 20),
      
      (item_set_slot, "itm_knife4", ":scotch_price_slot", 20),
      (item_set_slot, "itm_irish_short_sword2", ":scotch_price_slot", 20),
      (item_set_slot, "itm_irish_long_sword4", ":scotch_price_slot", 21),
      (item_set_slot, "itm_irish_long_sword7", ":scotch_price_slot", 21),
      (try_for_range,":item","itm_light_spear1","itm_new_mace"),
        (item_get_slot,":price_mod",":item",":scotch_price_slot"),
        (val_mul,":price_mod",50),
        (val_div,":price_mod",100),
        (item_set_slot,":item",":scotch_price_slot",":price_mod"),
      (try_end),
      (item_set_slot, "itm_just_man_boots_medium", ":scotch_price_slot", 50),
      (item_set_slot, "itm_carbatinae_12q", ":scotch_price_slot", 60),
      (item_set_slot, "itm_gael_tunic_01", ":scotch_price_slot", 30),
      (item_set_slot, "itm_pictish_painted1", ":scotch_price_slot", 30),
      (item_set_slot, "itm_briton_tunic18", ":scotch_price_slot", 0),
      (item_set_slot, "itm_picts_hoodtunic_16", ":scotch_price_slot", 65),
      (item_set_slot, "itm_picts_hoodtunic_06", ":scotch_price_slot", 30),
      (item_set_slot, "itm_picts_hoodtunic_18", ":scotch_price_slot", 65),
      (item_set_slot, "itm_gambeson3gael", ":scotch_price_slot", 45),
      (item_set_slot, "itm_gambeson8gael", ":scotch_price_slot", 45),
      # (item_set_slot, "itm_mail_shirt_1", ":scotch_price_slot", 40),
      (item_set_slot, "itm_mail_shirt_5", ":scotch_price_slot", 45),
      (item_set_slot, "itm_khergit_arrows", ":scotch_price_slot", 33),
      (item_set_slot, "itm_javelin", ":scotch_price_slot", 200),
      (item_set_slot, "itm_javelin_skirmishes", ":scotch_price_slot", 200),
      (item_set_slot, "itm_phrygian8", ":scotch_price_slot", 20),
      (item_set_slot, "itm_phrygian11", ":scotch_price_slot", 20),
      (item_set_slot, "itm_phrygian15", ":scotch_price_slot", 70),
      (item_set_slot, "itm_angle_helmet2", ":scotch_price_slot", 12),
      (item_set_slot, "itm_angle_helmet3", ":scotch_price_slot", 11),
      (item_set_slot, "itm_angle_helmet6", ":scotch_price_slot", 17),
      (item_set_slot, "itm_angle_helmet5", ":scotch_price_slot", 15),
      (item_set_slot, "itm_wild_pony", ":scotch_price_slot", 25),
      (item_set_slot, "itm_wild_pony2", ":scotch_price_slot", 25),
      (item_set_slot, "itm_wild_horse", ":scotch_price_slot", 25),
      (item_set_slot, "itm_wild_horse2", ":scotch_price_slot", 25),
      
      (call_script,"script_mp_set_item_prices"),
      
      #special items
      (item_set_slot, "itm_horn_multi", slot_item_multiplayer_item_class, multi_item_class_type_bow),  #combine with shields to conserve space
      (item_set_slot, "itm_standard", slot_item_multiplayer_item_class, multi_item_class_type_bow),
      (item_set_slot, "itm_standard_dragon", slot_item_multiplayer_item_class, multi_item_class_type_bow),
      
      #arrows
      (item_set_slot, "itm_arrows", slot_item_multiplayer_item_class, multi_item_class_type_bow),
      (item_set_slot, "itm_khergit_arrows", slot_item_multiplayer_item_class, multi_item_class_type_bow),
      (item_set_slot, "itm_sling_rock1", slot_item_multiplayer_item_class, multi_item_class_type_bow),  #sling shot
      
      #bolts
      
      #bows
      (item_set_slot, "itm_long_bow", slot_item_multiplayer_item_class, multi_item_class_type_bow),
      (item_set_slot, "itm_long_bow2", slot_item_multiplayer_item_class, multi_item_class_type_bow),
      
      #slings
      (item_set_slot, "itm_sling3", slot_item_multiplayer_item_class, multi_item_class_type_bow),
      
      #swords
      (item_set_slot, "itm_old_swordv", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_old_swordv2", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_old_swordv3", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_old_swordv4", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_new_sword3", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_new_sword4", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_spatha", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_spatha_7", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_sword", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_sword_5", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_sword_6", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_sword_7", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_sword_8", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_noble_sword", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_noble_sword_2", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_noble_sword_3", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_noble_sword_4", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_noble_sword_7", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_noble_sword_8", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_noble_sword_9", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_noble_sword_10", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_spatha_8", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_noble_swordv4", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      
      (item_set_slot, "itm_longseax5", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_longseax6", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_longseax7", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_longseax8", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_longseax9", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_seax_2", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_seax_3", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_seax_4", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      
      (item_set_slot, "itm_championsword2", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_irish_long_sword2", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_irish_long_sword3", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_irish_long_sword4", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_irish_long_sword7", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_irish_short_sword1", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_irish_short_sword2", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_noble_sword_12", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_noble_swordv3", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      (item_set_slot, "itm_old_swordv5", slot_item_multiplayer_item_class, multi_item_class_type_sword),
      
      #axe
      (item_set_slot, "itm_hatchet", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_hand_axe", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_axe", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_axe_2", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_axe_3", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_axe_4", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_axe_9", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_one_handed_war_axe_b", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_one_handed_war_axe_c", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_long_axe_b", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      
      (item_set_slot, "itm_pictish_hatchet10", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_pictish_hatchet11", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_pictish_hatchet2", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_pictish_hatchet3", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_pictish_hatchet5", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_pictish_hatchet6", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_pictish_hatchet7", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_pictish_hatchet8", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      (item_set_slot, "itm_pictish_hatchet9", slot_item_multiplayer_item_class, multi_item_class_type_axe),
      
      #blunt
      (item_set_slot, "itm_wooden_stick", slot_item_multiplayer_item_class, multi_item_class_type_axe),  #combine categories to take less space
      
      #spears
      (item_set_slot, "itm_quarter_staff", slot_item_multiplayer_item_class, multi_item_class_type_spear),  #combine categories to take less space
      (item_set_slot, "itm_staff", slot_item_multiplayer_item_class, multi_item_class_type_spear),
      (item_set_slot, "itm_light_spear1", slot_item_multiplayer_item_class, multi_item_class_type_spear), #short to long
      (item_set_slot, "itm_light_spear2", slot_item_multiplayer_item_class, multi_item_class_type_spear),
      (item_set_slot, "itm_war_spear1", slot_item_multiplayer_item_class, multi_item_class_type_spear),
      (item_set_slot, "itm_war_spear2", slot_item_multiplayer_item_class, multi_item_class_type_spear),
      (item_set_slot, "itm_heavy_spear1", slot_item_multiplayer_item_class, multi_item_class_type_spear),
      (item_set_slot, "itm_heavy_spear2", slot_item_multiplayer_item_class, multi_item_class_type_spear),
      (item_set_slot, "itm_long_light_spear1", slot_item_multiplayer_item_class, multi_item_class_type_spear),
      (item_set_slot, "itm_long_light_spear2", slot_item_multiplayer_item_class, multi_item_class_type_spear),
      (item_set_slot, "itm_long_war_spear1", slot_item_multiplayer_item_class, multi_item_class_type_spear),
      (item_set_slot, "itm_long_war_spear2", slot_item_multiplayer_item_class, multi_item_class_type_spear),
      (item_set_slot, "itm_long_heavy_spear1", slot_item_multiplayer_item_class, multi_item_class_type_spear),
      (item_set_slot, "itm_long_heavy_spear2", slot_item_multiplayer_item_class, multi_item_class_type_spear),
      
      #lance
      
      #shields
      #Norse
      (item_set_slot, "itm_viking_shield_round_02", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_03", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_04", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_06", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_07", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_08", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_10", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_12", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_13", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_16", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_17", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_18", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_19", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_20", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_21", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_tab_shield_round_01_device", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_tab_shield_round_07_device", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_tab_shield_round_12_device", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      #Saxon
      (item_set_slot, "itm_shield_10", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_01", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_viking_shield_round_09", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_tab_shield_round_05_device", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_tab_shield_round_06_device", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_tab_shield_round_08_device", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_tab_shield_round_10_device", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_tab_shield_round_11_device", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      
      (item_set_slot, "itm_gael_bigroundshield_01", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_gael_bigroundshield_03", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_gael_bigroundshield_05", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_gael_bigroundshield_06", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_h_shield2", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_h_shield3", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_h_shield4", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_shield_2", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_shield_3", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_shield_5", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_shield_6", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_small_roundsh1", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_small_roundsh3", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_squaresh_2", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_squaresh_4", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_squaresh_5", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_tab_shield_round_05_nodevice", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_tab_shield_round_10_nodevice", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      (item_set_slot, "itm_tab_shield_small_round_c", slot_item_multiplayer_item_class, multi_item_class_type_small_shield),
      
      #throwing
      (item_set_slot, "itm_stones", slot_item_multiplayer_item_class, multi_item_class_type_throwing),
      (item_set_slot, "itm_javelin", slot_item_multiplayer_item_class, multi_item_class_type_throwing),
      (item_set_slot, "itm_javelin_skirmishes", slot_item_multiplayer_item_class, multi_item_class_type_throwing),
      (item_set_slot, "itm_throwing_spears", slot_item_multiplayer_item_class, multi_item_class_type_throwing),
      (item_set_slot, "itm_javelin_skirmishesel", slot_item_multiplayer_item_class, multi_item_class_type_throwing),
      (item_set_slot, "itm_javelin_jinetes", slot_item_multiplayer_item_class, multi_item_class_type_throwing),
      (item_set_slot, "itm_throwing_spears2", slot_item_multiplayer_item_class, multi_item_class_type_throwing),
      
      #armors
      (item_set_slot, "itm_ptunic_1", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_btunic_3", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_btunic_8", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_ptunic_2", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_ptunic_3", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_btunic_15", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_bl_tunic02", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_brat3", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_briton_tunic14", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_briton_tunic17", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_briton_tunic18", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_briton_tunic26", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_briton_tunic27", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_briton_tunic3", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_gael_hoodtunic_11", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_gael_tunic_01", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_gael_tunic_03", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_picts_hoodtunic_06", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_picts_hoodtunic_16", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_picts_hoodtunic_18", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_bl_tunic04", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_bl_tunic06", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_pictish_painted1", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_briton_tunic6", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_briton_tunic8", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      (item_set_slot, "itm_briton_tunic10", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
      
      (item_set_slot, "itm_gambeson10", slot_item_multiplayer_item_class, multi_item_class_type_medium_armor),
      (item_set_slot, "itm_gambeson13", slot_item_multiplayer_item_class, multi_item_class_type_medium_armor),
      (item_set_slot, "itm_gambeson25", slot_item_multiplayer_item_class, multi_item_class_type_medium_armor),
      (item_set_slot, "itm_gambeson39", slot_item_multiplayer_item_class, multi_item_class_type_medium_armor),
      (item_set_slot, "itm_gambeson32", slot_item_multiplayer_item_class, multi_item_class_type_medium_armor),
      (item_set_slot, "itm_gambeson42", slot_item_multiplayer_item_class, multi_item_class_type_medium_armor),
      (item_set_slot, "itm_gambeson13gael", slot_item_multiplayer_item_class, multi_item_class_type_medium_armor),
      (item_set_slot, "itm_gambeson16gael", slot_item_multiplayer_item_class, multi_item_class_type_medium_armor),
      (item_set_slot, "itm_gambeson3", slot_item_multiplayer_item_class, multi_item_class_type_medium_armor),
      (item_set_slot, "itm_gambeson3gael", slot_item_multiplayer_item_class, multi_item_class_type_medium_armor),
      (item_set_slot, "itm_gambeson8gael", slot_item_multiplayer_item_class, multi_item_class_type_medium_armor),
      
      (item_set_slot, "itm_mail_shirt_2", slot_item_multiplayer_item_class, multi_item_class_type_heavy_armor),
      (item_set_slot, "itm_mail_shirt_2", slot_item_multiplayer_item_class, multi_item_class_type_heavy_armor),
      (item_set_slot, "itm_mail_shirt_5", slot_item_multiplayer_item_class, multi_item_class_type_heavy_armor),
      (item_set_slot, "itm_byrnie6", slot_item_multiplayer_item_class, multi_item_class_type_heavy_armor),
      (item_set_slot, "itm_byrnie15", slot_item_multiplayer_item_class, multi_item_class_type_heavy_armor),
      (item_set_slot, "itm_byrnie17", slot_item_multiplayer_item_class, multi_item_class_type_heavy_armor),
      (item_set_slot, "itm_byrnie21", slot_item_multiplayer_item_class, multi_item_class_type_heavy_armor),
      (item_set_slot, "itm_byrnie22", slot_item_multiplayer_item_class, multi_item_class_type_heavy_armor),
      (item_set_slot, "itm_byrnie28", slot_item_multiplayer_item_class, multi_item_class_type_heavy_armor),
      (item_set_slot, "itm_byrnie30", slot_item_multiplayer_item_class, multi_item_class_type_heavy_armor),
      (item_set_slot, "itm_byrnie34", slot_item_multiplayer_item_class, multi_item_class_type_heavy_armor),
      (item_set_slot, "itm_byrnie38", slot_item_multiplayer_item_class, multi_item_class_type_heavy_armor),
      
      #boots
      (item_set_slot, "itm_carbatinae_1", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_2", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_5", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_6", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_7", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_8", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_9", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_10", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_1s", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_2s", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_3s", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_6s", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_1v", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_2v", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_3v", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_6v", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_vc1", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_vc3", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_vc1v", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_14qv", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_vc2", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_vc2s", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_11qs", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_12qs", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_12q", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_13qs", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_14qs", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_vc1s", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_carbatinae_vc4s", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      
      
      (item_set_slot, "itm_bare_foot_man", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_gaelshoes_3", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_gaelshoes_1", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_gaelshoes_2", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_just_man_boots_dark", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_just_man_boots_light", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      (item_set_slot, "itm_just_man_boots_medium", slot_item_multiplayer_item_class, multi_item_class_type_light_foot),
      
      #helmets
      (item_set_slot, "itm_phrygian1", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian2", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian3", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian7", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian8", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian10", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian11", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian12", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian13", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian14", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian15", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian16", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian17", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian4", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian5", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian6", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      (item_set_slot, "itm_phrygian9", slot_item_multiplayer_item_class, multi_item_class_type_light_helm),
      
      (item_set_slot, "itm_spangenhelm_1", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_2", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_3", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_4", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_5", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_6", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_7", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_8", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_9", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_10", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_13", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_14", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_15", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_16", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_17", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_18", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_19", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_20", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_21", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_22", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_25", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_28", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_35", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_38", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_39", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_spangenhelm_40", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      
      (item_set_slot, "itm_vikingold_helm14", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_vikingold_helm15", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_viking_helm", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_viking_helm4", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_viking_helm8", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_viking_helm12", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_viking_helm15", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_viking_helm16", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_viking_helm17", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_viking_elitehelm1", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_viking_elitehelm2", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_viking_elitehelm3", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_viking_elitehelm4", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_viking_elitehelm7", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_viking_noblehelm1", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_vikingold_elitehelm9", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_angle_helmet1", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_angle_helmet2", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_angle_helmet5", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_angle_helmet6", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_angle_helmet4", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_angle_helmet3", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      
      (item_set_slot, "itm_briton_helm", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm2", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm3", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm4", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm8", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm13", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm14", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm18", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm19", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm20", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm21", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm22", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm23", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm29", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm33", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm34", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      (item_set_slot, "itm_briton_helm39", slot_item_multiplayer_item_class, multi_item_class_type_heavy_helm),
      
      #gloves
      (item_set_slot, "itm_leather_gloves", slot_item_multiplayer_item_class, multi_item_class_type_glove),
      
      #horses
      #(item_set_slot, "itm_donkey_mount", slot_item_multiplayer_item_class, multi_item_class_type_horse),	# Thats a cow in the moment!
      (item_set_slot, "itm_common_horse2", slot_item_multiplayer_item_class, multi_item_class_type_horse),
      (item_set_slot, "itm_common_horse", slot_item_multiplayer_item_class, multi_item_class_type_horse),
      (item_set_slot, "itm_common_pony", slot_item_multiplayer_item_class, multi_item_class_type_horse),
      (item_set_slot, "itm_common_pony2", slot_item_multiplayer_item_class, multi_item_class_type_horse),
      (item_set_slot, "itm_wild_horse", slot_item_multiplayer_item_class, multi_item_class_type_horse),
      (item_set_slot, "itm_wild_horse2", slot_item_multiplayer_item_class, multi_item_class_type_horse),
      (item_set_slot, "itm_wild_pony", slot_item_multiplayer_item_class, multi_item_class_type_horse),
      (item_set_slot, "itm_wild_pony2", slot_item_multiplayer_item_class, multi_item_class_type_horse),
      
      (try_begin),
        (assign, reg0, 0),
        (get_operation_set_version, reg0),
        (gt, reg0, warband_version-1),
        (call_script,"script_mp_assign_troop_equipment"),
      (try_end),  #version check
  ]),
  
  #script_get_army_size_from_slider_value
  # INPUT: arg1 = slider_value
  # OUTPUT: reg0 = army_size
  ("get_army_size_from_slider_value",
    [
      (store_script_param, ":slider_value", 1),
      (assign, ":army_size", ":slider_value"),
      (try_begin),
        (gt, ":slider_value", 20),
        (store_sub, ":adder_value", ":slider_value", 20),
        (val_add, ":army_size", ":adder_value"),
        (try_begin),
          (gt, ":slider_value", 50),
          (store_sub, ":adder_value", ":slider_value", 50),
          (val_mul, ":adder_value", 3),
          (val_add, ":army_size", ":adder_value"),
          (try_begin),
            (gt, ":slider_value", 80),
            (store_sub, ":adder_value", ":slider_value", 80),
            (val_mul, ":adder_value", 5),
            (val_add, ":army_size", ":adder_value"),
          (try_end),
        (try_end),
      (try_end),
      (assign, reg0, ":army_size"),
  ]),
  
  #script_spawn_quick_battle_army
  # INPUT: arg1 = initial_entry_point, arg2 = faction_no, arg3 = infantry_ratio, arg4 = archers_ratio, arg5 = cavalry_ratio, arg6 = divide_archer_entry_points, arg7 = player_team
  # OUTPUT: none
  ("spawn_quick_battle_army",
    [
      (store_script_param, ":cur_entry_point", 1),
      (store_script_param, ":faction_no", 2),
      (store_script_param, ":infantry_ratio", 3),
      (store_script_param, ":archers_ratio", 4),
      (store_script_param, ":cavalry_ratio", 5),
      (store_script_param, ":divide_archer_entry_points", 6),
      (store_script_param, ":player_team", 7),
      
      (try_begin),
        (eq, ":player_team", 1),
        (call_script, "script_get_army_size_from_slider_value", "$g_quick_battle_army_1_size"),
        (assign, ":army_size", reg0),
        (try_begin),
          (neq, "$g_quick_battle_troop", "trp_quick_battle_troops_cam"),
          (set_player_troop, "$g_quick_battle_troop"),
          (set_visitor, ":cur_entry_point", "$g_quick_battle_troop"),
        (try_end),
        
        (try_begin),
          (eq, ":cur_entry_point", 0),
          (try_begin),
            (is_between, ":faction_no", cb_factions_begin, cb_factions_end),
            (faction_get_slot, "$g_quick_battle_team_0_banner", ":faction_no", slot_faction_banner),
          (else_try),
            (assign, "$g_quick_battle_team_0_banner", banner_default),
          (try_end),
        (else_try),
          (try_begin),
            (is_between, ":faction_no", cb_factions_begin, cb_factions_end),
            (faction_get_slot, "$g_quick_battle_team_1_banner", ":faction_no", slot_faction_banner),
          (else_try),
            (assign, "$g_quick_battle_team_1_banner", banner_default),
          (try_end),
        (try_end),
        (val_add, ":cur_entry_point", 1),
        
      (else_try),
        (call_script, "script_get_army_size_from_slider_value", "$g_quick_battle_army_2_size"),
        (assign, ":army_size", reg0),
        (try_begin),
          (eq, ":cur_entry_point", 0),
          (assign, "$g_quick_battle_team_0_banner", "mesh_banner_extra12"),
        (else_try),
          (assign, "$g_quick_battle_team_1_banner", "mesh_banner_extra17"),
        (try_end),
        (val_add, ":cur_entry_point", 1),
      (try_end),
      
      (store_mul, ":num_infantry", ":infantry_ratio", ":army_size"),
      (val_div, ":num_infantry", 100),
      (store_mul, ":num_archers", ":archers_ratio", ":army_size"),
      (val_div, ":num_archers", 100),
      (store_mul, ":num_cavalry", ":cavalry_ratio", ":army_size"),
      (val_div, ":num_cavalry", 100),
      
      (try_begin),
        (store_add, ":num_total", ":num_infantry", ":num_archers"),
        (val_add, ":num_total", ":num_cavalry"),
        (neq, ":num_total", ":army_size"),
        (store_sub, ":leftover", ":army_size", ":num_total"),
        (try_begin),
          (gt, ":infantry_ratio", ":archers_ratio"),
          (gt, ":infantry_ratio", ":cavalry_ratio"),
          (val_add, ":num_infantry", ":leftover"),
        (else_try),
          (gt, ":archers_ratio", ":cavalry_ratio"),
          (val_add, ":num_archers", ":leftover"),
        (else_try),
          (val_add, ":num_cavalry", ":leftover"),
        (try_end),
      (try_end),
      
      (store_mul, ":rand_min", ":num_infantry", 15),
      (val_div, ":rand_min", 100),
      (store_mul, ":rand_max", ":num_infantry", 45),
      (val_div, ":rand_max", 100),
      (store_random_in_range, ":num_tier_2_infantry", ":rand_min", ":rand_max"),
      (store_sub, ":num_tier_1_infantry", ":num_infantry", ":num_tier_2_infantry"),
      (store_mul, ":rand_min", ":num_archers", 15),
      (val_div, ":rand_min", 100),
      (store_mul, ":rand_max", ":num_archers", 45),
      (val_div, ":rand_max", 100),
      (store_random_in_range, ":num_tier_2_archers", ":rand_min", ":rand_max"),
      (store_sub, ":num_tier_1_archers", ":num_archers", ":num_tier_2_archers"),
      (store_mul, ":rand_min", ":num_cavalry", 15),
      (val_div, ":rand_min", 100),
      (store_mul, ":rand_max", ":num_cavalry", 45),
      (val_div, ":rand_max", 100),
      (store_random_in_range, ":num_tier_2_cavalry", ":rand_min", ":rand_max"),
      (store_sub, ":num_tier_1_cavalry", ":num_cavalry", ":num_tier_2_cavalry"),
      
      (faction_get_slot, ":cur_troop", ":faction_no", slot_faction_quick_battle_tier_2_infantry),
      (set_visitors, ":cur_entry_point", ":cur_troop", ":num_tier_2_infantry"),
      (val_add, ":cur_entry_point", 1),
      (faction_get_slot, ":cur_troop", ":faction_no", slot_faction_quick_battle_tier_1_infantry),
      (set_visitors, ":cur_entry_point", ":cur_troop", ":num_tier_1_infantry"),
      (val_add, ":cur_entry_point", 1),
      (faction_get_slot, ":cur_troop", ":faction_no", slot_faction_quick_battle_tier_2_cavalry),
      (try_begin),
        (gt, ":cur_troop", 0),
        (set_visitors, ":cur_entry_point", ":cur_troop", ":num_tier_2_cavalry"),
        
        #no high tier cavalry, so use low if any
      (else_try),
        (faction_get_slot, ":cur_troop", ":faction_no", slot_faction_quick_battle_tier_1_cavalry),
        (gt, ":cur_troop", 0),
        (set_visitors, ":cur_entry_point", ":cur_troop", ":num_tier_2_cavalry"),
      (try_end),
      (val_add, ":cur_entry_point", 1),
      (faction_get_slot, ":cur_troop", ":faction_no", slot_faction_quick_battle_tier_1_cavalry),
      (try_begin),
        (gt, ":cur_troop", 0),
        (set_visitors, ":cur_entry_point", ":cur_troop", ":num_tier_1_cavalry"),
      (try_end),
      (val_add, ":cur_entry_point", 1),
      
      (try_begin),
        (eq, ":divide_archer_entry_points", 0),
        (faction_get_slot, ":cur_troop", ":faction_no", slot_faction_quick_battle_tier_2_archer),
        (try_begin),
          (gt, ":cur_troop", 0),
          (set_visitors, ":cur_entry_point", ":cur_troop", ":num_tier_2_archers"),
          
          #no high tier archers, so use low if any
        (else_try),
          (faction_get_slot, ":cur_troop", ":faction_no", slot_faction_quick_battle_tier_1_archer),
          (gt, ":cur_troop", 0),
          (set_visitors, ":cur_entry_point", ":cur_troop", ":num_tier_2_archers"),
        (try_end),
        (val_add, ":cur_entry_point", 1),
        (faction_get_slot, ":cur_troop", ":faction_no", slot_faction_quick_battle_tier_1_archer),
        (set_visitors, ":cur_entry_point", ":cur_troop", ":num_tier_1_archers"),
        (val_add, ":cur_entry_point", 1),
      (else_try),
        (assign, ":cur_entry_point", 40), #archer positions begin point
        (store_div, ":num_tier_1_archers_ceil_8", ":num_tier_1_archers", 8),
        (val_mul, ":num_tier_1_archers_ceil_8", 8),
        (try_begin),
          (neq, ":num_tier_1_archers_ceil_8", ":num_tier_1_archers"),
          (val_div, ":num_tier_1_archers_ceil_8", 8),
          (val_add, ":num_tier_1_archers_ceil_8", 1),
          (val_mul, ":num_tier_1_archers_ceil_8", 8),
        (try_end),
        (store_div, ":num_tier_2_archers_ceil_8", ":num_tier_2_archers", 8),
        (val_mul, ":num_tier_2_archers_ceil_8", 8),
        (try_begin),
          (neq, ":num_tier_2_archers_ceil_8", ":num_tier_2_archers"),
          (val_div, ":num_tier_2_archers_ceil_8", 8),
          (val_add, ":num_tier_2_archers_ceil_8", 1),
          (val_mul, ":num_tier_2_archers_ceil_8", 8),
        (try_end),
        (store_add, ":num_archers_ceil_8", ":num_tier_1_archers_ceil_8", ":num_tier_2_archers_ceil_8"),
        (store_div, ":num_archers_per_entry_point", ":num_archers_ceil_8", 8),
        (assign, ":left_tier_1_archers", ":num_tier_1_archers"),
        (assign, ":left_tier_2_archers", ":num_tier_2_archers"),
        (assign, ":end_cond", 1000),
        (try_for_range, ":unused", 0, ":end_cond"),
          (try_begin),
            (gt, ":left_tier_2_archers", 0),
            (assign, ":used_tier_2_archers", ":num_archers_per_entry_point"),
            (val_min, ":used_tier_2_archers", ":left_tier_2_archers"),
            (faction_get_slot, ":cur_troop", ":faction_no", slot_faction_quick_battle_tier_2_archer),
            (try_begin),
              (gt, ":cur_troop", 0),
              (set_visitors, ":cur_entry_point", ":cur_troop", ":used_tier_2_archers"),
              
              #no high tier archers, so use low if any
            (else_try),
              (faction_get_slot, ":cur_troop", ":faction_no", slot_faction_quick_battle_tier_1_archer),
              (gt, ":cur_troop", 0),
              (set_visitors, ":cur_entry_point", ":cur_troop", ":num_tier_2_archers"),
            (try_end),
            (val_add, ":cur_entry_point", 1),
            (val_sub, ":left_tier_2_archers", ":used_tier_2_archers"),
          (else_try),
            (gt, ":left_tier_1_archers", 0),
            (assign, ":used_tier_1_archers", ":num_archers_per_entry_point"),
            (val_min, ":used_tier_1_archers", ":left_tier_1_archers"),
            (faction_get_slot, ":cur_troop", ":faction_no", slot_faction_quick_battle_tier_1_archer),
            (set_visitors, ":cur_entry_point", ":cur_troop", ":used_tier_1_archers"),
            (val_add, ":cur_entry_point", 1),
            (val_sub, ":left_tier_1_archers", ":used_tier_1_archers"),
          (else_try),
            (assign, ":end_cond", 0),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  #script_game_set_multiplayer_mission_end
  # This script is called from the game engine when a multiplayer map is ended in clients (not in server).
  # INPUT:
  # none
  # OUTPUT:
  # none
  ("game_set_multiplayer_mission_end",
    [
      (assign, "$g_multiplayer_mission_end_screen", 1),
  ]),
  #script_game_enable_cheat_menu
  # This script is called from the game engine when user enters "cheatmenu from command console (ctrl+~).
  # INPUT:
  # none
  # OUTPUT:
  # none
  ("game_enable_cheat_menu",
    [
      (store_script_param, ":input", 1),
      (try_begin),
        (eq, ":input", 0),
        (assign, "$cheat_mode", 0),
      (else_try),
        (eq, ":input", 1),
        (assign, "$cheat_mode", 1),
      (try_end),
  ]),
  
  #script_game_get_console_command
  # This script is called from the game engine when a console command is entered from the dedicated server.
  # INPUT: anything
  # OUTPUT: s0 = result text
  ("game_get_console_command",
    [
      (store_script_param, ":input", 1),
      (store_script_param, ":val1", 2),
      (try_begin),
        #getting val2 for some commands
        (eq, ":input", 2),
        (store_script_param, ":val2", 3),
      (end_try),
      (try_begin),
        (eq, ":input", 1),
        (assign, reg0, ":val1"),
        (try_begin),
          (eq, ":val1", 1),
          (assign, reg1, "$g_multiplayer_num_bots_team_1"),
          (str_store_string, s0, "str_team_reg0_bot_count_is_reg1"),
        (else_try),
          (eq, ":val1", 2),
          (assign, reg1, "$g_multiplayer_num_bots_team_2"),
          (str_store_string, s0, "str_team_reg0_bot_count_is_reg1"),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 2),
        (assign, reg0, ":val1"),
        (assign, reg1, ":val2"),
        (try_begin),
          (eq, ":val1", 1),
          (ge, ":val2", 0),
          (assign, "$g_multiplayer_num_bots_team_1", ":val2"),
          (str_store_string, s0, "str_team_reg0_bot_count_is_reg1"),
        (else_try),
          (eq, ":val1", 2),
          (ge, ":val2", 0),
          (assign, "$g_multiplayer_num_bots_team_2", ":val2"),
          (str_store_string, s0, "str_team_reg0_bot_count_is_reg1"),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 3),
        (assign, reg0, "$g_multiplayer_round_max_seconds"),
        (str_store_string, s0, "str_maximum_seconds_for_round_is_reg0"),
      (else_try),
        (eq, ":input", 4),
        (assign, reg0, ":val1"),
        (try_begin),
          (is_between, ":val1", multiplayer_round_max_seconds_min, multiplayer_round_max_seconds_max),
          (assign, "$g_multiplayer_round_max_seconds", ":val1"),
          (str_store_string, s0, "str_maximum_seconds_for_round_is_reg0"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_round_max_seconds, ":val1"),
          (try_end),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 5),
        (assign, reg0, "$g_multiplayer_respawn_period"),
        (str_store_string, s0, "str_respawn_period_is_reg0_seconds"),
      (else_try),
        (eq, ":input", 6),
        (assign, reg0, ":val1"),
        (try_begin),
          (is_between, ":val1", multiplayer_respawn_period_min, multiplayer_respawn_period_max),
          (assign, "$g_multiplayer_respawn_period", ":val1"),
          (str_store_string, s0, "str_respawn_period_is_reg0_seconds"),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 7),
        (assign, reg0, "$g_multiplayer_num_bots_voteable"),
        (str_store_string, s0, "str_bots_upper_limit_for_votes_is_reg0"),
      (else_try),
        (eq, ":input", 8),
        (try_begin),
          (is_between, ":val1", 0, 51),
          (assign, "$g_multiplayer_num_bots_voteable", ":val1"),
          (store_add, "$g_multiplayer_max_num_bots", ":val1", 1),
          (assign, reg0, "$g_multiplayer_num_bots_voteable"),
          (str_store_string, s0, "str_bots_upper_limit_for_votes_is_reg0"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_num_bots_voteable, ":val1"),
          (try_end),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 9),
        (try_begin),
          (eq, "$g_multiplayer_maps_voteable", 1),
          (str_store_string, s0, "str_map_is_voteable"),
        (else_try),
          (str_store_string, s0, "str_map_is_not_voteable"),
        (try_end),
      (else_try),
        (eq, ":input", 10),
        (try_begin),
          (is_between, ":val1", 0, 2),
          (assign, "$g_multiplayer_maps_voteable", ":val1"),
          (try_begin),
            (eq, ":val1", 1),
            (str_store_string, s0, "str_map_is_voteable"),
          (else_try),
            (str_store_string, s0, "str_map_is_not_voteable"),
          (try_end),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_maps_voteable, ":val1"),
          (try_end),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 11),
        (try_begin),
          (eq, "$g_multiplayer_factions_voteable", 1),
          (str_store_string, s0, "str_factions_are_voteable"),
        (else_try),
          (str_store_string, s0, "str_factions_are_not_voteable"),
        (try_end),
      (else_try),
        (eq, ":input", 12),
        (try_begin),
          (is_between, ":val1", 0, 2),
          (assign, "$g_multiplayer_factions_voteable", ":val1"),
          (try_begin),
            (eq, ":val1", 1),
            (str_store_string, s0, "str_factions_are_voteable"),
          (else_try),
            (str_store_string, s0, "str_factions_are_not_voteable"),
          (try_end),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_factions_voteable, ":val1"),
          (try_end),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 13),
        (try_begin),
          (eq, "$g_multiplayer_player_respawn_as_bot", 1),
          (str_store_string, s0, "str_players_respawn_as_bot"),
        (else_try),
          (str_store_string, s0, "str_players_do_not_respawn_as_bot"),
        (try_end),
      (else_try),
        (eq, ":input", 14),
        (try_begin),
          (is_between, ":val1", 0, 2),
          (assign, "$g_multiplayer_player_respawn_as_bot", ":val1"),
          (try_begin),
            (eq, ":val1", 1),
            (str_store_string, s0, "str_players_respawn_as_bot"),
          (else_try),
            (str_store_string, s0, "str_players_do_not_respawn_as_bot"),
          (try_end),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_player_respawn_as_bot, ":val1"),
          (try_end),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 15),
        (try_begin),
          (eq, "$g_multiplayer_kick_voteable", 1),
          (str_store_string, s0, "str_kicking_a_player_is_voteable"),
        (else_try),
          (str_store_string, s0, "str_kicking_a_player_is_not_voteable"),
        (try_end),
      (else_try),
        (eq, ":input", 16),
        (try_begin),
          (is_between, ":val1", 0, 2),
          (assign, "$g_multiplayer_kick_voteable", ":val1"),
          (try_begin),
            (eq, ":val1", 1),
            (str_store_string, s0, "str_kicking_a_player_is_voteable"),
          (else_try),
            (str_store_string, s0, "str_kicking_a_player_is_not_voteable"),
          (try_end),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_kick_voteable, ":val1"),
          (try_end),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 17),
        (try_begin),
          (eq, "$g_multiplayer_ban_voteable", 1),
          (str_store_string, s0, "str_banning_a_player_is_voteable"),
        (else_try),
          (str_store_string, s0, "str_banning_a_player_is_not_voteable"),
        (try_end),
      (else_try),
        (eq, ":input", 18),
        (try_begin),
          (is_between, ":val1", 0, 2),
          (assign, "$g_multiplayer_ban_voteable", ":val1"),
          (try_begin),
            (eq, ":val1", 1),
            (str_store_string, s0, "str_banning_a_player_is_voteable"),
          (else_try),
            (str_store_string, s0, "str_banning_a_player_is_not_voteable"),
          (try_end),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_ban_voteable, ":val1"),
          (try_end),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 19),
        (assign, reg0, "$g_multiplayer_valid_vote_ratio"),
        (str_store_string, s0, "str_percentage_of_yes_votes_required_for_a_poll_to_get_accepted_is_reg0"),
      (else_try),
        (eq, ":input", 20),
        (try_begin),
          (is_between, ":val1", 50, 101),
          (assign, "$g_multiplayer_valid_vote_ratio", ":val1"),
          (assign, reg0, ":val1"),
          (str_store_string, s0, "str_percentage_of_yes_votes_required_for_a_poll_to_get_accepted_is_reg0"),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 21),
        (assign, reg0, "$g_multiplayer_auto_team_balance_limit"),
        (str_store_string, s0, "str_auto_team_balance_threshold_is_reg0"),
      (else_try),
        (eq, ":input", 22),
        (try_begin),
          (is_between, ":val1", 2, 7),
          (assign, "$g_multiplayer_auto_team_balance_limit", ":val1"),
          (assign, reg0, "$g_multiplayer_auto_team_balance_limit"),
          (str_store_string, s0, "str_auto_team_balance_threshold_is_reg0"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_auto_team_balance_limit, ":val1"),
          (try_end),
        (else_try),
          (ge, ":val1", 7),
          (assign, "$g_multiplayer_auto_team_balance_limit", 1000),
          (assign, reg0, "$g_multiplayer_auto_team_balance_limit"),
          (str_store_string, s0, "str_auto_team_balance_threshold_is_reg0"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_auto_team_balance_limit, ":val1"),
          (try_end),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 23),
        (assign, reg0, "$g_multiplayer_initial_gold_multiplier"),
        (str_store_string, s0, "str_starting_gold_ratio_is_reg0"),
      (else_try),
        (eq, ":input", 24),
        (try_begin),
          (is_between, ":val1", 0, 1001),
          (assign, "$g_multiplayer_initial_gold_multiplier", ":val1"),
          (assign, reg0, "$g_multiplayer_initial_gold_multiplier"),
          (str_store_string, s0, "str_starting_gold_ratio_is_reg0"),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 25),
        (assign, reg0, "$g_multiplayer_battle_earnings_multiplier"),
        (str_store_string, s0, "str_combat_gold_bonus_ratio_is_reg0"),
      (else_try),
        (eq, ":input", 26),
        (try_begin),
          (is_between, ":val1", 0, 1001),
          (assign, "$g_multiplayer_battle_earnings_multiplier", ":val1"),
          (assign, reg0, "$g_multiplayer_battle_earnings_multiplier"),
          (str_store_string, s0, "str_combat_gold_bonus_ratio_is_reg0"),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 27),
        (assign, reg0, "$g_multiplayer_round_earnings_multiplier"),
        (str_store_string, s0, "str_round_gold_bonus_ratio_is_reg0"),
      (else_try),
        (eq, ":input", 28),
        (try_begin),
          (is_between, ":val1", 0, 1001),
          (assign, "$g_multiplayer_round_earnings_multiplier", ":val1"),
          (assign, reg0, "$g_multiplayer_round_earnings_multiplier"),
          (str_store_string, s0, "str_round_gold_bonus_ratio_is_reg0"),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 29),
        (try_begin),
          (eq, "$g_multiplayer_allow_player_banners", 1),
          (str_store_string, s0, "str_player_banners_are_allowed"),
        (else_try),
          (str_store_string, s0, "str_player_banners_are_not_allowed"),
        (try_end),
      (else_try),
        (eq, ":input", 30),
        (try_begin),
          (is_between, ":val1", 0, 2),
          (assign, "$g_multiplayer_allow_player_banners", ":val1"),
          (try_begin),
            (eq, ":val1", 1),
            (str_store_string, s0, "str_player_banners_are_allowed"),
          (else_try),
            (str_store_string, s0, "str_player_banners_are_not_allowed"),
          (try_end),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 31),
        (try_begin),
          (eq, "$g_multiplayer_force_default_armor", 1),
          (str_store_string, s0, "str_default_armor_is_forced"),
        (else_try),
          (str_store_string, s0, "str_default_armor_is_not_forced"),
        (try_end),
      (else_try),
        (eq, ":input", 32),
        (try_begin),
          (is_between, ":val1", 0, 2),
          (assign, "$g_multiplayer_force_default_armor", ":val1"),
          (try_begin),
            (eq, ":val1", 1),
            (str_store_string, s0, "str_default_armor_is_forced"),
          (else_try),
            (str_store_string, s0, "str_default_armor_is_not_forced"),
          (try_end),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 33),
        (assign, reg0, "$g_multiplayer_point_gained_from_flags"),
        (str_store_string, s0, "str_point_gained_from_flags_is_reg0"),
      (else_try),
        (eq, ":input", 34),
        (try_begin),
          (is_between, ":val1", 25, 401),
          (assign, "$g_multiplayer_point_gained_from_flags", ":val1"),
          (assign, reg0, "$g_multiplayer_point_gained_from_flags"),
          (str_store_string, s0, "str_point_gained_from_flags_is_reg0"),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 35),
        (assign, reg0, "$g_multiplayer_point_gained_from_capturing_flag"),
        (str_store_string, s0, "str_point_gained_from_capturing_flag_is_reg0"),
      (else_try),
        (eq, ":input", 36),
        (try_begin),
          (is_between, ":val1", 0, 11),
          (assign, "$g_multiplayer_point_gained_from_capturing_flag", ":val1"),
          (assign, reg0, "$g_multiplayer_point_gained_from_capturing_flag"),
          (str_store_string, s0, "str_point_gained_from_capturing_flag_is_reg0"),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 37),
        (assign, reg0, "$g_multiplayer_game_max_minutes"),
        (str_store_string, s0, "str_map_time_limit_is_reg0"),
      (else_try),
        (eq, ":input", 38),
        (try_begin),
          (is_between, ":val1", 5, 121),
          (assign, "$g_multiplayer_game_max_minutes", ":val1"),
          (assign, reg0, "$g_multiplayer_game_max_minutes"),
          (str_store_string, s0, "str_map_time_limit_is_reg0"),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 39),
        (assign, reg0, "$g_multiplayer_game_max_points"),
        (str_store_string, s0, "str_team_points_limit_is_reg0"),
      (else_try),
        (eq, ":input", 40),
        (try_begin),
          (is_between, ":val1", 3, 1001),
          (assign, "$g_multiplayer_game_max_points", ":val1"),
          (assign, reg0, "$g_multiplayer_game_max_points"),
          (str_store_string, s0, "str_team_points_limit_is_reg0"),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 41),
        (assign, reg0, "$g_multiplayer_number_of_respawn_count"),
        (try_begin),
          (eq, reg0, 0),
          (str_store_string, s1, "str_unlimited"),
        (else_try),
          (str_store_string, s1, "str_reg0"),
        (try_end),
        (str_store_string, s0, "str_defender_spawn_count_limit_is_s1"),
      (else_try),
        (eq, ":input", 42),
        (try_begin),
          (is_between, ":val1", 0, 6),
          (assign, "$g_multiplayer_number_of_respawn_count", ":val1"),
          (assign, reg0, "$g_multiplayer_number_of_respawn_count"),
          (try_begin),
            (eq, reg0, 0),
            (str_store_string, s1, "str_unlimited"),
          (else_try),
            (str_store_string, s1, "str_reg0"),
          (try_end),
          (str_store_string, s0, "str_defender_spawn_count_limit_is_s1"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_respawn_count, ":val1"),
          (try_end),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 43),
        (try_begin),
          (eq, "$g_multiplayer_disallow_ranged_weapons", 1),
          (str_store_string, s0, "str_ranged_weapons_are_disallowed"),
        (else_try),
          (str_store_string, s0, "str_ranged_weapons_are_allowed"),
        (try_end),
      (else_try),
        (eq, ":input", 44),
        (try_begin),
          (is_between, ":val1", 0, 2),
          (assign, "$g_multiplayer_disallow_ranged_weapons", ":val1"),
          (try_begin),
            (eq, ":val1", 1),
            (str_store_string, s0, "str_ranged_weapons_are_disallowed"),
          (else_try),
            (str_store_string, s0, "str_ranged_weapons_are_allowed"),
          (try_end),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 56), #allow_multiple_firearms -> setting if poll to change gamemode are allowed (0 by default)
        (try_begin),
          (is_between, ":val1", 0, 2),
          (assign, "$g_multiplayer_gametype_voteable", ":val1"),
          (try_begin),
            (eq, ":val1", 1),
            (str_store_string, s0, "str_changing_gametype_is_voteable"),
          (else_try),
            (str_store_string, s0, "str_changing_gametype_is_not_voteable"),
          (try_end),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (eq, ":input", 81), #limit_general -> delay between polls
        (try_begin),
          (is_between, ":val1", 0, 1000),
          (assign, "$g_multiplayer_poll_cooldown", ":val1"),
          (assign, reg1, ":val1"),
          (str_store_string, s0, "str_poll_cooldown_is_now_reg1"),
        (else_try),
          (str_store_string, s0, "str_input_is_not_correct_for_the_command_type_help_for_more_information"),
        (try_end),
      (else_try),
        (str_store_string, s0, "@{!}DEBUG : SYSTEM ERROR!"),
      (try_end),
  ]),
  
  # script_game_event_party_encounter:
  # This script is called from the game engine whenever player party encounters another party or a battle on the world map
  # INPUT:
  # param1: encountered_party
  # param2: second encountered_party (if this was a battle
  ("game_event_party_encounter",
    [
      (assign,"$on_map",0),
      (store_script_param_1, "$g_encountered_party"),
      (store_script_param_2, "$g_encountered_party_2"),# encountered_party2 is set when we come across a battle or siege, otherwise it's a negative value
      #       (store_encountered_party, "$g_encountered_party"),
      #       (store_encountered_party2,"$g_encountered_party_2"), # encountered_party2 is set when we come across a battle or siege, otherwise it's a minus value
      (store_faction_of_party, "$g_encountered_party_faction","$g_encountered_party"),
      (store_relation, "$g_encountered_party_relation", "$g_encountered_party_faction", "fac_player_faction"),
      
      (party_get_slot, "$g_encountered_party_type", "$g_encountered_party", slot_party_type),
      (party_get_template_id,"$g_encountered_party_template","$g_encountered_party"),
      
      #NPC companion changes begin
      (call_script, "script_party_count_fit_regulars", "p_main_party"),
      (assign, "$playerparty_prebattle_regulars", reg0),
      
      #        (try_begin),
      #            (assign, "$player_party__regulars", 0),
      #            (call_script, "script_party_count_fit_regulars", "p_main_party"),
      #            (gt, reg0, 0),
      #            (assign, "$player_party_contains_regulars", 1),
      #        (try_end),
      #NPC companion changes end
      
      
      (assign, "$vc_menu_result", 0), #VC-3142
      (assign, "$g_last_rest_center", -1),
      (assign, "$talk_context", 0),
      (assign,"$g_player_surrenders",0),
      (assign,"$g_enemy_surrenders",0),
      (assign, "$g_leave_encounter",0),
      (assign, "$g_engaged_enemy", 0),
      #       (assign,"$waiting_for_arena_fight_result", 0),
      #       (assign,"$arena_bet_amount",0),
      #       (assign,"$g_player_raiding_village",0),
      (try_begin),
        (neg|is_between, "$g_encountered_party", centers_begin, centers_end),
        (rest_for_hours, 0), #stop waiting
        (assign, "$g_infinite_camping", 0),
      (try_end),
      #       (assign, "$g_permitted_to_center",0),
      (assign, "$new_encounter", 1), #check this in the menu.
      (try_begin),
        (lt, "$g_encountered_party_2",0), #Normal encounter. Not battle or siege.
        (try_begin),
          (party_slot_eq, "$g_encountered_party", slot_party_type, spt_town),
          (jump_to_menu, "mnu_castle_outside"),
        (else_try),
          (party_slot_eq, "$g_encountered_party", slot_party_type, spt_castle),
          (jump_to_menu, "mnu_castle_outside"),
        (else_try),
          (party_slot_eq, "$g_encountered_party", slot_party_type, spt_village),
          (assign, "$town_entered", 0),	#VC-2242
          (jump_to_menu, "mnu_village"),
        (else_try),
          (party_slot_eq, "$g_encountered_party", slot_party_type, spt_cattle_herd),
          (jump_to_menu, "mnu_cattle_herd"),
        (else_try),
          (is_between, "$g_encountered_party", training_grounds_begin, training_grounds_end),
          (jump_to_menu, "mnu_training_ground"),
        (else_try),
          (party_get_template_id, ":template", "$g_encountered_party"),
          (ge, ":template", "pt_steppe_bandit_lair"),
          (lt, ":template", "pt_bandit_lair_templates_end"),
          (assign, "$loot_screen_shown", 0),
          #          (call_script, "script_encounter_init_variables"),
          (jump_to_menu, "mnu_bandit_lair"),
        (else_try),
          (eq, "$g_encountered_party", "p_zendar"),
          (jump_to_menu, "mnu_zendar"),
        (else_try),
          (eq, "$g_encountered_party", "p_salt_mine"),
          (jump_to_menu, "mnu_salt_mine"),
        (else_try),
          (eq, "$g_encountered_party", "p_four_ways_inn"),
          (jump_to_menu, "mnu_four_ways_inn"),
          
          ### VC STORY ENCOUNTERS
        (else_try),
          (eq, "$g_encountered_party_template", "pt_sven_doccinga_ca"), #chief spawn sven for doccinga coastal assault chief
          (jump_to_menu, "mnu_doccinga_c_assault2"), #chief acaba
        (else_try),
          (eq, "$g_encountered_party_template", "pt_sigurd_farm_men"), #chief spawn farmstead sigur men
          (jump_to_menu, "mnu_farmstead_sigurd4"), #chief acaba
        (else_try),
          (eq, "$g_encountered_party_template", "pt_sven_lair_deffense"), #chief empieza
          (jump_to_menu, "mnu_sven_lair"),
        (else_try), #quarry specail scene
          (is_between, "$g_encountered_party", "p_quarry1","p_saltmine1"),
          (jump_to_menu, "mnu_quarry1"),
        (else_try), #salt mine special scene
          (is_between, "$g_encountered_party", "p_saltmine1", "p_lumbercamp1"),
          (jump_to_menu, "mnu_salt_mine_vc"),
        (else_try), #lumber camp scene
          (is_between, "$g_encountered_party", "p_lumbercamp1","p_farmsteadsp1"),
          (jump_to_menu, "mnu_lumber_camp"),
        (else_try), #farmstead camp scene
          (is_between, "$g_encountered_party", "p_farmsteadsp1", "p_hadrian_wall1"),
          (jump_to_menu, "mnu_farmstead_special"),
        (else_try), #chief empieza
          (eq, "$g_encountered_party", "p_frisa_beach"),
          (jump_to_menu, "mnu_frisa_beach"),
        (else_try), #chief empieza
          (eq, "$g_encountered_party", "p_the_thing"),
          (jump_to_menu, "mnu_the_thing"),
        (else_try), #bjorn camp
          (eq, "$g_encountered_party", "p_bjorn_camp"),
          (jump_to_menu, "mnu_bjorn_camp"),
        (else_try), #chief empieza
          (eq, "$g_encountered_party", "p_yourlair"),
          (jump_to_menu, "mnu_player_lair_menus"),
        (else_try), #phaiak edits fleet quest
          (eq, "$g_encountered_party", "p_the_fleet"),
          (try_begin), #
            (party_slot_eq, "p_main_party", slot_party_on_water, 0),
            (jump_to_menu, "mnu_auto_return_to_map"),
          (else_try),
            (check_quest_active,"qst_douar_an_enez"),
            (jump_to_menu, "mnu_way_to_douaranenez"),
          (else_try),
            (check_quest_active,"qst_the_fleet"),
            (neg|quest_slot_ge,"qst_the_fleet",slot_quest_current_state, 5),
            (jump_to_menu, "mnu_partida_ribe_fleet"),
          (else_try),
            (party_set_flags, "p_the_fleet", pf_is_static, 1),
            (disable_party, "p_the_fleet"),#VC-2960
            (quest_set_slot,"qst_the_fleet",slot_quest_current_state, 10),
            (call_script, "script_conclude_quest", "qst_the_fleet"),
            (set_show_messages, 0),
            (call_script, "script_end_quest", "qst_the_fleet"),
            (set_show_messages, 1),
            (jump_to_menu, "mnu_auto_return_to_map"),
          (try_end),
        (else_try), #aescesdun
          (eq, "$g_encountered_party", "p_aescesdun"),
          (try_begin), #
            (eq, "$player_side", 2), #danish
            (check_quest_active,"qst_aescesdun"),
            (quest_get_slot, ":quest_current_state", "qst_aescesdun", slot_quest_current_state),
            (ge, ":quest_current_state", 12),
            (jump_to_menu, "mnu_mega_battle_ashdown"),
          (else_try),
            (jump_to_menu, "mnu_aescesdun_camp"),
          (try_end),
        (else_try), #Readingum
          (eq, "$g_encountered_party", "p_readingum"),
          (try_begin), #
            (check_quest_active,"qst_aescesdun"),
            (quest_get_slot, ":quest_current_state", "qst_aescesdun", slot_quest_current_state),
            (ge, ":quest_current_state", 12),
            (jump_to_menu, "mnu_aescesdun_empty"),
          (else_try),
            (check_quest_active,"qst_svenbn_final"),
            (jump_to_menu, "mnu_sven_bullneck_readingum"),
          (else_try),
            (jump_to_menu, "mnu_readingum_camp"),
          (try_end),
        (else_try), #Mega Danish army
          (eq, "$g_encountered_party", "p_mega_danishrmy"),
          (jump_to_menu, "mnu_mega_battle_ashdown"),
        (else_try),
          (eq, "$g_encountered_party", "p_thiaderd_lair"), #chief spawn thiaderd
          (jump_to_menu, "mnu_camp_thiaderd"), #chief acaba
        (else_try),
          (eq, "$g_encountered_party", "p_celidon_forest"),
          (jump_to_menu, "mnu_celidon_forest"),
        (else_try),
          (eq, "$g_encountered_party", "p_hidden_valley"),
          (jump_to_menu, "mnu_hidden_valley"),
        (else_try),
          (eq, "$g_encountered_party", "p_circle_mystic1"),
          (jump_to_menu, "mnu_circle_mystic"),
        (else_try),
          (eq, "$g_encountered_party", "p_troll_bridge"),
          (jump_to_menu, "mnu_troll_bridge"),
        (else_try),
          (eq, "$g_encountered_party", "p_battle_stones"),
          (jump_to_menu, "mnu_battle_stones"),
        (else_try),
          (eq, "$g_encountered_party", "p_roman_fort"),
          (jump_to_menu, "mnu_roman_fort"), #riddles quest
        (else_try),
          (eq, "$g_encountered_party", "p_farmland_special"),
          (jump_to_menu, "mnu_farmland_menu"),
        (else_try),
          (eq, "$g_encountered_party", "p_hadrian_wall1"),
          (jump_to_menu, "mnu_hadrian_wall1"),
        (else_try),
          (eq, "$g_encountered_party", "p_odin_cave"),
          (jump_to_menu, "mnu_cueva_odin"),
        (else_try),
          (eq, "$g_encountered_party", "p_boar_grove"),
          (jump_to_menu, "mnu_boar_grove"),
        (else_try), #Add-ons
          (eq, "$g_encountered_party", "p_oldpagan_hut"),
          (eq, "$game_started_with_content_update", 1),#VC-3109
          (jump_to_menu, "mnu_first_attack_morrigan"),
        (else_try),
          (eq, "$g_encountered_party", "p_morrigan_lair"),
          (eq, "$game_started_with_content_update", 1),#VC-3109
          (jump_to_menu, "mnu_morrigan_lair_attack"),
        (else_try),
          (eq, "$g_encountered_party", "p_bresail_farm"),
          (eq, "$game_started_with_content_update", 1),#VC-3109
          (jump_to_menu, "mnu_bresail_farm_visit"),
        (else_try),
          (eq, "$g_encountered_party", "p_bresail_fort"),
          (eq, "$game_started_with_content_update", 1),#VC-3109
          (jump_to_menu, "mnu_bresail_fort_visit"),
          
          ### VC SEA TRAVEL SYSTEM ENCOUNTERS
        (else_try),
          (eq, "$g_encountered_party", "p_landing_point"),
          (jump_to_menu, "mnu_landing_point_encounter"),
        (else_try),
          (eq, "$g_encountered_party_template", "pt_landet_ships"),
          (jump_to_menu, "mnu_landet_ships_encounter"),
        (else_try),
          (eq, "$g_encountered_party_template", "pt_port"),
          (jump_to_menu, "mnu_port_encounter"),
        (else_try),
          (is_between, "$g_encountered_party", "p_ferry_1a", "p_jetty_1"),
          (jump_to_menu, "mnu_ferry_encounter"),
        (else_try),
          (this_or_next|eq, "$g_encountered_party", "p_troop_camp_1"),
          (			 eq, "$g_encountered_party", "p_troop_camp_2"),
          (jump_to_menu, "mnu_troop_camp"),
          
          ### other encounters
        (else_try),
          (eq, "$g_encountered_party_template", "pt_boar_herd"),
          (jump_to_menu, "mnu_hunting"),
        (else_try),
          (eq, "$g_encountered_party_template", "pt_slave_hideout"),
          (jump_to_menu, "mnu_slave_hideout"),
        (else_try),
          (is_between, "$g_encountered_party", "p_destroy1", "p_Bridge_1"),
          (jump_to_menu, "mnu_lugar_destruido"),
        (else_try),
          (is_between, "$g_encountered_party", "p_monasterio1", "p_yourlair"),
          (jump_to_menu, "mnu_monasterio_visit"),
        (else_try), #pagan holy sites
          (is_between, "$g_encountered_party", "p_paganholysites1", "p_reserved_1"),
          (eq, "$game_started_with_content_update", 1),#VC-3109
          (jump_to_menu, "mnu_paganholysites_visit"),
        (else_try),
          (eq, "$g_encountered_party", "p_farmland"),
          (jump_to_menu, "mnu_farmstead_sigurd"),
        (else_try), #chief acaba
          (eq, "$g_encountered_party", "p_test_scene"),
          (jump_to_menu, "mnu_test_scene"),
        (else_try),
          (eq, "$g_encountered_party", "p_battlefields"),
          (jump_to_menu, "mnu_battlefields"),
        (else_try),
          (eq, "$g_encountered_party", "p_training_ground"),
          (jump_to_menu, "mnu_tutorial"),
        (else_try),
          (eq, "$g_encountered_party", "p_camp_bandits"),
          (jump_to_menu, "mnu_camp"),
        (else_try),
          (jump_to_menu, "mnu_simple_encounter"),
        (try_end),
      (else_try), #Battle or siege
        (try_begin),
          (this_or_next|party_slot_eq, "$g_encountered_party", slot_party_type, spt_town),
          (party_slot_eq, "$g_encountered_party", slot_party_type, spt_castle),
          (try_begin),
            (eq, "$auto_enter_town", "$g_encountered_party"),
            (jump_to_menu, "mnu_town"),
          (else_try),
            (eq, "$auto_besiege_town", "$g_encountered_party"),
            (jump_to_menu, "mnu_besiegers_camp_with_allies"),
          (else_try),
            (jump_to_menu, "mnu_join_siege_outside"),
          (try_end),
        (else_try),
          (jump_to_menu, "mnu_pre_join"),
        (try_end),
      (try_end),
      (assign,"$auto_enter_town",0),
      (assign,"$auto_besiege_town",0),
  ]),
  
  #script_game_event_simulate_battle:
  # This script is called whenever the game simulates the battle between two parties on the map.
  # INPUT:
  # param1: Defender Party
  # param2: Attacker Party
  ("game_event_simulate_battle",
    [
      (store_script_param_1, ":root_defender_party"),
      (store_script_param_2, ":root_attacker_party"),
      
      (assign, "$marshal_defeated_in_battle", -1),
      
      (store_current_hours, ":hours"),
      
      (try_for_parties, ":party"),
        (party_get_battle_opponent, ":opponent", ":party"),
        (gt, ":opponent", 0),
        (party_set_slot, ":party", slot_party_last_in_combat, ":hours"),
      (try_end),
      
      (assign, ":trigger_result", 1),
      (try_begin),
        (ge, ":root_defender_party", 0),
        (ge, ":root_attacker_party", 0),
        (party_is_active, ":root_defender_party"),
        (party_is_active, ":root_attacker_party"),
        (store_faction_of_party, ":defender_faction", ":root_defender_party"),
        (store_faction_of_party, ":attacker_faction", ":root_attacker_party"),
        #(neq, ":defender_faction", "fac_player_faction"),
        #(neq, ":attacker_faction", "fac_player_faction"),
        (store_relation, ":reln", ":defender_faction", ":attacker_faction"),
        (lt, ":reln", 0),
        (assign, ":trigger_result", 0),
        
        (try_begin),
          (this_or_next|eq, "$g_battle_simulation_cancel_for_party", ":root_defender_party"),
          (eq, "$g_battle_simulation_cancel_for_party", ":root_attacker_party"),
          (assign, "$g_battle_simulation_cancel_for_party", -1),
          (assign, "$auto_enter_town", "$g_battle_simulation_auto_enter_town_after_battle"),
          (assign, ":trigger_result", 1),
        (else_try),
          (try_begin),
            (this_or_next|party_slot_eq, ":root_defender_party", slot_party_retreat_flag, 1),
            (party_slot_eq, ":root_attacker_party", slot_party_retreat_flag, 1),
            (assign, ":trigger_result", 1), #End battle!
          (try_end),
          (party_set_slot, ":root_attacker_party", slot_party_retreat_flag, 0),
          
          #(assign, ":cancel_attack", 0),
          
          (party_collect_attachments_to_party, ":root_defender_party", "p_collective_ally"),
          (party_collect_attachments_to_party, ":root_attacker_party", "p_collective_enemy"),
          
          #(call_script, "script_party_count_fit_for_battle", "p_collective_ally"),
          (call_script, "script_party_calculate_strength", "p_collective_ally", 0),
          (assign, ":defender_strength", reg0),
          #(call_script, "script_party_count_fit_for_battle", "p_collective_enemy"),
          (call_script, "script_party_calculate_strength", "p_collective_enemy", 0),
          (assign, ":attacker_strength", reg0),
          
          (store_div, ":defender_strength", ":defender_strength", 20),
          (val_min, ":defender_strength", 50),
          (val_max, ":defender_strength", 1),
          (store_div, ":attacker_strength", ":attacker_strength", 20),
          (val_min, ":attacker_strength", 50),
          (val_add, ":attacker_strength", 1),
          (try_begin),
            #For sieges increase attacker casualties and reduce defender casualties.
            (this_or_next|party_slot_eq, ":root_defender_party", slot_party_type, spt_castle),
            (party_slot_eq, ":root_defender_party", slot_party_type, spt_town),
            (val_mul, ":defender_strength", 123), #it was 1.5 in old version, now it is only 1.23
            (val_div, ":defender_strength", 100),
            
            (val_mul, ":attacker_strength", 100), #it was 0.5 in old version, now it is only 1 / 1.23
            (val_div, ":attacker_strength", 123),
          (try_end),
          
          (call_script, "script_party_count_fit_for_battle", "p_collective_ally", 0),
          (assign, ":old_defender_strength", reg0),
          
          (try_begin),
            (neg|is_currently_night), #Don't fight at night
            (inflict_casualties_to_party_group, ":root_attacker_party", ":defender_strength", "p_temp_casualties"),
            (party_collect_attachments_to_party, ":root_attacker_party", "p_collective_enemy"),
          (try_end),
          (call_script, "script_party_count_fit_for_battle", "p_collective_enemy", 0),
          (assign, ":new_attacker_strength", reg0),
          
          (try_begin),
            (gt, ":new_attacker_strength", 0),
            (neg|is_currently_night), #Don't fight at night
            (inflict_casualties_to_party_group, ":root_defender_party", ":attacker_strength", "p_temp_casualties"),
            (party_collect_attachments_to_party, ":root_defender_party", "p_collective_ally"),
          (try_end),
          (call_script, "script_party_count_fit_for_battle", "p_collective_ally", 0),
          (assign, ":new_defender_strength", reg0),
          
          (try_begin),
            (this_or_next|eq, ":new_attacker_strength", 0),
            (eq, ":new_defender_strength", 0),
            # Battle concluded! determine winner
            
            (assign, ":do_not_end_battle", 0),
            (try_begin),
              (neg|troop_is_wounded, "trp_player"),
              (eq, ":new_defender_strength", 0),
              (eq, "$auto_enter_town", "$g_encountered_party"),
              (eq, ":old_defender_strength", ":new_defender_strength"),
              (assign, ":do_not_end_battle", 1),
            (try_end),
            (eq, ":do_not_end_battle", 0),
            
            (try_begin),
              (eq, ":new_attacker_strength", 0),
              (eq, ":new_defender_strength", 0),
              (assign, ":root_winner_party", -1),
              (assign, ":root_defeated_party", -1),
              (assign, ":collective_casualties", -1),
            (else_try),
              (eq, ":new_attacker_strength", 0),
              (assign, ":root_winner_party", ":root_defender_party"),
              (assign, ":root_defeated_party", ":root_attacker_party"),
              (assign, ":collective_casualties", "p_collective_enemy"),
            (else_try),
              (assign, ":root_winner_party", ":root_attacker_party"),
              (assign, ":root_defeated_party", ":root_defender_party"),
              (assign, ":collective_casualties", "p_collective_ally"),
            (try_end),
            
            (try_begin),
              (ge, ":root_winner_party", 0),
              (call_script, "script_get_nonempty_party_in_group", ":root_winner_party"),
              (assign, ":nonempty_winner_party", reg0),
              (store_faction_of_party, ":faction_receiving_prisoners", ":nonempty_winner_party"),
              (store_faction_of_party, ":defeated_faction", ":root_defeated_party"),
            (else_try),
              (assign, ":nonempty_winner_party", -1),
            (try_end),
            
            (try_begin),
              (ge, ":collective_casualties", 0),
              (party_get_num_companion_stacks, ":num_stacks", ":collective_casualties"),
            (else_try),
              (assign, ":num_stacks", 0),
            (try_end),
            
            (try_for_range, ":troop_iterator", 0, ":num_stacks"),
              (party_stack_get_troop_id, ":cur_troop_id", ":collective_casualties", ":troop_iterator"),
              (troop_is_hero, ":cur_troop_id"),
              
              (try_begin),
                #abort quest if troop loses a battle during rest time
                (check_quest_active, "qst_lend_surgeon"),
                (quest_slot_eq, "qst_lend_surgeon", slot_quest_giver_troop, ":cur_troop_id"),
                (call_script, "script_abort_quest", "qst_lend_surgeon", 0),
              (try_end),
              
              (call_script, "script_remove_troop_from_prison", ":cur_troop_id"),
              
              (troop_set_slot, ":cur_troop_id", slot_troop_leaded_party, -1),
              
              (store_random_in_range, ":rand", 0, 100),
              (str_store_troop_name_link, s1, ":cur_troop_id"),
              (str_store_faction_name_link, s2, ":faction_receiving_prisoners"),
              (store_troop_faction, ":defeated_troop_faction", ":cur_troop_id"),
              (str_store_faction_name_link, s3, ":defeated_troop_faction"),
              (try_begin),
                (ge, ":rand", hero_escape_after_defeat_chance),
                (party_stack_get_troop_id, ":leader_troop_id", ":nonempty_winner_party", 0),
                (is_between, ":leader_troop_id", active_npcs_begin, active_npcs_end), #disable non-kingdom parties capturing enemy lords
                (party_add_prisoners, ":nonempty_winner_party", ":cur_troop_id", 1),
                (gt, reg0, 0),
                #(troop_set_slot, ":cur_troop_id", slot_troop_is_prisoner, 1),
                (troop_set_slot, ":cur_troop_id", slot_troop_prisoner_of_party, ":nonempty_winner_party"),
                #chief color
                (try_begin),
                  (this_or_next|ge, "$vc_show_all_messages", 1),	#VC-2479
                  (this_or_next|eq, ":defeated_troop_faction", "fac_player_supporters_faction"),
                  (eq, ":defeated_troop_faction", "$players_kingdom"),
                  (faction_get_color, ":faction_color", ":defeated_troop_faction"),
                  (display_log_message, "str_hero_taken_prisoner", ":faction_color"),
                (end_try),
                #chief color acaba
                (try_begin),
                  (call_script, "script_cf_prisoner_offered_parole", ":cur_troop_id"),
                  
                  (try_begin),
                    (eq, "$cheat_mode", 1),
                    (display_message, "@{!}DEBUG : Prisoner granted parole"),
                  (try_end),
                  
                  (call_script, "script_troop_change_relation_with_troop", ":leader_troop_id", ":cur_troop_id", 3),
                  (val_add, "$total_battle_enemy_changes", 3),
                (else_try),
                  (try_begin),
                    (eq, "$cheat_mode", 1),
                    (display_message, "@{!}DEBUG : Prisoner not offered parole"),
                  (try_end),
                  
                  (call_script, "script_troop_change_relation_with_troop", ":leader_troop_id", ":cur_troop_id", -5),
                  (val_add, "$total_battle_enemy_changes", -5),
                (try_end),
                
                (store_faction_of_party, ":capturer_faction", ":nonempty_winner_party"),
                (call_script, "script_update_troop_location_notes_prisoned", ":cur_troop_id", ":capturer_faction"),
              (else_try),
                ## chief
                (faction_get_color, ":faction_color", ":defeated_troop_faction"),
                (display_message,"@{s1} of the {s3} was defeated in battle but managed to escape.", ":faction_color"),
                #                (display_message,"@{s1} of {s3} was defeated in battle but managed to escape."),
                ## acaba color
              (try_end),
              
              (try_begin),
                (store_troop_faction, ":cur_troop_faction", ":cur_troop_id"),
                (is_between, ":cur_troop_faction", kingdoms_begin, kingdoms_end),
                (faction_slot_eq, ":cur_troop_faction", slot_faction_marshal, ":cur_troop_id"),
                (is_between, ":cur_troop_faction", kingdoms_begin, kingdoms_end),
                (assign, "$marshal_defeated_in_battle", ":cur_troop_id"),
                #marshal is defeated, refresh ai.
                (assign, "$g_recalculate_ais", 1),
              (try_end),
            (try_end),
            
            (try_begin),
              (ge, ":collective_casualties", 0),
              (party_get_num_prisoner_stacks, ":num_stacks", ":collective_casualties"),
            (else_try),
              (assign, ":num_stacks", 0),
            (try_end),
            (try_for_range, ":troop_iterator", 0, ":num_stacks"),
              (party_prisoner_stack_get_troop_id, ":cur_troop_id", ":collective_casualties", ":troop_iterator"),
              (troop_is_hero, ":cur_troop_id"),
              (call_script, "script_remove_troop_from_prison", ":cur_troop_id"),
              (store_troop_faction, ":cur_troop_faction", ":cur_troop_id"),
              (str_store_troop_name_link, s1, ":cur_troop_id"),
              (str_store_faction_name_link, s2, ":faction_receiving_prisoners"),
              (str_store_faction_name_link, s3, ":cur_troop_faction"),
              ## chief
              (faction_get_color, ":faction_color", ":cur_troop_faction"),
              (display_log_message,"str_hero_freed", ":faction_color"),
              ## Chief acaba
              #               (display_log_message,"str_hero_freed"),
            (try_end),
            
            (try_begin),
              (ge, ":collective_casualties", 0),
              (party_clear, "p_temp_party"),
              (assign, "$g_move_heroes", 0), #heroes are already processed above. Skip them here.
              (call_script, "script_party_add_party_prisoners", "p_temp_party", ":collective_casualties"),
              (call_script, "script_party_prisoners_add_party_companions", "p_temp_party", ":collective_casualties"),
              (distribute_party_among_party_group, "p_temp_party", ":root_winner_party"),
              
              (call_script, "script_battle_political_consequences", ":root_defeated_party", ":root_winner_party"),
              
              (call_script, "script_clear_party_group", ":root_defeated_party"),
            (try_end),
            (assign, ":trigger_result", 1), #End battle!
            
            #Center captured
            (try_begin),
              (ge, ":collective_casualties", 0),
              (party_get_slot, ":cur_party_type", ":root_defeated_party", slot_party_type),
              (this_or_next|eq, ":cur_party_type", spt_town),
              (eq, ":cur_party_type", spt_castle),
              
              (assign, "$g_recalculate_ais", 1),
              
              (store_faction_of_party, ":winner_faction", ":root_winner_party"),
              (store_faction_of_party, ":defeated_faction", ":root_defeated_party"),
              
              (str_store_party_name, s1, ":root_defeated_party"),
              (str_store_faction_name, s2, ":winner_faction"),
              (str_store_faction_name, s3, ":defeated_faction"),
              ##chief
              (try_begin),
                (this_or_next|ge, "$vc_show_all_messages", 1),	#VC-2479
                (this_or_next|eq, ":faction_color", "fac_player_supporters_faction"),
                (eq, ":faction_color", "$players_kingdom"),
                (faction_get_color, ":faction_color", ":winner_faction"),
                (display_log_message, "str_center_captured", ":faction_color"),
              (end_try),
              ##chief acaba
              
              (store_current_hours, ":hours"),
              (faction_set_slot, ":winner_faction", slot_faction_ai_last_decisive_event, ":hours"),
              
              (try_begin),
                (eq, "$g_encountered_party", ":root_defeated_party"),
                (call_script, "script_add_log_entry", logent_player_participated_in_siege, "trp_player",  "$g_encountered_party", 0, "$g_encountered_party_faction"),
              (try_end),
              
              (try_begin),
                (party_get_num_companion_stacks, ":num_stacks", ":root_winner_party"),
                (gt, ":num_stacks", 0),
                (party_stack_get_troop_id, ":leader_troop_no", ":root_winner_party", 0),
                (is_between, ":leader_troop_no", active_npcs_begin, active_npcs_end),
                (party_set_slot, ":root_defeated_party", slot_center_last_taken_by_troop, ":leader_troop_no"),
              (else_try),
                (party_set_slot, ":root_defeated_party", slot_center_last_taken_by_troop, -1),
              (try_end),
              
              (call_script, "script_lift_siege", ":root_defeated_party", 0),
              (store_faction_of_party, ":fortress_faction", ":root_defeated_party"),
              (try_begin),
                (is_between, ":root_defeated_party", towns_begin, towns_end),
                (assign, ":damage", 40),
              (else_try),
                (assign, ":damage", 20),
              (try_end),
              (call_script, "script_faction_inflict_war_damage_on_faction", ":winner_faction", ":fortress_faction", ":damage"),
              
              (call_script, "script_give_center_to_faction", ":root_defeated_party", ":winner_faction"),
              (try_begin),
                (eq, ":defeated_faction", "fac_player_supporters_faction"),
                (eq, "$g_infinite_camping", 0),
                (call_script, "script_add_notification_menu", "mnu_notification_center_lost", ":root_defeated_party", ":winner_faction"),
              (try_end),
              
              (party_get_num_attached_parties, ":num_attached_parties",  ":root_attacker_party"),
              (try_for_range, ":attached_party_rank", 0, ":num_attached_parties"),
                (party_get_attached_party_with_rank, ":attached_party", ":root_attacker_party", ":attached_party_rank"),
                
                (party_get_num_companion_stacks, ":num_stacks", ":attached_party"),
                (assign, ":total_size", 0),
                (try_for_range, ":i_stack", 0, ":num_stacks"),
                  (party_stack_get_size, ":stack_size", ":attached_party", ":i_stack"),
                  (val_add, ":total_size", ":stack_size"),
                (try_end),
                
                (try_begin),
                  (ge, ":total_size", 10),
                  
                  (assign, ":stacks_added", 0),
                  (assign, ":last_random_stack", -1),
                  
                  (assign, ":end_condition", 10),
                  (try_for_range, ":unused", 0, ":end_condition"),
                    (store_random_in_range, ":random_stack", 1, ":num_stacks"),
                    (party_stack_get_troop_id, ":random_stack_troop", ":attached_party", ":random_stack"),
                    (party_stack_get_size, ":stack_size", ":attached_party", ":random_stack"),
                    (ge, ":stack_size", 4),
                    (neq, ":random_stack", ":last_random_stack"),
                    
                    (store_mul, ":total_size_mul_2", ":total_size", 2),
                    (assign, ":percentage", ":total_size_mul_2"),
                    (val_min, ":percentage", 100),
                    
                    (val_mul, ":stack_size", ":percentage"),
                    (val_div, ":stack_size", 100),
                    
                    (party_stack_get_troop_id, ":party_leader", ":attached_party", 0),
                    
                    (try_begin),
                      (this_or_next|troop_slot_eq, ":party_leader", slot_lord_reputation_type, lrep_goodnatured),
                      (this_or_next|troop_slot_eq, ":party_leader", slot_lord_reputation_type, lrep_upstanding),
                      (troop_slot_eq, ":party_leader", slot_lord_reputation_type, lrep_martial),
                      (assign, reg2, 0),
                      (store_random_in_range, ":random_percentage", 40, 50), #average 45%
                    (else_try),
                      (this_or_next|troop_slot_eq, ":party_leader", slot_lord_reputation_type, lrep_quarrelsome),
                      (troop_slot_eq, ":party_leader", slot_lord_reputation_type, lrep_cunning),
                      (assign, reg2, 1),
                      (store_random_in_range, ":random_percentage", 30, 40), #average 35%
                    (else_try),
                      (this_or_next|troop_slot_eq, ":party_leader", slot_lord_reputation_type, lrep_selfrighteous),
                      (this_or_next|troop_slot_eq, ":party_leader", slot_lord_reputation_type, lrep_roguish),
                      (troop_slot_eq, ":party_leader", slot_lord_reputation_type, lrep_debauched),
                      (assign, reg2, 2),
                      (store_random_in_range, ":random_percentage", 20, 30), #average 25%
                    (else_try),
                      ##                       (this_or_next|troop_slot_eq, ":party_leader", slot_lord_reputation_type, lrep_benefactor),
                      ##                       (troop_slot_eq, ":party_leader", slot_lord_reputation_type, lrep_custodian),
                      (assign, reg2, 3),
                      (store_random_in_range, ":random_percentage", 50, 60), #average 55%
                    (try_end),
                    
                    (val_min, ":random_percentage", 100),
                    (val_mul, ":stack_size", ":random_percentage"),
                    (val_div, ":stack_size", 100),
                    
                    (party_add_members, ":root_defender_party", ":random_stack_troop", ":stack_size"),
                    (party_remove_members, ":attached_party", ":random_stack_troop", ":stack_size"),
                    
                    (val_add, ":stacks_added", 1),
                    (assign, ":last_random_stack", ":random_stack"),
                    
                    (try_begin),
                      #if troops from three different stack is already added then break
                      (eq, ":stacks_added", 3),
                      (assign, ":end_condition", 0),
                    (try_end),
                  (try_end),
                (try_end),
              (try_end),
              
              #Reduce prosperity of the center by 5
              (try_begin),
                (neg|is_between, ":root_defeated_party", castles_begin, castles_end),
                (call_script, "script_change_center_prosperity", ":root_defeated_party", -5),
                (val_add, "$newglob_total_prosperity_from_townloot", -5),
              (try_end),
              (call_script, "script_order_best_besieger_party_to_guard_center", ":root_defeated_party", ":winner_faction"),
              (call_script, "script_cf_reinforce_party", ":root_defeated_party"),
              (call_script, "script_cf_reinforce_party", ":root_defeated_party"),
              (party_get_slot, ":food_stores", ":root_defeated_party", slot_party_food_store),
              (call_script, "script_center_get_food_store_limit", ":root_defeated_party"),
              (val_min, ":food_stores", reg0),
              (val_div, reg0, 2),
              (val_max, ":food_stores", reg0),
              (party_set_slot, ":root_defeated_party", slot_party_food_store, ":food_stores"),
            (try_end),
          (try_end),
          
          #ADD XP
          (try_begin),
            (party_slot_eq, ":root_attacker_party", slot_party_type, spt_kingdom_hero_party),
            
            (assign, ":xp_gained_attacker", 200),
            (options_get_campaign_ai, ":reduce_campaign_ai"), #moto chief
            (store_faction_of_party, ":root_attacker_party_faction", ":root_attacker_party"),
            (try_begin),
              (this_or_next|eq, ":root_attacker_party", "p_main_party"),
              (this_or_next|eq, ":root_attacker_party_faction", "fac_player_supporters_faction"),
              (eq, ":root_attacker_party_faction", "$players_kingdom"),
              #same
            (else_try),
              (eq, ":reduce_campaign_ai", 0), #hard (1.5x)
              (val_mul, ":xp_gained_attacker", 3),
              (val_div, ":xp_gained_attacker", 2),
            (else_try),
              (eq, ":reduce_campaign_ai", 1), #moderate (1.0x)
              #same
            (else_try),
              (eq, ":reduce_campaign_ai", 2), #easy (0.5x)
              (val_div, ":xp_gained_attacker", 2),
            (try_end),
            
            (gt, ":new_attacker_strength", 0),
            (call_script, "script_upgrade_hero_party", ":root_attacker_party", ":xp_gained_attacker"),
          (try_end),
          (try_begin),
            (party_slot_eq, ":root_defender_party", slot_party_type, spt_kingdom_hero_party),
            
            (assign, ":xp_gained_defender", 200),
            (store_faction_of_party, ":root_defender_party_faction", ":root_defender_party"),
            (options_get_campaign_ai, ":reduce_campaign_ai"), #moto chief
            (try_begin),
              (this_or_next|eq, ":root_defender_party", "p_main_party"),
              (this_or_next|eq, ":root_defender_party_faction", "fac_player_supporters_faction"),
              (eq, ":root_defender_party_faction", "$players_kingdom"),
              #same
            (else_try),
              (eq, ":reduce_campaign_ai", 0), #hard (1.5x)
              (val_mul, ":xp_gained_defender", 3),
              (val_div, ":xp_gained_defender", 2),
            (else_try),
              (eq, ":reduce_campaign_ai", 1), #moderate (1.0x)
              #same
            (else_try),
              (eq, ":reduce_campaign_ai", 2), #easy (0.5x)
              (val_div, ":xp_gained_defender", 2),
            (try_end),
            
            (gt, ":new_defender_strength", 0),
            (call_script, "script_upgrade_hero_party", ":root_defender_party", ":xp_gained_defender"),
          (try_end),
          
          (try_begin),
            #ozan - do not randomly end battles aganist towns or castles.
            (neg|party_slot_eq, ":root_defender_party", slot_party_type, spt_castle), #added by ozan
            (neg|party_slot_eq, ":root_defender_party", slot_party_type, spt_town),   #added by ozan
            #end ozan
            
            (party_get_slot, ":attacker_root_strength", ":root_attacker_party", slot_party_cached_strength),
            (party_get_slot, ":attacker_nearby_friend_strength", ":root_attacker_party", slot_party_nearby_friend_strength),
            (party_get_slot, ":strength_of_attacker_followers", ":root_attacker_party", slot_party_follower_strength),
            (store_add, ":total_attacker_strength", ":attacker_root_strength", ":attacker_nearby_friend_strength"),
            (val_add, ":total_attacker_strength", ":strength_of_attacker_followers"),
            
            (party_get_slot, ":defender_root_strength", ":root_defender_party", slot_party_cached_strength),
            (party_get_slot, ":defender_nearby_friend_strength", ":root_defender_party", slot_party_nearby_friend_strength),
            (party_get_slot, ":strength_of_defender_followers", ":root_defender_party", slot_party_follower_strength),
            (store_add, ":total_defender_strength", ":defender_root_strength", ":defender_nearby_friend_strength"),
            (val_add, ":total_attacker_strength", ":strength_of_defender_followers"),
            
            #Players can make save loads and change history because these random values are not determined from random_slots of troops
            (store_random_in_range, ":random_num", 0, 100),
            
            (try_begin),
              (lt, ":random_num", 10),
              (assign, ":trigger_result", 1), #End battle!
            (try_end),
          (else_try),
            (party_get_slot, ":attacker_root_strength", ":root_attacker_party", slot_party_cached_strength),
            (party_get_slot, ":attacker_nearby_friend_strength", ":root_attacker_party", slot_party_nearby_friend_strength),
            (party_get_slot, ":strength_of_followers", ":root_attacker_party", slot_party_follower_strength),
            (store_add, ":total_attacker_strength", ":attacker_root_strength", ":attacker_nearby_friend_strength"),
            (val_add, ":total_attacker_strength", ":strength_of_followers"),
            
            (party_get_slot, ":defender_root_strength", ":root_defender_party", slot_party_cached_strength),
            (party_get_slot, ":defender_nearby_friend_strength", ":root_defender_party", slot_party_nearby_friend_strength),
            (store_add, ":total_defender_strength", ":defender_root_strength", ":defender_nearby_friend_strength"),
            
            (val_mul, ":total_defender_strength", 13), #multiply defender strength with 1.3
            (val_div, ":total_defender_strength", 10),
            
            (gt, ":total_defender_strength", ":total_attacker_strength"),
            (gt, ":total_defender_strength", 3),
            
            #Players can make save loads and change history because these random values are not determined from random_slots of troops
            (store_random_in_range, ":random_num", 0, 100),
            
            (try_begin),
              (lt, ":random_num", 15), #15% is a bit higher than 10% (which is open area escape probability)
              (assign, ":trigger_result", 1), #End battle!
              
              (assign, "$g_recalculate_ais", 1), #added new
              
              (try_begin),
                (eq, "$cheat_mode", 1),
                (display_message, "@{!}DEBUG : Siege attackers are running away"),
              (try_end),
            (try_end),
          (try_end),
        (try_end),
      (try_end),
      (set_trigger_result, ":trigger_result"),
  ]),
  
  #script_game_event_battle_end:
  # This script is called whenever the game ends the battle between two parties on the map.
  # INPUT:
  # param1: Defender Party
  # param2: Attacker Party
  ("game_event_battle_end",
    [
      ##       (store_script_param_1, ":root_defender_party"),
      ##       (store_script_param_2, ":root_attacker_party"),
      
      #Fixing deleted heroes
      (try_for_range, ":cur_troop", active_npcs_begin, active_npcs_end),
        (troop_slot_eq, ":cur_troop", slot_troop_occupation, slto_kingdom_hero),
        (troop_get_slot, ":cur_party", ":cur_troop", slot_troop_leaded_party),
        (troop_get_slot, ":cur_prisoner_of_party", ":cur_troop", slot_troop_prisoner_of_party),
        (try_begin),
          (ge, ":cur_party", 0),
          (assign, ":continue", 0),
          (try_begin),
            (neg|party_is_active, ":cur_party"),
            (assign, ":continue", 1),
          (else_try),
            (party_count_companions_of_type, ":amount", ":cur_party", ":cur_troop"),
            (le, ":amount", 0),
            (assign, ":continue", 1),
          (try_end),
          (eq, ":continue", 1),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, s1, ":cur_troop"),
            (display_message, "@{!}DEBUG: {s1} no longer leads a party."),
          (try_end),
          
          (troop_set_slot, ":cur_troop", slot_troop_leaded_party, -1),
          #(str_store_troop_name, s5, ":cur_troop"),
          #(display_message, "@{!}DEBUG : {s5}'s troop_leaded_party set to -1"),
        (try_end),
        (try_begin),
          (ge, ":cur_prisoner_of_party", 0),
          (assign, ":continue", 0),
          (try_begin),
            (neg|party_is_active, ":cur_prisoner_of_party"),
            (assign, ":continue", 1),
          (else_try),
            (party_count_prisoners_of_type, ":amount", ":cur_prisoner_of_party", ":cur_troop"),
            (le, ":amount", 0),
            (assign, ":continue", 1),
          (try_end),
          (eq, ":continue", 1),
          (neg|is_between, ":cur_prisoner_of_party", centers_begin, centers_end), #VC-3798 ports are made inactive on sailing
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, s1, ":cur_troop"),
            (display_message, "@{!}DEBUG: {s1} is no longer a prisoner."),
          (try_end),
          (call_script, "script_remove_troop_from_prison", ":cur_troop"),
          #searching player
          (try_begin),
            (party_count_prisoners_of_type, ":amount", "p_main_party", ":cur_troop"),
            (gt, ":amount", 0),
            (troop_set_slot, ":cur_troop", slot_troop_prisoner_of_party, "p_main_party"),
            (assign, ":continue", 0),
            (try_begin),
              (eq, "$cheat_mode", 1),
              (str_store_troop_name, s1, ":cur_troop"),
              (display_message, "@{!}DEBUG: {s1} is now a prisoner of player."),
            (try_end),
          (try_end),
          (eq, ":continue", 1),
          #searching kingdom heroes
          (try_for_range, ":cur_troop_2", active_npcs_begin, active_npcs_end),
            (troop_slot_eq, ":cur_troop_2", slot_troop_occupation, slto_kingdom_hero),
            (eq, ":continue", 1),
            (troop_get_slot, ":cur_prisoner_of_party_2", ":cur_troop_2", slot_troop_leaded_party),
            (party_is_active, ":cur_prisoner_of_party_2"),
            (party_count_prisoners_of_type, ":amount", ":cur_prisoner_of_party_2", ":cur_troop"),
            (gt, ":amount", 0),
            (troop_set_slot, ":cur_troop", slot_troop_prisoner_of_party, ":cur_prisoner_of_party_2"),
            (assign, ":continue", 0),
            (try_begin),
              (eq, "$cheat_mode", 1),
              (str_store_troop_name, s1, ":cur_troop"),
              (str_store_party_name, s2, ":cur_prisoner_of_party_2"),
              (display_message, "@{!}DEBUG: {s1} is now a prisoner of {s2}."),
            (try_end),
          (try_end),
          #searching walled centers
          (try_for_range, ":cur_prisoner_of_party_2", walled_centers_begin, walled_centers_end),
            (eq, ":continue", 1),
            (party_count_prisoners_of_type, ":amount", ":cur_prisoner_of_party_2", ":cur_troop"),
            (gt, ":amount", 0),
            (troop_set_slot, ":cur_troop", slot_troop_prisoner_of_party, ":cur_prisoner_of_party_2"),
            (assign, ":continue", 0),
            (try_begin),
              (eq, "$cheat_mode", 1),
              (str_store_troop_name, s1, ":cur_troop"),
              (str_store_party_name, s2, ":cur_prisoner_of_party_2"),
              (display_message, "@{!}DEBUG: {s1} is now a prisoner of {s2}."),
            (try_end),
          (try_end),
        (try_end),
      (try_end),
      
      #VC-2295
      (store_script_param_1, ":root_defender_party"),
      (try_begin),
        (ge, ":root_defender_party", 0),
        (party_is_active, ":root_defender_party"),
        (call_script, "script_update_party_icon", ":root_defender_party"),
      (end_try),
      (store_script_param_2, ":root_attacker_party"),
      (try_begin),
        (ge, ":root_attacker_party", 0),
        (party_is_active, ":root_attacker_party"),
        (call_script, "script_update_party_icon", ":root_attacker_party"),
      (end_try),
  ]),
  
  #script_order_best_besieger_party_to_guard_center:
  # INPUT:
  # param1: defeated_center, param2: winner_faction
  # OUTPUT:
  # none
  ("order_best_besieger_party_to_guard_center",
    [
      (store_script_param, ":defeated_center", 1),
      (store_script_param, ":winner_faction", 2),
      (assign, ":best_party", -1),
      (assign, ":best_party_strength", 0),
      (try_for_range, ":kingdom_hero", active_npcs_begin, active_npcs_end),
        (troop_get_slot, ":kingdom_hero_party", ":kingdom_hero", slot_troop_leaded_party),
        (gt, ":kingdom_hero_party", 0),
        (party_is_active, ":kingdom_hero_party"),
        (store_faction_of_party, ":kingdom_hero_party_faction", ":kingdom_hero_party"),
        (eq, ":winner_faction", ":kingdom_hero_party_faction"),
        (store_distance_to_party_from_party, ":dist", ":kingdom_hero_party", ":defeated_center"),
        (lt, ":dist", 5),
        #If marshal has captured the castle, then do not leave him behind.
        (neg|faction_slot_eq, ":winner_faction", slot_faction_marshal, ":kingdom_hero"),
        (assign, ":has_besiege_ai", 0),
        (try_begin),
          (party_slot_eq, ":kingdom_hero_party", slot_party_ai_state, spai_besieging_center),
          (party_slot_eq, ":kingdom_hero_party", slot_party_ai_object, ":defeated_center"),
          (assign, ":has_besiege_ai", 1),
        (else_try),
          (party_slot_eq, ":kingdom_hero_party", slot_party_ai_state, spai_accompanying_army),
          (party_get_slot, ":kingdom_hero_party_commander_party", ":kingdom_hero_party", slot_party_ai_object),
          (party_slot_eq, ":kingdom_hero_party_commander_party", slot_party_ai_state, spai_besieging_center),
          (party_slot_eq, ":kingdom_hero_party_commander_party", slot_party_ai_object, ":defeated_center"),
          (assign, ":has_besiege_ai", 1),
        (try_end),
        (eq, ":has_besiege_ai", 1),
        (party_get_slot, ":kingdom_hero_party_strength", ":kingdom_hero_party", slot_party_cached_strength),#recently calculated
        (gt, ":kingdom_hero_party_strength", ":best_party_strength"),
        (assign, ":best_party_strength", ":kingdom_hero_party_strength"),
        (assign, ":best_party", ":kingdom_hero_party"),
      (try_end),
      (try_begin),
        (gt, ":best_party", 0),
        (call_script, "script_party_set_ai_state", ":best_party", spai_holding_center, ":defeated_center"),
        #(party_set_slot, ":best_party", slot_party_commander_party, -1),
        (party_set_flags, ":best_party", pf_default_behavior, 1),
      (try_end),
  ]),
  
  #script_game_get_item_buy_price_factor:
  # This script is called from the game engine for calculating the buying price of any item.
  # INPUT:
  # param1: item_kind_id
  # OUTPUT:
  # trigger_result and reg0 = price_factor
  ("game_get_item_buy_price_factor",
    [
      (store_script_param_1, ":item_kind_id"),
      (assign, ":price_factor", 100),
      
      (call_script, "script_get_trade_penalty", ":item_kind_id"),
      (assign, ":trade_penalty", reg0),
      (store_attribute_level, ":charisma", "trp_player", ca_charisma),
      (party_get_skill_level, ":trade_skill", "p_main_party", "skl_trade"),
      # JuJu70 - modification to include special places
      (try_begin),
        (is_between, "$g_encountered_party", "p_quarry1", "p_hadrian_wall1"),
        (try_begin),
          (is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
          (val_mul, ":trade_skill", 2),
          (val_div, ":charisma", 2),
          (val_add, ":trade_skill", ":charisma"),
          (store_sub, ":trade_benefit", 95, ":trade_skill"),
          (assign, ":price_factor", ":trade_benefit"),
        (else_try),
          (this_or_next|eq, ":item_kind_id", "itm_cow1"),
          (eq, ":item_kind_id", "itm_cow2"),
          (val_mul, ":trade_skill", 10),
          (val_div, ":trade_skill", 3),
          (val_div, ":charisma", 3),
          (val_add, ":trade_skill", ":charisma"),
          (store_sub, ":trade_benefit", 95, ":trade_skill"),
          (assign, ":price_factor", ":trade_benefit"),
        (try_end),
      (try_end),
      #JuJu70 - end
      (try_begin),
        (is_between, "$g_encountered_party", centers_begin, centers_end),
        (is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
        (store_sub, ":item_slot_no", ":item_kind_id", trade_goods_begin),
        (val_add, ":item_slot_no", slot_town_trade_good_prices_begin),
        (party_get_slot, ":price_factor", "$g_encountered_party", ":item_slot_no"),
        (val_mul, ":price_factor", 100), #normalize price factor to range 0..100
        (val_div, ":price_factor", average_price_factor),
      (else_try),
        (is_between, "$g_encountered_party", centers_begin, centers_end),
        (neg|is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
        (val_div, ":charisma", 2),
        (val_sub, ":trade_penalty", ":charisma"),
        (val_max, ":trade_penalty", 5),
      (try_end),
      
      #VC-3251 begin
      (try_begin),
        (is_between, "$g_encountered_party", "p_quarry1", "p_farmsteadsp1"),
        (call_script, "script_get_closest_center", "$g_encountered_party"),
        (assign, ":closest_center", reg0),
        (is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
        (store_sub, ":item_slot_no", ":item_kind_id", trade_goods_begin),
        (val_add, ":item_slot_no", slot_town_trade_good_prices_begin),
        (party_get_slot, ":price_factor", ":closest_center", ":item_slot_no"),
        # (val_mul, ":price_factor", 100), #normalize price factor to range 0..100
        (val_mul, ":price_factor", 95),# special places good for buying
        (val_div, ":price_factor", average_price_factor),
      (try_end),
      #VC-3251 end
      
      (store_add, ":penalty_factor", 100, ":trade_penalty"),
      
      (val_mul, ":price_factor", ":penalty_factor"),
      (val_div, ":price_factor", 100),
      
      (assign, reg0, ":price_factor"),
      (set_trigger_result, reg0),
  ]),
  
  #script_game_get_item_sell_price_factor:
  # This script is called from the game engine for calculating the selling price of any item.
  # INPUT:
  # param1: item_kind_id
  # OUTPUT:
  # trigger_result and reg0 = price_factor
  ("game_get_item_sell_price_factor",
    [
      (store_script_param_1, ":item_kind_id"),
      (assign, ":price_factor", 100),
      
      (call_script, "script_get_trade_penalty", ":item_kind_id"),
      (assign, ":trade_penalty", reg0),
      (try_begin),
        (is_between, "$g_encountered_party", "p_quarry1", "p_farmsteadsp1"),
        (call_script, "script_get_closest_center", "$g_encountered_party"),
        (assign, ":closest_center", reg0),
        (is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
        (store_sub, ":item_slot_no", ":item_kind_id", trade_goods_begin),
        (val_add, ":item_slot_no", slot_town_trade_good_prices_begin),
        (party_get_slot, ":price_factor", ":closest_center", ":item_slot_no"),
        # (val_mul, ":price_factor", 100),#normalize price factor to range 0..100
        (val_mul, ":price_factor", 95),# special places not good for selling
        (val_div, ":price_factor", average_price_factor),
      (else_try),
        #JuJu70 - modification to include special places
        (this_or_next|is_between, "$g_encountered_party", "p_quarry1", "p_hadrian_wall1"),
        (is_between, "$g_encountered_party", "p_monasterio1", "p_yourlair"),
        (store_attribute_level, ":charisma", "trp_player", ca_charisma),
        (party_get_skill_level, ":trade_skill", "p_main_party", "skl_trade"),
        (try_begin),
          (is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
          (val_mul, ":trade_skill", 2),
          (val_div, ":charisma", 2),
          (val_add, ":trade_skill", ":charisma"),
          (store_sub, ":trade_benefit", 95, ":trade_skill"),
          (assign, ":price_factor", ":trade_benefit"),
        (else_try),
          (this_or_next|eq, ":item_kind_id", "itm_cow1"),
          (eq, ":item_kind_id", "itm_cow2"),
          (val_mul, ":trade_skill", 3),
          (val_div, ":trade_skill", 4),
          (val_div, ":charisma", 3),
          (val_add, ":trade_skill", ":charisma"),
          (store_sub, ":trade_benefit", 95, ":trade_skill"),
          (assign, ":price_factor", ":trade_benefit"),
        (else_try),
          (assign, ":price_factor", 20),
        (try_end),
      (try_end),
      # JuJu70 end
      
      (try_begin),
        (is_between, "$g_encountered_party", centers_begin, centers_end),
        (is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
        (store_sub, ":item_slot_no", ":item_kind_id", trade_goods_begin),
        (val_add, ":item_slot_no", slot_town_trade_good_prices_begin),
        (party_get_slot, ":price_factor", "$g_encountered_party", ":item_slot_no"),
        (val_mul, ":price_factor", 100),#normalize price factor to range 0..100
        (val_div, ":price_factor", average_price_factor),
      (else_try),
        (is_between, "$g_encountered_party", centers_begin, centers_end),
        #increase trade penalty while selling weapons, armor, and horses
        # JuJu70 - modified to make it more variable
        (this_or_next|is_between, ":item_kind_id", "itm_carbatinae_1", "itm_gambeson1"),
        (this_or_next|is_between, ":item_kind_id", "itm_hood_01", "itm_briton_helm"),
        (this_or_next|is_between, ":item_kind_id", "itm_wooden_stick", "itm_club_troll"),
        (this_or_next|is_between, ":item_kind_id", "itm_knife", "itm_longseax1"),
        (this_or_next|is_between, ":item_kind_id", "itm_seax_1", "itm_ragnar_seax"),
        (this_or_next|is_between, ":item_kind_id", "itm_staff", "itm_light_spear1"),
        (is_between, ":item_kind_id", "itm_sling", "itm_torch"),
        (val_mul, ":trade_penalty", 20),
      (else_try),
        (is_between, "$g_encountered_party", centers_begin, centers_end),
        (this_or_next|is_between, ":item_kind_id", "itm_darts", "itm_stones"),
        (this_or_next|is_between, ":item_kind_id", "itm_light_spear1", "itm_new_mace"),
        (this_or_next|is_between, ":item_kind_id", "itm_wooden_shield", "itm_elf_shield"),
        (is_between, ":item_kind_id", "itm_tab_shield_round_c", "itm_darts"),
        (val_mul, ":trade_penalty", 15),
      (else_try),
        (is_between, "$g_encountered_party", centers_begin, centers_end),
        (this_or_next|is_between, ":item_kind_id", "itm_gambeson1", "itm_mail_shirt"),
        (this_or_next|is_between, ":item_kind_id", "itm_longseax1", "itm_seax_1"),
        (this_or_next|is_between, ":item_kind_id", "itm_hatchet", "itm_long_axe_b"),
        (this_or_next|is_between, ":item_kind_id", "itm_briton_helm", "itm_crown1"),
        (is_between, ":item_kind_id", "itm_long_bow", "itm_sling_rock1"),
        (val_mul, ":trade_penalty", 9),
      (else_try),
        (is_between, "$g_encountered_party", centers_begin, centers_end),
        (is_between, ":item_kind_id", "itm_common_horse", "itm_cow1"),
        (val_mul, ":trade_penalty", 4),
      (else_try),
        (is_between, "$g_encountered_party", centers_begin, centers_end),
        (is_between, ":item_kind_id", "itm_cow1", "itm_arrows"),
        (val_mul, ":trade_penalty", 2),
      (else_try),
        (is_between, "$g_encountered_party", centers_begin, centers_end),
        (val_mul, ":trade_penalty", 6),
      (try_end),
      
      (store_add, ":penalty_divisor", 100, ":trade_penalty"),
      
      (val_mul, ":price_factor", 100),
      (val_div, ":price_factor", ":penalty_divisor"),
      
      (assign, reg0, ":price_factor"),
      (set_trigger_result, reg0),
  ]),
  
  # script_get_trade_penalty
  # param1: troop_id,
  # Output: reg0
  ("get_trade_penalty",
    [
      (store_script_param_1, ":item_kind_id"),
      (assign, ":penalty",0),
      
      (party_get_skill_level, ":trade_skill", "p_main_party", skl_trade),
      (try_begin),
        (is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
        (assign, ":penalty", 23), #reduced slightly 20-15-12         MOTO chief affect more with merchant skill
        (store_mul, ":skill_bonus", ":trade_skill", 2),    #MOTO   chief   affect more with merchant skill, increase from 1
        #(assign, ":penalty", 12), #reduced slightly 20-15-12
        #  (store_mul, ":skill_bonus", ":trade_skill", 1),
        (val_sub, ":penalty", ":skill_bonus"),
        (val_max, ":penalty", 3),
      (else_try),
        #JuJu70 - cows
        (this_or_next|eq, ":item_kind_id", "itm_cow1"),
        (eq, ":item_kind_id", "itm_cow2"),
        (assign, ":penalty", 25),
        (store_mul, ":skill_bonus", ":trade_skill", 2),
        (val_sub, ":penalty", ":skill_bonus"),
        (val_max, ":penalty",5),
      (else_try),
        (assign, ":penalty",80),    #MOTO chief make non-trade items      more tradeable, reduce from 100
        (store_mul, ":skill_bonus", ":trade_skill", 5),
        (val_sub, ":penalty", ":skill_bonus"),
      (try_end),
      
      (assign, ":penalty_multiplier", 1000),
      ##       # Apply penalty if player is hostile to merchants faction
      ##      (store_relation, ":merchants_reln", "fac_merchants", "fac_player_faction"),
      ##      (try_begin),
      ##        (lt, ":merchants_reln", 0),
      ##        (store_sub, ":merchants_reln_dif", 10, ":merchants_reln"),
      ##        (store_mul, ":merchants_relation_penalty", ":merchants_reln_dif", 20),
      ##        (val_add, ":penalty_multiplier", ":merchants_relation_penalty"),
      ##      (try_end),
      
      # Apply penalty if player is on bad terms with the town
      (try_begin),
        (is_between, "$g_encountered_party", centers_begin, centers_end),
        (party_get_slot, ":center_relation", "$g_encountered_party", slot_center_player_relation),
        (store_mul, ":center_relation_penalty", ":center_relation", -3),
        (val_add, ":penalty_multiplier", ":center_relation_penalty"),
        (try_begin),
          (lt, ":center_relation", 0),
          (store_sub, ":center_penalty_multiplier", 100, ":center_relation"),
          (val_mul, ":penalty_multiplier", ":center_penalty_multiplier"),
          (val_div, ":penalty_multiplier", 100),
        (try_end),
      (try_end),
      
      # Apply penalty if player is on bad terms with the merchant
      (try_begin),
        (is_between, "$g_talk_troop", armor_merchants_begin, horse_merchants_end),
        (call_script, "script_troop_get_player_relation", "$g_talk_troop"),
        (assign, ":troop_reln", reg0),
        #(troop_get_slot, ":troop_reln", "$g_talk_troop", slot_troop_player_relation),
        (try_begin),
          (lt, ":troop_reln", 0),
          (store_sub, ":troop_reln_dif", 0, ":troop_reln"),
          (store_mul, ":troop_relation_penalty", ":troop_reln_dif", 10),
          (val_add, ":penalty_multiplier", ":troop_relation_penalty"),
        (try_end),
      (try_end),
      
      (try_begin),
        (is_between, "$g_encountered_party", villages_begin, villages_end),
        (val_mul, ":penalty", 5), #1.25x trade penalty in villages.
        (val_div, ":penalty", 4),
      (try_end),
      
      #(try_begin),
      #(is_between, "$g_encountered_party", centers_begin, centers_end),
      ##Double trade penalty if no local production or consumption
      #(is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
      #(call_script, "script_center_get_production", "$g_encountered_party", ":item_kind_id"),
      #(eq, reg0, 0),
      #(call_script, "script_center_get_consumption", "$g_encountered_party", ":item_kind_id"),
      #(eq, reg0, 0),
      #(val_mul, ":penalty", 2),
      #(try_end),
      
      (val_mul, ":penalty",  ":penalty_multiplier"),
      (val_div, ":penalty", 1000),
      (val_max, ":penalty", 1),
      (assign, reg0, ":penalty"),
  ]),
  
  #script_game_event_buy_item:
  # This script is called from the game engine when player buys an item.
  # INPUT:
  # param1: item_kind_id
  ("game_event_buy_item",
    [
      (store_script_param_1, ":item_kind_id"),
      (store_script_param_2, ":reclaim_mode"),
      
      #for debug log begin
      (item_get_value, ":item_value_debug", ":item_kind_id"),
      (try_begin),
        (neq, ":reclaim_mode", 1),
        (call_script, "script_game_get_item_buy_price_factor", ":item_kind_id"),
        (val_mul, ":item_value_debug", reg0),
        (val_div, ":item_value_debug", 100),	#we cant check imod
        (try_begin),
          (is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
          (val_add, "$num_buyed_goods_in_week", 1),
          (val_add, "$buyed_goods_in_week_for", ":item_value_debug"),
        (else_try),
          (val_add, "$num_buyed_items_in_week", 1),
          (val_add, "$buyed_items_in_week_for", ":item_value_debug"),
        (end_try),
      (end_try),
      #for debug log end
      
      (try_begin),
        (is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
        (store_sub, ":item_slot_no", ":item_kind_id", trade_goods_begin),
        (val_add, ":item_slot_no", slot_town_trade_good_prices_begin),
        (party_get_slot, ":multiplier", "$g_encountered_party", ":item_slot_no"),
        (try_begin),
          (eq, ":reclaim_mode", 0),
          (val_add, ":multiplier", 14),
        (else_try),
          (val_add, ":multiplier", 21), #VC minimum margin (penalty to player) is 2%
        (try_end),
        
        # (store_item_value, ":item_value", ":item_kind_id"),
        # (try_begin),
          # (ge, ":item_value", 100),
          # (store_sub, ":item_value_sub_100", ":item_value", 100),
          # (store_div, ":item_value_sub_100_div_8", ":item_value_sub_100", 8),
          # (val_add, ":multiplier", ":item_value_sub_100_div_8"),
        # (try_end),
        
        (val_min, ":multiplier", maximum_price_factor),
        
        (party_set_slot, "$g_encountered_party", ":item_slot_no", ":multiplier"),
      (try_end),
      
      #debug log
      (try_begin),
        (eq, ":reclaim_mode", 1),
        (call_script, "script_game_get_item_sell_price_factor", ":item_kind_id"),
        (val_mul, ":item_value_debug", reg0),
        (val_div, ":item_value_debug", 100),	#we cant check imod
        (try_begin),
          (is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
          (val_sub, "$num_selled_goods_in_week", 1),
          (val_sub, "$selled_goods_in_week_for", ":item_value_debug"),
        (else_try),
          (val_sub, "$num_selled_items_in_week", 1),
          (val_sub, "$selled_items_in_week_for", ":item_value_debug"),
        (end_try),
      (end_try),
      
      (try_begin),
        (ge, "$vc_debug_mode", 1),
        (assign, reg1, "$num_selled_goods_in_week"),
        (assign, reg2, "$selled_goods_in_week_for"),
        (assign, reg3, "$num_selled_items_in_week"),
        (assign, reg4, "$selled_items_in_week_for"),
        (display_message, "str_print_player_trade"),
        (assign, reg1, "$num_buyed_goods_in_week"),
        (assign, reg2, "$buyed_goods_in_week_for"),
        (assign, reg3, "$num_buyed_items_in_week"),
        (assign, reg4, "$buyed_items_in_week_for"),
        (display_message, "str_print_player_trade"),
      (end_try),
  ]),
  
  #script_game_event_sell_item:
  # This script is called from the game engine when player sells an item.
  # INPUT:
  # param1: item_kind_id
  ("game_event_sell_item",
    [
      (store_script_param_1, ":item_kind_id"),
      (store_script_param_2, ":return_mode"),
      
      #for debug log
      (item_get_value, ":item_value_debug", ":item_kind_id"),
      (try_begin),
        (neq, ":return_mode", 1),
        (call_script, "script_game_get_item_sell_price_factor", ":item_kind_id"),
        (val_mul, ":item_value_debug", reg0),
        (val_div, ":item_value_debug", 100),	#we cant check imod
        (try_begin),
          (is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
          (val_add, "$num_selled_goods_in_week", 1),
          (val_add, "$selled_goods_in_week_for", ":item_value_debug"),
        (else_try),
          (val_add, "$num_selled_items_in_week", 1),
          (val_add, "$selled_items_in_week_for", ":item_value_debug"),
        (end_try),
      (end_try),
      #debug log end
      
      (try_begin),
        (is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
        (store_sub, ":item_slot_no", ":item_kind_id", trade_goods_begin),
        (val_add, ":item_slot_no", slot_town_trade_good_prices_begin),
        (party_get_slot, ":multiplier", "$g_encountered_party", ":item_slot_no"),
        (try_begin),
          (eq, ":return_mode", 0),
          (val_sub, ":multiplier", 21), #above values, compensating for this being a relatively greater percentage
        (else_try),
          (val_sub, ":multiplier", 14),
        (try_end),
        
        # (store_item_value, ":item_value", ":item_kind_id"),
        # (try_begin),
          # (ge, ":item_value", 100),
          # (store_sub, ":item_value_sub_100", ":item_value", 100),
          # (store_div, ":item_value_sub_100_div_8", ":item_value_sub_100", 8),
          # (val_sub, ":multiplier", ":item_value_sub_100_div_8"),
        # (try_end),
        
        (val_max, ":multiplier", minimum_price_factor),
        
        (party_set_slot, "$g_encountered_party", ":item_slot_no", ":multiplier"),
      (try_end),
      
      # debug log
      (try_begin),
        (eq, ":return_mode", 1),
        (call_script, "script_game_get_item_buy_price_factor", ":item_kind_id"),
        (val_mul, ":item_value_debug", reg0),
        (val_div, ":item_value_debug", 100),	#we cant check imod
        (try_begin),
          (is_between, ":item_kind_id", trade_goods_begin, trade_goods_end),
          (val_sub, "$num_buyed_goods_in_week", 1),
          (val_sub, "$buyed_goods_in_week_for", ":item_value_debug"),
        (else_try),
          (val_sub, "$num_buyed_items_in_week", 1),
          (val_sub, "$buyed_items_in_week_for", ":item_value_debug"),
        (end_try),
      (end_try),
      
      (try_begin),
        (ge, "$vc_debug_mode", 1),
        (assign, reg1, "$num_selled_goods_in_week"),
        (assign, reg2, "$selled_goods_in_week_for"),
        (assign, reg3, "$num_selled_items_in_week"),
        (assign, reg4, "$selled_items_in_week_for"),
        (display_message, "str_print_player_trade"),
        (assign, reg1, "$num_buyed_goods_in_week"),
        (assign, reg2, "$buyed_goods_in_week_for"),
        (assign, reg3, "$num_buyed_items_in_week"),
        (assign, reg4, "$buyed_items_in_week_for"),
        (display_message, "str_print_player_trade"),
      (end_try),
  ]),
  
  #script_start_wedding_cutscene
  # INPUT: arg1 = groom_troop, arg2 = bride_troop
  # OUTPUT: none
  # ("start_wedding_cutscene",
    # [
      # (store_script_param, "$g_wedding_groom_troop", 1),
      # (store_script_param, "$g_wedding_bride_troop", 2),
      
      # (assign, "$g_wedding_bishop_troop", "trp_temporary_minister"),
      # (try_begin),
        # (is_between, "$players_kingdom", kingdoms_begin, kingdoms_end),
        # (neg|faction_slot_eq, "$players_kingdom", slot_faction_leader, "$g_wedding_groom_troop"),
        # (neg|faction_slot_eq, "$players_kingdom", slot_faction_leader, "$g_wedding_bride_troop"),
        # (faction_get_slot, ":players_king", "$players_kingdom", slot_faction_leader),
        # (troop_get_type, ":troop_type", ":players_king"),
        # (val_mod, ":troop_type", 2),
        # (eq, ":troop_type", 0), #male
        # (neq, ":players_king", "$g_wedding_groom_troop"),
        # (assign, "$g_wedding_bishop_troop", ":players_king"),
      # (else_try),
        # (eq, "$players_kingdom", "fac_player_supporters_faction"),
        # (faction_slot_eq, "$players_kingdom", slot_faction_leader, "trp_player"),
        # (gt, "$g_player_minister", 0),
        # (troop_get_type, ":troop_type", "$g_player_minister"),
        # (val_mod, ":troop_type", 2),
        # (eq, ":troop_type", 0), #male
        # (neq, "$g_player_minister", "$g_wedding_groom_troop"),
        # (assign, "$g_wedding_bishop_troop", "$g_player_minister"),
      # (try_end),
      
      # (assign, "$g_wedding_brides_dad_troop", "trp_temporary_minister"),
      # (try_begin),
        # (neq, "$g_wedding_bride_troop", "trp_player"),
        # (try_begin),
          # (troop_get_slot, ":father", "$g_wedding_bride_troop", slot_troop_father),
          # (gt, ":father", 0),
          # (troop_get_type, ":troop_type", ":father"), #just to make sure
          # (val_mod, ":troop_type", 2),
          # (eq, ":troop_type", 0), #male
          # (neq, ":father", "$g_wedding_groom_troop"), #this might be 0 due to an error
          # (neq, ":father", "$g_wedding_bishop_troop"),
          # (assign, "$g_wedding_brides_dad_troop", ":father"),
        # (else_try),
          # (troop_get_slot, ":guardian", "$g_wedding_bride_troop", slot_troop_guardian),
          # (gt, ":guardian", 0),
          # (troop_get_type, ":troop_type", ":guardian"), #just to make sure
          # (val_mod, ":troop_type", 2),
          # (eq, ":troop_type", 0), #male
          # (neq, ":guardian", "$g_wedding_groom_troop"), #this might be 0 due to an error
          # (neq, ":guardian", "$g_wedding_bishop_troop"),
          # (assign, "$g_wedding_brides_dad_troop", ":guardian"),
        # (try_end),
      # (else_try),
        # (try_for_range, ":cur_companion", companions_begin, companions_end),
          # (this_or_next|troop_slot_eq, ":cur_companion", slot_troop_occupation, slto_player_companion),
          # (troop_slot_eq, ":cur_companion", slot_troop_occupation, slto_kingdom_hero),
          # (troop_get_type, ":troop_type", ":cur_companion"), #just to make sure
          # (val_mod, ":troop_type", 2),
          # (eq, ":troop_type", 0), #male
          # (neq, ":cur_companion", "$g_wedding_groom_troop"),
          # (neq, ":cur_companion", "$g_wedding_bishop_troop"),
          # (assign, "$g_wedding_brides_dad_troop", ":cur_companion"),
        # (try_end),
      # (try_end),
      
      # (modify_visitors_at_site,"scn_wedding"),
      # (reset_visitors,0),
      # (set_visitor, 0, "$g_wedding_groom_troop"),
      # (set_visitor, 1, "$g_wedding_bride_troop"),
      # (set_visitor, 2, "$g_wedding_brides_dad_troop"),
      # (set_visitor, 3, "$g_wedding_bishop_troop"),
      # (assign, ":num_visitors", 4),
      # (assign, ":num_male_visitors", 0),
      # (try_for_range, ":cur_npc", active_npcs_begin, kingdom_ladies_end),
        # (lt, ":num_visitors", 32),
        # (neq, ":cur_npc", "$g_wedding_groom_troop"),
        # (neq, ":cur_npc", "$g_wedding_bride_troop"),
        # (neq, ":cur_npc", "$g_wedding_brides_dad_troop"),
        # (neq, ":cur_npc", "$g_wedding_bishop_troop"),
        # (store_troop_faction, ":npc_faction", ":cur_npc"),
        # (is_between, ":npc_faction", kingdoms_begin, kingdoms_end),
        # (eq, ":npc_faction", "$players_kingdom"),
        # (this_or_next|troop_slot_eq, ":cur_npc", slot_troop_occupation, slto_player_companion),
        # (this_or_next|troop_slot_eq, ":cur_npc", slot_troop_occupation, slto_kingdom_hero),
        # (troop_slot_eq, ":cur_npc", slot_troop_occupation, slto_kingdom_lady),
        # (troop_get_type, ":troop_type", ":cur_npc"),
        # (val_mod, ":troop_type", 2),
        # (assign, ":continue_adding", 1),
        # (try_begin),
          # (eq, ":troop_type", 0),
          # (assign, ":continue_adding", 0),
          # (lt, ":num_male_visitors", 16), #limit number of male visitors
          # (assign, ":continue_adding", 1),
          # (val_add, ":num_male_visitors", 1),
        # (try_end),
        # (eq, ":continue_adding", 1),
        # (set_visitor, ":num_visitors", ":cur_npc"),
        # (val_add, ":num_visitors", 1),
      # (try_end),
      # (set_jump_mission,"mt_wedding"),
      # (jump_to_scene,"scn_wedding"),
      # (change_screen_mission),
  # ]),
  
  
  # script_game_get_troop_wage
  # This script is called from the game engine for calculating troop wages.
  # Input:
  # param1: troop_id, param2: party-id
  # Output: reg0: weekly wage sueldos
  # troops level = or less than 23 are cheap. x2 for troops more level
  ("game_get_troop_wage",
    [
      (store_script_param_1, ":troop_id"),
      (store_script_param_2, ":unused"), #party id
      
      (assign,":wage", 0),
      (store_skill_level, ":leadership_level", "skl_leadership", "trp_player"), #good leadership mean pay a lot less
      (store_skill_level, ":persuasion", "skl_persuasion", "trp_player"),
      (val_sub, ":persuasion", 6),
      (try_begin),
        (this_or_next|eq, ":troop_id", "trp_player"),
        (this_or_next|eq, ":troop_id", "trp_sacerdote_1"),
        (eq, ":troop_id", "trp_kidnapped_girl"),
      (else_try),
        (is_between, ":troop_id", pretenders_begin, pretenders_end),
      (else_try), #chief cambia sueldos de tropas, produces wage =  (troop_level + 20)  * (troop_level + 20) / 20
        (store_character_level, ":troop_level", ":troop_id"), #produces wage chief =  (troop_level * 20)  + (troop_level * 20) / 25
        (assign, ":wage", ":troop_level"),
        (try_begin),
          (le, ":troop_level", 23), #26 are good troops and cheap -- too cheap
          (val_add, ":wage", 6), #chief cambia
          (val_sub, ":wage", ":leadership_level"), #chief cambia
        (else_try),
          (le, ":troop_level", 26),
          (val_add, ":wage", 12), #chief cambia
          (val_sub, ":wage", ":leadership_level"), #chief cambia
          (val_sub, ":wage", ":persuasion"),
          (val_mul, ":wage", 6),
          (val_div, ":wage", 5),
        (else_try),
          #       (gt, ":troop_level", 23), #27 or higher level expensive
          (val_add, ":wage", 22), #chief cambia
          (val_sub, ":wage", ":leadership_level"), #chief cambia
          (val_sub, ":wage", ":persuasion"),
          (val_mul, ":wage", 4), #chief cambia
          (val_div, ":wage", 3),
        (try_end),
        (val_max, ":wage", 1),
      (try_end),
      
      (try_begin), #mounted troops cost 25% more than the normal cost
        (neg|is_between, ":troop_id", companions_begin, companions_end),
        (troop_is_mounted, ":troop_id"),
        (val_mul, ":wage", 5), #chief cambiado
        (val_div, ":wage", 4), #chief cambiado
      (try_end),
      
      (try_begin), #mercenaries cost %25 more than the normal cost
        (this_or_next|is_between, ":troop_id", mercenary_troops_begin, mercenary_troops_end),
        (is_between, ":troop_id", "trp_manhunter", "trp_follower_woman"),
        (val_mul, ":wage", 6),
        (val_div, ":wage", 5), #chief cambiado
      (try_end),
      
      
      (try_begin),
        (is_between, ":troop_id", companions_begin, companions_end),
        (val_mul, ":wage", 4), #they are special.
      (try_end),
      
      (try_begin), #aumenta bandidos
        (is_between, ":troop_id", bandits_begin, "trp_steppe_bandit"),
        (try_begin),
          (gt,"$player_honor",-50),
          (val_mul, ":wage", 4),
          (val_div, ":wage", 3), #chief cambiado
        (else_try),
          (val_mul, ":wage", 7),
          (val_div, ":wage", 8), #chief cambiado
        (try_end),
      (else_try),
        (is_between, ":troop_id", "trp_steppe_bandit","trp_manhunter"),
        (try_begin),
          (gt,"$player_honor",-50),
          (val_mul, ":wage", 3),
          (val_div, ":wage", 2), #chief cambiado
        (else_try),
          (val_mul, ":wage", 5),
          (val_div, ":wage", 4), #chief cambiado
        (try_end),
      (try_end),
      
      (try_begin),
        (neq, ":troop_id", "trp_player"),
        (neq, ":troop_id", "trp_kidnapped_girl"),
        (neg|is_between, ":troop_id", pretenders_begin, pretenders_end),
        (val_max, ":wage", 1),
      (try_end),
      (try_begin),
        (eq, "$easy_wage", 1),
        (val_mul, ":wage", 60),
        (val_div, ":wage", 100),
      (try_end),
      (assign, reg0, ":wage"),
      (set_trigger_result, reg0),
  ]),
  
  # script_game_get_total_wage
  # This script is called from the game engine for calculating total wage of the player party which is shown at the party window.
  # Input: none
  # Output: reg0: weekly wage
  
  ("game_get_total_wage",
    [
      (assign, ":total_wage", 0),
      (party_get_num_companion_stacks, ":num_stacks", "p_main_party"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop", "p_main_party", ":i_stack"),
        (party_stack_get_size, ":stack_size", "p_main_party", ":i_stack"),
        (call_script, "script_game_get_troop_wage", ":stack_troop", 0),
        (val_mul, reg0, ":stack_size"),
        (val_add, ":total_wage", reg0),
      (try_end),
      (assign, reg0, ":total_wage"),
      (set_trigger_result, reg0),
  ]),
  
  # script_game_get_join_cost
  # This script is called from the game engine for calculating troop join cost.
  # Input:
  # param1: troop_id,
  # Output: reg0: weekly wage
  
  ("game_get_join_cost",
    [
      (store_script_param_1, ":troop_id"),
      
      (assign,":join_cost", 0),
      (try_begin),
        (troop_is_hero, ":troop_id"),
      (else_try),
        (store_character_level, ":troop_level", ":troop_id"),
        (assign, ":join_cost", ":troop_level"),
        (try_begin),
          (eq, ":troop_level", 18),
          (val_add, ":join_cost", 10),
        (else_try),
          (val_add, ":join_cost", 20),
        (try_end),
        (val_mul, ":join_cost", ":join_cost"),
        (val_add, ":join_cost", 400), #was 40
        (val_div, ":join_cost", 5),
        #JuJu70
        (try_begin),
          (troop_get_slot, ":player_renown", "trp_player", slot_troop_renown),
          (ge, ":player_renown", 400),
          (val_min, ":player_renown" ,1000),
          (val_sub, ":player_renown", 100),
          (val_div, ":player_renown", 10),
          (val_sub, ":join_cost", ":player_renown"),
        (try_end),
        #JuJu70 end
        (try_begin), #mounted troops cost %100 more than the normal cost chief cambia
          (troop_is_mounted, ":troop_id"),
          (val_mul, ":join_cost", 2),
        (try_end),
      (try_end),
      (assign, reg0, ":join_cost"),
      (set_trigger_result, reg0),
  ]),
  
  # script_game_get_upgrade_xp
  # This script is called from game engine for calculating needed troop upgrade exp
  # Input:
  # param1: troop_id,
  # Output: reg0 = needed exp for upgrade
  ("game_get_upgrade_xp",
    [
      (store_script_param_1, ":troop_id"),
      
      (assign, ":needed_upgrade_xp", 0),
      #formula : int needed_upgrade_xp = 2 * (30 + 0.006f * level_boundaries[troops[troop_id].level + 3]);
      (store_character_level, ":troop_level", ":troop_id"),
      (try_begin),
        (eq,"$easy_levelling", 0),
        (try_begin),
          (ge, ":troop_level", 28),
          (store_add, ":needed_upgrade_xp", ":troop_level", 4),# was 8
        (else_try),
          (store_add, ":needed_upgrade_xp", ":troop_level", 8), #high level troops more difficult to upgrade -10
        (try_end),
      (else_try),
        (eq,"$easy_levelling", 1),
        (try_begin),
          (ge, ":troop_level", 28),
          (store_add, ":needed_upgrade_xp", ":troop_level", 2),# was 8
        (else_try),
          (store_add, ":needed_upgrade_xp", ":troop_level", 4), #high level troops more difficult to upgrade -10
        (try_end),
      (else_try),
        (eq,"$easy_levelling", 2),
        (try_begin),
          (le, ":troop_level", 23),
          (store_add, ":needed_upgrade_xp", ":troop_level", 8),# was 8
        (else_try),
          (store_add, ":needed_upgrade_xp", ":troop_level", 10), #high level troops more difficult to upgrade -10
        (try_end),
      (try_end),
      (get_level_boundary, reg0, ":needed_upgrade_xp"),
      (val_mul, reg0, 6),
      (val_div, reg0, 1000),
      (val_add, reg0, 30),
      (try_begin),
        (gt, ":troop_level", 23),
        (try_begin),
          (eq,"$easy_levelling", 1),
          (val_mul, reg0, 6),
          (val_div, reg0, 5),
        (else_try),
          (eq,"$easy_levelling", 0),
          (val_mul, reg0, 4),
          (val_div, reg0, 3),
        (else_try),
          (eq, "$easy_levelling", 2),
          (val_mul, reg0, 3), #high level troops more difficult to upgrade -- was 3
        (try_end),
      (try_end),
      (try_begin),
        (eq, "$easy_levelling", 2),
        (is_between, ":troop_id", bandits_begin, bandits_end),
        (val_mul, reg0, 2),
      (else_try),
        (this_or_next|eq,"$easy_levelling", 1),
        (eq,"$easy_levelling", 0),
        (is_between, ":troop_id", bandits_begin, bandits_end),
        (val_mul, reg0, 2),
        (val_div, reg0, 3),
      (try_end),
      (set_trigger_result, reg0),
  ]),
  
  # script_game_get_upgrade_cost
  # This script is called from game engine for calculating needed troop upgrade exp
  # Input:
  # param1: troop_id,
  # Output: reg0 = needed cost for upgrade
  ("game_get_upgrade_cost",
    [
      (store_script_param_1, ":troop_id"),
      
      (store_character_level, ":troop_level", ":troop_id"),
      (try_begin),
        (le, ":troop_level", 23),
        (assign, ":cost", ":troop_level"),
        (try_begin),
          (eq, "$easy_wage", 1), #easy half cost
          (val_mul, ":cost", 8),
        (else_try),
          (val_mul, ":cost", 16),# was 4 - simplified calculations + expensive. Upgrading is expensive.
        (try_end),
      (else_try),
        (assign, ":cost", ":troop_level"),
        (try_begin),
          (eq, "$easy_wage", 1), #easy half cost
          (val_mul, ":cost", 26),
        (else_try),
          (val_mul, ":cost", 52),
        (try_end),
      (try_end),
      (try_begin), #mounted troops cost 33% more than the normal cost
        (troop_is_mounted, ":troop_id"),
        (val_mul, ":cost", 4),
        (val_div, ":cost", 3),
      (try_end),
      (assign, reg0, ":cost"),
      (set_trigger_result, reg0),
  ]),
  
  # script_game_get_prisoner_price
  # This script is called from the game engine for calculating prisoner price
  # Input:
  # param1: troop_id,
  # Output: reg0
  #cambiado chief para que siempre sea importante el nivel
  ("game_get_prisoner_price",
    [
      (store_script_param_1, ":troop_id"),
      (store_character_level, ":troop_level", ":troop_id"),
      (try_begin),
        (ge, ":troop_level", 29),
        (store_mul, ":ransom_max", ":troop_level", 5),
      (else_try),
        (is_between, ":troop_level", 23, 29),
        (store_mul, ":ransom_max", ":troop_level", 3),
        (val_add, ":ransom_max", 5),
      (else_try),
        (this_or_next|eq, ":troop_id", "trp_farmer"),
        (this_or_next|eq, ":troop_id", "trp_follower_woman"),
        (this_or_next|eq, ":troop_id", "trp_refugee"),
        (eq, ":troop_id", "trp_peasant_woman"),
        (store_mul, ":ransom_max", ":troop_level", 2),
        (val_add, ":ransom_max", 3),
      (else_try),
        (neq, ":troop_id", "trp_farmer"),
        (neq, ":troop_id", "trp_follower_woman"),
        (neq, ":troop_id", "trp_refugee"),
        (neq, ":troop_id", "trp_peasant_woman"),
        (store_mul, ":ransom_max", ":troop_level", 5),
        (val_div, ":ransom_max", 2),
        (val_add, ":ransom_max", 5),
      (try_end),
      (assign, ":ransom_amount", ":troop_level"),
      (store_attribute_level, ":charisma", "trp_player", ca_charisma),
      (store_skill_level, ":trade", "skl_trade", "trp_player"),
      (val_sub, ":charisma", 12),
      (val_div, ":trade", 3),
      (val_add, ":ransom_amount", ":charisma"),
      (val_add, ":ransom_amount", ":trade"),
      (val_mul, ":ransom_amount", ":ransom_amount"),
      (try_begin),
        (lt, "$player_honor", -30),
        (val_mul, ":ransom_amount", 115),
        (val_div, ":ransom_amount", 100),
      (try_end),
      (try_begin),
        (is_between, "$g_talk_troop", ransom_brokers_begin, ransom_brokers_end),
        (val_div, ":ransom_amount", 8),
      (else_try),
        (val_div, ":ransom_amount", 12),
      (try_end),
      (val_add, ":ransom_max", 1), #ajust high prices
      (val_clamp, ":ransom_amount", 15,":ransom_max"),
      #(val_min, ":ransom_amount", ":ransom_max"),
      (assign, reg0, ":ransom_amount"),
      (set_trigger_result, reg0),
  ]),
  
  # script_game_check_prisoner_can_be_sold
  # This script is called from the game engine for checking if a given troop can be sold.
  # Input:
  # param1: troop_id,
  # Output: reg0: 1= can be sold; 0= cannot be sold.
  
  ("game_check_prisoner_can_be_sold",
    [
      (store_script_param_1, ":troop_id"),
      (assign, reg0, 0),
      (try_begin),
        (neg|troop_is_hero, ":troop_id"),
        (assign, reg0, 1),
      (try_end),
      (set_trigger_result, reg0),
  ]),
  
  # script_game_get_morale_of_troops_from_faction
  # This script is called from the game engine
  # Input:
  # param1: faction_no,
  # Output: reg0: extra morale x 100
  
  ("game_get_morale_of_troops_from_faction",
    [
      (store_script_param_1, ":troop_no"),
      
      (store_troop_faction, ":faction_no", ":troop_no"),
      
      (try_begin),
        (ge, ":faction_no", npc_kingdoms_begin),
        (lt, ":faction_no", npc_kingdoms_end),
        
        (faction_get_slot, reg0, ":faction_no",  slot_faction_morale_of_player_troops),
        
        #(assign, reg1, ":faction_no"),
        #(assign, reg2, ":troop_no"),
        #(assign, reg3, reg0),
        #(display_message, "@extra morale for troop {reg2} of faction {reg1} is {reg3}"),
      (else_try),
        (assign, reg0, 0),
      (try_end),
      
      (val_div, reg0, 100),
      
      (party_get_morale, reg1, "p_main_party"),
      
      (val_add, reg0, reg1),
      
      (set_trigger_result, reg0),
  ]),
  
  #script_game_event_detect_party:
  # This script is called from the game engine when player party inspects another party.
  # INPUT:
  # param1: Party-id
  ("game_event_detect_party",
    [
      (store_script_param_1, ":party_id"),
      (try_begin),
        (party_slot_eq, ":party_id", slot_party_type, spt_kingdom_hero_party),
        (party_stack_get_troop_id, ":leader", ":party_id", 0),
        (is_between, ":leader", active_npcs_begin, active_npcs_end),
        (call_script, "script_update_troop_location_notes", ":leader", 0),
      (else_try),
        (is_between, ":party_id", walled_centers_begin, walled_centers_end),
        (party_get_num_attached_parties, ":num_attached_parties",  ":party_id"),
        (try_for_range, ":attached_party_rank", 0, ":num_attached_parties"),
          (party_get_attached_party_with_rank, ":attached_party", ":party_id", ":attached_party_rank"),
          (party_stack_get_troop_id, ":leader", ":attached_party", 0),
          (is_between, ":leader", active_npcs_begin, active_npcs_end),
          (call_script, "script_update_troop_location_notes", ":leader", 0),
        (try_end),
      (try_end),
  ]),
  
  #script_game_event_undetect_party:
  # This script is called from the game engine when player party inspects another party.
  # INPUT:
  # param1: Party-id
  ("game_event_undetect_party",
    [
      (store_script_param_1, ":party_id"),
      (try_begin),
        (party_slot_eq, ":party_id", slot_party_type, spt_kingdom_hero_party),
        (party_stack_get_troop_id, ":leader", ":party_id", 0),
        (is_between, ":leader", active_npcs_begin, active_npcs_end),
        (call_script, "script_update_troop_location_notes", ":leader", 0),
      (try_end),
  ]),
  
  #script_game_get_statistics_line:
  # This script is called from the game engine when statistics page is opened.
  # INPUT:
  # param1: line_no
  ("game_get_statistics_line",
    [
      (store_script_param_1, ":line_no"),
      (try_begin),
        (eq, ":line_no", 0),
        (get_player_agent_kill_count, reg1),
        (str_store_string, s1, "str_number_of_troops_killed_reg1"),
        (set_result_string, s1),
      (else_try),
        (eq, ":line_no", 1),
        (get_player_agent_kill_count, reg1, 1),
        (str_store_string, s1, "str_number_of_troops_wounded_reg1"),
        (set_result_string, s1),
      (else_try),
        (eq, ":line_no", 2),
        (get_player_agent_own_troop_kill_count, reg1),
        (str_store_string, s1, "str_number_of_own_troops_killed_reg1"),
        (set_result_string, s1),
      (else_try),
        (eq, ":line_no", 3),
        (get_player_agent_own_troop_kill_count, reg1, 1),
        (str_store_string, s1, "str_number_of_own_troops_wounded_reg1"),
        (set_result_string, s1),
      (try_end),
  ]),
  
  #script_game_get_date_text:
  # This script is called from the game engine when the date needs to be displayed.
  # INPUT: arg1 = number of days passed since the beginning of the game
  # OUTPUT: result string = date
  ("game_get_date_text",
    [
      (store_script_param_2, ":num_hours"),
      (store_div, ":num_days", ":num_hours", 24),
      (store_add, ":cur_day", ":num_days", 2),
      (assign, ":cur_month", 2),
      (assign, ":cur_year", 868),
      (assign, ":try_range", 99999),
      (try_for_range, ":unused", 0, ":try_range"),
        (try_begin),
          (this_or_next|eq, ":cur_month", 1),
          (this_or_next|eq, ":cur_month", 3),
          (this_or_next|eq, ":cur_month", 5),
          (this_or_next|eq, ":cur_month", 7),
          (this_or_next|eq, ":cur_month", 8),
          (this_or_next|eq, ":cur_month", 10),
          (eq, ":cur_month", 12),
          (assign, ":month_day_limit", 31),
        (else_try),
          (this_or_next|eq, ":cur_month", 4),
          (this_or_next|eq, ":cur_month", 6),
          (this_or_next|eq, ":cur_month", 9),
          (eq, ":cur_month", 11),
          (assign, ":month_day_limit", 30),
        (else_try),
          (try_begin),
            (store_div, ":cur_year_div_4", ":cur_year", 4),
            (val_mul, ":cur_year_div_4", 4),
            (eq, ":cur_year_div_4", ":cur_year"),
            (assign, ":month_day_limit", 29),
          (else_try),
            (assign, ":month_day_limit", 28),
          (try_end),
        (try_end),
        (try_begin),
          (gt, ":cur_day", ":month_day_limit"),
          (val_sub, ":cur_day", ":month_day_limit"),
          (val_add, ":cur_month", 1),
          (try_begin),
            (gt, ":cur_month", 12),
            (val_sub, ":cur_month", 12),
            (val_add, ":cur_year", 1),
          (try_end),
        (else_try),
          (assign, ":try_range", 0),
        (try_end),
      (try_end),
      (assign, reg1, ":cur_day"),
      (assign, reg2, ":cur_year"),
      (assign, "$g_cur_month", ":cur_month"), #chief siege warfare
      (try_begin),
        (eq, ":cur_month", 1),
        (str_store_string, s1, "str_january_reg1_reg2"),
      (else_try),
        (eq, ":cur_month", 2),
        (str_store_string, s1, "str_february_reg1_reg2"),
      (else_try),
        (eq, ":cur_month", 3),
        (str_store_string, s1, "str_march_reg1_reg2"),
      (else_try),
        (eq, ":cur_month", 4),
        (str_store_string, s1, "str_april_reg1_reg2"),
      (else_try),
        (eq, ":cur_month", 5),
        (str_store_string, s1, "str_may_reg1_reg2"),
      (else_try),
        (eq, ":cur_month", 6),
        (str_store_string, s1, "str_june_reg1_reg2"),
      (else_try),
        (eq, ":cur_month", 7),
        (str_store_string, s1, "str_july_reg1_reg2"),
      (else_try),
        (eq, ":cur_month", 8),
        (str_store_string, s1, "str_august_reg1_reg2"),
      (else_try),
        (eq, ":cur_month", 9),
        (str_store_string, s1, "str_september_reg1_reg2"),
      (else_try),
        (eq, ":cur_month", 10),
        (str_store_string, s1, "str_october_reg1_reg2"),
      (else_try),
        (eq, ":cur_month", 11),
        (str_store_string, s1, "str_november_reg1_reg2"),
      (else_try),
        (eq, ":cur_month", 12),
        (str_store_string, s1, "str_december_reg1_reg2"),
      (try_end),
      (set_result_string, s1),
  ]),
  
  #script_game_get_money_text:
  # This script is called from the game engine when an amount of money needs to be displayed.
  # INPUT: arg1 = amount in units
  # OUTPUT: result string = money in text
  ("game_get_money_text",
    [
      (store_script_param_1, ":amount"),
      (try_begin),
        (eq, ":amount", 1),
        (str_store_string, s1, "str_1_denar"),
      (else_try),
        (assign, reg1, ":amount"),
        (str_store_string, s1, "str_reg1_denars"),
      (try_end),
      (set_result_string, s1),
  ]),
  
  #chief native off, vamos a usar motomataru
  ##  #script_game_get_party_companion_limit:
  ##  # This script is called from the game engine when the companion limit is needed for a party.
  ##  # INPUT: arg1 = none
  ##  # OUTPUT: reg0 = companion_limit
  ##  ("game_get_party_companion_limit",
  ##    [
  ##      (assign, ":troop_no", "trp_player"),
  ##
  ##      (assign, ":limit", 5), #chief cambia para reducir importancia liderazgo
  ##      (store_skill_level, ":skill", "skl_leadership", ":troop_no"),
  ##      (store_attribute_level, ":charisma", ":troop_no", ca_charisma),
  ##      (val_mul, ":skill", 5),
  ##      (val_add, ":limit", ":skill"),
  ##      (val_add, ":limit", ":charisma"),
  ##
  ##      (troop_get_slot, ":troop_renown", ":troop_no", slot_troop_renown),
  ##      (store_div, ":renown_bonus", ":troop_renown", 25),
  ##      (val_add, ":limit", ":renown_bonus"),
  ##
  ##      (assign, reg0, ":limit"),
  ##      (set_trigger_result, reg0),
  ##  ]),
  
  #script_game_get_party_companion_limit: Version motomataru
  # This script is called from the game engine when the companion limit is needed for a party.
  # INPUT: arg1 = none
  # OUTPUT: reg0 = companion_limit, reg31 = base, reg32= leadership contribution, reg33 = charisma contribution, reg34 = renown contribution
  ("game_get_party_companion_limit", [
      (assign, reg31, 0),
      (assign, reg0, reg31),
      
      (assign, reg32, 0),
      (party_get_num_companion_stacks, ":num_stacks","p_main_party"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop","p_main_party",":i_stack"),
        (troop_is_hero, ":stack_troop"),
        (store_skill_level, ":hero_leadership", "skl_leadership", ":stack_troop"),
        (val_mul, ":hero_leadership", 5), #max 50 men, this should help with player begin game
        (val_add, reg32, ":hero_leadership"),
      (try_end),
      (val_add, reg0, reg32),
      
      (store_attribute_level, reg33, "trp_player", ca_charisma), #max 20 men with new system
      (val_add, reg0, reg33),
      
      (troop_get_slot, reg34, "trp_player", slot_troop_renown),
      (val_div, reg34, 9), #Renown was important in Brytenwalda, It is important in VC but no the key, propierties are important too
      (val_add, reg0, reg34),
      
      (assign, ":fyrd", 0),
      (assign, reg35, 0),
      (try_for_range, ":cur_center", walled_centers_begin, walled_centers_end), #kingdom
        (party_slot_eq, ":cur_center", slot_town_lord, "trp_player"),
        (try_begin), #towns
          (party_slot_eq, ":cur_center", slot_party_type, spt_town),
          (val_add, ":fyrd", 80),
        (else_try), #castles forts
          (val_add, ":fyrd", 40),
        (try_end),
      (try_end),
      (val_add, reg35, ":fyrd"),
      (val_add, reg0, ":fyrd"),
      
      # phaiak begin (VC-786)
      (try_begin),
        (party_slot_eq, "p_main_party", slot_party_on_water, 1), 		# on water
        (assign, "$temp", reg0), #we need a backup for morale calcuations (VC-3389)
        (neg|party_slot_eq, "p_main_party", slot_party_1_ship_type, 0),	# with own ship
        (try_begin),
          (call_script, "script_cf_crew_fit_in_ships", "p_main_party"),
        (try_end),
        (assign, reg31, 0),
        (assign, reg32, 0),
        (assign, reg33, 0),
        (assign, reg34, 0),
        (assign, reg35, 0),
        (val_min, reg0, reg2), #reg2 = max_crew_of_all_ships
      (try_end),
      # phaiak end
      
      (set_trigger_result, reg0),
  ]),
  
  #script_game_reset_player_party_name:
  # This script is called from the game engine when the player name is changed.
  # INPUT: none
  # OUTPUT: none
  ("game_reset_player_party_name",
    [(str_store_troop_name, s5, "trp_player"),
      (party_set_name, "p_main_party", s5),
      (str_clear,s5),
  ]),
  
  #script_game_get_troop_note
  # This script is called from the game engine when the notes of a troop is needed.
  # INPUT: arg1 = troop_no, arg2 = note_index
  # OUTPUT: s0 = note
  ("game_get_troop_note",
    [
      (store_script_param_1, ":troop_no"),
      (store_script_param_2, ":note_index"),
      
      # VC-3945 remove detritus from deprecated troop notes system
      (str_clear, s54),
      (add_troop_note_from_sreg, ":troop_no", 0, s54, 0),
      
      (try_begin),
        (this_or_next|eq, "$cheat_mode", 1),
        (eq, "$cheat_mode", 3),
      (else_try),
        (add_troop_note_from_sreg, ":troop_no", 1, s54, 0), #this is debugging info
      (try_end),
      # (add_troop_note_from_sreg, ":troop_no", 2, s54, 0), still using this
      # (add_troop_note_from_sreg, ":troop_no", 3, s54, 0), clean by viewing lords in character or kingdom screens
      
      (set_trigger_result, 0),
      (str_clear, s0),
      
      (try_begin),
      (eq, ":note_index", 0),
      
      (str_store_troop_name, s54, ":troop_no"),
      (try_begin),
        (eq, ":troop_no", "trp_player"),
        (this_or_next|eq, "$player_has_homage", 1),
        (eq, "$players_kingdom", "fac_player_supporters_faction"),
        (assign, ":troop_faction", "$players_kingdom"),
      (else_try),
        (store_troop_faction, ":troop_faction", ":troop_no"),
      (try_end),
      
      (try_begin),
        (is_between, ":troop_faction", kingdoms_begin, kingdoms_end),
        (faction_get_slot, ":faction_leader", ":troop_faction", slot_faction_leader),
      (else_try),
        (assign, ":faction_leader", -1),    #VC-3942 avoid false IDs to trp_player
      (try_end),
      
      (try_begin),
        (eq, ":troop_no", "trp_player"),
        (str_store_troop_name, s4, ":troop_no"),
        (str_store_string, s8, "str_under_arms"),
        (str_store_string, s5, "str_in_your_party"),
        (str_store_string, s49, "str_s4_s8_s5"),    #VC-3975 avoid engine difficulties w empty string
      (else_try),
        (troop_get_slot, reg1, ":troop_no", slot_troop_age),
        (str_store_string, s49, "str__age_reg1"),
      (try_end),
      
      #Family notes
      (try_begin),
        (this_or_next|is_between, ":troop_no", companions_begin, kingdom_ladies_end),
        (eq, ":troop_no", "trp_player"),
        (neg|is_between, ":troop_no", pretenders_begin, pretenders_end),
        (assign, ":num_relations", 0),
        
        (try_begin),
          (call_script, "script_troop_get_family_relation_to_troop", "trp_player", ":troop_no"),
          (gt, reg0, 0),
          (val_add, ":num_relations", 1),
        (try_end),
        (try_for_range, ":aristocrat", companions_begin, kingdom_ladies_end),
          (call_script, "script_troop_get_family_relation_to_troop", ":aristocrat", ":troop_no"),
          (gt, reg0, 0),
          (val_add, ":num_relations", 1),
        (try_end),
        (try_begin),
          (gt, ":num_relations", 0),
          (str_store_string, s12, "str__family_"),
          (str_store_string, s49, "@{s49}{s12}"),
          (try_begin),
            (call_script, "script_troop_get_family_relation_to_troop", "trp_player", ":troop_no"),
            (gt, reg0, 0),
            (str_store_troop_name_link, s12, "trp_player"),
            (val_sub, ":num_relations", 1),
            (try_begin),
              (eq, ":num_relations", 0),
              (str_store_string, s49, "str_s49_s12_s11_end"),
            (else_try),
              (str_store_string, s49, "str_s49_s12_s11"),
            (try_end),
          (try_end),
          (try_for_range, ":aristocrat", companions_begin, kingdom_ladies_end),
            (call_script, "script_troop_get_family_relation_to_troop", ":aristocrat", ":troop_no"),
            (gt, reg0, 0),
            (try_begin),
              (neg|is_between, ":aristocrat", kingdom_ladies_begin, kingdom_ladies_end),
              (eq, "$cheat_mode", 1),
              (str_store_troop_name_link, s12, ":aristocrat"),
              (call_script, "script_troop_get_relation_with_troop", ":aristocrat", ":troop_no"),
              (str_store_string, s49, "str_s49_s12_s11_rel_reg0"),
            (else_try),
              (str_store_troop_name_link, s12, ":aristocrat"),
              (val_sub, ":num_relations", 1),
              (try_begin),
                (eq, ":num_relations", 0),
                (str_store_string, s49, "str_s49_s12_s11_end"),
              (else_try),
                (str_store_string, s49, "str_s49_s12_s11"),
              (try_end),
            (try_end),
          (try_end),
        (try_end),
      (try_end),
      
      (troop_get_slot, ":prison", ":troop_no", slot_troop_prisoner_of_party),
      
      (try_begin),
        (is_between, ":troop_no", companions_begin, companions_end),
        (neg|troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
        (set_trigger_result, 1),
        (assign, ":companion", ":troop_no"),
        (str_store_troop_name, s4, ":companion"),
        (try_begin),
          (troop_get_slot, ":days_left", ":companion", slot_troop_days_on_mission),
          
          (this_or_next|main_party_has_troop, ":companion"),
          (this_or_next|troop_slot_ge, ":companion", slot_troop_current_mission, 1),
          (eq, "$g_player_minister", ":companion"),
          
          (try_begin),
            (troop_slot_eq, ":companion", slot_troop_current_mission, npc_mission_kingsupport),
            (str_store_string, s8, "str_gathering_support"),
            (try_begin),
              (eq, ":days_left", 1),
              (str_store_string, s5, "str_expected_back_imminently"),
            (else_try),
              (assign, reg3, ":days_left"),
              (str_store_string, s5, "str_expected_back_in_approximately_reg3_days"),
            (try_end),
          (else_try),
            (troop_slot_eq, ":companion", slot_troop_current_mission, npc_mission_gather_intel),
            (troop_get_slot, ":town_with_contacts", ":companion", slot_troop_town_with_contacts),
            (str_store_party_name, s11, ":town_with_contacts"),
            
            (str_store_string, s8, "str_gathering_intelligence"),
            (try_begin),
              (eq, ":days_left", 1),
              (str_store_string, s5, "str_expected_back_imminently"),
            (else_try),
              (assign, reg3, ":days_left"),
              (str_store_string, s5, "str_expected_back_in_approximately_reg3_days"),
            (try_end),
          (else_try),
            (troop_slot_eq, ":companion", slot_troop_current_mission, npc_mission_improve_relations),
            (troop_get_slot, ":mission_object", ":companion", slot_troop_mission_object),
            (str_store_party_name_link, s44, ":mission_object"),
            (try_begin),
              (neg|troop_slot_ge, ":companion", slot_troop_prisoner_of_party, 0),
              (str_store_string, s8, "@Trying to improve your relations with {s44}"),
              (try_begin),
                (eq, ":days_left", 1),
                (str_store_string, s5, "str_expected_back_imminently"),
              (else_try),
                (assign, reg3, ":days_left"),
                (str_store_string, s5, "str_expected_back_in_approximately_reg3_days"),
              (try_end),
            (else_try),
              (str_store_string, s8, "@Is imprisoned in the dungeon of {s44}"),
            (try_end),
          (else_try),
            (troop_slot_ge, ":companion", slot_troop_current_mission, npc_mission_peace_request),
            (neg|troop_slot_ge, ":companion", slot_troop_current_mission, 8),
            
            (troop_get_slot, ":faction", ":companion", slot_troop_mission_object),
            (str_store_faction_name_link, s9, ":faction"),
            (str_store_string, s8, "str_diplomatic_embassy_to_s9"),
            (try_begin),
              (eq, ":days_left", 1),
              (str_store_string, s5, "str_expected_back_imminently"),
            (else_try),
              (assign, reg3, ":days_left"),
              (str_store_string, s5, "str_expected_back_in_approximately_reg3_days"),
            (try_end),
          (else_try),
            (eq, ":companion", "$g_player_minister"),
            (str_store_string, s8, "str_serving_as_minister"),
            (str_store_party_name_link, s9, "$g_player_court"),
            (is_between, "$g_player_court", centers_begin, centers_end),
            (str_store_string, s5, "str_in_your_court_at_s9"),
          (else_try),
            (eq, ":companion", "$g_player_minister"),
            (str_store_string, s8, "str_serving_as_minister"),
            (str_store_string, s5, "str_awaiting_the_capture_of_a_fortress_which_can_serve_as_your_court"),
          (else_try),
            (main_party_has_troop, ":companion"),
            (str_store_string, s8, "str_under_arms"),
            (str_store_string, s5, "str_in_your_party"),
          (try_end),
          
          (str_store_string, s0, "str_s4_s8_s5"),
          
        (else_try),
          (str_store_string, s0, "str_whereabouts_unknown"),
        (try_end),
        
        (str_store_string, s0, "@{!}{s0}^{s49}"),   #add bio info as companions may have become heroes
        
      (else_try),
        (is_between, ":troop_no", pretenders_begin, pretenders_end),
        (neg|troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero), #case successful rebellion covered by normal lords case next
        (neq, ":troop_no", "$supported_pretender"),
        (set_trigger_result, 1),
        
        (try_begin),
          (this_or_next|eq, ":troop_faction", "fac_neutral"),    #VC-3942 recognize mechanism for pretender exile (see module_dialogs)
          (troop_slot_eq, ":troop_no", slot_troop_cur_center, 0),    #VC-3942 recognize mechanism for removing pretenders from game (see mnu_start_game)
          (str_store_string, s0, "str_s54_has_left_the_realm"),
          
        (else_try),
          (troop_get_slot, ":orig_faction", ":troop_no", slot_troop_original_faction),
          (faction_slot_eq, ":orig_faction", slot_faction_state, sfs_active),
          (faction_slot_eq, ":orig_faction", slot_faction_has_rebellion_chance, 1),
          (try_begin),
            (str_store_faction_name_link, s56, ":orig_faction"),
            (str_store_string, s0, "@{s54} is a claimant to the throne of the {s56}.", 0),
          (try_end),
          
        (else_try),
          (str_store_string, s0, "str_whereabouts_unknown"),
        (try_end),
        
      #VC-3942 lord/lady has left the realm
      (else_try),
        (eq, ":troop_faction", "fac_outlaws"),
        (str_store_string, s0, "str_s54_has_left_the_realm"),
        (str_store_string, s0, "@{!}{s0}^{s49}"),
        (set_trigger_result, 1),
          
      #VC-3942 leaders of defeated factions
      (else_try),
        (this_or_next|eq, ":troop_faction", "fac_commoners"),   #pre-v 2.063 (see module_simple_triggers)
        (eq, ":troop_faction", "fac_adventurers"),
        (str_store_string_reg, s0, s49),
        (set_trigger_result, 1),
          
      #VC-3942 lords in prison
      (else_try),
        (ge, ":prison", 0),
        (store_faction_of_party, ":prison_faction", ":prison"),
        (faction_get_slot, ":prison_faction_leader", ":prison_faction", slot_faction_leader),
        
        (try_begin),
          (is_between, ":prison_faction", kingdoms_begin, kingdoms_end),
          (this_or_next|eq, ":faction_leader", "trp_player"),
          (eq, ":prison_faction_leader", "trp_player"),
          
          (str_store_party_name_link, s44, ":prison"),
          (str_store_string, s0, "@Is imprisoned in the dungeon of {s44}"),
          
        (else_try),
          (str_store_string, s0, "str_whereabouts_unknown"),
        (try_end),
        
        (str_store_string, s0, "@{!}{s0}^{s49}"),
        (set_trigger_result, 1),
          
      #VC-3942 lords defecting to player
      (else_try),
        (eq, ":faction_leader", "trp_player"),
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_inactive),
        (troop_slot_eq, ":troop_no", slot_troop_change_to_faction, 0),  #hasn't already been refused by player (see lord recruit dialog)
        (neq, ":troop_no", "$g_player_minister"),
        # (neg|is_between, ":troop_no", kings_begin, kings_end),  #these barred from joining player (see module_simple_triggers)
        (neg|is_between, ":troop_no", kingdom_ladies_begin, kingdom_ladies_end),
        (is_between, "$g_player_court", centers_begin, centers_end),
        (str_store_party_name_link, s9, "$g_player_court"),
        (str_store_string, s0, "str_in_your_court_at_s9"),
        (str_store_string, s0, "@{!}{s0}^{s49}"),
        (set_trigger_result, 1),
          
      (else_try),
          (ge, ":faction_leader", 0),
          (neg|troop_slot_eq, ":troop_no", slot_troop_occupation, slto_inactive),   #VC-3909 avoid reporting that heros applying for vassalship are already with player
          (str_store_troop_name_link, s55, ":faction_leader"),
          (str_store_faction_name_link, s56, ":troop_faction"),
          (assign, ":troop_is_player_faction", 0),
          (assign, ":troop_is_faction_leader", 0),
          (try_begin),
            (eq, ":troop_faction", "fac_player_faction"),
            (assign, ":troop_is_player_faction", 1),
          (else_try),
            (eq, ":faction_leader", ":troop_no"),
            (assign, ":troop_is_faction_leader", 1),
          (try_end),
          (assign, ":num_centers", 0),
          (str_store_string, s58, "@nowhere"),
          (try_for_range_backwards, ":cur_center", centers_begin, centers_end),
            (party_slot_eq, ":cur_center", slot_town_lord, ":troop_no"),
            (try_begin),
              (eq, ":num_centers", 0),
              (str_store_party_name_link, s58, ":cur_center"),
            (else_try),
              (eq, ":num_centers", 1),
              (str_store_party_name_link, s57, ":cur_center"),
              (str_store_string, s58, "@{s57} and {s58}"),
            (else_try),
              (str_store_party_name_link, s57, ":cur_center"),
              (str_store_string, s58, "@{!}{s57}, {s58}"),
            (try_end),
            (val_add, ":num_centers", 1),
          (try_end),
          (troop_get_type, reg3, ":troop_no"),
          (val_mod, reg3, 2),
          (troop_get_slot, reg5, ":troop_no", slot_troop_renown),
          (troop_get_slot, reg15, ":troop_no", slot_troop_controversy),
          
          (str_clear, s59),
          (try_begin),
            (call_script, "script_troop_get_player_relation", ":troop_no"),
            (assign, ":relation", reg0),
            (store_add, ":normalized_relation", ":relation", 100),
            (val_add, ":normalized_relation", 5),
            (store_div, ":str_offset", ":normalized_relation", 10),
            (val_clamp, ":str_offset", 0, 20),
            (store_add, ":str_id", "str_relation_mnus_100_ns",  ":str_offset"),
            (neq, ":str_id", "str_relation_plus_0_ns"),
            (str_store_string, s60, "@{reg3?She:He}"),
            (str_store_string, s59, ":str_id"),
            (str_store_string, s59, "@{!}^{s59}"),
          (try_end),
          (str_clear, s29),
          (try_begin),
            (troop_slot_eq, ":troop_no", slot_troop_religion, 1),
            (str_store_string, s29, "@Christian"),
          (else_try),
            (str_store_string, s29, "@Pagan"),
          (try_end),
          #lord recruitment changes begin
          #This sends a bunch of political information to s47.
          
          #refresh registers
          (assign, reg9, ":num_centers"),
          (troop_get_type, reg3, ":troop_no"),
          (val_mod, reg3, 2),
          (troop_get_slot, reg5, ":troop_no", slot_troop_renown),
          (assign, reg4, ":troop_is_faction_leader"),
          (assign, reg6, ":troop_is_player_faction"),
          
          (troop_get_slot, reg17, ":troop_no", slot_troop_wealth), #DEBUGS
          (str_store_string, s0, "str_lord_info_string", 0),
          
          #lord recruitment changes end
          (add_troop_note_tableau_mesh, ":troop_no", "tableau_troop_note_mesh"),
          (set_trigger_result, 1),
          
      #VC-3975 assume that player can figure out where their character is
      (else_try),
        (eq, ":troop_no", "trp_player"),
        (str_store_string_reg, s0, s49),
        (set_trigger_result, 1),
        
      (else_try),
        (str_store_string, s0, "str_whereabouts_unknown"),
        (str_store_string, s0, "@{!}{s0}^{s49}"),
        (set_trigger_result, 1),
      (try_end),
      (try_end),    #note_index 0
  ]),
  
  #script_game_get_center_note
  # This script is called from the game engine when the notes of a center is needed.
  # INPUT: arg1 = center_no, arg2 = note_index
  # OUTPUT: s0 = note
  ("game_get_center_note",
    [
      (store_script_param_1, ":center_no"),
      (store_script_param_2, ":note_index"),
      
      (set_trigger_result, 0),
      (try_begin),
        (eq, ":note_index", 0),
        (party_get_slot, ":lord_troop", ":center_no", slot_town_lord),
        (try_begin),
          (ge, ":lord_troop", 0),
          (store_troop_faction, ":lord_faction", ":lord_troop"),
          (str_store_troop_name_link, s1, ":lord_troop"),
          (try_begin),
            (eq, ":lord_troop", "trp_player"),
            (gt, "$players_kingdom", 0),
            (str_store_faction_name_link, s2, "$players_kingdom"),
          (else_try),
            (str_store_faction_name_link, s2, ":lord_faction"),
          (try_end),
          (str_store_party_name, s50, ":center_no"),
          (try_begin),
            (party_slot_eq, ":center_no", slot_party_type, spt_town),
            (str_store_string, s51, "@The town of {s50}"),
          (else_try),
            (party_slot_eq, ":center_no", slot_party_type, spt_village),
            (party_get_slot, ":bound_center", ":center_no", slot_village_bound_center),
            (str_store_party_name_link, s52, ":bound_center"),
            (str_store_string, s51, "@The village of {s50} near {s52}"),
          (else_try),
            (str_store_string, s51, "@{!}{s50}"),
          (try_end),
          (str_store_string, s2, "@{s51} belongs to {s1} of the {s2}.^"),
        (else_try),
          (str_clear, s2),
        (try_end),
        (try_begin),
          (is_between, ":center_no", villages_begin, villages_end),
        (else_try),
          (assign, ":num_villages", 0),
          (try_for_range_backwards, ":village_no", villages_begin, villages_end),
            (party_slot_eq, ":village_no", slot_village_bound_center, ":center_no"),
            (try_begin),
              (eq, ":num_villages", 0),
              (str_store_party_name_link, s8, ":village_no"),
            (else_try),
              (eq, ":num_villages", 1),
              (str_store_party_name_link, s7, ":village_no"),
              (str_store_string, s8, "@{s7} and {s8}"),
            (else_try),
              (str_store_party_name_link, s7, ":village_no"),
              (str_store_string, s8, "@{!}{s7}, {s8}"),
            (try_end),
            (val_add, ":num_villages", 1),
          (try_end),
          (try_begin),
            (eq, ":num_villages", 0),
            (str_store_string, s2, "@{s2}It has no villages.^"),
          (else_try),
            (store_sub, reg0, ":num_villages", 1),
            (str_store_string, s2, "@{s2}{reg0?Its villages are:Its village is} {s8}.^"),
          (try_end),
        (try_end),
        (call_script, "script_get_prosperity_text_to_s50", ":center_no"),
        #(party_get_slot, reg7, ":center_no", slot_town_prosperity),
        (str_store_string, s0, "@{s2}Its prosperity is: {s50}.", 0),
        
        (set_trigger_result, 1),
      (try_end),
  ]),
  
  #script_game_get_faction_note
  # This script is called from the game engine when the notes of a faction is needed.
  # INPUT: arg1 = faction_no, arg2 = note_index
  # OUTPUT: s0 = note
  ("game_get_faction_note",
    [
      (store_script_param_1, ":faction_no"),
      (store_script_param_2, ":note_index"),
      (set_trigger_result, 0),
      
      (try_begin),
        (is_between, ":faction_no", kingdoms_begin, kingdoms_end),
        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        
        (try_begin),
          (eq, ":note_index", 0),
          
          (call_script, "script_game_get_faction_note_faction_ruler", ":faction_no"),
          (assign, ":faction_leader", reg0),
          
          (str_clear, s1),
          (str_store_string_reg, s1, s6),
          (try_begin),
            (faction_slot_eq, ":faction_no", slot_faction_religion, cb3_pagan),
            (str_store_string, s6, "str_com_pagan"),
          (else_try),
            (str_store_string, s6, "str_com_catholic"),
          (try_end),
          
          (call_script, "script_game_get_faction_note_centers", ":faction_no"),
          (call_script, "script_game_get_faction_note_vassals", ":faction_no", ":faction_leader"),
          
          (assign, ":show_link", 1),
          (call_script, "script_game_faction_note_diplomatic_relations", ":faction_no", ":show_link"),
          
          (str_clear, s0),
          (str_store_string, s0, "str_the_s5_is_ruled_by_s6_it_occupies_s8_its_vassals_are_s10__s21"),
          (set_trigger_result, 1),
        (try_end),
        
      (else_try),
        (call_script, "script_cf_game_get_faction_note_for_defeated_factions", ":note_index", ":faction_no"),
        
      (else_try),
        (call_script, "script_game_get_faction_note_invalid_faction", ":note_index"),
        
      (try_end),
  ]),
  
  # "script_game_get_faction_note_faction_ruler"
  # Input: faction_no
  # Output: s5, s6, s8, reg0:faction_leader
  ("game_get_faction_note_faction_ruler",
    [
      (store_script_param_1, ":faction_no"),
      
      (faction_get_slot, ":faction_leader", ":faction_no", slot_faction_leader),
      (str_store_faction_name, s5, ":faction_no"),
      (str_store_troop_name_link, s6, ":faction_leader"),
      (str_store_string, s8, "@nowhere"),
      (assign, reg0, ":faction_leader"),
      
      (try_begin),
        (eq, debug_diplomatic_relations, 1),
        (display_log_message, "@{!}DEBUG: Faction: {s5}"),
      (try_end),
  ]),
  
  # "script_game_get_faction_note_centers"
  # Input: faction_no, s8
  # Output: s8
  ("game_get_faction_note_centers",
    [
      (store_script_param_1, ":faction_no"),
      
      (assign, ":num_centers", 0),
      (try_for_range_backwards, ":cur_center", centers_begin, centers_end),
        (store_faction_of_party, ":center_faction", ":cur_center"),
        (eq, ":center_faction", ":faction_no"),
        
        (try_begin),
          (eq, ":num_centers", 0),
          (str_store_party_name_link, s8, ":cur_center"),
        (else_try),
          (eq, ":num_centers", 1),
          (str_store_party_name_link, s7, ":cur_center"),
          (str_store_string, s8, "@{s7} and {s8}"),
        (else_try),
          (str_store_party_name_link, s7, ":cur_center"),
          (str_store_string, s8, "@{!}{s7}, {s8}"),
        (try_end),
        
        (val_add, ":num_centers", 1),
      (try_end),
      
      (try_begin),
        (eq, debug_diplomatic_relations, 1),
        (assign, reg1, ":num_centers"),
        (display_log_message, "@{!}DEBUG: Num_centers: {reg1}"),
      (try_end),
  ]),
  
  # "script_game_get_faction_note_vassals"
  # Input: faction_no
  # Output: s10
  ("game_get_faction_note_vassals",
    [
      (store_script_param_1, ":faction_no"),
      (store_script_param_2, ":faction_leader"),
      (assign, ":num_members", 0),
      (str_store_string, s10, "str_noone"),
      
      (try_for_range_backwards, ":loop_var", "trp_kingdom_heroes_including_player_begin", active_npcs_end),
        (assign, ":cur_troop", ":loop_var"),
        
        (try_begin),
          (eq, ":loop_var", "trp_kingdom_heroes_including_player_begin"),
          (assign, ":cur_troop", "trp_player"),
          (assign, ":troop_faction", "$players_kingdom"),
        (else_try),
          (store_troop_faction, ":troop_faction", ":cur_troop"),
        (try_end),
        
        (eq, ":troop_faction", ":faction_no"),
        (neq, ":cur_troop", ":faction_leader"),
        
        (this_or_next|eq, ":cur_troop", "trp_player"),  #VC-3834
        (troop_slot_eq, ":cur_troop", slot_troop_occupation, slto_kingdom_hero),
        
        (try_begin),
          (eq, ":num_members", 0),
          (str_store_troop_name_link, s10, ":cur_troop"),
        (else_try),
          (eq, ":num_members", 1),
          (str_store_troop_name_link, s9, ":cur_troop"),
          (str_store_string, s10, "@{s9} and {s10}"),
        (else_try),
          (str_store_troop_name_link, s9, ":cur_troop"),
          (str_store_string, s10, "@{!}{s9}, {s10}"),
        (try_end),
        
        (val_add, ":num_members", 1),
      (try_end),
      
      (try_begin),
        (eq, debug_diplomatic_relations, 1),
        (assign, reg1, ":num_members"),
        (display_log_message, "@{!}DEBUG: Num_members: {reg1}"),
      (try_end),
  ]),
  
  # "script_game_faction_note_diplomatic_relations"
  # Input: faction_no
  # Output: s21
  ("game_faction_note_diplomatic_relations",
    [
      (store_script_param_1, ":faction_no"),
      (store_script_param_2, ":show_link"),
      
      (str_clear, s11), # ally
      (str_clear, s12), # war
      (str_clear, s13), # casus belli
      (str_clear, s21),
      (str_clear, s1),
      
      (assign, ":num_wars", 0),
      (assign, ":num_alliances", 0),
      (assign, ":num_casus_belli", 0),
      
      (try_for_range, ":cur_faction", kingdoms_begin, kingdoms_end),
        
        (faction_slot_eq, ":cur_faction", slot_faction_state, sfs_active),
        (neq, ":cur_faction", ":faction_no"),
        (str_clear, s14),
        
        (try_begin),
          (eq, ":show_link", 1),
          (str_store_faction_name_link, s14, ":cur_faction"),
          
        (else_try),
          (str_store_faction_name, s14, ":cur_faction"),
        (try_end),
        
        (call_script, "script_diplomacy_faction_get_diplomatic_status_with_faction", ":faction_no", ":cur_faction"),
        (assign, ":diplomatic_status", reg0),
        (assign, ":duration_of_status", reg1),
        
        #result:
        # -1 faction_1 has a casus belli against faction_2.
        # +1 faction_1 has a truce with faction_2,
        # -2 the two factions are at war
        # +2 allianza
        (try_begin),
          (eq, reg0, -2), # war
          (val_add, ":num_wars", 1),
          (try_begin),
            (eq, ":num_wars", 1),
            (str_store_string, s12, "@{s12}{s14}"),
          (else_try),
            (str_store_string, s12, "@{s12}, {s14}"),
          (try_end),
          
        (else_try),
          (eq, reg0, +2), # ally
          (val_add, ":num_alliances", 1),
          
          (try_begin),
            (eq, ":num_alliances", 1),
            (str_store_string, s11, "@{s11}{s14}"),
          (else_try),
            (str_store_string, s11, "@{s11}, {s14}"),
          (try_end),
          
        (else_try),
          (eq, reg0, -1), # casus belli
          (val_add, ":num_casus_belli", 1),
          
          (try_begin),
            (eq, ":num_casus_belli", 1),
            (str_store_string, s13, "@{s13}{s14}"),
          (else_try),
            (str_store_string, s13, "@{s13}, {s14}"),
          (try_end),
          
        (try_end),
        
        (call_script, "script_diplomacy_faction_get_diplomatic_status_with_faction", ":cur_faction", ":faction_no"),
        (assign, ":reverse_diplomatic_status", reg0),
        #(assign, ":reverse_diplomatic_duration", reg1),
        
        (try_begin),
          (eq, ":diplomatic_status", -2),
          (str_store_string, s21, "str_s21__the_s5_is_at_war_with_the_s14"),
          (store_add, ":slot_war_damage_inflicted", ":cur_faction", slot_faction_war_damage_inflicted_on_factions_begin),
          (val_sub, ":slot_war_damage_inflicted", kingdoms_begin),
          (faction_get_slot, ":war_damage_inflicted", ":faction_no", ":slot_war_damage_inflicted"),
          (store_mul, ":war_damage_inflicted_x_2", ":war_damage_inflicted", 2),
          
          (store_add, ":slot_war_damage_suffered", ":faction_no", slot_faction_war_damage_inflicted_on_factions_begin),
          (val_sub, ":slot_war_damage_suffered", kingdoms_begin),
          (faction_get_slot, ":war_damage_suffered", ":cur_faction", ":slot_war_damage_suffered"),
          (store_mul, ":war_damage_suffered_x_2", ":war_damage_suffered", 2),
          
          
          (assign, ":war_cause", 0),
          (assign, ":attacker", 0),
          
          (try_for_range, ":log_entry", 0, "$num_log_entries"),
            (troop_get_slot, ":type", "trp_log_array_entry_type", ":log_entry"),
            (is_between, ":type", logent_faction_declares_war_out_of_personal_enmity, logent_war_declaration_types_end),
            (troop_get_slot, ":actor", "trp_log_array_actor", ":log_entry"),
            (troop_get_slot, ":object", "trp_log_array_faction_object", ":log_entry"),
            
            (try_begin),
              (eq, ":actor", ":cur_faction"),
              (eq, ":object", ":faction_no"),
              (assign, ":war_cause", ":type"),
              (assign, ":attacker", ":actor"),
            (else_try),
              (eq, ":actor", ":faction_no"),
              (eq, ":object", ":cur_faction"),
              (assign, ":war_cause", ":type"),
              (assign, ":attacker", ":actor"),
            (try_end),
          (try_end),
          
          #bug fix! backing up s8 to somewhere else
          (str_store_string, s25, s8),
          
          (try_begin),
            (gt, ":war_cause", 0),
            (str_store_faction_name, s8, ":attacker"),
            
            (try_begin),
              (eq, ":war_cause", logent_faction_declares_war_out_of_personal_enmity),
              (str_store_string, s21, "str_s21_the_s8_declared_war_out_of_personal_enmity"),
            (else_try),
              (eq, ":war_cause", logent_faction_declares_war_to_respond_to_provocation),
              (str_store_string, s21, "str_s21_the_s8_declared_war_in_response_to_border_provocations"),
            (else_try),
              (eq, ":war_cause", logent_faction_declares_war_to_curb_power),
              (str_store_string, s21, "str_s21_the_s8_declared_war_to_curb_the_other_realms_power"),
            (else_try),
              (eq, ":war_cause", logent_faction_declares_war_to_regain_territory),
              (str_store_string, s21, "str_s21_the_s8_declared_war_to_regain_lost_territory"),
              ##moto diplomacy
            (else_try),
              (eq, ":war_cause", logent_faction_declares_war_to_fulfil_alliance),
              (str_store_string, s21, "@{s21}. The {s8} declared war to fulfil an alliance."),
              ## end
            (else_try),
              (eq, ":war_cause", logent_player_faction_declares_war),
              (neq, ":attacker", "fac_player_supporters_faction"),
              (str_store_string, s21, "str_s21_the_s8_declared_war_as_part_of_a_bid_to_conquer_all_calradia"),
            (try_end),
          (try_end),
          
          #bug fix! restoring the back up to s8
          (str_store_string, s8, s25),
          
          (try_begin),
            (gt, ":war_damage_inflicted", ":war_damage_suffered_x_2"),
            (str_store_string, s21, "str_s21_the_s5_has_had_the_upper_hand_in_the_fighting"),
          (else_try),
            (gt, ":war_damage_suffered", ":war_damage_inflicted_x_2"),
            (str_store_string, s21, "str_s21_the_s5_has_gotten_the_worst_of_the_fighting"),
          (else_try),
            (gt, ":war_damage_inflicted", 100),
            (gt, ":war_damage_inflicted", 100),
            (str_store_string, s21, "str_s21_the_fighting_has_gone_on_for_some_time_and_the_war_may_end_soon_with_a_truce"),
          (else_try),
            (str_store_string, s21, "str_s21_the_fighting_has_begun_relatively_recently_and_the_war_may_continue_for_some_time"),
          (try_end),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (assign, reg4, ":war_damage_inflicted"),
            (assign, reg5, ":war_damage_suffered"),
            (str_store_string, s21, "str_s21_reg4reg5"),
          (try_end),
        (else_try),
          (eq, ":diplomatic_status", 1),
          (str_clear, s18),
          (try_begin),
            (neq, ":reverse_diplomatic_status", 1),
            (str_store_string, s18, "str__however_the_truce_is_no_longer_binding_on_the_s14"),
          (try_end),
          
          (assign, reg1, ":duration_of_status"),
          
          (try_begin), #chief
            (is_between, ":duration_of_status", 1, truce_time + 1), #chief
            (str_store_string, s21, "str_s21__the_s5_is_bound_by_truce_not_to_attack_the_s14s18_the_truce_will_expire_in_reg1_days"),
          (else_try),
            (is_between, ":duration_of_status", truce_time + 1, alliance_time + 1),
            (str_store_string, s21, "str_alliance_durantion_days_expire"),
            
            #VC-2378 summary of alliances -> special case for ally in truce period
            (val_add, ":num_alliances", 1),
            
            (try_begin),
              (eq, ":num_alliances", 1),
              (str_store_string, s11, "@{s11}{s14}"),
            (else_try),
              (str_store_string, s11, "@{s11}, {s14}"),
            (try_end),
            
          (try_end),
          
        (else_try),
          (eq, ":diplomatic_status", -1),
          #VC-2378 cleaning up report: won't show casus belli
          #(str_store_string, s21, "str_s21__the_s5_has_recently_suffered_provocation_by_subjects_of_the_s14_and_there_is_a_risk_of_war"),
        (else_try),
          (eq, ":diplomatic_status", 0),
          #VC-2378 cleaning up report: won't show peace
          #(str_store_string, s21, "str_s21__the_s5_has_no_outstanding_issues_with_the_s14"),
        (try_end),
        
        (try_begin),
          (eq, ":reverse_diplomatic_status", -1),
          (str_store_string, s21, "str_s21_the_s14_was_recently_provoked_by_subjects_of_the_s5_and_there_is_a_risk_of_war_"),
        (try_end),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (call_script, "script_npc_decision_checklist_peace_or_war", ":faction_no", ":cur_faction", -1),
          (str_store_string, s21, "@{!}DEBUG : {s21}.^CHEAT MODE ASSESSMENT: {s14}^"),
        (try_end),
        
      (try_end),
      
      (call_script,
        "script_game_get_faction_note_diplomatic_relations_format_s21",
        ":num_wars",
        ":num_alliances",
        ":num_casus_belli",
      ":show_link"),
  ]),
  
  # "script_game_get_faction_note_diplomatic_relations_format_s21"
  # Description
  # Input: s11, s12, s13, s21, num_alliances, num_wars, num_casus_belli
  # Output: none
  ("game_get_faction_note_diplomatic_relations_format_s21",
    [
      (store_script_param, ":num_wars", 1),
      (store_script_param, ":num_alliances", 2),
      (store_script_param, ":num_casus_belli", 3),
      (store_script_param, ":show_link", 4),
      
      (assign, reg1, ":num_wars"),
      (try_begin),
        (eq, reg1, 0),
        (str_store_string, s12, "@No wars!"),
        
      (else_try),
        (str_store_string, s12, "@At war with ({reg1}): {s12}"),
      (try_end),
      
      
      (assign, reg2, ":num_alliances"),
      (try_begin),
        (eq, reg2, 0),
        (str_store_string, s11, "@No allies!"),
        
      (else_try),
        (str_store_string, s11, "@Alliances ({reg2})  : {s11}"),
      (try_end),
      
      (assign, reg3, ":num_casus_belli"),
      (try_begin),
        (eq, reg3, 0),
        (str_store_string, s13, "@No casus belli!"),
      (else_try),
        (str_store_string, s13, "@Casus belli   ({reg3}): {s13}"),
      (try_end),
      
      (str_store_string, s21, "@{s12}^{s11}^{s13}^^{s21}"),
      
      (str_store_string, s1, "str_foreign_relations__"),
      
      (try_begin),
        (eq, ":show_link", 1),
        (str_store_string, s21, "@^-------------------------------------------------------------------------------^{s1}^{s21}"),
      (try_end),
      
      (try_begin),
        (eq, debug_diplomatic_relations, 1),
        (display_log_message, "@{!}DEBUG: Wars: {reg1}, Alliance: {reg2}, Casus: {reg3}^^"),
        (display_log_message, s21),
      (try_end),
  ]),
  
  # "script_cf_game_get_faction_note_for_defeated_factions"
  # Input: note_index
  # Output: s0
  ("cf_game_get_faction_note_for_defeated_factions",
    [
      (store_script_param_1, ":note_index"),
      (store_script_param_2, ":faction_no"),
      
      (is_between, ":faction_no", kingdoms_begin, kingdoms_end),
      (faction_slot_eq, ":faction_no", slot_faction_state, sfs_defeated),
      
      (try_begin),
        (eq, ":note_index", 0),
        (str_store_faction_name, s5, ":faction_no"),
        (str_store_string, s0, "@The {s5} has been defeated!", 0),
        (set_trigger_result, 1),
        
      (else_try),
        (eq, ":note_index", 1),
        (str_clear, s0),
        (set_trigger_result, 1),
      (try_end),
      
      (try_begin),
        (eq, debug_diplomatic_relations, 1),
        (display_log_message, "@{!}DEBUG: Defeated faction: {s0}"),
      (try_end),
  ]),
  
  # "script_game_get_faction_note_invalid_faction"
  # Input: note_index
  # Output: may reset s0
  ("game_get_faction_note_invalid_faction",
    [
      (store_script_param_1, ":note_index"),
      (try_begin),
        (this_or_next|eq, ":note_index", 0),
        (eq, ":note_index", 1),
        (str_clear, s0),
        (set_trigger_result, 1),
        
        (try_begin),
          (eq, debug_diplomatic_relations, 1),
          (display_log_message, "@{!}Error: script_game_get_faction_note_invalid_faction was called"),
        (try_end),
        
      (try_end),
  ]),
  
  #script_game_get_quest_note
  # This script is called from the game engine when the notes of a quest is needed.
  # INPUT: arg1 = quest_no, arg2 = note_index
  # OUTPUT: s0 = note
  ("game_get_quest_note",
    [
      ##      (store_script_param_1, ":quest_no"),
      ##      (store_script_param_2, ":note_index"),
      (set_trigger_result, 0), # set it to 1 if this script is wanted to be used rather than static notes
  ]),
  
  #script_game_get_info_page_note
  # This script is called from the game engine when the notes of a info_page is needed.
  # INPUT: arg1 = info_page_no, arg2 = note_index
  # OUTPUT: s0 = note
  ("game_get_info_page_note",
    [
      ##      (store_script_param_1, ":info_page_no"),
      ##      (store_script_param_2, ":note_index"),
      (set_trigger_result, 0), # set it to 1 if this script is wanted to be used rather than static notes
  ]),
  
  #script_game_get_scene_name
  # This script is called from the game engine when a name for the scene is needed.
  # INPUT: arg1 = scene_no
  # OUTPUT: s0 = name
  ("game_get_scene_name",
    [
      (store_script_param, ":scene_no", 1),
      (try_begin),
        (is_between, ":scene_no", multiplayer_scenes_begin, multiplayer_scenes_end),
        (store_sub, ":string_id", ":scene_no", multiplayer_scenes_begin),
        (val_add, ":string_id", multiplayer_scene_names_begin),
        (str_store_string, s0, ":string_id"),
      (else_try),
        (is_between, ":scene_no", multiplayer_addon_scenes_begin, multiplayer_addon_scenes_end),
        (store_sub, ":string_id", ":scene_no", multiplayer_addon_scenes_begin),
        (val_add, ":string_id", multiplayer_addon_scene_names_begin),
        (str_store_string, s0, ":string_id"),
      (try_end),
  ]),
  
  #script_game_get_mission_template_name
  # This script is called from the game engine when a name for the mission template is needed.
  # INPUT: arg1 = mission_template_no
  # OUTPUT: s0 = name
  ("game_get_mission_template_name",
    [
      (store_script_param, ":mission_template_no", 1),
      (call_script, "script_multiplayer_get_mission_template_game_type", ":mission_template_no"),
      (assign, ":game_type", reg0),
      (try_begin),
        (is_between, ":game_type", 0, multiplayer_num_game_types),
        (store_add, ":string_id", ":game_type", multiplayer_game_type_names_begin),
        (str_store_string, s0, ":string_id"),
      (try_end),
  ]),
  
  #script_add_kill_death_counts
  # INPUT: arg1 = killer_agent_no, arg2 = dead_agent_no
  # OUTPUT: none
  ("add_kill_death_counts",
    [
      (store_script_param, ":killer_agent_no", 1),
      (store_script_param, ":dead_agent_no", 2),
      
      (try_begin),
        (ge, ":killer_agent_no", 0),
        (agent_get_team, ":killer_agent_team", ":killer_agent_no"),
      (else_try),
        (assign, ":killer_agent_team", -1),
      (try_end),
      
      (try_begin),
        (ge, ":dead_agent_no", 0),
        (agent_get_team, ":dead_agent_team", ":dead_agent_no"),
      (else_try),
        (assign, ":dead_agent_team", -1),
      (try_end),
      
      #adjusting kill counts of players/bots
      (try_begin),
        (try_begin),
          (ge, ":killer_agent_no", 0),
          (ge, ":dead_agent_no", 0),
          (agent_is_human, ":killer_agent_no"),
          (agent_is_human, ":dead_agent_no"),
          (neq, ":killer_agent_no", ":dead_agent_no"),
          
          (this_or_next|neq, ":killer_agent_team", ":dead_agent_team"),
          (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_deathmatch),
          (eq, "$g_multiplayer_game_type", multiplayer_game_type_duel),
          
          (agent_get_player_id, ":killer_agent_player", ":killer_agent_no"),
          (try_begin),
            (agent_is_non_player, ":killer_agent_no"), #if killer agent is bot then increase bot kill counts of killer agent's team by one.
            (agent_get_team, ":killer_agent_team", ":killer_agent_no"),
            (team_get_bot_kill_count, ":killer_agent_team_bot_kill_count", ":killer_agent_team"),
            (val_add, ":killer_agent_team_bot_kill_count", 1),
            (team_set_bot_kill_count, ":killer_agent_team", ":killer_agent_team_bot_kill_count"),
          (else_try), #if killer agent is not bot then increase kill counts of killer agent's player by one.
            (player_is_active, ":killer_agent_player"),
            (player_get_kill_count, ":killer_agent_player_kill_count", ":killer_agent_player"),
            (val_add, ":killer_agent_player_kill_count", 1),
            (player_set_kill_count, ":killer_agent_player", ":killer_agent_player_kill_count"),
          (try_end),
        (try_end),
        
        (try_begin),
          (ge, ":dead_agent_no", 0),
          (agent_is_human, ":dead_agent_no"),
          (try_begin),
            (agent_is_non_player, ":dead_agent_no"), #if dead agent is bot then increase bot kill counts of dead agent's team by one.
            (agent_get_team, ":dead_agent_team", ":dead_agent_no"),
            (team_get_bot_death_count, ":dead_agent_team_bot_death_count", ":dead_agent_team"),
            (val_add, ":dead_agent_team_bot_death_count", 1),
            (team_set_bot_death_count, ":dead_agent_team", ":dead_agent_team_bot_death_count"),
          (else_try), #if dead agent is not bot then increase death counts of dead agent's player by one.
            (agent_get_player_id, ":dead_agent_player", ":dead_agent_no"),
            (player_is_active, ":dead_agent_player"),
            (player_get_death_count, ":dead_agent_player_death_count", ":dead_agent_player"),
            (val_add, ":dead_agent_player_death_count", 1),
            (player_set_death_count, ":dead_agent_player", ":dead_agent_player_death_count"),
          (try_end),
          
          (try_begin),
            (assign, ":continue", 0),
            
            (try_begin),
              (this_or_next|lt, ":killer_agent_no", 0), #if he killed himself (1a(team change) or 1b(self kill)) then decrease kill counts of killer player by one.
              (eq, ":killer_agent_no", ":dead_agent_no"),
              (assign, ":continue", 1),
            (try_end),
            
            (try_begin),
              (eq, ":killer_agent_team", ":dead_agent_team"), #if he killed a teammate and game mod is not deathmatch then decrease kill counts of killer player by one.
              (neq, "$g_multiplayer_game_type", multiplayer_game_type_deathmatch),
              (neq, "$g_multiplayer_game_type", multiplayer_game_type_duel),
              (assign, ":continue", 1),
            (try_end),
            
            (eq, ":continue", 1),
            
            (try_begin),
              (ge, ":killer_agent_no", 0),
              (assign, ":responsible_agent", ":killer_agent_no"),
            (else_try),
              (assign, ":responsible_agent", ":dead_agent_no"),
            (try_end),
            
            (try_begin),
              (ge, ":responsible_agent", 0),
              (neg|agent_is_non_player, ":responsible_agent"),
              (agent_get_player_id, ":responsible_player", ":responsible_agent"),
              (ge, ":responsible_player", 0),
              (player_get_kill_count, ":dead_agent_player_kill_count", ":responsible_player"),
              (val_add, ":dead_agent_player_kill_count", -1),
              (player_set_kill_count, ":responsible_player", ":dead_agent_player_kill_count"),
            (try_end),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  #script_warn_player_about_auto_team_balance
  # INPUT: none
  # OUTPUT: none
  ("warn_player_about_auto_team_balance",
    [
      (assign, "$g_multiplayer_message_type", multiplayer_message_type_auto_team_balance_next),
      (start_presentation, "prsnt_multiplayer_message_2"),
  ]),
  
  #script_check_team_balance
  # INPUT: none
  # OUTPUT: none
  ("check_team_balance",
    [
      (try_begin),
        (multiplayer_is_server),
        
        (assign, ":number_of_players_at_team_1", 0),
        (assign, ":number_of_players_at_team_2", 0),
        (get_max_players, ":num_players"),
        (try_for_range, ":cur_player", 0, ":num_players"),
          (player_is_active, ":cur_player"),
          (player_get_team_no, ":player_team", ":cur_player"),
          (try_begin),
            (eq, ":player_team", 0),
            (val_add, ":number_of_players_at_team_1", 1),
          (else_try),
            (eq, ":player_team", 1),
            (val_add, ":number_of_players_at_team_2", 1),
          (try_end),
        (try_end),
        
        (store_sub, ":difference_of_number_of_players", ":number_of_players_at_team_1", ":number_of_players_at_team_2"),
        (assign, ":number_of_players_will_be_moved", 0),
        (try_begin),
          (try_begin),
            (store_mul, ":checked_value", "$g_multiplayer_auto_team_balance_limit", -1),
            (le, ":difference_of_number_of_players", ":checked_value"),
            (store_div, ":number_of_players_will_be_moved", ":difference_of_number_of_players", -2),
            (assign, ":team_with_more_players", 1),
            (assign, ":team_with_less_players", 0),
          (else_try),
            (ge, ":difference_of_number_of_players", "$g_multiplayer_auto_team_balance_limit"),
            (store_div, ":number_of_players_will_be_moved", ":difference_of_number_of_players", 2),
            (assign, ":team_with_more_players", 0),
            (assign, ":team_with_less_players", 1),
          (try_end),
        (try_end),
        #team balance checks are done
        (try_begin),
          (gt, ":number_of_players_will_be_moved", 0),
          (try_begin),
            (eq, "$g_team_balance_next_round", 1), #if warning is given
            
            #auto team balance starts
            (try_for_range, ":unused", 0, ":number_of_players_will_be_moved"),
              (assign, ":max_player_join_time", 0),
              (assign, ":latest_joined_player_no", -1),
              (get_max_players, ":num_players"),
              (try_for_range, ":player_no", 0, ":num_players"),
                (player_is_active, ":player_no"),
                (player_get_team_no, ":player_team", ":player_no"),
                (eq, ":player_team", ":team_with_more_players"),
                (player_get_slot, ":player_join_time", ":player_no", slot_player_join_time),
                (try_begin),
                  (gt, ":player_join_time", ":max_player_join_time"),
                  (assign, ":max_player_join_time", ":player_join_time"),
                  (assign, ":latest_joined_player_no", ":player_no"),
                (try_end),
              (try_end),
              (try_begin),
                (ge, ":latest_joined_player_no", 0),
                (try_begin),
                  #if player is living add +1 to his kill count because he will get -1 because of team change while living.
                  (player_get_agent_id, ":latest_joined_agent_id", ":latest_joined_player_no"),
                  (ge, ":latest_joined_agent_id", 0),
                  (agent_is_alive, ":latest_joined_agent_id"),
                  
                  (player_get_kill_count, ":player_kill_count", ":latest_joined_player_no"), #adding 1 to his kill count, because he will lose 1 undeserved kill count for dying during team change
                  (val_add, ":player_kill_count", 1),
                  (player_set_kill_count, ":latest_joined_player_no", ":player_kill_count"),
                  
                  (player_get_death_count, ":player_death_count", ":latest_joined_player_no"), #subtracting 1 to his death count, because he will gain 1 undeserved death count for dying during team change
                  (val_sub, ":player_death_count", 1),
                  (player_set_death_count, ":latest_joined_player_no", ":player_death_count"),
                  
                  (player_get_score, ":player_score", ":latest_joined_player_no"), #adding 1 to his score count, because he will lose 1 undeserved score for dying during team change
                  (val_add, ":player_score", 1),
                  (player_set_score, ":latest_joined_player_no", ":player_score"),
                  
                  (try_for_range, ":player_no", 1, ":num_players"), #0 is server so starting from 1
                    (player_is_active, ":player_no"),
                    (multiplayer_send_4_int_to_player, ":player_no", multiplayer_event_set_player_score_kill_death, ":latest_joined_player_no", ":player_score", ":player_kill_count", ":player_death_count"),
                  (try_end),
                  
                  (player_get_value_of_original_items, ":old_items_value", ":latest_joined_player_no"),
                  (player_get_gold, ":player_gold", ":latest_joined_player_no"),
                  (val_add, ":player_gold", ":old_items_value"),
                  (player_set_gold, ":latest_joined_player_no", ":player_gold", multi_max_gold_that_can_be_stored),
                (end_try),
                
                (player_set_troop_id, ":latest_joined_player_no", -1),
                (player_set_team_no, ":latest_joined_player_no", ":team_with_less_players"),
                (multiplayer_send_message_to_player, ":latest_joined_player_no", multiplayer_event_force_start_team_selection),
              (try_end),
            (try_end),
            
            #for only server itself-----------------------------------------------------------------------------------------------
            (call_script, "script_show_multiplayer_message", multiplayer_message_type_auto_team_balance_done, 0), #0 is useless here
            #for only server itself-----------------------------------------------------------------------------------------------
            (get_max_players, ":num_players"),
            (try_for_range, ":player_no", 1, ":num_players"),
              (player_is_active, ":player_no"),
              (multiplayer_send_int_to_player, ":player_no", multiplayer_event_show_multiplayer_message, multiplayer_message_type_auto_team_balance_done),
            (try_end),
            (assign, "$g_team_balance_next_round", 0),
            #auto team balance done
          (else_try),
            #tutorial message (next round there will be auto team balance)
            (assign, "$g_team_balance_next_round", 1),
            
            #for only server itself-----------------------------------------------------------------------------------------------
            (call_script, "script_show_multiplayer_message", multiplayer_message_type_auto_team_balance_next, 0), #0 is useless here
            #for only server itself-----------------------------------------------------------------------------------------------
            (get_max_players, ":num_players"),
            (try_for_range, ":player_no", 1, ":num_players"),
              (player_is_active, ":player_no"),
              (multiplayer_send_int_to_player, ":player_no", multiplayer_event_show_multiplayer_message, multiplayer_message_type_auto_team_balance_next),
            (try_end),
          (try_end),
        (else_try),
          (assign, "$g_team_balance_next_round", 0),
        (try_end),
      (try_end),
  ]),
  
  #script_check_creating_ladder_dust_effect
  # INPUT: arg1 = instance_id, arg2 = remaining_time
  # OUTPUT: none
  ("check_creating_ladder_dust_effect",
    [
      (store_trigger_param_1, ":instance_id"),
      (store_trigger_param_2, ":remaining_time"),
      
      (try_begin),
        (lt, ":remaining_time", 15), #less then 0.15 seconds
        (gt, ":remaining_time", 3), #more than 0.03 seconds
        
        (scene_prop_get_slot, ":smoke_effect_done", ":instance_id", scene_prop_smoke_effect_done),
        (scene_prop_get_slot, ":opened_or_closed", ":instance_id", scene_prop_open_or_close_slot),
        
        (try_begin),
          (eq, ":smoke_effect_done", 0),
          (eq, ":opened_or_closed", 0),
          
          (prop_instance_get_position, pos0, ":instance_id"),
          
          (assign, ":smallest_dist", -1),
          (try_for_range, ":entry_point_no", multi_entry_points_for_usable_items_start, multi_entry_points_for_usable_items_end),
            (entry_point_get_position, pos1, ":entry_point_no"),
            (get_sq_distance_between_positions, ":dist", pos0, pos1),
            (this_or_next|eq, ":smallest_dist", -1),
            (lt, ":dist", ":smallest_dist"),
            (assign, ":smallest_dist", ":dist"),
            (assign, ":nearest_entry_point", ":entry_point_no"),
          (try_end),
          
          (try_begin),
            (set_fixed_point_multiplier, 100),
            
            (ge, ":smallest_dist", 0),
            (lt, ":smallest_dist", 22500), #max 15m distance
            
            (entry_point_get_position, pos1, ":nearest_entry_point"),
            (position_rotate_x, pos1, -90),
            
            (prop_instance_get_scene_prop_kind, ":scene_prop_kind", ":instance_id"),
            (try_begin),
              (eq, ":scene_prop_kind", "spr_siege_ladder_move_6m"),
              (init_position, pos2),
              (position_set_z, pos2, 300),
              (position_transform_position_to_parent, pos3, pos1, pos2),
              (particle_system_burst, "psys_ladder_dust_6m", pos3, 100),
              (particle_system_burst, "psys_ladder_straw_6m", pos3, 100),
            (else_try),
              (eq, ":scene_prop_kind", "spr_siege_ladder_move_8m"),
              (init_position, pos2),
              (position_set_z, pos2, 400),
              (position_transform_position_to_parent, pos3, pos1, pos2),
              (particle_system_burst, "psys_ladder_dust_8m", pos3, 100),
              (particle_system_burst, "psys_ladder_straw_8m", pos3, 100),
            (else_try),
              (eq, ":scene_prop_kind", "spr_siege_ladder_move_10m"),
              (init_position, pos2),
              (position_set_z, pos2, 500),
              (position_transform_position_to_parent, pos3, pos1, pos2),
              (particle_system_burst, "psys_ladder_dust_10m", pos3, 100),
              (particle_system_burst, "psys_ladder_straw_10m", pos3, 100),
            (else_try),
              (eq, ":scene_prop_kind", "spr_siege_ladder_move_12m"),
              (init_position, pos2),
              (position_set_z, pos2, 600),
              (position_transform_position_to_parent, pos3, pos1, pos2),
              (particle_system_burst, "psys_ladder_dust_12m", pos3, 100),
              (particle_system_burst, "psys_ladder_straw_12m", pos3, 100),
            (else_try),
              (eq, ":scene_prop_kind", "spr_siege_ladder_move_14m"),
              (init_position, pos2),
              (position_set_z, pos2, 700),
              (position_transform_position_to_parent, pos3, pos1, pos2),
              (particle_system_burst, "psys_ladder_dust_14m", pos3, 100),
              (particle_system_burst, "psys_ladder_straw_14m", pos3, 100),
            (try_end),
            
            (scene_prop_set_slot, ":instance_id", scene_prop_smoke_effect_done, 1),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  #script_money_management_after_agent_death
  # INPUT: arg1 = killer_agent_no, arg2 = dead_agent_no
  # OUTPUT: none
  ("money_management_after_agent_death",
    [
      (store_script_param, ":killer_agent_no", 1),
      (store_script_param, ":dead_agent_no", 2),
      
      (assign, ":dead_agent_player_id", -1),
      
      (try_begin),
        (multiplayer_is_server),
        (ge, ":killer_agent_no", 0),
        (ge, ":dead_agent_no", 0),
        (agent_is_human, ":dead_agent_no"), #if dead agent is not horse
        (agent_is_human, ":killer_agent_no"), #if killer agent is not horse
        (agent_get_team, ":killer_agent_team", ":killer_agent_no"),
        (agent_get_team, ":dead_agent_team", ":dead_agent_no"),
        
        (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_thor),
        (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_deathmatch),
        (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_duel),
        (neq, ":killer_agent_team", ":dead_agent_team"), #if these agents are enemies
        
        (neq, ":dead_agent_no", ":killer_agent_no"), #if agents are different, do not remove it is needed because in deathmatch mod, self killing passes here because of this or next.
        
        (try_begin),
          (neg|agent_is_non_player, ":dead_agent_no"),
          (agent_get_player_id, ":dead_player_no", ":dead_agent_no"),
          (player_get_slot, ":dead_agent_equipment_value", ":dead_player_no", slot_player_total_equipment_value),
        (else_try),
          (assign, ":dead_agent_equipment_value", 0),
        (try_end),
        
        (assign, ":dead_agent_team_human_players_count", 0),
        (get_max_players, ":num_players"),
        (try_for_range, ":player_no", 0, ":num_players"),
          (player_is_active, ":player_no"),
          (player_get_team_no, ":player_team", ":player_no"),
          (eq, ":player_team", ":dead_agent_team"),
          (val_add, ":dead_agent_team_human_players_count", 1),
        (try_end),
        
        (try_for_range, ":player_no", 0, ":num_players"),
          (player_is_active, ":player_no"),
          
          (try_begin),
            (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_invasion),
            (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_raid),
            (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_sea),#naval battles
            #  (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle), #capitan chief
            (eq, "$g_multiplayer_game_type", multiplayer_game_type_battle),
            (assign, ":one_spawn_per_round_game_type", 1),
          (else_try),
            (assign, ":one_spawn_per_round_game_type", 0),
          (try_end),
          
          (this_or_next|eq, ":one_spawn_per_round_game_type", 0),
          (this_or_next|player_slot_eq, ":player_no", slot_player_spawned_this_round, 0),
          (player_slot_eq, ":player_no", slot_player_spawned_this_round, 1),
          
          (player_get_agent_id, ":agent_no", ":player_no"),
          (try_begin),
            (eq, ":agent_no", ":dead_agent_no"), #if this agent is dead agent then get share from total loot. (20% of total equipment value)
            (player_get_gold, ":player_gold", ":player_no"),
            
            (assign, ":dead_agent_player_id", ":player_no"),
            
            #dead agent loot share (32%-48%-64%, norm : 48%)
            (store_mul, ":share_of_dead_agent", ":dead_agent_equipment_value", multi_dead_agent_loot_percentage_share),
            (val_div, ":share_of_dead_agent", 100),
            (val_mul, ":share_of_dead_agent", "$g_multiplayer_battle_earnings_multiplier"),
            (val_div, ":share_of_dead_agent", 100),
            (try_begin),
              (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_thor), #(4/3x) share if current mod is deathmatch
              (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_deathmatch), #(4/3x) share if current mod is deathmatch
              (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_duel), #(4/3x) share if current mod is duel
              (eq, "$g_multiplayer_game_type", multiplayer_game_type_team_deathmatch), #(4/3x) share if current mod is team_deathmatch
              (val_mul, ":share_of_dead_agent", 4),
              (val_div, ":share_of_dead_agent", 3),
              (val_add, ":player_gold", ":share_of_dead_agent"),
            (else_try),
              (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_invasion), #(2/3x) share if current mod is inv
              (eq, "$g_multiplayer_game_type", multiplayer_game_type_battle), #(2/3x) share if current mod is battle
              (val_mul, ":share_of_dead_agent", 2),
              (val_div, ":share_of_dead_agent", 3),
              (val_add, ":player_gold", ":share_of_dead_agent"),
            (else_try),
              (val_add, ":player_gold", ":share_of_dead_agent"), #(3/3x) share if current mod is siege
            (try_end),
            (player_set_gold, ":player_no", ":player_gold", multi_max_gold_that_can_be_stored),
          (else_try),
            (eq, ":agent_no", ":killer_agent_no"), #if this agent is killer agent then get share from total loot. (10% of total equipment value)
            (player_get_gold, ":player_gold", ":player_no"),
            
            #killer agent standart money (100-150-200, norm : 150)
            (assign, ":killer_agent_standard_money_addition", multi_killer_agent_standard_money_add),
            (val_mul, ":killer_agent_standard_money_addition", "$g_multiplayer_battle_earnings_multiplier"),
            (val_div, ":killer_agent_standard_money_addition", 100),
            (try_begin),
              (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_thor), #(4/3x) share if current mod is deathmatch
              (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_deathmatch), #(4/3x) share if current mod is deathmatch
              (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_duel), #(4/3x) share if current mod is duel
              (eq, "$g_multiplayer_game_type", multiplayer_game_type_team_deathmatch), #(4/3x) share if current mod is team_deathmatch
              (val_mul, ":killer_agent_standard_money_addition", 4),
              (val_div, ":killer_agent_standard_money_addition", 3),
              (val_add, ":player_gold", ":killer_agent_standard_money_addition"),
            (else_try),
              (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_invasion), #(2/3x) share if current mod is inv
              (eq, "$g_multiplayer_game_type", multiplayer_game_type_battle), #(2/3x) share if current mod is battle
              (val_mul, ":killer_agent_standard_money_addition", 2),
              (val_div, ":killer_agent_standard_money_addition", 3),
              (val_add, ":player_gold", ":killer_agent_standard_money_addition"),
            (else_try),
              (val_add, ":player_gold", ":killer_agent_standard_money_addition"), #(3/3x) share if current mod is siege
            (try_end),
            
            #killer agent loot share (8%-12%-16%, norm : 12%)
            (store_mul, ":share_of_killer_agent", ":dead_agent_equipment_value", multi_killer_agent_loot_percentage_share),
            (val_div, ":share_of_killer_agent", 100),
            (val_mul, ":share_of_killer_agent", "$g_multiplayer_battle_earnings_multiplier"),
            (val_div, ":share_of_killer_agent", 100),
            (try_begin),
              (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_thor), #(4/3x) share if current mod is deathmatch
              (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_deathmatch), #(4/3x) share if current mod is deathmatch
              (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_duel), #(4/3x) share if current mod is duel
              (eq, "$g_multiplayer_game_type", multiplayer_game_type_team_deathmatch), #(4/3x) share if current mod is team_deathmatch
              (val_mul, ":share_of_killer_agent", 4),
              (val_div, ":share_of_killer_agent", 3),
              (val_add, ":player_gold", ":share_of_killer_agent"),
            (else_try),
              (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_invasion), #(2/3x) share if current mod is inv
              (eq, "$g_multiplayer_game_type", multiplayer_game_type_battle), #(2/3x) share if current mod is battle
              (val_mul, ":share_of_killer_agent", 2),
              (val_div, ":share_of_killer_agent", 3),
              (val_add, ":player_gold", ":share_of_killer_agent"),
            (else_try),
              (val_add, ":player_gold", ":share_of_killer_agent"), #(3/3x) share if current mod is siege
            (try_end),
            (player_set_gold, ":player_no", ":player_gold", multi_max_gold_that_can_be_stored),
          (try_end),
        (try_end),
      (try_end),
      
      #(below lines added new at 25.11.09 after Armagan decided new money system)
      (try_begin),
        (multiplayer_is_server),
        (neq, "$g_multiplayer_game_type", multiplayer_game_type_battle),
        (neq, "$g_multiplayer_game_type", multiplayer_game_type_raid),
        (neq, "$g_multiplayer_game_type", multiplayer_game_type_sea),#naval battles
        (neq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle), #capitan chief
        
        (ge, ":dead_agent_no", 0),
        (agent_is_human, ":dead_agent_no"), #if dead agent is not horse
        (agent_get_player_id, ":dead_agent_player_id", ":dead_agent_no"),
        (ge, ":dead_agent_player_id", 0),
        
        (player_get_gold, ":player_gold", ":dead_agent_player_id"),
        (try_begin),
          (store_mul, ":minimum_gold", "$g_multiplayer_initial_gold_multiplier", 10),
          (lt, ":player_gold", ":minimum_gold"),
          (assign, ":player_gold", ":minimum_gold"),
        (try_end),
        (player_set_gold, ":dead_agent_player_id", ":player_gold"),
      (try_end),
      #new money system addition end
  ]),
  
  ("initialize_aristocracy",
    [
      #LORD OCCUPATIONS, BLOOD RELATIONSHIPS, RENOWN AND REPUTATIONS
      
      #King ages chief cambiado entero
      
      
      
      (try_for_range, ":cur_troop", kings_begin, kings_end),
        (troop_set_slot, ":cur_troop", slot_troop_occupation, slto_kingdom_hero),
        ##      (store_random_in_range, ":age", 50, 65),
        ##      (troop_set_slot, ":cur_troop", slot_troop_age, ":age"),
        ##      (eq, ":cur_troop", "trp_kingdom_5_lord"),
        ##      (troop_set_slot, ":cur_troop", slot_troop_age, 47),
        (troop_set_slot, "trp_kingdom_1_lord", slot_troop_age, 44),
        (troop_set_slot, "trp_kingdom_2_lord", slot_troop_age, 18),  #Harald was born 850
        (troop_set_slot, "trp_kingdom_3_lord", slot_troop_age, 58),
        (troop_set_slot, "trp_kingdom_4_lord", slot_troop_age, 37),
        (troop_set_slot, "trp_kingdom_5_lord", slot_troop_age, 25),
        (troop_set_slot, "trp_kingdom_6_lord", slot_troop_age, 46),
        (troop_set_slot, "trp_kingdom_7_lord", slot_troop_age, 55),
        (troop_set_slot, "trp_kingdom_8_lord", slot_troop_age, 32),
        (troop_set_slot, "trp_kingdom_9_lord", slot_troop_age, 46),
        (troop_set_slot, "trp_kingdom_10_lord", slot_troop_age, 47),
        (troop_set_slot, "trp_kingdom_11_lord", slot_troop_age, 33),
        (troop_set_slot, "trp_kingdom_12_lord", slot_troop_age, 70),
        (troop_set_slot, "trp_kingdom_13_lord", slot_troop_age, 32),
        (troop_set_slot, "trp_kingdom_14_lord", slot_troop_age, 44),
        (troop_set_slot, "trp_kingdom_15_lord", slot_troop_age, 42),
        (troop_set_slot, "trp_kingdom_16_lord", slot_troop_age, 31),
        (troop_set_slot, "trp_kingdom_17_lord", slot_troop_age, 26),
        (troop_set_slot, "trp_kingdom_18_lord", slot_troop_age, 46),
        (troop_set_slot, "trp_kingdom_19_lord", slot_troop_age, 41),
        (troop_set_slot, "trp_kingdom_20_lord", slot_troop_age, 37),
        (troop_set_slot, "trp_kingdom_21_lord", slot_troop_age, 61),
      (try_end),
      
      # Kingdom 1
      
      (try_for_range, ":cur_lady", kingdom_ladies_begin, kingdom_ladies_end),
        (troop_set_slot, ":cur_lady", slot_troop_occupation, slto_kingdom_lady),
        #         (store_random_in_range, ":age", 20, 35),
        
        #-ages women
        (troop_set_slot, "trp_kingdom_1_lady_1", slot_troop_age, 62),
        (troop_set_slot, "trp_kingdom_1_lady_2", slot_troop_age, 34),
        (troop_set_slot, "trp_kingdom_1_lady_3", slot_troop_age, 24),
        (troop_set_slot, "trp_kingdom_1_lady_4", slot_troop_age, 42),
        
        (troop_set_slot, "trp_kingdom_2_lady_1", slot_troop_age, 16), #Gyda born c852
        (troop_set_slot, "trp_kingdom_2_lady_2", slot_troop_age, 38), #Ketils wife
        (troop_set_slot, "trp_kingdom_2_lady_3", slot_troop_age, 18), #Ketilsdottir
        (troop_set_slot, "trp_kingdom_2_lady_4", slot_troop_age, 45), #Guthorm regent's wife
        
        (troop_set_slot, "trp_kingdom_3_lady_1", slot_troop_age, 49),
        (troop_set_slot, "trp_kingdom_3_lady_2", slot_troop_age, 27),
        (troop_set_slot, "trp_kingdom_3_lady_3", slot_troop_age, 18),
        (troop_set_slot, "trp_kingdom_3_lady_4", slot_troop_age, 19),
        
        (troop_set_slot, "trp_kingdom_4_lady_1", slot_troop_age, 37),
        (troop_set_slot, "trp_kingdom_4_lady_2", slot_troop_age, 40),
        (troop_set_slot, "trp_kingdom_4_lady_3", slot_troop_age, 18),
        (troop_set_slot, "trp_kingdom_4_lady_4", slot_troop_age, 23),
        
        (troop_set_slot, "trp_kingdom_5_lady_1", slot_troop_age, 64),
        (troop_set_slot, "trp_kingdom_5_lady_2", slot_troop_age, 35),
        (troop_set_slot, "trp_kingdom_5_lady_3", slot_troop_age, 21),
        (troop_set_slot, "trp_kingdom_5_lady_4", slot_troop_age, 36),
        (troop_set_slot, "trp_kingdom_5_lady_5", slot_troop_age, 18),
        (troop_set_slot, "trp_kingdom_5_lady_6", slot_troop_age, 19),
        (troop_set_slot, "trp_kingdom_5_lady_7", slot_troop_age, 18),
        
        (troop_set_slot, "trp_kingdom_6_lady_1", slot_troop_age, 28), #sister
        (troop_set_slot, "trp_kingdom_6_lady_2", slot_troop_age, 34),
        (troop_set_slot, "trp_kingdom_6_lady_3", slot_troop_age, 37),
        (troop_set_slot, "trp_kingdom_6_lady_4", slot_troop_age, 18), #daughter
        
        (troop_set_slot, "trp_kingdom_7_lady_1", slot_troop_age, 41), #king wife
        (troop_set_slot, "trp_kingdom_7_lady_2", slot_troop_age, 36), #
        (troop_set_slot, "trp_kingdom_7_lady_3", slot_troop_age, 27), #
        (troop_set_slot, "trp_kingdom_7_lady_4", slot_troop_age, 28), #
        (troop_set_slot, "trp_kingdom_7_lady_5", slot_troop_age, 30), #
        (troop_set_slot, "trp_kingdom_7_lady_6", slot_troop_age, 41), #
        (troop_set_slot, "trp_kingdom_7_lady_7", slot_troop_age, 20), #
        (troop_set_slot, "trp_kingdom_7_lady_8", slot_troop_age, 20), #
        (troop_set_slot, "trp_kingdom_7_lady_9", slot_troop_age, 18), #
        
        (troop_set_slot, "trp_kingdom_8_lady_1", slot_troop_age, 31), #sister
        (troop_set_slot, "trp_kingdom_8_lady_2", slot_troop_age, 36),
        (troop_set_slot, "trp_kingdom_8_lady_4", slot_troop_age, 39),
        (troop_set_slot, "trp_kingdom_8_lady_3", slot_troop_age, 38),
        (troop_set_slot, "trp_kingdom_8_lady_5", slot_troop_age, 20),
        (troop_set_slot, "trp_kingdom_8_lady_6", slot_troop_age, 25),
        (troop_set_slot, "trp_kingdom_8_lady_7", slot_troop_age, 20),
        (troop_set_slot, "trp_kingdom_8_lady_8", slot_troop_age, 18),
        (troop_set_slot, "trp_kingdom_8_lady_9", slot_troop_age, 18),
        
        (troop_set_slot, "trp_kingdom_9_lady_1", slot_troop_age, 55),
        (troop_set_slot, "trp_kingdom_9_lady_2", slot_troop_age, 28), #daughter
        (troop_set_slot, "trp_kingdom_9_lady_3", slot_troop_age, 19),#daughter
        (troop_set_slot, "trp_kingdom_9_lady_4", slot_troop_age, 18),#daughter
        (troop_set_slot, "trp_kingdom_9_lady_5", slot_troop_age, 27),
        (troop_set_slot, "trp_kingdom_9_lady_6", slot_troop_age, 29),
        
        (troop_set_slot, "trp_kingdom_10_lady_1", slot_troop_age, 40),
        (troop_set_slot, "trp_kingdom_10_lady_2", slot_troop_age, 26),
        (troop_set_slot, "trp_kingdom_10_lady_3", slot_troop_age, 18),
        
        
        (troop_set_slot, "trp_kingdom_11_lady_1", slot_troop_age, 37),
        (troop_set_slot, "trp_kingdom_11_lady_2", slot_troop_age, 31),
        (troop_set_slot, "trp_kingdom_11_lady_3", slot_troop_age, 21),
        (troop_set_slot, "trp_kingdom_11_lady_4", slot_troop_age, 20),
        
        (troop_set_slot, "trp_kingdom_12_lady_1", slot_troop_age, 51),
        (troop_set_slot, "trp_kingdom_12_lady_2", slot_troop_age, 34),
        (troop_set_slot, "trp_kingdom_12_lady_3", slot_troop_age, 23),
        (troop_set_slot, "trp_kingdom_12_lady_4", slot_troop_age, 18),
        
        (troop_set_slot, "trp_kingdom_13_lady_1", slot_troop_age, 38),
        (troop_set_slot, "trp_kingdom_13_lady_2", slot_troop_age, 32),
        (troop_set_slot, "trp_kingdom_13_lady_3", slot_troop_age, 21),
        
        (troop_set_slot, "trp_kingdom_14_lady_1", slot_troop_age, 41),
        (troop_set_slot, "trp_kingdom_14_lady_2", slot_troop_age, 26),
        (troop_set_slot, "trp_kingdom_14_lady_3", slot_troop_age, 21),
        (troop_set_slot, "trp_kingdom_14_lady_4", slot_troop_age, 19),
        
        (troop_set_slot, "trp_kingdom_15_lady_1", slot_troop_age, 36),
        (troop_set_slot, "trp_kingdom_15_lady_2", slot_troop_age, 38),
        (troop_set_slot, "trp_kingdom_15_lady_3", slot_troop_age, 19),
        (troop_set_slot, "trp_kingdom_15_lady_4", slot_troop_age, 18),
        
        (troop_set_slot, "trp_kingdom_16_lady_1", slot_troop_age, 36),
        (troop_set_slot, "trp_kingdom_16_lady_2", slot_troop_age, 37),
        (troop_set_slot, "trp_kingdom_16_lady_3", slot_troop_age, 27),
        (troop_set_slot, "trp_kingdom_16_lady_4", slot_troop_age, 18),
        (troop_set_slot, "trp_kingdom_16_lady_5", slot_troop_age, 21),
        (troop_set_slot, "trp_kingdom_16_lady_6", slot_troop_age, 18),
        
        (troop_set_slot, "trp_kingdom_17_lady_1", slot_troop_age, 47),
        (troop_set_slot, "trp_kingdom_17_lady_2", slot_troop_age, 20),
        (troop_set_slot, "trp_kingdom_17_lady_3", slot_troop_age, 35),
        (troop_set_slot, "trp_kingdom_17_lady_4", slot_troop_age, 41),
        (troop_set_slot, "trp_kingdom_17_lady_5", slot_troop_age, 23),
        (troop_set_slot, "trp_kingdom_17_lady_6", slot_troop_age, 18),
        
        (troop_set_slot, "trp_kingdom_18_lady_2", slot_troop_age, 41),
        (troop_set_slot, "trp_kingdom_18_lady_3", slot_troop_age, 36),
        (troop_set_slot, "trp_kingdom_18_lady_1", slot_troop_age, 26),
        (troop_set_slot, "trp_kingdom_18_lady_4", slot_troop_age, 28),
        (troop_set_slot, "trp_kingdom_18_lady_5", slot_troop_age, 18),
        (troop_set_slot, "trp_kingdom_18_lady_6", slot_troop_age, 18),
        
        (troop_set_slot, "trp_kingdom_19_lady_1", slot_troop_age, 37), #wife
        (troop_set_slot, "trp_kingdom_19_lady_2", slot_troop_age, 19), #daughter
        (troop_set_slot, "trp_kingdom_19_lady_3", slot_troop_age, 38), #wife
        (troop_set_slot, "trp_kingdom_19_lady_4", slot_troop_age, 22), #wife
        (troop_set_slot, "trp_kingdom_19_lady_5", slot_troop_age, 18), #daughter
        
        (troop_set_slot, "trp_kingdom_20_lady_1", slot_troop_age, 18), #daughter
        (troop_set_slot, "trp_kingdom_20_lady_2", slot_troop_age, 41),
        (troop_set_slot, "trp_kingdom_20_lady_3", slot_troop_age, 29),
        (troop_set_slot, "trp_kingdom_20_lady_4", slot_troop_age, 26),
        (troop_set_slot, "trp_kingdom_20_lady_5", slot_troop_age, 18),
        (troop_set_slot, "trp_kingdom_20_lady_6", slot_troop_age, 18),
        (troop_set_slot, "trp_kingdom_20_lady_7", slot_troop_age, 23),
        
        (troop_set_slot, "trp_kingdom_21_lady_1", slot_troop_age, 20),
        (troop_set_slot, "trp_kingdom_21_lady_2", slot_troop_age, 21),
        
        (store_random_in_range, ":lady_reputation", 20, 26),
        
        
        (try_begin),
          (eq, ":lady_reputation", 20),
          (assign, ":lady_reputation", lrep_moralist),
        (else_try),
          (eq, ":lady_reputation", 21),
          (assign, ":lady_reputation", lrep_ambitious),
        (else_try),
          (eq, ":lady_reputation", 22),
          (assign, ":lady_reputation", lrep_adventurous),
        (else_try),
          (eq, ":lady_reputation", 23),
          (assign, ":lady_reputation", lrep_otherworldly),
        (else_try),
          (eq, ":lady_reputation", 24),
          (assign, ":lady_reputation", lrep_ambitious),
        (else_try),
          
          (assign, ":lady_reputation", lrep_conventional),
          
        (try_end),
        (troop_set_slot, ":cur_lady", slot_lord_reputation_type, ":lady_reputation"),
        
        (call_script, "script_add_lady_items", ":cur_lady"),
        
      (try_end),
      
      (try_for_range, ":cur_troop", lords_begin, lords_end),
        (troop_set_slot, ":cur_troop", slot_troop_occupation, slto_kingdom_hero),
        
        #edades lords
        #
        (troop_set_slot, "trp_knight_1_1", slot_troop_age, 62),
        (troop_set_slot, "trp_knight_1_2", slot_troop_age, 26),
        (troop_set_slot, "trp_knight_1_3", slot_troop_age, 47),
        (troop_set_slot, "trp_knight_1_4", slot_troop_age, 34),
        (troop_set_slot, "trp_knight_1_5", slot_troop_age, 46),
        (troop_set_slot, "trp_knight_1_6", slot_troop_age, 28),
        
        (troop_set_slot, "trp_knight_2_1", slot_troop_age,53), #Guthorm Regent
        (troop_set_slot, "trp_knight_2_2", slot_troop_age,48), #King Eirik
        (troop_set_slot, "trp_knight_2_3", slot_troop_age,26), #Ketil's son-in-law
        (troop_set_slot, "trp_knight_2_4", slot_troop_age,46), #Ketil
        (troop_set_slot, "trp_knight_2_5", slot_troop_age,20), #Ketil's son
        
        (troop_set_slot, "trp_knight_3_1", slot_troop_age,50),
        (troop_set_slot, "trp_knight_3_2", slot_troop_age,41),
        (troop_set_slot, "trp_knight_3_3", slot_troop_age,36),
        (troop_set_slot, "trp_knight_3_4", slot_troop_age,48),
        (troop_set_slot, "trp_knight_3_5", slot_troop_age,31),
        (troop_set_slot, "trp_knight_3_6", slot_troop_age,20),
        (troop_set_slot, "trp_knight_3_7", slot_troop_age,29),
        (troop_set_slot, "trp_knight_3_8", slot_troop_age,30),
        
        (troop_set_slot, "trp_knight_4_1", slot_troop_age,40),
        (troop_set_slot, "trp_knight_4_2", slot_troop_age,42),
        (troop_set_slot, "trp_knight_4_3", slot_troop_age,39),
        
        (troop_set_slot, "trp_knight_5_1", slot_troop_age,21),
        (troop_set_slot, "trp_knight_5_2", slot_troop_age,38),
        (troop_set_slot, "trp_knight_5_3", slot_troop_age,32),
        (troop_set_slot, "trp_knight_5_4", slot_troop_age,39),
        (troop_set_slot, "trp_knight_5_5", slot_troop_age,42),
        (troop_set_slot, "trp_knight_5_6", slot_troop_age,46),
        (troop_set_slot, "trp_knight_5_7", slot_troop_age,38),
        (troop_set_slot, "trp_knight_5_8", slot_troop_age,25),
        (troop_set_slot, "trp_knight_5_9", slot_troop_age,31),
        (troop_set_slot, "trp_knight_5_10", slot_troop_age,34),
        (troop_set_slot, "trp_knight_5_11", slot_troop_age,24),
        
        (troop_set_slot, "trp_knight_6_1", slot_troop_age,39),
        (troop_set_slot, "trp_knight_6_2", slot_troop_age,35),
        (troop_set_slot, "trp_knight_6_3", slot_troop_age,57),
        (troop_set_slot, "trp_knight_6_4", slot_troop_age,36),
        (troop_set_slot, "trp_knight_6_5", slot_troop_age,25),
        
        (troop_set_slot, "trp_knight_7_1", slot_troop_age,45),
        (troop_set_slot, "trp_knight_7_2", slot_troop_age,56),
        (troop_set_slot, "trp_knight_7_3", slot_troop_age,58),
        (troop_set_slot, "trp_knight_7_4", slot_troop_age,61),
        (troop_set_slot, "trp_knight_7_5", slot_troop_age,32),
        (troop_set_slot, "trp_knight_7_6", slot_troop_age,35),
        (troop_set_slot, "trp_knight_7_7", slot_troop_age,58),
        (troop_set_slot, "trp_knight_7_8", slot_troop_age,50),
        (troop_set_slot, "trp_knight_7_9", slot_troop_age,26),
        
        (troop_set_slot, "trp_knight_8_1", slot_troop_age,55),
        (troop_set_slot, "trp_knight_8_2", slot_troop_age,35),
        (troop_set_slot, "trp_knight_8_3", slot_troop_age,41),
        (troop_set_slot, "trp_knight_8_4", slot_troop_age,68),
        (troop_set_slot, "trp_knight_8_5", slot_troop_age,31),
        (troop_set_slot, "trp_knight_8_6", slot_troop_age,28),
        (troop_set_slot, "trp_knight_8_7", slot_troop_age,51),
        (troop_set_slot, "trp_knight_8_8", slot_troop_age,45),
        (troop_set_slot, "trp_knight_8_9", slot_troop_age,46),
        (troop_set_slot, "trp_knight_8_10", slot_troop_age,26),
        (troop_set_slot, "trp_knight_8_11", slot_troop_age,34),
        (troop_set_slot, "trp_knight_8_12", slot_troop_age,22),
        (troop_set_slot, "trp_knight_8_13", slot_troop_age,44),
        (troop_set_slot, "trp_knight_8_14", slot_troop_age,33),
        (troop_set_slot, "trp_knight_8_15", slot_troop_age,34),
        
        (troop_set_slot, "trp_knight_9_1", slot_troop_age,35),
        (troop_set_slot, "trp_knight_9_2", slot_troop_age,29),
        (troop_set_slot, "trp_knight_9_3", slot_troop_age,26),
        (troop_set_slot, "trp_knight_9_4", slot_troop_age,60),
        (troop_set_slot, "trp_knight_9_5", slot_troop_age,30),
        (troop_set_slot, "trp_knight_9_6", slot_troop_age,18),
        (troop_set_slot, "trp_knight_9_7", slot_troop_age,44),
        (troop_set_slot, "trp_knight_9_8", slot_troop_age,24),
        (troop_set_slot, "trp_knight_9_9", slot_troop_age,44),
        
        (troop_set_slot, "trp_knight_10_1", slot_troop_age,48),
        
        (troop_set_slot, "trp_knight_11_1", slot_troop_age,44),
        (troop_set_slot, "trp_knight_11_2", slot_troop_age,42),
        (troop_set_slot, "trp_knight_11_3", slot_troop_age,34),
        (troop_set_slot, "trp_knight_11_4", slot_troop_age,31),
        (troop_set_slot, "trp_knight_11_5", slot_troop_age,29),
        (troop_set_slot, "trp_knight_11_6", slot_troop_age,36),
        (troop_set_slot, "trp_knight_11_7", slot_troop_age,21),
        (troop_set_slot, "trp_knight_11_8", slot_troop_age,26),
        
        (troop_set_slot, "trp_knight_12_1", slot_troop_age,56),
        (troop_set_slot, "trp_knight_12_2", slot_troop_age,26),
        (troop_set_slot, "trp_knight_12_3", slot_troop_age,58),
        
        (troop_set_slot, "trp_knight_13_1", slot_troop_age,53),
        (troop_set_slot, "trp_knight_13_2", slot_troop_age,41),
        (troop_set_slot, "trp_knight_13_3", slot_troop_age,46),
        
        (troop_set_slot, "trp_knight_14_1", slot_troop_age,36),
        (troop_set_slot, "trp_knight_14_2", slot_troop_age,43),
        (troop_set_slot, "trp_knight_14_3", slot_troop_age,51),
        (troop_set_slot, "trp_knight_14_4", slot_troop_age,42),
        (troop_set_slot, "trp_knight_14_5", slot_troop_age,18),
        
        (troop_set_slot, "trp_knight_15_1", slot_troop_age,38),
        (troop_set_slot, "trp_knight_15_2", slot_troop_age,36),
        (troop_set_slot, "trp_knight_15_3", slot_troop_age,64),
        (troop_set_slot, "trp_knight_15_4", slot_troop_age,46),
        (troop_set_slot, "trp_knight_15_5", slot_troop_age,34),
        (troop_set_slot, "trp_knight_15_6", slot_troop_age,21),
        
        (troop_set_slot, "trp_knight_16_1", slot_troop_age,44),
        (troop_set_slot, "trp_knight_16_2", slot_troop_age,48),
        (troop_set_slot, "trp_knight_16_3", slot_troop_age,36),
        (troop_set_slot, "trp_knight_16_4", slot_troop_age,42),
        (troop_set_slot, "trp_knight_16_5", slot_troop_age,28),
        (troop_set_slot, "trp_knight_16_6", slot_troop_age,24),
        
        (troop_set_slot, "trp_knight_17_1", slot_troop_age,45),
        (troop_set_slot, "trp_knight_17_2", slot_troop_age,21),
        (troop_set_slot, "trp_knight_17_3", slot_troop_age,50),
        (troop_set_slot, "trp_knight_17_4", slot_troop_age,46),
        (troop_set_slot, "trp_knight_17_5", slot_troop_age,38),
        (troop_set_slot, "trp_knight_17_6", slot_troop_age,48),
        
        (troop_set_slot, "trp_knight_18_1", slot_troop_age,33),
        (troop_set_slot, "trp_knight_18_2", slot_troop_age,41),
        (troop_set_slot, "trp_knight_18_3", slot_troop_age,36),
        (troop_set_slot, "trp_knight_18_4", slot_troop_age,48),
        (troop_set_slot, "trp_knight_18_5", slot_troop_age,38),
        (troop_set_slot, "trp_knight_18_6", slot_troop_age,28),
        (troop_set_slot, "trp_knight_18_7", slot_troop_age,23),
        
        (troop_set_slot, "trp_knight_19_1", slot_troop_age,39),
        (troop_set_slot, "trp_knight_19_2", slot_troop_age,31),
        (troop_set_slot, "trp_knight_19_3", slot_troop_age,45),
        (troop_set_slot, "trp_knight_19_4", slot_troop_age,26),
        (troop_set_slot, "trp_knight_19_5", slot_troop_age,21),
        
        (troop_set_slot, "trp_knight_20_1", slot_troop_age,53),
        (troop_set_slot, "trp_knight_20_2", slot_troop_age,55),
        (troop_set_slot, "trp_knight_20_3", slot_troop_age,39),
        (troop_set_slot, "trp_knight_20_4", slot_troop_age,36),
        (troop_set_slot, "trp_knight_20_5", slot_troop_age,28),
        (troop_set_slot, "trp_knight_20_6", slot_troop_age,19),
        (troop_set_slot, "trp_knight_20_7", slot_troop_age,56),
        (troop_set_slot, "trp_knight_20_8", slot_troop_age,26),
        
        (troop_set_slot, "trp_knight_21_1", slot_troop_age,41),
        (troop_set_slot, "trp_knight_21_2", slot_troop_age,40),
        (troop_set_slot, "trp_knight_21_3", slot_troop_age,36),
        
        #          (store_random_in_range, ":lord_renown", 200, 350),
        #         (troop_set_slot, ":cur_troop", slot_troop_renown, ":lord_renown"),
        
        (store_random_in_range, ":lord_reputation", 20, 27),
        (try_begin),
          (eq, ":lord_reputation", 20),
          (assign, ":lord_reputation", lrep_martial),
        (else_try),
          (eq, ":lord_reputation", 21),
          (assign, ":lord_reputation", lrep_quarrelsome),
        (else_try),
          (eq, ":lord_reputation", 22),
          (assign, ":lord_reputation", lrep_selfrighteous),
        (else_try),
          (eq, ":lord_reputation", 23),
          (assign, ":lord_reputation", lrep_cunning),
        (else_try),
          (eq, ":lord_reputation", 24),
          (assign, ":lord_reputation", lrep_upstanding),
        (else_try),
          
          (assign, ":lord_reputation", lrep_debauched),
          
        (try_end),
        
        
        (troop_set_slot, ":cur_troop", slot_lord_reputation_type, ":lord_reputation"),
        
        
        
      (try_end),
      (try_begin),
        (eq, "$campaign_type", camp_storyline),
        (troop_set_slot, "trp_knight_4_3", slot_lord_reputation_type,lrep_cunning),
        (troop_set_slot, "trp_knight_1_5", slot_lord_reputation_type,lrep_selfrighteous),
      (try_end),
      
      #reinos relaciones familiares
      (troop_set_slot, "trp_kingdom_1_lady_1", slot_troop_guardian, "trp_knight_1_1"),
      (troop_set_slot, "trp_knight_1_5", slot_troop_mother, "trp_kingdom_1_lady_1"),
      (troop_set_slot, "trp_knight_8_14", slot_troop_mother, "trp_kingdom_1_lady_1"),
      (troop_set_slot, "trp_kingdom_1_lady_3", slot_troop_mother, "trp_kingdom_1_lady_1"),
      (troop_set_slot, "trp_knight_8_2", slot_troop_mother, "trp_kingdom_1_lady_1"),
      (troop_set_slot, "trp_knight_8_13", slot_troop_mother, "trp_kingdom_1_lady_1"),
      
      (troop_set_slot, "trp_knight_1_5", slot_troop_spouse, "trp_kingdom_1_lady_2"),
      (troop_set_slot, "trp_kingdom_1_lady_2", slot_troop_spouse, "trp_knight_1_5"),
      (troop_set_slot, "trp_kingdom_1_lady_3", slot_troop_guardian, "trp_knight_1_5"),
      
      (troop_set_slot, "trp_knight_1_2", slot_troop_mother, "trp_kingdom_1_lady_4"),
      
      (troop_set_slot, "trp_knight_1_3", slot_troop_spouse, "trp_kingdom_1_lady_4"),
      (troop_set_slot, "trp_kingdom_1_lady_4", slot_troop_spouse, "trp_knight_1_3"),
      
      
      (troop_set_slot, "trp_knight_2_4", slot_troop_spouse, "trp_kingdom_2_lady_2"), #Ketil
      (troop_set_slot, "trp_kingdom_2_lady_2", slot_troop_spouse, "trp_knight_2_4"),
      (troop_set_slot, "trp_knight_2_5", slot_troop_father, "trp_knight_2_4"), #Ketilson
      (troop_set_slot, "trp_knight_2_5", slot_troop_mother, "trp_kingdom_2_lady_2"),
      (troop_set_slot, "trp_kingdom_2_lady_3", slot_troop_father, "trp_knight_2_4"), #Ketilsdottir
      (troop_set_slot, "trp_kingdom_2_lady_3", slot_troop_mother, "trp_kingdom_2_lady_2"),
      (troop_set_slot, "trp_knight_2_3", slot_troop_spouse, "trp_kingdom_2_lady_3"),
      (troop_set_slot, "trp_kingdom_2_lady_3", slot_troop_spouse, "trp_knight_2_3"),
      
      (troop_set_slot, "trp_knight_2_1", slot_troop_spouse, "trp_kingdom_2_lady_4"), #Guthorm, regent
      (troop_set_slot, "trp_kingdom_2_lady_4", slot_troop_spouse, "trp_knight_2_1"),
      
      (troop_set_slot, "trp_kingdom_2_lady_1", slot_troop_father, "trp_knight_2_2"), #Gyda Eriksdottir
      
      (troop_set_slot, "trp_knight_3_1", slot_troop_spouse, "trp_kingdom_3_lady_1"),
      (troop_set_slot, "trp_kingdom_3_lady_1", slot_troop_spouse, "trp_knight_3_1"),
      (troop_set_slot, "trp_knight_3_3", slot_troop_spouse, "trp_kingdom_3_lady_2"),
      (troop_set_slot, "trp_kingdom_3_lady_2", slot_troop_spouse, "trp_knight_3_3"),
      (troop_set_slot, "trp_kingdom_3_lady_4", slot_troop_mother, "trp_kingdom_3_lady_1"),
      (troop_set_slot, "trp_kingdom_3_lady_4", slot_troop_father, "trp_knight_3_1"),
      (troop_set_slot, "trp_kingdom_3_lady_3", slot_troop_guardian, "trp_knight_3_2"),
      (troop_set_slot, "trp_knight_3_8", slot_troop_father, "trp_knight_3_1"),
      (troop_set_slot, "trp_knight_3_6", slot_troop_father, "trp_knight_3_1"),
      (troop_set_slot, "trp_knight_3_8", slot_troop_mother, "trp_kingdom_3_lady_1"),
      (troop_set_slot, "trp_knight_3_6", slot_troop_mother, "trp_kingdom_3_lady_1"),
      
      (troop_set_slot, "trp_knight_4_1", slot_troop_spouse, "trp_kingdom_4_lady_1"),
      (troop_set_slot, "trp_kingdom_4_lady_1", slot_troop_spouse, "trp_knight_4_1"),
      (troop_set_slot, "trp_knight_4_2", slot_troop_spouse, "trp_kingdom_4_lady_2"),
      (troop_set_slot, "trp_kingdom_4_lady_2", slot_troop_spouse, "trp_knight_4_2"),
      (troop_set_slot, "trp_kingdom_4_lady_3", slot_troop_mother, "trp_kingdom_4_lady_1"),
      (troop_set_slot, "trp_kingdom_4_lady_4", slot_troop_mother, "trp_kingdom_4_lady_2"),
      (troop_set_slot, "trp_kingdom_4_lady_3", slot_troop_father, "trp_knight_4_1"),
      (troop_set_slot, "trp_kingdom_4_lady_4", slot_troop_father, "trp_knight_4_2"),
      
      (troop_set_slot, "trp_kingdom_5_lady_1", slot_troop_guardian, "trp_knight_5_5"),
      (troop_set_slot, "trp_knight_5_2", slot_troop_mother, "trp_kingdom_5_lady_1"),
      
      (troop_set_slot, "trp_knight_5_5", slot_troop_spouse, "trp_kingdom_5_lady_2"),
      (troop_set_slot, "trp_kingdom_5_lady_2", slot_troop_spouse, "trp_knight_5_5"),
      (troop_set_slot, "trp_knight_5_1", slot_troop_spouse, "trp_kingdom_5_lady_3"),
      (troop_set_slot, "trp_kingdom_5_lady_3", slot_troop_spouse, "trp_knight_5_1"),
      (troop_set_slot, "trp_knight_5_2", slot_troop_spouse, "trp_kingdom_5_lady_4"),
      (troop_set_slot, "trp_kingdom_5_lady_4", slot_troop_spouse, "trp_knight_5_2"),
      
      (troop_set_slot, "trp_kingdom_5_lady_5", slot_troop_mother, "trp_kingdom_5_lady_4"),
      (troop_set_slot, "trp_kingdom_5_lady_5", slot_troop_father, "trp_knight_5_2"),
      (troop_set_slot, "trp_knight_5_11", slot_troop_father, "trp_knight_5_6"),
      
      (troop_set_slot, "trp_kingdom_5_lady_6", slot_troop_father, "trp_knight_5_5"),
      (troop_set_slot, "trp_kingdom_5_lady_7", slot_troop_father, "trp_knight_5_4"),
      
      (troop_set_slot, "trp_kingdom_6_lady_1", slot_troop_guardian, "trp_knight_6_1"),
      (troop_set_slot, "trp_knight_6_2", slot_troop_spouse, "trp_kingdom_6_lady_2"),
      (troop_set_slot, "trp_kingdom_6_lady_2", slot_troop_spouse, "trp_knight_6_2"),
      (troop_set_slot, "trp_knight_6_3", slot_troop_spouse, "trp_kingdom_6_lady_3"),
      (troop_set_slot, "trp_kingdom_6_lady_3", slot_troop_spouse, "trp_knight_6_3"),
      
      (troop_set_slot, "trp_kingdom_6_lady_4", slot_troop_mother, "trp_kingdom_6_lady_3"),
      (troop_set_slot, "trp_kingdom_6_lady_4", slot_troop_father, "trp_knight_6_3"),
      
      (troop_set_slot, "trp_knight_7_1", slot_troop_spouse, "trp_kingdom_7_lady_3"),
      (troop_set_slot, "trp_kingdom_7_lady_3", slot_troop_spouse, "trp_knight_7_1"),
      
      (troop_set_slot, "trp_knight_7_4", slot_troop_spouse, "trp_kingdom_7_lady_2"),
      (troop_set_slot, "trp_kingdom_7_lady_2", slot_troop_spouse, "trp_knight_7_4"),
      
      (troop_set_slot, "trp_knight_7_2", slot_troop_spouse, "trp_kingdom_7_lady_1"),
      (troop_set_slot, "trp_kingdom_7_lady_1", slot_troop_spouse, "trp_knight_7_2"),
      
      (troop_set_slot, "trp_knight_7_3", slot_troop_spouse, "trp_kingdom_7_lady_4"),
      (troop_set_slot, "trp_kingdom_7_lady_4", slot_troop_spouse, "trp_knight_7_3"),
      
      (troop_set_slot, "trp_knight_7_5", slot_troop_spouse, "trp_kingdom_7_lady_5"),
      (troop_set_slot, "trp_kingdom_7_lady_5", slot_troop_spouse, "trp_knight_7_5"),
      
      (troop_set_slot, "trp_knight_7_8", slot_troop_spouse, "trp_kingdom_7_lady_6"),
      (troop_set_slot, "trp_kingdom_7_lady_6", slot_troop_spouse, "trp_knight_7_8"),
      
      (troop_set_slot, "trp_kingdom_7_lady_9", slot_troop_mother, "trp_kingdom_7_lady_6"),
      (troop_set_slot, "trp_kingdom_7_lady_9", slot_troop_father, "trp_knight_7_8"),
      
      (troop_set_slot, "trp_kingdom_7_lady_7", slot_troop_father, "trp_knight_7_7"),
      (troop_set_slot, "trp_kingdom_7_lady_8", slot_troop_guardian, "trp_knight_7_9"),
      
      (troop_set_slot, "trp_kingdom_8_lady_1", slot_troop_guardian, "trp_knight_8_2"),
      
      (troop_set_slot, "trp_knight_8_3", slot_troop_spouse, "trp_kingdom_8_lady_2"),
      (troop_set_slot, "trp_kingdom_8_lady_2", slot_troop_spouse, "trp_knight_8_3"),
      (troop_set_slot, "trp_kingdom_8_lady_9", slot_troop_mother, "trp_kingdom_8_lady_2"),
      (troop_set_slot, "trp_kingdom_8_lady_9", slot_troop_father, "trp_knight_8_3"),
      
      (troop_set_slot, "trp_knight_8_4", slot_troop_spouse, "trp_kingdom_8_lady_4"),
      (troop_set_slot, "trp_kingdom_8_lady_4", slot_troop_spouse, "trp_knight_8_4"),
      (troop_set_slot, "trp_kingdom_8_lady_8", slot_troop_mother, "trp_kingdom_8_lady_4"),
      (troop_set_slot, "trp_kingdom_8_lady_8", slot_troop_father, "trp_knight_8_4"),
      
      (troop_set_slot, "trp_knight_8_8", slot_troop_spouse, "trp_kingdom_8_lady_3"),
      (troop_set_slot, "trp_kingdom_8_lady_3", slot_troop_spouse, "trp_knight_8_8"),
      (troop_set_slot, "trp_kingdom_8_lady_7", slot_troop_father, "trp_knight_8_8"),
      (troop_set_slot, "trp_kingdom_8_lady_7", slot_troop_mother, "trp_kingdom_8_lady_3"),
      
      (troop_set_slot, "trp_kingdom_8_lady_5", slot_troop_father, "trp_knight_8_9"),
      (troop_set_slot, "trp_kingdom_8_lady_6", slot_troop_guardian, "trp_knight_8_10"),
      
      (troop_set_slot, "trp_kingdom_9_lady_1", slot_troop_guardian, "trp_knight_9_1"),
      (troop_set_slot, "trp_knight_9_1", slot_troop_mother, "trp_kingdom_9_lady_1"),
      (troop_set_slot, "trp_knight_9_2", slot_troop_mother, "trp_kingdom_9_lady_1"),
      (troop_set_slot, "trp_knight_9_3", slot_troop_mother, "trp_kingdom_9_lady_1"),
      (troop_set_slot, "trp_knight_9_6", slot_troop_mother, "trp_kingdom_9_lady_1"),
      
      (troop_set_slot, "trp_kingdom_9_lady_2", slot_troop_guardian, "trp_knight_9_2"),
      (troop_set_slot, "trp_kingdom_9_lady_2", slot_troop_mother, "trp_kingdom_9_lady_1"),
      (troop_set_slot, "trp_kingdom_9_lady_3", slot_troop_guardian, "trp_knight_9_3"),
      (troop_set_slot, "trp_kingdom_9_lady_3", slot_troop_mother, "trp_kingdom_9_lady_1"),
      (troop_set_slot, "trp_kingdom_9_lady_4", slot_troop_guardian, "trp_knight_9_6"),
      (troop_set_slot, "trp_kingdom_9_lady_4", slot_troop_mother, "trp_kingdom_9_lady_1"),
      
      (troop_set_slot, "trp_knight_9_4", slot_troop_spouse, "trp_kingdom_9_lady_6"),
      (troop_set_slot, "trp_kingdom_9_lady_6", slot_troop_spouse, "trp_knight_9_4"),
      
      (troop_set_slot, "trp_knight_9_5", slot_troop_spouse, "trp_kingdom_9_lady_5"),
      (troop_set_slot, "trp_kingdom_9_lady_5", slot_troop_spouse, "trp_knight_9_5"),
      
      
      (troop_set_slot, "trp_kingdom_10_lady_2", slot_troop_guardian, "trp_knight_10_1"),
      (troop_set_slot, "trp_knight_10_1", slot_troop_spouse, "trp_kingdom_10_lady_1"),
      (troop_set_slot, "trp_kingdom_10_lady_1", slot_troop_spouse, "trp_knight_10_1"),
      (troop_set_slot, "trp_kingdom_10_lady_3", slot_troop_father, "trp_knight_10_1"),
      (troop_set_slot, "trp_kingdom_10_lady_3", slot_troop_mother, "trp_kingdom_10_lady_1"),
      
      (troop_set_slot, "trp_knight_11_1", slot_troop_spouse, "trp_kingdom_11_lady_1"),
      (troop_set_slot, "trp_kingdom_11_lady_1", slot_troop_spouse, "trp_knight_11_1"),
      (troop_set_slot, "trp_kingdom_11_lady_4", slot_troop_father, "trp_knight_11_1"),
      (troop_set_slot, "trp_kingdom_11_lady_4", slot_troop_mother, "trp_kingdom_11_lady_1"),
      
      (troop_set_slot, "trp_kingdom_11_lady_2", slot_troop_spouse, "trp_knight_11_3"),
      (troop_set_slot, "trp_knight_11_3", slot_troop_spouse, "trp_kingdom_11_lady_2"),
      
      (troop_set_slot, "trp_kingdom_11_lady_3", slot_troop_spouse, "trp_knight_11_2"),
      (troop_set_slot, "trp_knight_11_2", slot_troop_spouse, "trp_kingdom_11_lady_3"),
      
      (troop_set_slot, "trp_knight_12_1", slot_troop_spouse, "trp_kingdom_12_lady_2"),
      (troop_set_slot, "trp_kingdom_12_lady_2", slot_troop_spouse, "trp_knight_12_1"),
      
      (troop_set_slot, "trp_knight_12_3", slot_troop_spouse, "trp_kingdom_12_lady_1"),
      (troop_set_slot, "trp_kingdom_12_lady_1", slot_troop_spouse, "trp_knight_12_3"),
      (troop_set_slot, "trp_knight_12_2", slot_troop_spouse, "trp_kingdom_12_lady_3"),
      (troop_set_slot, "trp_kingdom_12_lady_3", slot_troop_spouse, "trp_knight_12_2"),
      
      (troop_set_slot, "trp_kingdom_12_lady_4", slot_troop_father, "trp_knight_12_3"),
      (troop_set_slot, "trp_kingdom_12_lady_4", slot_troop_mother, "trp_kingdom_12_lady_1"),
      
      (troop_set_slot, "trp_knight_13_1", slot_troop_spouse, "trp_kingdom_13_lady_1"),
      (troop_set_slot, "trp_kingdom_13_lady_1", slot_troop_spouse, "trp_knight_13_1"),
      (troop_set_slot, "trp_knight_13_2", slot_troop_spouse, "trp_kingdom_13_lady_2"),
      (troop_set_slot, "trp_kingdom_13_lady_2", slot_troop_spouse, "trp_knight_13_2"),
      (troop_set_slot, "trp_kingdom_13_lady_3", slot_troop_father, "trp_knight_13_3"),
      
      (troop_set_slot, "trp_knight_14_3", slot_troop_spouse, "trp_kingdom_14_lady_1"),
      (troop_set_slot, "trp_kingdom_14_lady_1", slot_troop_spouse, "trp_knight_14_3"),
      (troop_set_slot, "trp_kingdom_14_lady_4", slot_troop_father, "trp_knight_14_3"),
      (troop_set_slot, "trp_kingdom_14_lady_4", slot_troop_mother, "trp_kingdom_14_lady_1"),
      (troop_set_slot, "trp_knight_14_1", slot_troop_spouse, "trp_kingdom_14_lady_2"),
      (troop_set_slot, "trp_kingdom_14_lady_2", slot_troop_spouse, "trp_knight_14_1"),
      (troop_set_slot, "trp_knight_14_2", slot_troop_spouse, "trp_kingdom_14_lady_3"),
      (troop_set_slot, "trp_kingdom_14_lady_3", slot_troop_spouse, "trp_knight_14_2"),
      
      (troop_set_slot, "trp_knight_15_2", slot_troop_spouse, "trp_kingdom_15_lady_1"),
      (troop_set_slot, "trp_kingdom_15_lady_1", slot_troop_spouse, "trp_knight_15_2"),
      
      (troop_set_slot, "trp_knight_15_4", slot_troop_spouse, "trp_kingdom_15_lady_2"),
      (troop_set_slot, "trp_kingdom_15_lady_2", slot_troop_spouse, "trp_knight_15_4"),
      (troop_set_slot, "trp_kingdom_15_lady_4", slot_troop_father, "trp_knight_15_1"),
      (troop_set_slot, "trp_kingdom_15_lady_3", slot_troop_guardian, "trp_knight_15_6"),
      
      (troop_set_slot, "trp_knight_16_1", slot_troop_spouse, "trp_kingdom_16_lady_1"),
      (troop_set_slot, "trp_kingdom_16_lady_1", slot_troop_spouse, "trp_knight_16_1"),
      (troop_set_slot, "trp_kingdom_16_lady_6", slot_troop_father, "trp_knight_16_1"),
      (troop_set_slot, "trp_kingdom_16_lady_6", slot_troop_mother, "trp_kingdom_16_lady_1"),
      
      (troop_set_slot, "trp_knight_16_3", slot_troop_spouse, "trp_kingdom_16_lady_3"),
      (troop_set_slot, "trp_kingdom_16_lady_3", slot_troop_spouse, "trp_knight_16_3"),
      (troop_set_slot, "trp_knight_16_4", slot_troop_spouse, "trp_kingdom_16_lady_2"),
      (troop_set_slot, "trp_kingdom_16_lady_2", slot_troop_spouse, "trp_knight_16_4"),
      
      (troop_set_slot, "trp_knight_16_6", slot_troop_spouse, "trp_kingdom_16_lady_4"),
      (troop_set_slot, "trp_kingdom_16_lady_4", slot_troop_spouse, "trp_knight_16_6"),
      
      (troop_set_slot, "trp_kingdom_16_lady_5", slot_troop_guardian, "trp_knight_16_2"),
      
      (troop_set_slot, "trp_knight_17_1", slot_troop_spouse, "trp_kingdom_17_lady_1"),
      (troop_set_slot, "trp_kingdom_17_lady_1", slot_troop_spouse, "trp_knight_17_1"),
      
      (troop_set_slot, "trp_knight_17_2", slot_troop_spouse, "trp_kingdom_17_lady_2"),
      (troop_set_slot, "trp_kingdom_17_lady_2", slot_troop_spouse, "trp_knight_17_2"),
      
      (troop_set_slot, "trp_knight_17_3", slot_troop_spouse, "trp_kingdom_17_lady_3"),
      (troop_set_slot, "trp_kingdom_17_lady_3", slot_troop_spouse, "trp_knight_17_3"),
      
      (troop_set_slot, "trp_kingdom_17_lady_6", slot_troop_father, "trp_knight_17_4"),
      
      (troop_set_slot, "trp_knight_17_6", slot_troop_spouse, "trp_kingdom_17_lady_4"),
      (troop_set_slot, "trp_kingdom_17_lady_4", slot_troop_spouse, "trp_knight_17_6"),
      (troop_set_slot, "trp_kingdom_17_lady_5", slot_troop_father, "trp_knight_17_6"),
      (troop_set_slot, "trp_kingdom_17_lady_5", slot_troop_mother, "trp_kingdom_17_lady_4"),
      
      (troop_set_slot, "trp_knight_18_1", slot_troop_spouse, "trp_kingdom_18_lady_1"),
      (troop_set_slot, "trp_kingdom_18_lady_1", slot_troop_spouse, "trp_knight_18_1"),
      
      (troop_set_slot, "trp_knight_18_2", slot_troop_spouse, "trp_kingdom_18_lady_2"),
      (troop_set_slot, "trp_kingdom_18_lady_2", slot_troop_spouse, "trp_knight_18_2"),
      (troop_set_slot, "trp_kingdom_18_lady_6", slot_troop_father, "trp_knight_18_3"),
      (troop_set_slot, "trp_kingdom_18_lady_5", slot_troop_guardian, "trp_knight_18_4"),
      
      (troop_set_slot, "trp_knight_18_5", slot_troop_spouse, "trp_kingdom_18_lady_3"),
      (troop_set_slot, "trp_kingdom_18_lady_3", slot_troop_spouse, "trp_knight_18_5"),
      (troop_set_slot, "trp_knight_18_6", slot_troop_spouse, "trp_kingdom_18_lady_4"),
      (troop_set_slot, "trp_kingdom_18_lady_4", slot_troop_spouse, "trp_knight_18_6"),
      
      (troop_set_slot, "trp_knight_19_1", slot_troop_spouse, "trp_kingdom_19_lady_1"),
      (troop_set_slot, "trp_kingdom_19_lady_1", slot_troop_spouse, "trp_knight_19_1"),
      (troop_set_slot, "trp_kingdom_19_lady_5", slot_troop_father, "trp_knight_19_1"),
      (troop_set_slot, "trp_kingdom_19_lady_5", slot_troop_mother, "trp_kingdom_19_lady_1"),
      
      (troop_set_slot, "trp_knight_19_3", slot_troop_spouse, "trp_kingdom_19_lady_3"),
      (troop_set_slot, "trp_kingdom_19_lady_3", slot_troop_spouse, "trp_knight_19_3"),
      (troop_set_slot, "trp_kingdom_19_lady_2", slot_troop_father, "trp_knight_19_3"),
      (troop_set_slot, "trp_kingdom_19_lady_2", slot_troop_mother, "trp_kingdom_19_lady_3"),
      
      (troop_set_slot, "trp_knight_19_4", slot_troop_spouse, "trp_kingdom_19_lady_4"),
      (troop_set_slot, "trp_kingdom_19_lady_4", slot_troop_spouse, "trp_knight_19_4"),
      
      (troop_set_slot, "trp_kingdom_20_lady_1", slot_troop_father, "trp_knight_20_1"),
      
      (troop_set_slot, "trp_knight_20_3", slot_troop_spouse, "trp_kingdom_20_lady_2"),
      (troop_set_slot, "trp_kingdom_20_lady_2", slot_troop_spouse, "trp_knight_20_3"),
      
      (troop_set_slot, "trp_knight_20_2", slot_troop_spouse, "trp_kingdom_20_lady_3"),
      (troop_set_slot, "trp_kingdom_20_lady_3", slot_troop_spouse, "trp_knight_20_2"),
      
      (troop_set_slot, "trp_kingdom_20_lady_7", slot_troop_guardian, "trp_knight_20_4"),
      
      (troop_set_slot, "trp_kingdom_20_lady_6", slot_troop_father, "trp_knight_20_4"),
      
      (troop_set_slot, "trp_knight_20_5", slot_troop_spouse, "trp_kingdom_20_lady_4"),
      (troop_set_slot, "trp_kingdom_20_lady_4",slot_troop_spouse, "trp_knight_20_5"),
      
      (troop_set_slot, "trp_knight_20_6", slot_troop_spouse, "trp_kingdom_20_lady_5"),
      (troop_set_slot, "trp_kingdom_20_lady_5",slot_troop_spouse, "trp_knight_20_6"),
      
      
      (troop_set_slot, "trp_knight_21_3", slot_troop_spouse, "trp_kingdom_21_lady_2"),
      (troop_set_slot, "trp_kingdom_21_lady_2",slot_troop_spouse, "trp_knight_21_3"),
      (troop_set_slot, "trp_kingdom_21_lady_1", slot_troop_father, "trp_knight_21_1"),
      
      
      ##		(call_script, "script_init_troop_age", ":cur_troop", ":age"),
      (try_for_range, ":lady", kingdom_ladies_begin, kingdom_ladies_end),
        (troop_get_slot, ":spouse", ":lady", slot_troop_spouse),
        (ge, ":spouse", 1),
        (store_random_in_range, ":rel", 5, 45),
        (call_script, "script_troop_change_relation_with_troop", ":lady", ":spouse", ":rel"),
      (try_end),
      (try_begin),
        (eq, "$cheat_mode", 1),
        (assign, reg3, "$cheat_mode"),
        (display_message, "@{!}DEBUG -- Assigned lord reputation and relations"),
        
        #	    (display_message, "str_assigned_lord_reputation_and_relations_cheat_mode_reg3"), #This string can be removed
      (try_end),
      
      
      
      (try_for_range, ":cur_troop", pretenders_begin, pretenders_end),
        (troop_set_slot, ":cur_troop", slot_troop_occupation, slto_inactive_pretender),
        (store_random_in_range, ":age", 18, 50),
        (troop_set_slot, ":cur_troop", slot_troop_age, ":age"),
        (try_begin),
          (eq, ":cur_troop", "trp_kingdom_1_pretender"),
          (troop_set_slot, ":cur_troop", slot_troop_religion, 2),	#pagan
        (else_try),
          (troop_set_slot, ":cur_troop", slot_troop_religion, 1),	#christian
        (try_end),
        (eq, ":cur_troop", "trp_kingdom_5_pretender"),
        (troop_set_slot, ":cur_troop", slot_troop_age, 45),
      (try_end),
      
      
  ]),
  
  
  
  
  
  ("initialize_trade_routes",
    [
      #SARGOTH - 10 routes
      (call_script, "script_set_trade_route_between_centers", "p_town_1", "p_town_12"), #Sargoth - Wercheg
      (call_script, "script_set_trade_route_between_centers", "p_town_1", "p_town_11"), #Sargoth - Curaw
      (call_script, "script_set_trade_route_between_centers", "p_town_1", "p_town_8"), #Sargoth - Rivacheg
      (call_script, "script_set_trade_route_between_centers", "p_town_1", "p_town_14"), #Sargoth - Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_1", "p_town_23"), #Sargoth - Khudan
      
      #TIHR- 8 Routes
      (call_script, "script_set_trade_route_between_centers", "p_town_2", "p_town_1"), #Tihr- Suno
      (call_script, "script_set_trade_route_between_centers", "p_town_2", "p_town_10"), #Tihr - Praven
      (call_script, "script_set_trade_route_between_centers", "p_town_2", "p_town_13"), #Tihr - Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_2", "p_town_16"), #Tihr - Yalen
      (call_script, "script_set_trade_route_between_centers", "p_town_2", "p_town_26"), #Tihr - Wercheg
      
      #VELUCA - 8 Routes
      (call_script, "script_set_trade_route_between_centers", "p_town_3", "p_town_4"), #Veluca- Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_3", "p_town_6"), #Veluca - Jelkala
      (call_script, "script_set_trade_route_between_centers", "p_town_3", "p_town_12"), #Veluca - Yalen
      (call_script, "script_set_trade_route_between_centers", "p_town_3", "p_town_15"), #Veluca - Dhirim
      (call_script, "script_set_trade_route_between_centers", "p_town_3", "p_town_5"), #Veluca - Halmar
      
      #SUNO - 11 routes
      #Sargoth, Tihr, Veluca
      (call_script, "script_set_trade_route_between_centers", "p_town_4", "p_town_1"), #Suno - Wercheg
      (call_script, "script_set_trade_route_between_centers", "p_town_4", "p_town_3"), #Suno - Reyvadin
      (call_script, "script_set_trade_route_between_centers", "p_town_4", "p_town_5"), #Suno - Curaw
      (call_script, "script_set_trade_route_between_centers", "p_town_4", "p_town_12"), #Suno - Praven
      (call_script, "script_set_trade_route_between_centers", "p_town_4", "p_town_11"), #Suno - Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_4", "p_town_27"), #Suno - Dhirim
      
      #JELKALA - 6 ROUTES
      #Veluca, Suno
      (call_script, "script_set_trade_route_between_centers", "p_town_5", "p_town_4"), #Jelkala - Yalen
      (call_script, "script_set_trade_route_between_centers", "p_town_5", "p_town_7"), #Jelkala - Praven
      (call_script, "script_set_trade_route_between_centers", "p_town_5", "p_town_15"), #Jelkala - Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_5", "p_town_24"), #Jelkala - Shariz
      (call_script, "script_set_trade_route_between_centers", "p_town_5", "p_town_25"), #Suno - Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_5", "p_town_29"), #Suno - Dhirim
      
      #PRAVEN - 7 ROUTES
      #Tihr, Veluca, Suno, Jelkala
      (call_script, "script_set_trade_route_between_centers", "p_town_6", "p_town_3"), #Praven - Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_6", "p_town_9"), #Praven - Yalen
      (call_script, "script_set_trade_route_between_centers", "p_town_6", "p_town_13"), #Praven - Dhirim
      (call_script, "script_set_trade_route_between_centers", "p_town_6", "p_town_19"), #Praven - Yalen
      (call_script, "script_set_trade_route_between_centers", "p_town_6", "p_town_20"), #Praven - Dhirim
      (call_script, "script_set_trade_route_between_centers", "p_town_6", "p_town_28"), #Praven - Yalen
      
      #UXKHAL - 9 Routes
      #Sargoth, Tihr, Suno, Jelkala, Praven
      (call_script, "script_set_trade_route_between_centers", "p_town_7", "p_town_5"), #Yalen
      (call_script, "script_set_trade_route_between_centers", "p_town_7", "p_town_9"), #Dhirim
      (call_script, "script_set_trade_route_between_centers", "p_town_7", "p_town_13"), #Shariz
      (call_script, "script_set_trade_route_between_centers", "p_town_7", "p_town_17"), #Halmar
      (call_script, "script_set_trade_route_between_centers", "p_town_7", "p_town_18"), #Yalen
      (call_script, "script_set_trade_route_between_centers", "p_town_7", "p_town_28"), #Dhirim
      
      #REYVADIN - 9 Routes
      #Suno, Sargoth
      (call_script, "script_set_trade_route_between_centers", "p_town_8", "p_town_1"), #Khudan
      (call_script, "script_set_trade_route_between_centers", "p_town_8", "p_town_10"), #Curaw
      (call_script, "script_set_trade_route_between_centers", "p_town_8", "p_town_13"), #Wercheg
      (call_script, "script_set_trade_route_between_centers", "p_town_8", "p_town_16"), #Rivacheg
      (call_script, "script_set_trade_route_between_centers", "p_town_8", "p_town_21"), #Dhirim
      
      #KHUDAN - 9 Routes
      #Sargoth, Reyvadin
      (call_script, "script_set_trade_route_between_centers", "p_town_9", "p_town_3"), #Curaw
      (call_script, "script_set_trade_route_between_centers", "p_town_9", "p_town_6"), #Rivacheg
      (call_script, "script_set_trade_route_between_centers", "p_town_9", "p_town_7"), #Wercheg
      (call_script, "script_set_trade_route_between_centers", "p_town_9", "p_town_14"), #Ichamur
      (call_script, "script_set_trade_route_between_centers", "p_town_9", "p_town_17"), #Tulga
      (call_script, "script_set_trade_route_between_centers", "p_town_9", "p_town_21"), #Dhirim
      
      #TULGA - 7 Routes
      #Khudan
      (call_script, "script_set_trade_route_between_centers", "p_town_10", "p_town_2"), #Ichamur
      (call_script, "script_set_trade_route_between_centers", "p_town_10", "p_town_4"), #Narra
      (call_script, "script_set_trade_route_between_centers", "p_town_10", "p_town_5"), #Bariyye
      (call_script, "script_set_trade_route_between_centers", "p_town_10", "p_town_7"), #Ahmerrad
      (call_script, "script_set_trade_route_between_centers", "p_town_10", "p_town_13"), #Halmar
      (call_script, "script_set_trade_route_between_centers", "p_town_10", "p_town_19"), #Durquba
      
      #CURAW - 9 Routes
      #Khudan, Reyvadin, Sargoth, Suno
      (call_script, "script_set_trade_route_between_centers", "p_town_11", "p_town_1"), #Wercheg
      (call_script, "script_set_trade_route_between_centers", "p_town_11", "p_town_4"), #Rivacheg
      (call_script, "script_set_trade_route_between_centers", "p_town_11", "p_town_7"), #Halmar
      (call_script, "script_set_trade_route_between_centers", "p_town_11", "p_town_9"), #Dhirim
      (call_script, "script_set_trade_route_between_centers", "p_town_11", "p_town_16"), #Ichamur
      (call_script, "script_set_trade_route_between_centers", "p_town_11", "p_town_17"), #Ichamur
      
      #WERCHEG - 7 Routes
      #Sargoth, Suno, Reyvadin, Khudan, Curaw, Tihr
      (call_script, "script_set_trade_route_between_centers", "p_town_12", "p_town_6"), #Rivacheg
      (call_script, "script_set_trade_route_between_centers", "p_town_12", "p_town_11"), #Rivacheg
      (call_script, "script_set_trade_route_between_centers", "p_town_12", "p_town_4"), #Rivacheg
      (call_script, "script_set_trade_route_between_centers", "p_town_12", "p_town_14"), #Rivacheg
      (call_script, "script_set_trade_route_between_centers", "p_town_12", "p_town_23"), #Rivacheg
      
      #RIVACHEG - 6 Routes
      #Sargoth, Reyvadin, Khudan, Curaw, Wercheg
      (call_script, "script_set_trade_route_between_centers", "p_town_13", "p_town_10"), #Ichamur
      (call_script, "script_set_trade_route_between_centers", "p_town_13", "p_town_15"), #Ichamur
      (call_script, "script_set_trade_route_between_centers", "p_town_13", "p_town_17"), #Ichamur
      (call_script, "script_set_trade_route_between_centers", "p_town_13", "p_town_25"), #Ichamur
      (call_script, "script_set_trade_route_between_centers", "p_town_13", "p_town_29"), #Ichamur
      (call_script, "script_set_trade_route_between_centers", "p_town_13", "p_town_28"), #Ichamur
      
      #HALMAR- 11 Routes
      #Veluca, Uxkhal, Tulga, Curaw
      (call_script, "script_set_trade_route_between_centers", "p_town_14", "p_town_2"), #Ichamur
      (call_script, "script_set_trade_route_between_centers", "p_town_14", "p_town_4"), #Narra
      (call_script, "script_set_trade_route_between_centers", "p_town_14", "p_town_6"), #Ahmerrad
      (call_script, "script_set_trade_route_between_centers", "p_town_14", "p_town_15"), #Bariyye
      (call_script, "script_set_trade_route_between_centers", "p_town_14", "p_town_16"), #Durquba
      (call_script, "script_set_trade_route_between_centers", "p_town_14", "p_town_26"), #Shariz
      
      #YALEN - 7 Routes
      #Sargoth, Tihr, Veluca, Suno, Jelkala, Praven, Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_15", "p_town_18"), #Narra
      (call_script, "script_set_trade_route_between_centers", "p_town_15", "p_town_20"), #Durquba
      (call_script, "script_set_trade_route_between_centers", "p_town_15", "p_town_19"), #Shariz
      (call_script, "script_set_trade_route_between_centers", "p_town_15", "p_town_3"), #Narra
      (call_script, "script_set_trade_route_between_centers", "p_town_15", "p_town_23"), #Durquba
      (call_script, "script_set_trade_route_between_centers", "p_town_15", "p_town_26"), #Shariz
      
      #DHIRIM - 13 Routes
      #Sargoth, Thir, Veluca, Suno, Praven, Uxkhal, Reyvadin, Khudan, Curaw, Halmar
      (call_script, "script_set_trade_route_between_centers", "p_town_16", "p_town_6"), #Narra
      (call_script, "script_set_trade_route_between_centers", "p_town_16", "p_town_9"), #Durquba
      (call_script, "script_set_trade_route_between_centers", "p_town_16", "p_town_14"), #Shariz
      (call_script, "script_set_trade_route_between_centers", "p_town_16", "p_town_23"), #Narra
      (call_script, "script_set_trade_route_between_centers", "p_town_16", "p_town_27"), #Durquba
      (call_script, "script_set_trade_route_between_centers", "p_town_16", "p_town_11"), #Shariz
      
      #ICHAMUR - 7 Routes
      #Reyvadin, Khudan, Tulga, Curaw, Rivacheg, Halmar
      (call_script, "script_set_trade_route_between_centers", "p_town_17", "p_town_7"), #Narra
      (call_script, "script_set_trade_route_between_centers", "p_town_17", "p_town_9"), #Narra
      (call_script, "script_set_trade_route_between_centers", "p_town_17", "p_town_11"), #Narra
      (call_script, "script_set_trade_route_between_centers", "p_town_17", "p_town_13"), #Narra
      (call_script, "script_set_trade_route_between_centers", "p_town_17", "p_town_26"), #Narra
      (call_script, "script_set_trade_route_between_centers", "p_town_17", "p_town_22"), #Narra
      
      #NARRA - 9 Routes
      #Reyvadin, Khudan, Tulga, Halmar, Dhirim, Ichamur
      (call_script, "script_set_trade_route_between_centers", "p_town_18", "p_town_28"), #Durquba
      (call_script, "script_set_trade_route_between_centers", "p_town_18", "p_town_29"), #Ahmerrad
      (call_script, "script_set_trade_route_between_centers", "p_town_18", "p_town_22"), #Bariyye
      (call_script, "script_set_trade_route_between_centers", "p_town_18", "p_town_24"), #Durquba
      (call_script, "script_set_trade_route_between_centers", "p_town_18", "p_town_25"), #Ahmerrad
      (call_script, "script_set_trade_route_between_centers", "p_town_18", "p_town_21"), #Bariyye
      
      #SHARIZ - 8 Routes
      #Veluca, Jelkala, Uxkhal, Halmar, Dhirim
      (call_script, "script_set_trade_route_between_centers", "p_town_19", "p_town_3"), #Durquba
      (call_script, "script_set_trade_route_between_centers", "p_town_19", "p_town_9"), #Ahmerrad
      (call_script, "script_set_trade_route_between_centers", "p_town_19", "p_town_24"), #Bariyye
      (call_script, "script_set_trade_route_between_centers", "p_town_19", "p_town_25"), #Durquba
      (call_script, "script_set_trade_route_between_centers", "p_town_19", "p_town_28"), #Ahmerrad
      
      #DURQUBA - 7 Routes
      #Tulga, Halmar, Dhirim, Narra, Shariz
      (call_script, "script_set_trade_route_between_centers", "p_town_20", "p_town_5"), #Ahmerrad
      (call_script, "script_set_trade_route_between_centers", "p_town_20", "p_town_6"), #Bariyye
      (call_script, "script_set_trade_route_between_centers", "p_town_20", "p_town_13"), #Ahmerrad
      (call_script, "script_set_trade_route_between_centers", "p_town_20", "p_town_17"), #Bariyye
      (call_script, "script_set_trade_route_between_centers", "p_town_20", "p_town_22"), #Ahmerrad
      (call_script, "script_set_trade_route_between_centers", "p_town_20", "p_town_11"), #Bariyye
      
      #AHMERRAD - 6 Routes
      #Tulga, Halmar, Narra, Shariz, Durquba
      (call_script, "script_set_trade_route_between_centers", "p_town_21", "p_town_28"), #Bariyye
      (call_script, "script_set_trade_route_between_centers", "p_town_21", "p_town_29"), #Bariyye
      (call_script, "script_set_trade_route_between_centers", "p_town_21", "p_town_23"), #Bariyye
      (call_script, "script_set_trade_route_between_centers", "p_town_21", "p_town_20"), #Bariyye
      (call_script, "script_set_trade_route_between_centers", "p_town_21", "p_town_19"), #Bariyye
      (call_script, "script_set_trade_route_between_centers", "p_town_21", "p_town_1"), #Bariyye
      
      #BARIYYE - 6 Routes
      #Tulga, Halmar, Narra, Shariz, Durquba, Ahmerrad
      #Caer_Meguaidd - 10 routes
      (call_script, "script_set_trade_route_between_centers", "p_town_22", "p_town_12"), #Sargoth - Suno
      (call_script, "script_set_trade_route_between_centers", "p_town_22", "p_town_15"), #Sargoth - Wercheg
      (call_script, "script_set_trade_route_between_centers", "p_town_22", "p_town_16"), #Sargoth - Curaw
      (call_script, "script_set_trade_route_between_centers", "p_town_22", "p_town_27"), #Sargoth - Reyvadin
      (call_script, "script_set_trade_route_between_centers", "p_town_22", "p_town_18"), #Sargoth - Rivacheg
      
      #Licidfelth- 8 Routes
      (call_script, "script_set_trade_route_between_centers", "p_town_23", "p_town_3"), #Tihr- Suno
      (call_script, "script_set_trade_route_between_centers", "p_town_23", "p_town_6"), #Tihr - Yalen
      (call_script, "script_set_trade_route_between_centers", "p_town_23", "p_town_7"), #Tihr - Wercheg
      (call_script, "script_set_trade_route_between_centers", "p_town_23", "p_town_16"), #Tihr - Reyvadin
      (call_script, "script_set_trade_route_between_centers", "p_town_23", "p_town_25"), #Thir - Dhirim
      (call_script, "script_set_trade_route_between_centers", "p_town_23", "p_town_14"), #Thir - Dhirim
      
      #VELUCA - 8 Routes
      (call_script, "script_set_trade_route_between_centers", "p_town_24", "p_town_29"), #Veluca- Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_24", "p_town_28"), #Veluca - Jelkala
      (call_script, "script_set_trade_route_between_centers", "p_town_24", "p_town_26"), #Veluca - Halmar
      (call_script, "script_set_trade_route_between_centers", "p_town_24", "p_town_25"), #Veluca - Suno
      (call_script, "script_set_trade_route_between_centers", "p_town_24", "p_town_22"), #Veluca - Shariz
      (call_script, "script_set_trade_route_between_centers", "p_town_24", "p_town_19"), #Veluca - Suno
      (call_script, "script_set_trade_route_between_centers", "p_town_24", "p_town_13"), #Veluca - Shariz
      
      #SUNO - 11 routes
      #Sargoth, Tihr, Veluca
      (call_script, "script_set_trade_route_between_centers", "p_town_25", "p_town_2"), #Suno - Wercheg
      (call_script, "script_set_trade_route_between_centers", "p_town_25", "p_town_7"), #Suno - Praven
      (call_script, "script_set_trade_route_between_centers", "p_town_25", "p_town_11"), #Suno - Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_25", "p_town_14"), #Suno - Jelkala
      (call_script, "script_set_trade_route_between_centers", "p_town_25", "p_town_19"), #Suno - Yalen
      (call_script, "script_set_trade_route_between_centers", "p_town_25", "p_town_27"), #Suno - Yalen
      
      #JELKALA - 6 ROUTES
      #Veluca, Suno
      (call_script, "script_set_trade_route_between_centers", "p_town_26", "p_town_14"), #Jelkala - Yalen
      (call_script, "script_set_trade_route_between_centers", "p_town_26", "p_town_10"), #Jelkala - Praven
      (call_script, "script_set_trade_route_between_centers", "p_town_26", "p_town_12"), #Jelkala - Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_26", "p_town_18"), #Jelkala - Shariz
      (call_script, "script_set_trade_route_between_centers", "p_town_26", "p_town_27"), #Jelkala - Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_26", "p_town_29"), #Jelkala - Shariz
      
      #PRAVEN - 7 ROUTES
      #Tihr, Veluca, Suno, Jelkala
      (call_script, "script_set_trade_route_between_centers", "p_town_27", "p_town_4"), #Praven - Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_27", "p_town_5"), #Praven - Yalen
      (call_script, "script_set_trade_route_between_centers", "p_town_27", "p_town_12"), #Praven - Dhirim
      (call_script, "script_set_trade_route_between_centers", "p_town_27", "p_town_14"), #Praven - Uxkhal
      (call_script, "script_set_trade_route_between_centers", "p_town_27", "p_town_16"), #Praven - Yalen
      
      #UXKHAL - 9 Routes
      #Sargoth, Tihr, Suno, Jelkala, Praven
      (call_script, "script_set_trade_route_between_centers", "p_town_28", "p_town_29"), #Yalen
      (call_script, "script_set_trade_route_between_centers", "p_town_28", "p_town_7"), #Dhirim
      (call_script, "script_set_trade_route_between_centers", "p_town_28", "p_town_18"), #Shariz
      (call_script, "script_set_trade_route_between_centers", "p_town_28", "p_town_21"), #Halmar
      (call_script, "script_set_trade_route_between_centers", "p_town_28", "p_town_25"), #Shariz
      (call_script, "script_set_trade_route_between_centers", "p_town_28", "p_town_13"), #Halmar
      
      #REYVADIN - 9 Routes
      #Suno, Sargoth
      (call_script, "script_set_trade_route_between_centers", "p_town_29", "p_town_15"), #Curaw
      (call_script, "script_set_trade_route_between_centers", "p_town_29", "p_town_7"), #Wercheg
      (call_script, "script_set_trade_route_between_centers", "p_town_29", "p_town_17"), #Dhirim
      (call_script, "script_set_trade_route_between_centers", "p_town_29", "p_town_20"), #Narra
      (call_script, "script_set_trade_route_between_centers", "p_town_29", "p_town_24"), #Ichamur
      (call_script, "script_set_trade_route_between_centers", "p_town_29", "p_town_25"), #Ichamur
      #BARIYYE - 6 Routes
      #Tulga, Halmar, Narra, Shariz, Durquba, Ahmerrad
  ]),
  
  
  
  ("initialize_faction_troop_types",
    [
      
      (try_for_range, ":faction_no", npc_kingdoms_begin, kingdoms_end),
        (faction_get_slot, ":culture", ":faction_no", slot_faction_culture),
        
        (faction_get_slot, ":troop", ":culture",  slot_faction_tier_1_troop),
        (faction_set_slot, ":faction_no",  slot_faction_tier_1_troop, ":troop"),
        (faction_get_slot, ":troop", ":culture",  slot_faction_tier_2_troop),
        (faction_set_slot, ":faction_no",  slot_faction_tier_2_troop, ":troop"),
        (faction_get_slot, ":troop", ":culture",  slot_faction_tier_3_troop),
        (faction_set_slot, ":faction_no",  slot_faction_tier_3_troop, ":troop"),
        (faction_get_slot, ":troop", ":culture",  slot_faction_tier_4_troop),
        (faction_set_slot, ":faction_no",  slot_faction_tier_4_troop, ":troop"),
        (faction_get_slot, ":troop", ":culture",  slot_faction_tier_5_troop),
        (faction_set_slot, ":faction_no",  slot_faction_tier_5_troop, ":troop"),
        
        (try_begin),
          (eq, ":faction_no", "fac_kingdom_4"),  #special treatment Friese
          #   (faction_set_slot, ":faction_no",  slot_faction_tier_1_troop, "trp_frisian_basic"), #testing, basic frisian troop for Friese
          
          (faction_set_slot, ":faction_no", slot_faction_deserter_troop, "trp_norse_deserter"),
          (faction_set_slot, ":faction_no", slot_faction_guard_troop, "trp_norse_level1_landed"),
          (faction_set_slot, ":faction_no", slot_faction_messenger_troop, "trp_norse_messenger"),
          (faction_set_slot, ":faction_no", slot_faction_prison_guard_troop, "trp_norse_prison_guard"),
          (faction_set_slot, ":faction_no", slot_faction_castle_guard_troop, "trp_norse_castle_guard"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_a, "pt_kingdom_4_reinforcements_a"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_b, "pt_kingdom_4_reinforcements_b"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_c, "pt_kingdom_4_reinforcements_c"),
        (else_try),
          (eq, ":faction_no", "fac_kingdom_8"),  #special treatment Northanhymbre
          #    (faction_set_slot, ":faction_no",  slot_faction_tier_1_troop, "trp_angle_slave"), #testing, basic angle troop for Northanhymbre
          
          (faction_set_slot, ":faction_no", slot_faction_deserter_troop, "trp_norse_deserter"),
          (faction_set_slot, ":faction_no", slot_faction_guard_troop, "trp_norse_level1_landed"),
          (faction_set_slot, ":faction_no", slot_faction_messenger_troop, "trp_norse_messenger"),
          (faction_set_slot, ":faction_no", slot_faction_prison_guard_troop, "trp_norse_prison_guard"),
          (faction_set_slot, ":faction_no", slot_faction_castle_guard_troop, "trp_norse_castle_guard"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_a, "pt_kingdom_8_reinforcements_a"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_b, "pt_kingdom_8_reinforcements_b"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_c, "pt_kingdom_8_reinforcements_c"),
        (else_try),
          (eq, ":faction_no", "fac_kingdom_21"),  #special treatment Osrige
          (faction_set_slot, ":faction_no", slot_faction_deserter_troop, "trp_irish_deserter"),
          (faction_set_slot, ":faction_no", slot_faction_guard_troop, "trp_irish_level1_landed"),
          (faction_set_slot, ":faction_no", slot_faction_messenger_troop, "trp_irish_messenger"),
          (faction_set_slot, ":faction_no", slot_faction_prison_guard_troop, "trp_irish_prison_guard"),
          (faction_set_slot, ":faction_no", slot_faction_castle_guard_troop, "trp_irish_castle_guard"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_a, "pt_kingdom_21_reinforcements_a"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_b, "pt_kingdom_21_reinforcements_b"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_c, "pt_kingdom_21_reinforcements_c"),
        (else_try),
          
          (faction_slot_eq, ":faction_no", slot_faction_culture, "fac_culture_norse"),
          (faction_set_slot, ":faction_no", slot_faction_deserter_troop, "trp_norse_deserter"),
          (faction_set_slot, ":faction_no", slot_faction_guard_troop, "trp_norse_level1_landed"),
          (faction_set_slot, ":faction_no", slot_faction_messenger_troop, "trp_norse_messenger"),
          (faction_set_slot, ":faction_no", slot_faction_prison_guard_troop, "trp_norse_prison_guard"),
          (faction_set_slot, ":faction_no", slot_faction_castle_guard_troop, "trp_norse_castle_guard"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_a, "pt_norse_reinforcements_a"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_b, "pt_norse_reinforcements_b"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_c, "pt_norse_reinforcements_c"),
          
        (else_try),
          (faction_slot_eq, ":faction_no", slot_faction_culture, "fac_culture_saxon"),
          (faction_set_slot, ":faction_no", slot_faction_deserter_troop, "trp_saxon_deserter"),
          (faction_set_slot, ":faction_no", slot_faction_guard_troop, "trp_saxon_level1_landed"),
          (faction_set_slot, ":faction_no", slot_faction_messenger_troop, "trp_saxon_messenger"),
          (faction_set_slot, ":faction_no", slot_faction_prison_guard_troop, "trp_saxon_prison_guard"),
          (faction_set_slot, ":faction_no", slot_faction_castle_guard_troop, "trp_saxon_castle_guard"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_a, "pt_saxon_reinforcements_a"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_b, "pt_saxon_reinforcements_b"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_c, "pt_saxon_reinforcements_c"),
          
        (else_try),
          (faction_slot_eq, ":faction_no", slot_faction_culture, "fac_culture_angle"),
          (faction_set_slot, ":faction_no", slot_faction_deserter_troop, "trp_angle_deserter"),
          (faction_set_slot, ":faction_no", slot_faction_guard_troop, "trp_angle_level1_landed"),
          (faction_set_slot, ":faction_no", slot_faction_messenger_troop, "trp_angle_messenger"),
          (faction_set_slot, ":faction_no", slot_faction_prison_guard_troop, "trp_angle_prison_guard"),
          (faction_set_slot, ":faction_no", slot_faction_castle_guard_troop, "trp_angle_castle_guard"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_a, "pt_angle_reinforcements_a"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_b, "pt_angle_reinforcements_b"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_c, "pt_angle_reinforcements_c"),
          
        (else_try),
          (faction_slot_eq, ":faction_no", slot_faction_culture, "fac_culture_welsh"),
          (faction_set_slot, ":faction_no",  slot_faction_deserter_troop, "trp_briton_deserter"),
          (faction_set_slot, ":faction_no", slot_faction_guard_troop, "trp_briton_level1_landed"),
          (faction_set_slot, ":faction_no",  slot_faction_messenger_troop, "trp_briton_messenger"),
          (faction_set_slot, ":faction_no",  slot_faction_prison_guard_troop, "trp_briton_prison_guard"),
          (faction_set_slot, ":faction_no",  slot_faction_castle_guard_troop, "trp_briton_castle_guard"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_a, "pt_welsh_reinforcements_a"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_b, "pt_welsh_reinforcements_b"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_c, "pt_welsh_reinforcements_c"),
          
        (else_try),
          (faction_slot_eq, ":faction_no", slot_faction_culture, "fac_culture_irish"),
          (faction_set_slot, ":faction_no", slot_faction_deserter_troop, "trp_irish_deserter"),
          (faction_set_slot, ":faction_no", slot_faction_guard_troop, "trp_irish_level1_landed"),
          (faction_set_slot, ":faction_no", slot_faction_messenger_troop, "trp_irish_messenger"),
          (faction_set_slot, ":faction_no", slot_faction_prison_guard_troop, "trp_irish_prison_guard"),
          (faction_set_slot, ":faction_no", slot_faction_castle_guard_troop, "trp_irish_castle_guard"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_a, "pt_irish_reinforcements_a"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_b, "pt_irish_reinforcements_b"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_c, "pt_irish_reinforcements_c"),
          
        (else_try),
          (faction_slot_eq, ":faction_no", slot_faction_culture, "fac_culture_scotch"),
          (faction_set_slot, ":faction_no", slot_faction_deserter_troop, "trp_scotch_deserter"),
          (faction_set_slot, ":faction_no", slot_faction_guard_troop, "trp_scotch_level1_landed"),
          (faction_set_slot, ":faction_no", slot_faction_messenger_troop, "trp_scotch_messenger"),
          (faction_set_slot, ":faction_no", slot_faction_prison_guard_troop, "trp_scotch_prison_guard"),
          (faction_set_slot, ":faction_no", slot_faction_castle_guard_troop, "trp_scotch_castle_guard"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_a, "pt_scotch_reinforcements_a"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_b, "pt_scotch_reinforcements_b"),
          (faction_set_slot, ":faction_no",  slot_faction_reinforcements_c, "pt_scotch_reinforcements_c"),
        (try_end),
      (try_end),
  ]),
  #chief cambia varios
  ("initialize_item_info",
    [
      # Setting food bonuses - these have been changed to incentivize using historical rations. Bread is the most cost-efficient
      #Staples
      (item_set_slot, "itm_bread", slot_item_food_bonus, 4), #brought up from 4
      (item_set_slot, "itm_grain", slot_item_food_bonus, 3), #new - can be boiled as porridge
      
      #Fat sources - preserved
      (item_set_slot, "itm_smoked_fish", slot_item_food_bonus, 5),
      (item_set_slot, "itm_dried_meat", slot_item_food_bonus, 5),
      (item_set_slot, "itm_cheese", slot_item_food_bonus, 7),
      (item_set_slot, "itm_sausages", slot_item_food_bonus, 7),
      (item_set_slot, "itm_butter", slot_item_food_bonus, 6), #brought down from 8
      
      #Fat sources - perishable
      (item_set_slot, "itm_chicken", slot_item_food_bonus, 6), #brought up from 7
      (item_set_slot, "itm_cattle_meat", slot_item_food_bonus, 7), #brought down from 7
      (item_set_slot, "itm_pork", slot_item_food_bonus, 6), #brought down from 6
      
      #Produce
      (item_set_slot, "itm_venison", slot_item_food_bonus, 8),
      (item_set_slot, "itm_cabbages", slot_item_food_bonus, 5),
      (item_set_slot, "itm_barley", slot_item_food_bonus, 5),
      (item_set_slot, "itm_apples", slot_item_food_bonus, 4), #brought down from 5
      
      #Sweet items
      (item_set_slot, "itm_mead", slot_item_food_bonus, 9), #brought down from 8
      (item_set_slot, "itm_vc_honey", slot_item_food_bonus, 7), #brought down from 12
      
      (item_set_slot, "itm_wine", slot_item_food_bonus, 10),
      (item_set_slot, "itm_ale", slot_item_food_bonus, 8),
      
      #Item economic settings
      (item_set_slot, "itm_grain", slot_item_urban_demand, 20),
      (item_set_slot, "itm_grain", slot_item_rural_demand, 20),
      (item_set_slot, "itm_grain", slot_item_desert_demand, 20),
      (item_set_slot, "itm_grain", slot_item_production_slot, slot_center_acres_grain),
      (item_set_slot, "itm_grain", slot_item_production_string, "str_acres_grain"),
      (item_set_slot, "itm_grain", slot_item_base_price, 20),
      
      (item_set_slot, "itm_bread", slot_item_urban_demand, 30),
      (item_set_slot, "itm_bread", slot_item_rural_demand, 30),
      (item_set_slot, "itm_bread", slot_item_desert_demand, 30),
      (item_set_slot, "itm_bread", slot_item_production_slot, slot_center_mills),
      (item_set_slot, "itm_bread", slot_item_production_string, "str_mills"),
      (item_set_slot, "itm_bread", slot_item_primary_raw_material, "itm_grain"),
      (item_set_slot, "itm_bread", slot_item_input_number, 1),
      (item_set_slot, "itm_bread", slot_item_output_per_run, 17),
      (item_set_slot, "itm_bread", slot_item_overhead_per_run, 30),
      (item_set_slot, "itm_bread", slot_item_base_price, 20),
      (item_set_slot, "itm_bread", slot_item_enterprise_building_cost, 7000),
      
      (item_set_slot, "itm_ale", slot_item_urban_demand, 10),
      (item_set_slot, "itm_ale", slot_item_rural_demand, 15),
      (item_set_slot, "itm_ale", slot_item_desert_demand, 0),
      (item_set_slot, "itm_ale", slot_item_production_slot, slot_center_breweries),
      (item_set_slot, "itm_ale", slot_item_production_string, "str_breweries"),
      (item_set_slot, "itm_ale", slot_item_primary_raw_material, "itm_grain"),
      (item_set_slot, "itm_ale", slot_item_secondary_raw_material, "itm_barley"),
      (item_set_slot, "itm_ale", slot_item_input_number, 2),
      (item_set_slot, "itm_ale", slot_item_output_per_run, 3),
      (item_set_slot, "itm_ale", slot_item_overhead_per_run, 50),
      (item_set_slot, "itm_ale", slot_item_base_price, 280),
      (item_set_slot, "itm_ale", slot_item_enterprise_building_cost, 14500),
      
      (item_set_slot, "itm_wine", slot_item_urban_demand, 5),
      (item_set_slot, "itm_wine", slot_item_rural_demand, 1),
      (item_set_slot, "itm_wine", slot_item_desert_demand, -1),
      #      (item_set_slot, "itm_wine", slot_item_production_slot, slot_center_wine_presses),
      (item_set_slot, "itm_wine", slot_item_production_string, "str_caravans"),
      #(item_set_slot, "itm_wine", slot_item_primary_raw_material, "itm_barley"),
      (item_set_slot, "itm_wine", slot_item_input_number, 1),
      (item_set_slot, "itm_wine", slot_item_output_per_run, 2),
      (item_set_slot, "itm_wine", slot_item_overhead_per_run, 70),
      (item_set_slot, "itm_wine", slot_item_base_price, 420),
      (item_set_slot, "itm_wine", slot_item_enterprise_building_cost, 26000),
      
      # (item_set_slot, "itm_mead", slot_item_urban_demand, 5),
      # (item_set_slot, "itm_mead", slot_item_rural_demand, 5),
      # (item_set_slot, "itm_mead", slot_item_desert_demand, 0),
      # (item_set_slot, "itm_mead", slot_item_production_slot, slot_center_breweries),
      # (item_set_slot, "itm_mead", slot_item_production_string, "str_presses"),
      # (item_set_slot, "itm_mead", slot_item_base_price, 120),
      # (item_set_slot, "itm_mead", slot_item_primary_raw_material, "itm_vc_honey"),
      # (item_set_slot, "itm_mead", slot_item_input_number, 1), #chief cambia
      # (item_set_slot, "itm_mead", slot_item_output_per_run, 2),
      # (item_set_slot, "itm_mead", slot_item_overhead_per_run, 50), #chief cambiA
      # (item_set_slot, "itm_mead", slot_item_base_price, 330), #chief cambia
      # (item_set_slot, "itm_mead", slot_item_enterprise_building_cost, 20000), #chief cambia
      
      (item_set_slot, "itm_barley", slot_item_urban_demand, 10),
      (item_set_slot, "itm_barley", slot_item_rural_demand, 15),
      (item_set_slot, "itm_barley", slot_item_desert_demand, 5),
      (item_set_slot, "itm_grain", slot_item_production_slot, slot_center_acres_grain),
      (item_set_slot, "itm_barley", slot_item_production_slot, slot_center_acres_vineyard),
      #     (item_set_slot, "itm_barley", slot_item_production_string, "str_acres_grain"),
      #     (item_set_slot, "itm_barley", slot_item_is_raw_material_only_for, "itm_wine"),
      (item_set_slot, "itm_barley", slot_item_base_price, 18),
      
      (item_set_slot, "itm_apples", slot_item_urban_demand, 4),
      (item_set_slot, "itm_apples", slot_item_rural_demand, 4),
      (item_set_slot, "itm_apples", slot_item_desert_demand, 0),
      (item_set_slot, "itm_apples", slot_item_production_slot, slot_center_acres_vineyard),
      (item_set_slot, "itm_apples", slot_item_production_string, "str_acres_orchard"),
      (item_set_slot, "itm_apples", slot_item_base_price, 80),
      
      (item_set_slot, "itm_smoked_fish", slot_item_urban_demand, 16),
      (item_set_slot, "itm_smoked_fish", slot_item_rural_demand, 16),
      (item_set_slot, "itm_smoked_fish", slot_item_desert_demand, 16),
      (item_set_slot, "itm_smoked_fish", slot_item_production_slot, slot_center_fishing_fleet),
      (item_set_slot, "itm_smoked_fish", slot_item_production_string, "str_boats"),
      
      (item_set_slot, "itm_salt", slot_item_urban_demand, 5),
      (item_set_slot, "itm_salt", slot_item_rural_demand, 3),
      (item_set_slot, "itm_salt", slot_item_desert_demand, -1),
      (item_set_slot, "itm_salt", slot_item_production_slot, slot_center_salt_pans),
      (item_set_slot, "itm_salt", slot_item_production_string, "str_pans"),
      
      (item_set_slot, "itm_dried_meat", slot_item_urban_demand, 20),
      (item_set_slot, "itm_dried_meat", slot_item_rural_demand, 5),
      (item_set_slot, "itm_dried_meat", slot_item_desert_demand, -1),
      (item_set_slot, "itm_dried_meat", slot_item_production_slot, slot_center_head_cattle),
      (item_set_slot, "itm_dried_meat", slot_item_production_string, "str_head_cattle"),
      
      (item_set_slot, "itm_cattle_meat", slot_item_urban_demand, 12),
      (item_set_slot, "itm_cattle_meat", slot_item_rural_demand, 3),
      (item_set_slot, "itm_cattle_meat", slot_item_desert_demand, -1),
      (item_set_slot, "itm_cattle_meat", slot_item_production_slot, slot_center_head_cattle),
      (item_set_slot, "itm_cattle_meat", slot_item_production_string, "str_head_cattle"),
      
      (item_set_slot, "itm_cheese", slot_item_urban_demand, 10),
      (item_set_slot, "itm_cheese", slot_item_rural_demand, 10),
      (item_set_slot, "itm_cheese", slot_item_desert_demand, 10),
      (item_set_slot, "itm_cheese", slot_item_production_slot, slot_center_head_cattle),
      (item_set_slot, "itm_cheese", slot_item_production_string, "str_head_cattle"),
      
      (item_set_slot, "itm_butter", slot_item_urban_demand, 2),
      (item_set_slot, "itm_butter", slot_item_rural_demand, 2),
      (item_set_slot, "itm_butter", slot_item_desert_demand, 2),
      (item_set_slot, "itm_butter", slot_item_production_slot, slot_center_head_cattle),
      (item_set_slot, "itm_butter", slot_item_production_string, "str_head_cattle"),
      
      (item_set_slot, "itm_timber", slot_item_urban_demand, 5),
      (item_set_slot, "itm_timber", slot_item_rural_demand, 0),
      (item_set_slot, "itm_timber", slot_item_desert_demand, 0),
      (item_set_slot, "itm_timber", slot_item_production_slot, slot_center_forest),
      (item_set_slot, "itm_timber", slot_item_production_string, "str_tanneries"),
      #(item_set_slot, "itm_timber", slot_item_primary_raw_material, "itm_vc_raw_leather"),
      (item_set_slot, "itm_timber", slot_item_input_number, 3),
      (item_set_slot, "itm_timber", slot_item_output_per_run, 3),
      (item_set_slot, "itm_timber", slot_item_overhead_per_run, 50),
      (item_set_slot, "itm_timber", slot_item_base_price, 400),
      (item_set_slot, "itm_timber", slot_item_enterprise_building_cost, 20000),
      
      (item_set_slot, "itm_vc_raw_leather", slot_item_urban_demand, 5),
      (item_set_slot, "itm_vc_raw_leather", slot_item_rural_demand, 3),
      (item_set_slot, "itm_vc_raw_leather", slot_item_desert_demand, 0),
      (item_set_slot, "itm_vc_raw_leather", slot_item_production_slot, slot_center_head_cattle),
      (item_set_slot, "itm_vc_raw_leather", slot_item_production_string, "str_head_cattle"),
      #(item_set_slot, "itm_vc_raw_leather", slot_item_is_raw_material_only_for, "itm_timber"),
      (item_set_slot, "itm_vc_raw_leather", slot_item_base_price, 320),
      
      (item_set_slot, "itm_sausages", slot_item_urban_demand, 12),
      (item_set_slot, "itm_sausages", slot_item_rural_demand, 3),
      (item_set_slot, "itm_sausages", slot_item_desert_demand, -1),
      (item_set_slot, "itm_sausages", slot_item_production_slot, slot_center_head_sheep),
      (item_set_slot, "itm_sausages", slot_item_production_string, "str_head_sheep"),
      
      (item_set_slot, "itm_wool", slot_item_urban_demand, 0),
      (item_set_slot, "itm_wool", slot_item_rural_demand, 0),
      (item_set_slot, "itm_wool", slot_item_desert_demand, 0),
      (item_set_slot, "itm_wool", slot_item_production_slot, slot_center_head_sheep),
      (item_set_slot, "itm_wool", slot_item_production_string, "str_head_sheep"),
      (item_set_slot, "itm_wool", slot_item_is_raw_material_only_for, "itm_vc_wool_cloth"),
      (item_set_slot, "itm_wool", slot_item_base_price,230),
      
      (item_set_slot, "itm_vc_wool_cloth", slot_item_urban_demand, 15),
      (item_set_slot, "itm_vc_wool_cloth", slot_item_rural_demand, 20),
      (item_set_slot, "itm_vc_wool_cloth", slot_item_desert_demand, 5),
      (item_set_slot, "itm_vc_wool_cloth", slot_item_production_slot, slot_center_wool_looms),
      (item_set_slot, "itm_vc_wool_cloth", slot_item_production_string, "str_looms"),
      (item_set_slot, "itm_vc_wool_cloth", slot_item_primary_raw_material, "itm_wool"),
      (item_set_slot, "itm_vc_wool_cloth", slot_item_input_number, 2),
      (item_set_slot, "itm_vc_wool_cloth", slot_item_output_per_run, 5),
      (item_set_slot, "itm_vc_wool_cloth", slot_item_overhead_per_run, 120),
      (item_set_slot, "itm_vc_wool_cloth", slot_item_base_price, 300),
      (item_set_slot, "itm_vc_wool_cloth", slot_item_enterprise_building_cost, 16000),
      
      (item_set_slot, "itm_raw_flax", slot_item_urban_demand, 0),
      (item_set_slot, "itm_raw_flax", slot_item_rural_demand, 0),
      (item_set_slot, "itm_raw_flax", slot_item_desert_demand, 0),
      (item_set_slot, "itm_raw_flax", slot_item_production_slot, slot_center_acres_flax),
      (item_set_slot, "itm_raw_flax", slot_item_production_string, "str_acres_flax"),
      (item_set_slot, "itm_raw_flax", slot_item_is_raw_material_only_for, "itm_linen"),
      (item_set_slot, "itm_raw_flax", slot_item_base_price, 280),
      
      (item_set_slot, "itm_linen", slot_item_urban_demand, 7),
      (item_set_slot, "itm_linen", slot_item_rural_demand, 3),
      (item_set_slot, "itm_linen", slot_item_desert_demand, 15),
      (item_set_slot, "itm_linen", slot_item_production_slot, slot_center_linen_looms),
      (item_set_slot, "itm_linen", slot_item_production_string, "str_looms"),
      (item_set_slot, "itm_linen", slot_item_primary_raw_material, "itm_raw_flax"),
      (item_set_slot, "itm_linen", slot_item_input_number, 2),
      (item_set_slot, "itm_linen", slot_item_output_per_run, 3),
      (item_set_slot, "itm_linen", slot_item_overhead_per_run, 120),
      (item_set_slot, "itm_linen", slot_item_base_price, 365),
      (item_set_slot, "itm_linen", slot_item_enterprise_building_cost, 18000),
      
      (item_set_slot, "itm_iron", slot_item_urban_demand, 0),
      (item_set_slot, "itm_iron", slot_item_rural_demand, 0),
      (item_set_slot, "itm_iron", slot_item_desert_demand, 0),
      (item_set_slot, "itm_iron", slot_item_production_slot, slot_center_iron_deposits),
      (item_set_slot, "itm_iron", slot_item_production_string, "str_deposits"),
      (item_set_slot, "itm_iron", slot_item_is_raw_material_only_for, "itm_tools"),
      (item_set_slot, "itm_iron", slot_item_base_price, 304),
      
      #stone
      (item_set_slot, "itm_stone", slot_item_urban_demand, 0),
      (item_set_slot, "itm_stone", slot_item_rural_demand, 0),
      (item_set_slot, "itm_stone", slot_item_desert_demand, 0),
      (item_set_slot, "itm_stone", slot_item_production_slot, slot_center_iron_deposits),
      (item_set_slot, "itm_stone", slot_item_production_string, "str_deposits"),
      
      (item_set_slot, "itm_tools", slot_item_urban_demand, 7),
      (item_set_slot, "itm_tools", slot_item_rural_demand, 7),
      (item_set_slot, "itm_tools", slot_item_desert_demand, 7),
      (item_set_slot, "itm_tools", slot_item_production_slot, slot_center_smithies),
      (item_set_slot, "itm_tools", slot_item_production_string, "str_smithies"),
      (item_set_slot, "itm_tools", slot_item_primary_raw_material, "itm_iron"),
      (item_set_slot, "itm_tools", slot_item_input_number, 2),
      (item_set_slot, "itm_tools", slot_item_output_per_run, 4),
      (item_set_slot, "itm_tools", slot_item_overhead_per_run, 60),
      (item_set_slot, "itm_tools", slot_item_base_price, 410),
      (item_set_slot, "itm_tools", slot_item_enterprise_building_cost, 17500),
      
      (item_set_slot, "itm_jewelry", slot_item_urban_demand, 3),	#luxury
      (item_set_slot, "itm_jewelry", slot_item_rural_demand, -1),
      (item_set_slot, "itm_jewelry", slot_item_desert_demand, -1),
      (item_set_slot, "itm_jewelry", slot_item_production_slot, slot_center_pottery_kilns),
      (item_set_slot, "itm_jewelry", slot_item_production_string, "str_kilns"),
      
      (item_set_slot, "itm_tar", slot_item_urban_demand, 10),
      (item_set_slot, "itm_tar", slot_item_rural_demand, 5),
      (item_set_slot, "itm_tar", slot_item_desert_demand, -1),
      (item_set_slot, "itm_tar", slot_item_production_slot, slot_center_tar_ovens),
      (item_set_slot, "itm_tar", slot_item_production_string, "str_presses"),
      (item_set_slot, "itm_tar", slot_item_primary_raw_material, "itm_venison"),
      (item_set_slot, "itm_tar", slot_item_input_number, 6),
      (item_set_slot, "itm_tar", slot_item_output_per_run, 2),
      (item_set_slot, "itm_tar", slot_item_overhead_per_run, 80),
      (item_set_slot, "itm_tar", slot_item_base_price, 650),
      (item_set_slot, "itm_tar", slot_item_enterprise_building_cost, 24500),
      
      (item_set_slot, "itm_venison", slot_item_urban_demand, 12),
      (item_set_slot, "itm_venison", slot_item_rural_demand, 3),
      (item_set_slot, "itm_venison", slot_item_desert_demand, 0),
      (item_set_slot, "itm_venison", slot_item_production_slot, slot_center_acres_hunting),
      (item_set_slot, "itm_venison", slot_item_production_string, "str_hunting_forests"),
      #(item_set_slot, "itm_venison", slot_item_is_raw_material_only_for, "itm_tar"),
      (item_set_slot, "itm_venison", slot_item_base_price, 200),
      
      (item_set_slot, "itm_silver", slot_item_urban_demand, 7),	#luxury
      (item_set_slot, "itm_silver", slot_item_rural_demand, 0),
      (item_set_slot, "itm_silver", slot_item_desert_demand, -1),
      (item_set_slot, "itm_silver", slot_item_production_slot, slot_center_silver_deposits),
      (item_set_slot, "itm_silver", slot_item_production_string, "str_looms"),
      #(item_set_slot, "itm_silver", slot_item_primary_raw_material, "itm_wool"),
      (item_set_slot, "itm_silver", slot_item_input_number, 2),
      (item_set_slot, "itm_silver", slot_item_output_per_run, 2),
      (item_set_slot, "itm_silver", slot_item_overhead_per_run, 160),
      (item_set_slot, "itm_silver", slot_item_base_price, 725),
      #(item_set_slot, "itm_silver", slot_item_secondary_raw_material, "itm_ivory"),
      (item_set_slot, "itm_silver", slot_item_enterprise_building_cost, 33000),
      
      (item_set_slot, "itm_amber", slot_item_urban_demand, 6),	#luxury
      (item_set_slot, "itm_amber", slot_item_rural_demand, 0),
      (item_set_slot, "itm_amber", slot_item_production_slot, slot_center_amber_deposits),
      (item_set_slot, "itm_amber", slot_item_production_string, "str_amber_riverbeds"),
      ##      (item_set_slot, "itm_amber", slot_item_is_raw_material_only_for, "itm_deposits"),
      (item_set_slot, "itm_amber", slot_item_base_price, 600),
      
      (item_set_slot, "itm_ivory", slot_item_urban_demand, 6),	#luxury
      (item_set_slot, "itm_ivory", slot_item_rural_demand, 0),
      (item_set_slot, "itm_ivory", slot_item_desert_demand, -1),
      (item_set_slot, "itm_ivory", slot_item_production_slot, slot_center_walrus_fleet),	#eco_note
      (item_set_slot, "itm_ivory", slot_item_production_string, "str_caravans"),
      (item_set_slot, "itm_ivory", slot_item_base_price, 400),
      
      (item_set_slot, "itm_mead", slot_item_urban_demand, 15),
      (item_set_slot, "itm_mead", slot_item_rural_demand, 10),
      (item_set_slot, "itm_mead", slot_item_desert_demand, 5),
      (item_set_slot, "itm_mead", slot_item_production_slot, slot_center_wine_presses),
      (item_set_slot, "itm_mead", slot_item_production_string, "str_presses"),
      (item_set_slot, "itm_mead", slot_item_primary_raw_material, "itm_vc_honey"),
      
      (item_set_slot, "itm_vc_furs", slot_item_urban_demand, 13),
      (item_set_slot, "itm_vc_furs", slot_item_rural_demand, 0),
      (item_set_slot, "itm_vc_furs", slot_item_desert_demand, -1),
      (item_set_slot, "itm_vc_furs", slot_item_production_slot, slot_center_fur_traps),
      (item_set_slot, "itm_vc_furs", slot_item_production_string, "str_traps"),
      
      (item_set_slot, "itm_vc_honey", slot_item_urban_demand, 5),	#was 12 but it is needed for mead now...
      (item_set_slot, "itm_vc_honey", slot_item_rural_demand, 3),
      (item_set_slot, "itm_vc_honey", slot_item_desert_demand, -1),
      (item_set_slot, "itm_vc_honey", slot_item_production_slot, slot_center_apiaries),
      (item_set_slot, "itm_vc_honey", slot_item_production_string, "str_hives"),
      
      (item_set_slot, "itm_cabbages", slot_item_urban_demand, 7),
      (item_set_slot, "itm_cabbages", slot_item_rural_demand, 7),
      (item_set_slot, "itm_cabbages", slot_item_desert_demand, 7),
      (item_set_slot, "itm_cabbages", slot_item_production_slot, slot_center_household_gardens),
      (item_set_slot, "itm_cabbages", slot_item_production_string, "str_gardens"),
      
      (item_set_slot, "itm_soapstone", slot_item_urban_demand, 3),	#luxury???
      (item_set_slot, "itm_soapstone", slot_item_rural_demand, 3),
      (item_set_slot, "itm_soapstone", slot_item_desert_demand, 0),
      (item_set_slot, "itm_soapstone", slot_item_production_slot, slot_center_soapstone_depositis),
      (item_set_slot, "itm_soapstone", slot_item_production_string, "str_deposits"),
      
      (item_set_slot, "itm_chicken", slot_item_urban_demand, 40),
      (item_set_slot, "itm_chicken", slot_item_rural_demand, 10),
      (item_set_slot, "itm_chicken", slot_item_desert_demand, -1),
      
      (item_set_slot, "itm_pork", slot_item_urban_demand, 40),
      (item_set_slot, "itm_pork", slot_item_rural_demand, 10),
      (item_set_slot, "itm_pork", slot_item_desert_demand, -1),
      
      # Setting book intelligence requirements
      (item_set_slot, "itm_book_tactics", slot_item_intelligence_requirement, 9),
      (item_set_slot, "itm_book_persuasion", slot_item_intelligence_requirement, 8),
      (item_set_slot, "itm_book_leadership", slot_item_intelligence_requirement, 7),
      (item_set_slot, "itm_book_intelligence", slot_item_intelligence_requirement, 10),
      (item_set_slot, "itm_book_trade", slot_item_intelligence_requirement, 11),
      (item_set_slot, "itm_book_weapon_mastery", slot_item_intelligence_requirement, 9),
      (item_set_slot, "itm_book_engineering", slot_item_intelligence_requirement, 12),
      
      (item_set_slot, "itm_book_wound_treatment_reference", slot_item_intelligence_requirement, 10),
      (item_set_slot, "itm_book_training_reference", slot_item_intelligence_requirement, 10),
      (item_set_slot, "itm_book_surgery_reference", slot_item_intelligence_requirement, 10),
  ]),
  
  
  ("initialize_town_arena_info",
    [
      (try_for_range, ":town_no", towns_begin, towns_end),
        (party_set_slot, ":town_no", slot_town_tournament_max_teams, 4),
        (party_set_slot, ":town_no", slot_town_tournament_max_team_size, 8),
      (try_end),
      (party_set_slot, "p_town_6", slot_town_tournament_max_team_size, 2),
      
      (party_set_slot,"p_town_1", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_1", slot_town_arena_melee_1_team_size,   1),
      (party_set_slot,"p_town_1", slot_town_arena_melee_2_num_teams,   4),
      (party_set_slot,"p_town_1", slot_town_arena_melee_2_team_size,   1),
      (party_set_slot,"p_town_1", slot_town_arena_melee_3_num_teams,   4),
      (party_set_slot,"p_town_1", slot_town_arena_melee_3_team_size,   1),
      
      (party_set_slot,"p_town_2", slot_town_arena_melee_1_num_teams,   4),
      (party_set_slot,"p_town_2", slot_town_arena_melee_1_team_size,   4),
      (party_set_slot,"p_town_2", slot_town_arena_melee_2_num_teams,   4),
      (party_set_slot,"p_town_2", slot_town_arena_melee_2_team_size,   6),
      (party_set_slot,"p_town_2", slot_town_arena_melee_3_num_teams,   4),
      (party_set_slot,"p_town_2", slot_town_arena_melee_3_team_size,   8),
      
      (party_set_slot,"p_town_3", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_3", slot_town_arena_melee_1_team_size,   8),
      (party_set_slot,"p_town_3", slot_town_arena_melee_2_num_teams,   2),
      (party_set_slot,"p_town_3", slot_town_arena_melee_2_team_size,   8),
      (party_set_slot,"p_town_3", slot_town_arena_melee_3_num_teams,   2),
      (party_set_slot,"p_town_3", slot_town_arena_melee_3_team_size,   8),
      
      (party_set_slot,"p_town_4", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_4", slot_town_arena_melee_1_team_size,   8),
      (party_set_slot,"p_town_4", slot_town_arena_melee_2_num_teams,   3),
      (party_set_slot,"p_town_4", slot_town_arena_melee_2_team_size,   8),
      (party_set_slot,"p_town_4", slot_town_arena_melee_3_num_teams,   2),
      (party_set_slot,"p_town_4", slot_town_arena_melee_3_team_size,   5),
      
      (party_set_slot,"p_town_5", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_5", slot_town_arena_melee_1_team_size,   3),
      (party_set_slot,"p_town_5", slot_town_arena_melee_2_num_teams,   2),
      (party_set_slot,"p_town_5", slot_town_arena_melee_2_team_size,   5),
      (party_set_slot,"p_town_5", slot_town_arena_melee_3_num_teams,   2),
      (party_set_slot,"p_town_5", slot_town_arena_melee_3_team_size,   8),
      
      (party_set_slot,"p_town_6", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_6", slot_town_arena_melee_1_team_size,   4),
      (party_set_slot,"p_town_6", slot_town_arena_melee_2_num_teams,   3),
      (party_set_slot,"p_town_6", slot_town_arena_melee_2_team_size,   4),
      (party_set_slot,"p_town_6", slot_town_arena_melee_3_num_teams,   3),
      (party_set_slot,"p_town_6", slot_town_arena_melee_3_team_size,   6),
      
      (party_set_slot,"p_town_7", slot_town_arena_melee_1_num_teams,   4),
      (party_set_slot,"p_town_7", slot_town_arena_melee_1_team_size,   4),
      (party_set_slot,"p_town_7", slot_town_arena_melee_2_num_teams,   4),
      (party_set_slot,"p_town_7", slot_town_arena_melee_2_team_size,   6),
      (party_set_slot,"p_town_7", slot_town_arena_melee_3_num_teams,   4),
      (party_set_slot,"p_town_7", slot_town_arena_melee_3_team_size,   8),
      
      (party_set_slot,"p_town_8", slot_town_arena_melee_1_num_teams,   3),
      (party_set_slot,"p_town_8", slot_town_arena_melee_1_team_size,   1),
      (party_set_slot,"p_town_8", slot_town_arena_melee_2_num_teams,   3),
      (party_set_slot,"p_town_8", slot_town_arena_melee_2_team_size,   3),
      (party_set_slot,"p_town_8", slot_town_arena_melee_3_num_teams,   3),
      (party_set_slot,"p_town_8", slot_town_arena_melee_3_team_size,   7),
      
      (party_set_slot,"p_town_9", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_9", slot_town_arena_melee_1_team_size,   2),
      (party_set_slot,"p_town_9", slot_town_arena_melee_2_num_teams,   2),
      (party_set_slot,"p_town_9", slot_town_arena_melee_2_team_size,   5),
      (party_set_slot,"p_town_9", slot_town_arena_melee_3_num_teams,   2),
      (party_set_slot,"p_town_9", slot_town_arena_melee_3_team_size,   8),
      
      (party_set_slot,"p_town_10", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_10", slot_town_arena_melee_1_team_size,   3),
      (party_set_slot,"p_town_10", slot_town_arena_melee_2_num_teams,   2),
      (party_set_slot,"p_town_10", slot_town_arena_melee_2_team_size,   5),
      (party_set_slot,"p_town_10", slot_town_arena_melee_3_num_teams,   2),
      (party_set_slot,"p_town_10", slot_town_arena_melee_3_team_size,   8),
      
      (party_set_slot,"p_town_11", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_11", slot_town_arena_melee_1_team_size,   8),
      (party_set_slot,"p_town_11", slot_town_arena_melee_2_num_teams,   3),
      (party_set_slot,"p_town_11", slot_town_arena_melee_2_team_size,   4),
      (party_set_slot,"p_town_11", slot_town_arena_melee_3_num_teams,   3),
      (party_set_slot,"p_town_11", slot_town_arena_melee_3_team_size,   6),
      
      (party_set_slot,"p_town_12", slot_town_arena_melee_1_num_teams,   3),
      (party_set_slot,"p_town_12", slot_town_arena_melee_1_team_size,   8),
      (party_set_slot,"p_town_12", slot_town_arena_melee_2_num_teams,   4),
      (party_set_slot,"p_town_12", slot_town_arena_melee_2_team_size,   6),
      (party_set_slot,"p_town_12", slot_town_arena_melee_3_num_teams,   4),
      (party_set_slot,"p_town_12", slot_town_arena_melee_3_team_size,   5),
      
      (party_set_slot,"p_town_13", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_13", slot_town_arena_melee_1_team_size,   8),
      (party_set_slot,"p_town_13", slot_town_arena_melee_2_num_teams,   4),
      (party_set_slot,"p_town_13", slot_town_arena_melee_2_team_size,   5),
      (party_set_slot,"p_town_13", slot_town_arena_melee_3_num_teams,   4),
      (party_set_slot,"p_town_13", slot_town_arena_melee_3_team_size,   7),
      
      (party_set_slot,"p_town_14", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_14", slot_town_arena_melee_1_team_size,   4),
      (party_set_slot,"p_town_14", slot_town_arena_melee_2_num_teams,   2),
      (party_set_slot,"p_town_14", slot_town_arena_melee_2_team_size,   5),
      (party_set_slot,"p_town_14", slot_town_arena_melee_3_num_teams,   2),
      (party_set_slot,"p_town_14", slot_town_arena_melee_3_team_size,   6),
      
      (party_set_slot,"p_town_15", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_15", slot_town_arena_melee_1_team_size,   8),
      (party_set_slot,"p_town_15", slot_town_arena_melee_2_num_teams,   3),
      (party_set_slot,"p_town_15", slot_town_arena_melee_2_team_size,   4),
      (party_set_slot,"p_town_15", slot_town_arena_melee_3_num_teams,   3),
      (party_set_slot,"p_town_15", slot_town_arena_melee_3_team_size,   6),
      
      (party_set_slot,"p_town_16", slot_town_arena_melee_1_num_teams,   3),
      (party_set_slot,"p_town_16", slot_town_arena_melee_1_team_size,   8),
      (party_set_slot,"p_town_16", slot_town_arena_melee_2_num_teams,   4),
      (party_set_slot,"p_town_16", slot_town_arena_melee_2_team_size,   6),
      (party_set_slot,"p_town_16", slot_town_arena_melee_3_num_teams,   4),
      (party_set_slot,"p_town_16", slot_town_arena_melee_3_team_size,   5),
      
      (party_set_slot,"p_town_17", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_17", slot_town_arena_melee_1_team_size,   8),
      (party_set_slot,"p_town_17", slot_town_arena_melee_2_num_teams,   4),
      (party_set_slot,"p_town_17", slot_town_arena_melee_2_team_size,   5),
      (party_set_slot,"p_town_17", slot_town_arena_melee_3_num_teams,   4),
      (party_set_slot,"p_town_17", slot_town_arena_melee_3_team_size,   7),
      
      (party_set_slot,"p_town_18", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_18", slot_town_arena_melee_1_team_size,   4),
      (party_set_slot,"p_town_18", slot_town_arena_melee_2_num_teams,   2),
      (party_set_slot,"p_town_18", slot_town_arena_melee_2_team_size,   5),
      (party_set_slot,"p_town_18", slot_town_arena_melee_3_num_teams,   2),
      (party_set_slot,"p_town_18", slot_town_arena_melee_3_team_size,   6),
      
      (party_set_slot,"p_town_19", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_19", slot_town_arena_melee_1_team_size,   8),
      (party_set_slot,"p_town_19", slot_town_arena_melee_2_num_teams,   4),
      (party_set_slot,"p_town_19", slot_town_arena_melee_2_team_size,   4),
      (party_set_slot,"p_town_19", slot_town_arena_melee_3_num_teams,   4),
      (party_set_slot,"p_town_19", slot_town_arena_melee_3_team_size,   6),
      
      (party_set_slot,"p_town_20", slot_town_arena_melee_1_num_teams,   4),
      (party_set_slot,"p_town_20", slot_town_arena_melee_1_team_size,   2),
      (party_set_slot,"p_town_20", slot_town_arena_melee_2_num_teams,   4),
      (party_set_slot,"p_town_20", slot_town_arena_melee_2_team_size,   4),
      (party_set_slot,"p_town_20", slot_town_arena_melee_3_num_teams,   4),
      (party_set_slot,"p_town_20", slot_town_arena_melee_3_team_size,   6),
      
      (party_set_slot,"p_town_21", slot_town_arena_melee_1_num_teams,   3),
      (party_set_slot,"p_town_21", slot_town_arena_melee_1_team_size,   3),
      (party_set_slot,"p_town_21", slot_town_arena_melee_2_num_teams,   2),
      (party_set_slot,"p_town_21", slot_town_arena_melee_2_team_size,   6),
      (party_set_slot,"p_town_21", slot_town_arena_melee_3_num_teams,   2),
      (party_set_slot,"p_town_21", slot_town_arena_melee_3_team_size,   8),
      
      (party_set_slot,"p_town_22", slot_town_arena_melee_1_num_teams,   4),
      (party_set_slot,"p_town_22", slot_town_arena_melee_1_team_size,   3),
      (party_set_slot,"p_town_22", slot_town_arena_melee_2_num_teams,   3),
      (party_set_slot,"p_town_22", slot_town_arena_melee_2_team_size,   4),
      (party_set_slot,"p_town_22", slot_town_arena_melee_3_num_teams,   2),
      (party_set_slot,"p_town_22", slot_town_arena_melee_3_team_size,   6),
      #chief anade arenas
      (party_set_slot,"p_town_23", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_23", slot_town_arena_melee_1_team_size,   8),
      (party_set_slot,"p_town_23", slot_town_arena_melee_2_num_teams,   2),
      (party_set_slot,"p_town_23", slot_town_arena_melee_2_team_size,   8),
      (party_set_slot,"p_town_23", slot_town_arena_melee_3_num_teams,   2),
      (party_set_slot,"p_town_23", slot_town_arena_melee_3_team_size,   8),
      
      (party_set_slot,"p_town_24", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_24", slot_town_arena_melee_1_team_size,   8),
      (party_set_slot,"p_town_24", slot_town_arena_melee_2_num_teams,   3),
      (party_set_slot,"p_town_24", slot_town_arena_melee_2_team_size,   8),
      (party_set_slot,"p_town_24", slot_town_arena_melee_3_num_teams,   2),
      (party_set_slot,"p_town_24", slot_town_arena_melee_3_team_size,   5),
      
      (party_set_slot,"p_town_25", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_25", slot_town_arena_melee_1_team_size,   3),
      (party_set_slot,"p_town_25", slot_town_arena_melee_2_num_teams,   2),
      (party_set_slot,"p_town_25", slot_town_arena_melee_2_team_size,   5),
      (party_set_slot,"p_town_25", slot_town_arena_melee_3_num_teams,   2),
      (party_set_slot,"p_town_25", slot_town_arena_melee_3_team_size,   8),
      
      (party_set_slot,"p_town_26", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_26", slot_town_arena_melee_1_team_size,   4),
      (party_set_slot,"p_town_26", slot_town_arena_melee_2_num_teams,   3),
      (party_set_slot,"p_town_26", slot_town_arena_melee_2_team_size,   4),
      (party_set_slot,"p_town_26", slot_town_arena_melee_3_num_teams,   3),
      (party_set_slot,"p_town_26", slot_town_arena_melee_3_team_size,   6),
      
      (party_set_slot,"p_town_27", slot_town_arena_melee_1_num_teams,   4),
      (party_set_slot,"p_town_27", slot_town_arena_melee_1_team_size,   4),
      (party_set_slot,"p_town_27", slot_town_arena_melee_2_num_teams,   4),
      (party_set_slot,"p_town_27", slot_town_arena_melee_2_team_size,   6),
      (party_set_slot,"p_town_27", slot_town_arena_melee_3_num_teams,   4),
      (party_set_slot,"p_town_27", slot_town_arena_melee_3_team_size,   8),
      
      (party_set_slot,"p_town_28", slot_town_arena_melee_1_num_teams,   3),
      (party_set_slot,"p_town_28", slot_town_arena_melee_1_team_size,   1),
      (party_set_slot,"p_town_28", slot_town_arena_melee_2_num_teams,   3),
      (party_set_slot,"p_town_28", slot_town_arena_melee_2_team_size,   3),
      (party_set_slot,"p_town_28", slot_town_arena_melee_3_num_teams,   3),
      (party_set_slot,"p_town_28", slot_town_arena_melee_3_team_size,   7),
      
      (party_set_slot,"p_town_29", slot_town_arena_melee_1_num_teams,   2),
      (party_set_slot,"p_town_29", slot_town_arena_melee_1_team_size,   2),
      (party_set_slot,"p_town_29", slot_town_arena_melee_2_num_teams,   2),
      (party_set_slot,"p_town_29", slot_town_arena_melee_2_team_size,   5),
      (party_set_slot,"p_town_29", slot_town_arena_melee_3_num_teams,   2),
      (party_set_slot,"p_town_29", slot_town_arena_melee_3_team_size,   8),
      
  ]),
  
  ("initialize_banner_info",
    [
      #Banners
      # (try_for_range, ":cur_troop", active_npcs_begin, kingdom_ladies_end),
      # (troop_set_slot, ":cur_troop", slot_troop_custom_banner_flag_type, -1),
      # (troop_set_slot, ":cur_troop", slot_troop_custom_banner_map_flag_type, -1),
      # (try_end),
      # (troop_set_slot, "trp_player", slot_troop_custom_banner_flag_type, -1),
      # (troop_set_slot, "trp_player", slot_troop_custom_banner_map_flag_type, -1),
      # (store_random_in_range, "$g_election_date", 0, 45), #setting a random election date
      # #Assigning global constant
      # #(call_script, "script_store_average_center_value_per_faction"),
      
      # (troop_set_slot, "trp_player", slot_troop_custom_banner_bg_color_1, 0xFFFFFFFF),
      # (troop_set_slot, "trp_player", slot_troop_custom_banner_bg_color_2, 0xFFFFFFFF),
      # (troop_set_slot, "trp_player", slot_troop_custom_banner_charge_color_1, 0xFFFFFFFF),
      # (troop_set_slot, "trp_player", slot_troop_custom_banner_charge_color_2, 0xFFFFFFFF),
      # (troop_set_slot, "trp_player", slot_troop_custom_banner_charge_color_3, 0xFFFFFFFF),
      # (troop_set_slot, "trp_player", slot_troop_custom_banner_charge_color_4, 0xFFFFFFFF),
      
      #Setting background colors for banners
      #extras_1a.dds
      (troop_set_slot, "trp_banner_background_color_array", 0, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 1, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 2, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 3, 0xFF63080f),
      (troop_set_slot, "trp_banner_background_color_array", 4, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 5, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 6, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 7, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 8, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 9, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 10, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 11, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 12, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 13, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 14, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 15, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 16, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 17, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 18, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 19, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 20, arms_white),
      
      #extras_2a.dds
      (troop_set_slot, "trp_banner_background_color_array", 21, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 22, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 23, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 24, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 25, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 26, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 27, arms_white),  reserved for default
      (troop_set_slot, "trp_banner_background_color_array", 27, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 28, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 29, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 30, arms_red),
      # (troop_set_slot, "trp_banner_background_color_array", 32, arms_lorange),
      # (troop_set_slot, "trp_banner_background_color_array", 33, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 34, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 14, arms_green),
      # (troop_set_slot, "trp_banner_background_color_array", 15, arms_blue),
      # (troop_set_slot, "trp_banner_background_color_array", 16, arms_yellow),
      # (troop_set_slot, "trp_banner_background_color_array", 17, arms_red),
      # (troop_set_slot, "trp_banner_background_color_array", 18, arms_lorange),
      # (troop_set_slot, "trp_banner_background_color_array", 19, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 20, arms_white),
      
      #anglosaxons_1a.dds
      (troop_set_slot, "trp_banner_background_color_array", 31, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 32, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 33, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 34, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 35, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 36, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 37, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 38, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 39, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 40, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 41, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 42, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 43, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 44, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 45, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 46, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 47, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 48, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 49, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 50, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 51, arms_white),
      
      #anglosaxons_2a.dds
      (troop_set_slot, "trp_banner_background_color_array", 52, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 53, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 54, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 55, arms_red),
      # (troop_set_slot, "trp_banner_background_color_array", 4, arms_lorange),
      # (troop_set_slot, "trp_banner_background_color_array", 5, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 6, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 7, arms_green),
      # (troop_set_slot, "trp_banner_background_color_array", 8, arms_blue),
      # (troop_set_slot, "trp_banner_background_color_array", 9, arms_yellow),
      # (troop_set_slot, "trp_banner_background_color_array", 10, arms_red),
      # (troop_set_slot, "trp_banner_background_color_array", 11, arms_lorange),
      # (troop_set_slot, "trp_banner_background_color_array", 12, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 14, arms_green),
      # (troop_set_slot, "trp_banner_background_color_array", 15, arms_blue),
      # (troop_set_slot, "trp_banner_background_color_array", 16, arms_yellow),
      # (troop_set_slot, "trp_banner_background_color_array", 17, arms_red),
      # (troop_set_slot, "trp_banner_background_color_array", 18, arms_lorange),
      # (troop_set_slot, "trp_banner_background_color_array", 19, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 20, arms_white),
      
      #britons_1a.dds
      (troop_set_slot, "trp_banner_background_color_array", 56, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 57, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 58, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 59, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 60, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 61, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 62, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 63, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 64, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 65, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 66, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 67, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 68, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 69, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 70, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 71, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 72, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 73, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 74, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 75, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 76, arms_white),
      
      #britons_2a.dds
      (troop_set_slot, "trp_banner_background_color_array", 77, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 78, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 79, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 80, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 81, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 82, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 83, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 84, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 85, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 86, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 87, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 88, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 89, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 90, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 91, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 92, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 93, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 94, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 95, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 96, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 97, arms_white),
      
      #irish_1a.dds
      (troop_set_slot, "trp_banner_background_color_array", 98, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 99, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 100, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 101, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 102, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 103, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 104, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 105, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 106, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 107, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 108, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 109, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 110, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 111, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 112, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 113, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 114, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 115, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 116, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 117, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 118, arms_white),
      
      #irish_2a.dds
      (troop_set_slot, "trp_banner_background_color_array", 119, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 120, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 121, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 122, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 123, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 124, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 125, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 126, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 127, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 128, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 129, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 130, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 131, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_green),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_blue),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_yellow),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_red),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_lorange),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_white),
      
      #picts_a.dds
      (troop_set_slot, "trp_banner_background_color_array", 132, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 133, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 134, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 135, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 136, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 137, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 138, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 139, arms_green),
      # (troop_set_slot, "trp_banner_background_color_array", 127, arms_blue),
      # (troop_set_slot, "trp_banner_background_color_array", 128, arms_yellow),
      # (troop_set_slot, "trp_banner_background_color_array", 129, arms_red),
      # (troop_set_slot, "trp_banner_background_color_array", 130, arms_lorange),
      # (troop_set_slot, "trp_banner_background_color_array", 131, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_green),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_blue),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_yellow),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_red),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_lorange),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 13, arms_white),
      
      #vikings_1a.dds
      (troop_set_slot, "trp_banner_background_color_array", 140, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 141, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 142, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 143, 0xFFDFA96E),
      (troop_set_slot, "trp_banner_background_color_array", 144, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 145, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 146, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 147, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 148, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 149, 0xFFDFA96E),
      (troop_set_slot, "trp_banner_background_color_array", 150, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 151, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 152, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 153, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 154, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 155, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 156, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 157, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 158, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 159, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 160, arms_white),
      
      #vikings_2a.dds
      (troop_set_slot, "trp_banner_background_color_array", 161, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 162, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 163, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 164, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 165, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 166, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 167, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 168, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 169, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 170, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 171, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 172, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 173, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 174, arms_lorange),
      (troop_set_slot, "trp_banner_background_color_array", 175, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 176, arms_blue),
      # (troop_set_slot, "trp_banner_background_color_array", 177, arms_yellow),
      # (troop_set_slot, "trp_banner_background_color_array", 178, arms_red),
      # (troop_set_slot, "trp_banner_background_color_array", 179, arms_lorange),
      # (troop_set_slot, "trp_banner_background_color_array", 180, arms_white),
      # (troop_set_slot, "trp_banner_background_color_array", 181, arms_white),
      
      #kingdoms_a.dds
      (troop_set_slot, "trp_banner_background_color_array", 177, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 178, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 179, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 180, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 181, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 182, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 183, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 184, arms_green),
      (troop_set_slot, "trp_banner_background_color_array", 185, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 186, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 187, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 188, arms_yellow),
      (troop_set_slot, "trp_banner_background_color_array", 189, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 190, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 191, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 192, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 193, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 194, arms_blue),
      (troop_set_slot, "trp_banner_background_color_array", 195, arms_white),
      (troop_set_slot, "trp_banner_background_color_array", 196, arms_red),
      (troop_set_slot, "trp_banner_background_color_array", 197, arms_green),
      
      #Default banner
      (troop_set_slot, "trp_banner_background_color_array", 198, arms_white),
  ]),
  
  
  ("initialize_economic_information",
    [
      
      ### TOWN PRODUCTION
      (try_for_range, ":town_no", towns_begin, towns_end),
        
        (try_begin),
          
          ## DENMARK (amber, furs)
          (eq, ":town_no", "p_town_4"),	#Ribe
          
          # COMMONS:
          #wool clothes production - low
          (store_random_in_range, ":random_average_15_variation_10", 5, 25), #10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29 or 30
          (party_set_slot, ":town_no", slot_center_wool_looms, ":random_average_15_variation_10"),
          #ale production - medium
          (store_random_in_range, ":random_average_2_variation_1", 1, 3), #1,2 or 3
          (party_set_slot, ":town_no", slot_center_breweries, ":random_average_2_variation_1"),
          #tool production - ?
          (store_random_in_range, ":random_average_15_variation_9", 6, 15), #6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 or 24
          (party_set_slot, ":town_no", slot_center_smithies, ":random_average_15_variation_9"),
          #bread production - medium
          (store_random_in_range, ":random_average_5_variation_3", 2, 9), #2,3,4,5,6,7 or 8
          (party_set_slot, ":town_no", slot_center_mills, ":random_average_5_variation_3"),
          #mead production - low
          (store_random_in_range, ":random_average_1_variation_1", 0, 3), #0,1 or 2
          (party_set_slot, ":town_no", slot_center_wine_presses, ":random_average_1_variation_1"),
          #wheat/grain production - low
          (store_random_in_range, ":random_average_1000_variation_1000", 0, 500), #0..2000
          (party_set_slot, ":town_no", slot_center_acres_grain, ":random_average_1000_variation_1000"), #0..2000
          
          # SPECIALS:
          #fish production - high
          (store_random_in_range, ":rand", 15, 30),
          (party_set_slot, ":town_no", slot_center_fishing_fleet, ":rand"),
          
          #fur production - very high
          (store_random_in_range, ":rand", 4, 13),
          (party_set_slot, ":town_no", slot_center_fur_traps, ":rand"),
          
          (store_random_in_range, ":rand", 6, 9),
          (party_set_slot, ":town_no", slot_center_linen_looms, ":rand"),
        (else_try),
          ## NORWAY (timber, iron, soapstone, barley, tar, walrus ivory, furs)
          (eq, ":town_no", "p_town_5"),	#Tunsberg
          
          # COMMONS:
          #wool clothes production - low
          (store_random_in_range, ":random_average_15_variation_10", 5, 25), #10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29 or 30
          (party_set_slot, ":town_no", slot_center_wool_looms, ":random_average_15_variation_10"),
          #ale production - medium
          (store_random_in_range, ":random_average_2_variation_1", 1, 3), #1,2 or 3
          (party_set_slot, ":town_no", slot_center_breweries, ":random_average_2_variation_1"),
          #tool production - ?
          (store_random_in_range, ":random_average_15_variation_9", 6, 15), #6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 or 24
          (party_set_slot, ":town_no", slot_center_smithies, ":random_average_15_variation_9"),
          #bread production - medium
          (store_random_in_range, ":random_average_5_variation_3", 2, 9), #2,3,4,5,6,7 or 8
          (party_set_slot, ":town_no", slot_center_mills, ":random_average_5_variation_3"),
          #mead production - low
          (store_random_in_range, ":random_average_1_variation_1", 0, 3), #0,1 or 2
          (party_set_slot, ":town_no", slot_center_wine_presses, ":random_average_1_variation_1"),
          #wheat/grain production - low
          (store_random_in_range, ":random_average_1000_variation_1000", 0, 500), #0..2000
          (party_set_slot, ":town_no", slot_center_acres_grain, ":random_average_1000_variation_1000"), #0..2000
          
          # SPECIALS:
          #tar production - very high
          (store_random_in_range, ":random_average_2_variation_1", 10, 14), #1,2 or 3
          (party_set_slot, ":town_no", slot_center_tar_ovens, ":random_average_2_variation_1"),
          #barley production - medium
          (store_random_in_range, ":random_average_1000_variation_1000", 0, 2001), #0..2000
          (party_set_slot, ":town_no", slot_center_acres_vineyard, ":random_average_1000_variation_1000"), #0..2000
          #fish production - high
          (store_random_in_range, ":rand", 15, 30),
          (party_set_slot, ":town_no", slot_center_fishing_fleet, ":rand"),
          #ivory production
          (store_random_in_range, ":rand", 2, 5),
          (party_set_slot, ":town_no", slot_center_walrus_fleet, ":rand"),
          #fur production - very high
          (store_random_in_range, ":rand", 4, 13),
          (party_set_slot, ":town_no", slot_center_fur_traps, ":rand"),
          
          (store_random_in_range, ":rand", 6, 9),
          (party_set_slot, ":town_no", slot_center_linen_looms, ":rand"),
        (else_try),
          ## FRISIA (jewellery, wine, salt, woolen cloth.)
          (eq, ":town_no", "p_town_11"), #Dorestad
          
          # COMMONS:
          #wool clothes production - super high
          (store_random_in_range, ":random_average_180_variation_10", 100, 120), #10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29 or 30
          (party_set_slot, ":town_no", slot_center_wool_looms, ":random_average_180_variation_10"),
          #ale production - low
          (store_random_in_range, ":random_average_2_variation_1", 0, 2), #1,2 or 3
          (party_set_slot, ":town_no", slot_center_breweries, ":random_average_2_variation_1"),
          #tool production - high
          (store_random_in_range, ":random_average_15_variation_9", 18, 30), #6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 or 24
          (party_set_slot, ":town_no", slot_center_smithies, ":random_average_15_variation_9"),
          #bread production - low
          (store_random_in_range, ":random_average_5_variation_3", 1, 4), #2,3,4,5,6,7 or 8
          (party_set_slot, ":town_no", slot_center_mills, ":random_average_5_variation_3"),
          #mead production - low
          (store_random_in_range, ":random_average_1_variation_1", 0, 2), #0,1 or 2
          (party_set_slot, ":town_no", slot_center_wine_presses, ":random_average_1_variation_1"),
          #wheat/grain production - low
          (store_random_in_range, ":random_average_1000_variation_1000", 0, 500), #0..2000
          (party_set_slot, ":town_no", slot_center_acres_grain, ":random_average_1000_variation_1000"), #0..2000
          
          # SPECIALS:
          #jewellery production - very high
          (store_random_in_range, ":random_average_5_variation_3", 6, 9), #2,3,4,5,6,7 or 8
          (party_set_slot, ":town_no", slot_center_pottery_kilns, ":random_average_5_variation_3"),
          #salt production - very high
          (store_random_in_range, ":rand", 2, 5),
          (party_set_slot, ":town_no", slot_center_salt_pans, ":rand"),
          # silver export
          (store_random_in_range, ":rand", 8, 15),
          (party_set_slot, ":town_no", slot_center_silver_deposits, ":rand"),
          
          (store_random_in_range, ":rand", 4, 7),
          (party_set_slot, ":town_no", slot_center_linen_looms, ":rand"),
        (else_try),
          (eq, ":town_no", "p_town_27"), #bebbanburh
          #ivory and soapstone imports
          (store_random_in_range, ":rand", 2, 5),
          (party_set_slot, ":town_no", slot_center_walrus_fleet, ":rand"),
          
          (store_random_in_range, ":rand", 1, 3),
          (party_set_slot, ":town_no", slot_center_soapstone_depositis, ":rand"),
          #wool clothes production - low
          (store_random_in_range, ":random_average_15_variation_10", 2, 12), #10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29 or 30
          (party_set_slot, ":town_no", slot_center_wool_looms, ":random_average_15_variation_10"),
          #ale production - medium
          (store_random_in_range, ":random_average_2_variation_1", 1, 3), #1,2 or 3
          (party_set_slot, ":town_no", slot_center_breweries, ":random_average_2_variation_1"),
          #tool production - ?
          (store_random_in_range, ":random_average_15_variation_9", 2, 8), #6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 or 24
          (party_set_slot, ":town_no", slot_center_smithies, ":random_average_15_variation_9"),
          #bread production - medium
          (store_random_in_range, ":random_average_5_variation_3", 2, 8), #2,3,4,5,6,7 or 8
          (party_set_slot, ":town_no", slot_center_mills, ":random_average_5_variation_3"),
          #mead production - medium
          (store_random_in_range, ":random_average_1_variation_1", 0, 3), #0,1 or 2
          (party_set_slot, ":town_no", slot_center_wine_presses, ":random_average_1_variation_1"),
          #wheat/grain production - medium/high
          (store_random_in_range, ":random_average_1000_variation_1000", 0, 1800), #0..2000
          (party_set_slot, ":town_no", slot_center_acres_grain, ":random_average_1000_variation_1000"), #0..2000
          #linen production - medium
          (store_random_in_range, ":rand", 3, 11),
          (party_set_slot, ":town_no", slot_center_linen_looms, ":rand"),
          #barley production - medium
          (store_random_in_range, ":random_average_1000_variation_1000", 0, 2001), #0..2000
          (party_set_slot, ":town_no", slot_center_acres_vineyard, ":random_average_1000_variation_1000"), #0..2000
          
        (else_try),
          ## ENGLAND AND IRELAND  (tin, wheat, honey, woolens, silver, barley, linen.
          
          # COMMONS:
          #wool clothes production - low
          (store_random_in_range, ":random_average_15_variation_10", 2, 15), #10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29 or 30
          (party_set_slot, ":town_no", slot_center_wool_looms, ":random_average_15_variation_10"),
          #ale production - medium
          (store_random_in_range, ":random_average_2_variation_1", 1, 3), #1,2 or 3
          (party_set_slot, ":town_no", slot_center_breweries, ":random_average_2_variation_1"),
          #tool production - ?
          (store_random_in_range, ":random_average_15_variation_9", 2, 9), #6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 or 24
          (party_set_slot, ":town_no", slot_center_smithies, ":random_average_15_variation_9"),
          #bread production - medium
          (store_random_in_range, ":random_average_5_variation_3", 2, 9), #2,3,4,5,6,7 or 8
          (party_set_slot, ":town_no", slot_center_mills, ":random_average_5_variation_3"),
          #mead production - medium
          (store_random_in_range, ":random_average_1_variation_1", 0, 3), #0,1 or 2
          (party_set_slot, ":town_no", slot_center_wine_presses, ":random_average_1_variation_1"),
          #wheat/grain production - medium/high
          (store_random_in_range, ":random_average_1000_variation_1000", 0, 2000), #0..2000
          (party_set_slot, ":town_no", slot_center_acres_grain, ":random_average_1000_variation_1000"), #0..2000
          
          # SPECIALS:
          #linen production - medium
          (store_random_in_range, ":rand", 5, 15),
          (party_set_slot, ":town_no", slot_center_linen_looms, ":rand"),
          #barley production - medium
          (store_random_in_range, ":random_average_1000_variation_1000", 0, 2001), #0..2000
          (party_set_slot, ":town_no", slot_center_acres_vineyard, ":random_average_1000_variation_1000"), #0..2000
        (end_try),
      (end_try),
      
      
      
      ### VILLAGE PRODUCTION
      (try_for_range, ":village_no", villages_begin, villages_end),
        
        (party_get_slot, ":bound_center", ":village_no", slot_village_bound_center),
        (party_get_slot, ":market_town", ":village_no", slot_village_market_town),
        (store_faction_of_party, ":bound_center_faction", ":bound_center"),
        (store_faction_of_party, ":market_town_faction", ":market_town"),
        
        (try_begin),
          ## DENMARK (amber, furs)
          (this_or_next|eq, ":bound_center_faction", "fac_kingdom_1"),
          (			  eq, ":market_town_faction", "fac_kingdom_1"),
          
          # COMMONS:
          #cattle -low
          (store_random_in_range, ":random_cattle", 10, 60),
          (party_set_slot, ":village_no", slot_center_head_cattle, ":random_cattle"),
          #sheep - low
          (store_random_in_range, ":random_sheep", 10, 70),
          (party_set_slot, ":village_no", slot_center_head_sheep, ":random_sheep"),
          #grain production - low
          (store_random_in_range, ":random_value_between_0_and_40000", 0, 20000),
          (store_random_in_range, ":random_value_between_0_and_average_20000", 0, ":random_value_between_0_and_40000"),
          (party_set_slot, ":village_no", slot_center_acres_grain, ":random_value_between_0_and_average_20000"), #average : 10000, min : 0, max : 40000
          #barley production - very low
          (store_random_in_range, ":random_value_between_0_and_2000", 0, 500),
          (store_random_in_range, ":random_value_between_0_and_average_1000", 0, ":random_value_between_0_and_2000"),
          (party_set_slot, ":village_no", slot_center_acres_vineyard, ":random_value_between_0_and_average_1000"), #average : 500, min : 0, max : 2000
          #venison production - high
          (store_random_in_range, ":random_value_between_0_and_2000", 0, 4000),
          (store_random_in_range, ":random_value_between_0_and_average_1000", 0, ":random_value_between_0_and_2000"),
          (party_set_slot, ":village_no", slot_center_acres_hunting, ":random_value_between_0_and_average_1000"), #average : 500, min : 0, max : 2000
          #vc_honey production - medium
          (store_random_in_range, ":random_value_between_0_and_3", 0, 3),	#was ", 0, 3)"
          (party_set_slot, ":village_no", slot_center_apiaries, ":random_value_between_0_and_3"),
          #cabbage and fruit production - medium
          (store_random_in_range, ":random_value_between_0_and_5", 0, 5),
          (party_set_slot, ":village_no", slot_center_household_gardens, ":random_value_between_0_and_5"),
          #bread production - medium
          (store_random_in_range, ":random_value_between_0_and_3", 0, 3),
          (party_set_slot, ":village_no", slot_center_mills, ":random_value_between_0_and_3"),
          
          # SPECIALS:
          #amber production - very high
          (store_random_in_range, ":rand", 8, 24),
          (party_set_slot, ":village_no", slot_center_amber_deposits, ":rand"),
          #fur production - very high
          (store_random_in_range, ":rand", 4, 13),
          (party_set_slot, ":village_no", slot_center_fur_traps, ":rand"),
          
          (store_random_in_range, ":rand", 0, 500),
          (party_set_slot, ":village_no", slot_center_acres_flax, ":rand"),
        (else_try),
          ## NORWAY (timber, iron, soapstone, barley, tar, walrus ivory, furs)
          (this_or_next|eq, ":bound_center_faction", "fac_kingdom_2"),
          (			  eq, ":market_town_faction", "fac_kingdom_2"),
          
          # COMMONS:
          #cattle -low
          (store_random_in_range, ":random_cattle", 10, 60),
          (party_set_slot, ":village_no", slot_center_head_cattle, ":random_cattle"),
          #sheep - low
          (store_random_in_range, ":random_sheep", 10, 70),
          (party_set_slot, ":village_no", slot_center_head_sheep, ":random_sheep"),
          #grain production - low
          (store_random_in_range, ":random_value_between_0_and_40000", 0, 20000),
          (store_random_in_range, ":random_value_between_0_and_average_20000", 0, ":random_value_between_0_and_40000"),
          (party_set_slot, ":village_no", slot_center_acres_grain, ":random_value_between_0_and_average_20000"), #average : 10000, min : 0, max : 40000
          #barley production - high
          (store_random_in_range, ":random_value_between_0_and_2000", 0, 4000),
          (store_random_in_range, ":random_value_between_0_and_average_1000", 0, ":random_value_between_0_and_2000"),
          (party_set_slot, ":village_no", slot_center_acres_vineyard, ":random_value_between_0_and_average_1000"), #average : 500, min : 0, max : 2000
          #venison production - high
          (store_random_in_range, ":random_value_between_0_and_2000", 0, 4000),
          (store_random_in_range, ":random_value_between_0_and_average_1000", 0, ":random_value_between_0_and_2000"),
          (party_set_slot, ":village_no", slot_center_acres_hunting, ":random_value_between_0_and_average_1000"), #average : 500, min : 0, max : 2000
          #vc_honey production - medium
          (store_random_in_range, ":random_value_between_0_and_3", 0, 3),	#was ", 0, 3)"
          (party_set_slot, ":village_no", slot_center_apiaries, ":random_value_between_0_and_3"),
          #cabbage and fruit production - medium
          (store_random_in_range, ":random_value_between_0_and_5", 0, 5),
          (party_set_slot, ":village_no", slot_center_household_gardens, ":random_value_between_0_and_5"),
          #bread production - medium
          (store_random_in_range, ":random_value_between_0_and_3", 0, 3),
          (party_set_slot, ":village_no", slot_center_mills, ":random_value_between_0_and_3"),
          
          # SPECIALS:
          #timber production - very high
          (store_random_in_range, ":rand", 30, 40),
          (party_set_slot, ":village_no", slot_center_forest, ":rand"),
          #iron production - very high
          (store_random_in_range, ":rand", 10, 30),
          (party_set_slot, ":village_no", slot_center_iron_deposits, ":rand"),
          #soapstone production - very high
          (store_random_in_range, ":rand", 4, 12),
          (party_set_slot, ":village_no", slot_center_soapstone_depositis, ":rand"),
          #tar production - very high
          (store_random_in_range, ":rand", 4, 12),
          (party_set_slot, ":village_no", slot_center_tar_ovens, ":rand"),
          #walrus ivory production - very high
          (store_random_in_range, ":rand", 8, 24),
          (party_set_slot, ":village_no", slot_center_walrus_fleet, ":rand"),
          #fur production - very high
          (store_random_in_range, ":rand", 4, 13),
          (party_set_slot, ":village_no", slot_center_fur_traps, ":rand"),
          
          (store_random_in_range, ":rand", 0, 600),
          (party_set_slot, ":village_no", slot_center_acres_flax, ":rand"),
        (else_try),
          ## FRISIA (jewelry, wine, salt, woolen cloth.)
          (this_or_next|eq, ":bound_center_faction", "fac_kingdom_4"),
          (			  eq, ":market_town_faction", "fac_kingdom_4"),
          
          # COMMONS:
          #cattle -low
          (store_random_in_range, ":random_cattle", 10, 60),
          (party_set_slot, ":village_no", slot_center_head_cattle, ":random_cattle"),
          #sheep -medium
          (store_random_in_range, ":random_sheep", 40, 280),
          (party_set_slot, ":village_no", slot_center_head_sheep, ":random_sheep"),
          #grain production - low
          (store_random_in_range, ":random_value_between_0_and_40000", 0, 20000),
          (store_random_in_range, ":random_value_between_0_and_average_20000", 0, ":random_value_between_0_and_40000"),
          (party_set_slot, ":village_no", slot_center_acres_grain, ":random_value_between_0_and_average_20000"), #average : 10000, min : 0, max : 40000
          #barley production - very low
          (store_random_in_range, ":random_value_between_0_and_2000", 0, 500),
          (store_random_in_range, ":random_value_between_0_and_average_1000", 0, ":random_value_between_0_and_2000"),
          (party_set_slot, ":village_no", slot_center_acres_vineyard, ":random_value_between_0_and_average_1000"), #average : 500, min : 0, max : 2000
          #vension production - very low
          (store_random_in_range, ":random_value_between_0_and_2000", 0, 500),
          (store_random_in_range, ":random_value_between_0_and_average_1000", 0, ":random_value_between_0_and_2000"),
          (party_set_slot, ":village_no", slot_center_acres_hunting, ":random_value_between_0_and_average_1000"), #average : 500, min : 0, max : 2000
          #vc_honey production - low
          (store_random_in_range, ":random_value_between_0_and_3", 0, 1),	#was ", 0, 3)"
          (party_set_slot, ":village_no", slot_center_apiaries, ":random_value_between_0_and_3"),
          #cabbage and fruit production - medium
          (store_random_in_range, ":random_value_between_0_and_5", 0, 5),
          (party_set_slot, ":village_no", slot_center_household_gardens, ":random_value_between_0_and_5"),
          #bread production - medium
          (store_random_in_range, ":random_value_between_0_and_3", 0, 3),
          (party_set_slot, ":village_no", slot_center_mills, ":random_value_between_0_and_3"),
          
          # SPECIALS:
          #salt ptoduction - very high
          (store_random_in_range, ":rand", 2, 5),
          (party_set_slot, ":village_no", slot_center_salt_pans, ":rand"),
          
          (store_random_in_range, ":rand", 0, 500),
          (party_set_slot, ":village_no", slot_center_acres_flax, ":rand"),
        (else_try),
          ## ENGLAND AND IRELAND  (tin, wheat, vc_honey, woolens, silver, barley, linen.
          
          # COMMONS:
          #cattle -medium(native)
          (store_random_in_range, ":random_cattle", 20, 100),
          (party_set_slot, ":village_no", slot_center_head_cattle, ":random_cattle"),
          #sheep - high
          (store_random_in_range, ":random_sheep", 80, 680),
          (party_set_slot, ":village_no", slot_center_head_sheep, ":random_sheep"),
          #grain production - high
          (store_random_in_range, ":random_value_between_0_and_40000", 0, 60000),
          (store_random_in_range, ":random_value_between_0_and_average_20000", 0, ":random_value_between_0_and_40000"),
          (party_set_slot, ":village_no", slot_center_acres_grain, ":random_value_between_0_and_average_20000"), #average : 10000, min : 0, max : 40000
          #barley production - high
          (store_random_in_range, ":random_value_between_0_and_2000", 0, 4000),
          (store_random_in_range, ":random_value_between_0_and_average_1000", 0, ":random_value_between_0_and_2000"),
          (party_set_slot, ":village_no", slot_center_acres_vineyard, ":random_value_between_0_and_average_1000"), #average : 500, min : 0, max : 2000
          #venison production - medium
          (store_random_in_range, ":random_value_between_0_and_2000", 0, 2000),
          (store_random_in_range, ":random_value_between_0_and_average_1000", 0, ":random_value_between_0_and_2000"),
          (party_set_slot, ":village_no", slot_center_acres_hunting, ":random_value_between_0_and_average_1000"), #average : 500, min : 0, max : 2000
          #vc_honey production - high
          (store_random_in_range, ":random_value_between_0_and_3", 0, 6),	#was ", 0, 3)"
          (party_set_slot, ":village_no", slot_center_apiaries, ":random_value_between_0_and_3"),
          #cabbage and fruit production - medium
          (store_random_in_range, ":random_value_between_0_and_5", 0, 5),
          (party_set_slot, ":village_no", slot_center_household_gardens, ":random_value_between_0_and_5"),
          #bread production - medium
          (store_random_in_range, ":random_value_between_0_and_3", 0, 3),
          (party_set_slot, ":village_no", slot_center_mills, ":random_value_between_0_and_3"),
          
          # SPECIALS:
          
          # silver production in wales
          (try_begin),
            (is_between, ":village_no", "p_village_41", "p_village_48"),
            (store_random_in_range, ":rand", 10, 20),
            (party_set_slot, ":village_no", slot_center_silver_deposits, ":rand"),
          (else_try),
            (is_between, ":village_no", "p_village_106", "p_village_108"),
            (store_random_in_range, ":rand", 8, 17),
            (party_set_slot, ":village_no", slot_center_silver_deposits, ":rand"),
          (else_try),
            (this_or_next|is_between, ":village_no", "p_village_140", "p_village_142"),
            (is_between, ":village_no", "p_village_132", "p_village_134"),
            (store_random_in_range, ":rand", 7, 16),
            (party_set_slot, ":village_no", slot_center_silver_deposits, ":rand"),
            # (else_try),
            # (store_random_in_range, ":rand", 0, 3),
            # (party_set_slot, ":village_no", slot_center_silver_deposits, ":rand"),
          (try_end),
          
          #flax production - very high
          (store_random_in_range, ":rand", 0, 3000),
          (party_set_slot, ":village_no", slot_center_acres_flax, ":rand"),
          #iron production
          (store_random_in_range, ":rand", 0, 7),
          (party_set_slot, ":village_no", slot_center_iron_deposits, ":rand"),
        (end_try),
        
        #FISH		#Ocean and river villages, new map
        (try_begin),
          (this_or_next|eq, ":village_no", "p_village_1"),
          (this_or_next|eq, ":village_no", "p_village_3"),
          (this_or_next|eq, ":village_no", "p_village_7"),
          (this_or_next|eq, ":village_no", "p_village_11"),
          (this_or_next|eq, ":village_no", "p_village_22"),
          (this_or_next|eq, ":village_no", "p_village_24"),
          (this_or_next|eq, ":village_no", "p_village_27"),
          (this_or_next|eq, ":village_no", "p_village_29"),
          (this_or_next|eq, ":village_no", "p_village_31"),
          (this_or_next|eq, ":village_no", "p_village_33"),
          (this_or_next|eq, ":village_no", "p_village_37"),
          (this_or_next|eq, ":village_no", "p_village_38"),
          (this_or_next|eq, ":village_no", "p_village_47"),
          (this_or_next|eq, ":village_no", "p_village_48"),
          (this_or_next|eq, ":village_no", "p_village_54"),
          (this_or_next|eq, ":village_no", "p_village_59"),
          (this_or_next|eq, ":village_no", "p_village_61"),
          (this_or_next|eq, ":village_no", "p_village_63"),
          (this_or_next|eq, ":village_no", "p_village_66"),
          (this_or_next|eq, ":village_no", "p_village_68"),
          (this_or_next|eq, ":village_no", "p_village_69"),
          (this_or_next|eq, ":village_no", "p_village_75"),
          (this_or_next|eq, ":village_no", "p_village_82"),
          (this_or_next|eq, ":village_no", "p_village_86"),
          (this_or_next|eq, ":village_no", "p_village_87"),
          (this_or_next|eq, ":village_no", "p_village_88"),
          (this_or_next|eq, ":village_no", "p_village_93"),
          (this_or_next|eq, ":village_no", "p_village_94"),
          (this_or_next|eq, ":village_no", "p_village_95"),
          (this_or_next|eq, ":village_no", "p_village_102"),
          (this_or_next|eq, ":village_no", "p_village_103"),
          (this_or_next|eq, ":village_no", "p_village_104"),
          (this_or_next|eq, ":village_no", "p_village_106"),
          (this_or_next|eq, ":village_no", "p_village_107"),
          (this_or_next|eq, ":village_no", "p_village_112"),
          (this_or_next|eq, ":village_no", "p_village_113"),
          (this_or_next|eq, ":village_no", "p_village_114"),
          (this_or_next|eq, ":village_no", "p_village_115"),
          (this_or_next|eq, ":village_no", "p_village_123"),
          (this_or_next|eq, ":village_no", "p_village_126"),
          (this_or_next|eq, ":village_no", "p_village_128"),
          (this_or_next|eq, ":village_no", "p_village_129"),
          (this_or_next|eq, ":village_no", "p_village_146"),
          (this_or_next|eq, ":village_no", "p_village_148"),
          (this_or_next|eq, ":village_no", "p_village_149"),
          (			  eq, ":village_no", "p_village_150"),
          (party_set_slot, ":village_no", slot_center_fishing_fleet, 15),
        (try_end),
        
      (end_try),
      
      #Initialize pastureland
      (try_for_range, ":center", villages_begin, villages_end),
        (party_get_slot, ":head_cattle", ":center", slot_center_head_cattle),
        (party_get_slot, ":head_sheep", ":center", slot_center_head_sheep),
        (store_mul, ":num_acres", ":head_cattle", 4),
        (val_add, ":num_acres", ":head_sheep"),
        (val_add, ":num_acres", ":head_sheep"),
        (val_mul, ":num_acres", 6),
        (val_div, ":num_acres", 5),
        
        (store_random_in_range, ":random", 60, 150),
        (val_mul, ":num_acres", ":random"),
        (val_div, ":num_acres", 100),
        
        (party_set_slot, ":center", slot_center_acres_pasture, ":num_acres"),
      (try_end),
      
      (try_begin),#new
        # Info:
        # We want to avoid this last part of the script if it is called in the
        # simple trigger who is updating old save games to the content update.
        (eq, "$game_started_with_content_update", 1),
        
        #Initialize prices based on production, etc
        (try_for_range, ":unused", 0, 3), #15 cycles = 45 days. For a village with -20 production, this should lead to approximate +1000, modified
          (call_script, "script_update_trade_good_prices"), #changes prices based on production
        (try_end),
        
        #Initialize prosperity based on final prices
        (try_for_range, ":center_no", centers_begin, centers_end),
          (neg|is_between, ":center_no", castles_begin, castles_end),
          (store_random_in_range, ":random_prosperity_adder", -10, 10),
          (call_script, "script_get_center_ideal_prosperity", ":center_no"),
          (assign, ":prosperity", reg0),
          (val_add, ":prosperity", ":random_prosperity_adder"),
          (val_clamp, ":prosperity", 0, 100),
          (party_set_slot, ":center_no", slot_town_prosperity, ":prosperity"),
        (try_end),
        
        (try_for_range, ":cur_castle", castles_begin, castles_end),
          (call_script, "script_calculate_castle_prosperities_by_using_its_villages", ":cur_castle", 1),
        (try_end),
      (try_end),#new
  ]),
  
  #script_initialize_all_scene_prop_slots
  # INPUT: arg1 = scene_prop_no
  # OUTPUT: none
  ("initialize_all_scene_prop_slots",
    [
      (call_script, "script_initialize_scene_prop_slots", "spr_siege_ladder_move_6m"),
      (call_script, "script_initialize_scene_prop_slots", "spr_siege_ladder_move_8m"),
      (call_script, "script_initialize_scene_prop_slots", "spr_siege_ladder_move_10m"),
      (call_script, "script_initialize_scene_prop_slots", "spr_siege_ladder_move_12m"),
      (call_script, "script_initialize_scene_prop_slots", "spr_siege_ladder_move_14m"),
      (call_script, "script_initialize_scene_prop_slots", "spr_castle_e_sally_door_a"),
      (call_script, "script_initialize_scene_prop_slots", "spr_castle_f_sally_door_a"),
      (call_script, "script_initialize_scene_prop_slots", "spr_earth_sally_gate_left"),
      (call_script, "script_initialize_scene_prop_slots", "spr_earth_sally_gate_right"),
      (call_script, "script_initialize_scene_prop_slots", "spr_viking_keep_destroy_sally_door_left"),
      (call_script, "script_initialize_scene_prop_slots", "spr_viking_keep_destroy_sally_door_right"),
      (call_script, "script_initialize_scene_prop_slots", "spr_castle_f_door_a"),
      (call_script, "script_initialize_scene_prop_slots", "spr_belfry_a"),
      (call_script, "script_initialize_scene_prop_slots", "spr_belfry_b"),
      (call_script, "script_initialize_scene_prop_slots", "spr_winch_b"),
  ]),
  
  #script_initialize_scene_prop_slots
  # INPUT: arg1 = scene_prop_no
  # OUTPUT: none
  ("initialize_scene_prop_slots",
    [
      (store_script_param, ":scene_prop_no", 1),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", ":scene_prop_no"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", ":scene_prop_no", ":cur_instance"),
        (try_for_range, ":cur_slot", 0, scene_prop_slots_end),
          (scene_prop_set_slot, ":cur_instance_id", ":cur_slot", 0),
        (try_end),
      (try_end),
  ]),
  
  #script_use_item
  # INPUT: arg1 = agent_id, arg2 = instance_id
  # OUTPUT: none
  ("use_item",
    [
      (store_script_param, ":instance_id", 1),
      (store_script_param, ":user_id", 2),
      
      (try_begin),
        (game_in_multiplayer_mode),
        (prop_instance_get_scene_prop_kind, ":scene_prop_id", ":instance_id"),
        (eq, ":scene_prop_id", "spr_winch_b"),
        
        (multiplayer_get_my_player, ":my_player_no"),
        
        (this_or_next|gt, ":my_player_no", 0),
        (neg|multiplayer_is_dedicated_server),
        
        (ge, ":my_player_no", 0),
        (player_get_agent_id, ":my_agent_id", ":my_player_no"),
        (ge, ":my_agent_id", 0),
        (agent_is_active, ":my_agent_id"),
        (agent_get_team, ":my_team_no", ":my_agent_id"),
        (eq, ":my_team_no", 0),
        
        (scene_prop_get_slot, ":opened_or_closed", ":instance_id", scene_prop_open_or_close_slot),
        (ge, ":user_id", 0),
        (agent_is_active, ":user_id"),
        (agent_get_player_id, ":user_player", ":user_id"),
        (str_store_player_username, s7, ":user_player"),
        
        (try_begin),
          (eq, ":opened_or_closed", 0),
          (display_message, "@{s7} opened the gate."),
        (else_try),
          (display_message, "@{s7} closed the gate."),
        (try_end),
      (try_end),
      
      (prop_instance_get_scene_prop_kind, ":scene_prop_id", ":instance_id"),
      
      (try_begin),
        (this_or_next|eq, ":scene_prop_id", "spr_winch_b"),
        (eq, ":scene_prop_id", "spr_winch"),
        (assign, ":effected_object", "spr_portcullis"),
      (else_try),
        (this_or_next|eq, ":scene_prop_id", "spr_door_destructible"),
        (this_or_next|eq, ":scene_prop_id", "spr_castle_f_door_b"),
        (this_or_next|eq, ":scene_prop_id", "spr_castle_e_sally_door_a"),
        (this_or_next|eq, ":scene_prop_id", "spr_castle_f_sally_door_a"),
        (this_or_next|eq, ":scene_prop_id", "spr_earth_sally_gate_left"),
        (this_or_next|eq, ":scene_prop_id", "spr_earth_sally_gate_right"),
        (this_or_next|eq, ":scene_prop_id", "spr_viking_keep_destroy_sally_door_left"),
        (this_or_next|eq, ":scene_prop_id", "spr_viking_keep_destroy_sally_door_right"),
        (this_or_next|eq, ":scene_prop_id", "spr_castle_f_door_a"),
        (this_or_next|eq, ":scene_prop_id", "spr_siege_ladder_move_6m"),
        (this_or_next|eq, ":scene_prop_id", "spr_siege_ladder_move_8m"),
        (this_or_next|eq, ":scene_prop_id", "spr_siege_ladder_move_10m"),
        (this_or_next|eq, ":scene_prop_id", "spr_siege_ladder_move_12m"),
        (eq, ":scene_prop_id", "spr_siege_ladder_move_14m"),
        (assign, ":effected_object", ":scene_prop_id"),
      (try_end),
      
      (assign, ":smallest_dist", -1),
      (prop_instance_get_position, pos0, ":instance_id"),
      (scene_prop_get_num_instances, ":num_instances_of_effected_object", ":effected_object"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_effected_object"),
        (scene_prop_get_instance, ":cur_instance_id", ":effected_object", ":cur_instance"),
        (prop_instance_get_position, pos1, ":cur_instance_id"),
        (get_sq_distance_between_positions, ":dist", pos0, pos1),
        (this_or_next|eq, ":smallest_dist", -1),
        (lt, ":dist", ":smallest_dist"),
        (assign, ":smallest_dist", ":dist"),
        (assign, ":effected_object_instance_id", ":cur_instance_id"),
      (try_end),
      
      (try_begin),
        (ge, ":instance_id", 0),
        (ge, ":smallest_dist", 0),
        
        (try_begin),
          (eq, ":effected_object", "spr_portcullis"),
          (scene_prop_get_slot, ":opened_or_closed", ":instance_id", scene_prop_open_or_close_slot),
          
          (try_begin),
            (eq, ":opened_or_closed", 0), #open gate
            
            (scene_prop_enable_after_time, ":instance_id", 400), #4 seconds
            (try_begin),
              (this_or_next|multiplayer_is_server),
              (neg|game_in_multiplayer_mode),
              (prop_instance_get_position, pos0, ":effected_object_instance_id"),
              (position_move_z, pos0, 375),
              (prop_instance_animate_to_position, ":effected_object_instance_id", pos0, 400),
            (try_end),
            (scene_prop_set_slot, ":instance_id", scene_prop_open_or_close_slot, 1),
            
            (try_begin),
              (eq, ":scene_prop_id", "spr_winch_b"),
              (this_or_next|multiplayer_is_server),
              (neg|game_in_multiplayer_mode),
              (prop_instance_get_position, pos1, ":instance_id"),
              (prop_instance_rotate_to_position, ":instance_id", pos1, 400, 72000),
            (try_end),
          (else_try), #close gate
            (scene_prop_enable_after_time, ":instance_id", 400), #4 seconds
            (try_begin),
              (this_or_next|multiplayer_is_server),
              (neg|game_in_multiplayer_mode),
              (prop_instance_get_position, pos0, ":effected_object_instance_id"),
              (position_move_z, pos0, -375),
              (prop_instance_animate_to_position, ":effected_object_instance_id", pos0, 400),
            (try_end),
            (scene_prop_set_slot, ":instance_id", scene_prop_open_or_close_slot, 0),
            
            (try_begin),
              (eq, ":scene_prop_id", "spr_winch_b"),
              (this_or_next|multiplayer_is_server),
              (neg|game_in_multiplayer_mode),
              (prop_instance_get_position, pos1, ":instance_id"),
              (prop_instance_rotate_to_position, ":instance_id", pos1, 400, -72000),
            (try_end),
          (try_end),
        (else_try),
          (this_or_next|eq, ":effected_object", "spr_siege_ladder_move_6m"),
          (this_or_next|eq, ":effected_object", "spr_siege_ladder_move_8m"),
          (this_or_next|eq, ":effected_object", "spr_siege_ladder_move_10m"),
          (this_or_next|eq, ":effected_object", "spr_siege_ladder_move_12m"),
          (eq, ":effected_object", "spr_siege_ladder_move_14m"),
          
          (try_begin),
            (eq, ":effected_object", "spr_siege_ladder_move_6m"),
            (assign, ":animation_time_drop", 120),
            (assign, ":animation_time_elevate", 240),
          (else_try),
            (eq, ":effected_object", "spr_siege_ladder_move_8m"),
            (assign, ":animation_time_drop", 140),
            (assign, ":animation_time_elevate", 280),
          (else_try),
            (eq, ":effected_object", "spr_siege_ladder_move_10m"),
            (assign, ":animation_time_drop", 160),
            (assign, ":animation_time_elevate", 320),
          (else_try),
            (eq, ":effected_object", "spr_siege_ladder_move_12m"),
            (assign, ":animation_time_drop", 190),
            (assign, ":animation_time_elevate", 360),
          (else_try),
            (eq, ":effected_object", "spr_siege_ladder_move_14m"),
            (assign, ":animation_time_drop", 230),
            (assign, ":animation_time_elevate", 400),
          (try_end),
          
          (scene_prop_get_slot, ":opened_or_closed", ":instance_id", scene_prop_open_or_close_slot),
          
          (try_begin),
            (scene_prop_enable_after_time, ":effected_object_instance_id", ":animation_time_elevate"), #3 seconds in average
            (eq, ":opened_or_closed", 0), #ladder at ground
            (prop_instance_get_starting_position, pos0, ":effected_object_instance_id"),
            (prop_instance_enable_physics, ":effected_object_instance_id", 0),
            (prop_instance_animate_to_position, ":effected_object_instance_id", pos0, 300),
            (scene_prop_set_slot, ":effected_object_instance_id", scene_prop_open_or_close_slot, 1),
          (else_try), #ladder at wall
            (scene_prop_enable_after_time, ":effected_object_instance_id", ":animation_time_drop"), #1.5 seconds in average
            (prop_instance_get_position, pos0, ":instance_id"),
            
            (assign, ":smallest_dist", -1),
            (try_for_range, ":entry_point_no", multi_entry_points_for_usable_items_start, multi_entry_points_for_usable_items_end),
              (entry_point_get_position, pos1, ":entry_point_no"),
              (get_sq_distance_between_positions, ":dist", pos0, pos1),
              (this_or_next|eq, ":smallest_dist", -1),
              (lt, ":dist", ":smallest_dist"),
              (assign, ":smallest_dist", ":dist"),
              (assign, ":nearest_entry_point", ":entry_point_no"),
            (try_end),
            
            (try_begin),
              (ge, ":smallest_dist", 0),
              (lt, ":smallest_dist", 22500), #max 15m distance
              (entry_point_get_position, pos1, ":nearest_entry_point"),
              (position_rotate_x, pos1, -90),
              (scene_prop_set_slot, ":effected_object_instance_id", scene_prop_smoke_effect_done, 0),
              (prop_instance_enable_physics, ":effected_object_instance_id", 0),
              (prop_instance_animate_to_position, ":effected_object_instance_id", pos1, 130),
            (try_end),
            
            (scene_prop_set_slot, ":effected_object_instance_id", scene_prop_open_or_close_slot, 0),
          (try_end),
        (else_try),
          (this_or_next|eq, ":effected_object", "spr_door_destructible"),
          (this_or_next|eq, ":effected_object", "spr_castle_f_door_b"),
          (this_or_next|eq, ":scene_prop_id", "spr_castle_e_sally_door_a"),
          (this_or_next|eq, ":scene_prop_id", "spr_castle_f_sally_door_a"),
          (this_or_next|eq, ":scene_prop_id", "spr_earth_sally_gate_left"),
          (this_or_next|eq, ":scene_prop_id", "spr_earth_sally_gate_right"),
          (this_or_next|eq, ":scene_prop_id", "spr_viking_keep_destroy_sally_door_left"),
          (this_or_next|eq, ":scene_prop_id", "spr_viking_keep_destroy_sally_door_right"),
          (eq, ":scene_prop_id", "spr_castle_f_door_a"),
          
          (assign, ":effected_object_instance_id", ":instance_id"),
          (scene_prop_get_slot, ":opened_or_closed", ":effected_object_instance_id", scene_prop_open_or_close_slot),
          
          (try_begin),
            (eq, ":opened_or_closed", 0),
            
            (prop_instance_get_starting_position, pos0, ":effected_object_instance_id"),
            
            (scene_prop_enable_after_time, ":effected_object_instance_id", 100),
            
            (try_begin),
              (neg|eq, ":scene_prop_id", "spr_viking_keep_destroy_sally_door_left"),
              (neg|eq, ":scene_prop_id", "spr_earth_sally_gate_left"),
              
              (position_rotate_z, pos0, -85),
            (else_try),
              (position_rotate_z, pos0, 85),
            (try_end),
            
            (prop_instance_animate_to_position, ":effected_object_instance_id", pos0, 100),
            
            (scene_prop_set_slot, ":effected_object_instance_id", scene_prop_open_or_close_slot, 1),
          (else_try),
            (prop_instance_get_starting_position, pos0, ":effected_object_instance_id"),
            
            (scene_prop_enable_after_time, ":effected_object_instance_id", 100),
            
            (prop_instance_animate_to_position, ":effected_object_instance_id", pos0, 100),
            
            (scene_prop_set_slot, ":effected_object_instance_id", scene_prop_open_or_close_slot, 0),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  #script_determine_team_flags
  # INPUT: none
  # OUTPUT: none
  ("determine_team_flags",
    [
      (store_script_param, ":team_no", 1),
      
      (try_begin),
        (eq, ":team_no", 0),
        
        (team_get_faction, ":team_faction_no", 0),
        (try_begin),
          (eq, ":team_faction_no", "fac_culture_norse"),
          (assign, "$team_1_flag_scene_prop", "spr_headquarters_flag_team_1"),
        (else_try),
          (eq, ":team_faction_no", "fac_culture_saxon"),
          (assign, "$team_1_flag_scene_prop", "spr_headquarters_flag_team_2"),
        (else_try),
          (eq, ":team_faction_no", "fac_culture_angle"),
          (assign, "$team_1_flag_scene_prop", "spr_headquarters_flag_team_3"),
        (else_try),
          (eq, ":team_faction_no", "fac_culture_welsh"),
          (assign, "$team_1_flag_scene_prop", "spr_headquarters_flag_team_4"),
        (else_try),
          (eq, ":team_faction_no", "fac_culture_irish"),
          (assign, "$team_1_flag_scene_prop", "spr_headquarters_flag_team_5"),
        (else_try),
          (eq, ":team_faction_no", "fac_culture_scotch"),
          (assign, "$team_1_flag_scene_prop", "spr_headquarters_flag_team_6"),
        (try_end),
      (else_try),
        (team_get_faction, ":team_faction_no", 1),
        (try_begin),
          (eq, ":team_faction_no", "fac_culture_norse"),
          (assign, "$team_2_flag_scene_prop", "spr_headquarters_flag_team_1"),
        (else_try),
          (eq, ":team_faction_no", "fac_culture_saxon"),
          (assign, "$team_2_flag_scene_prop", "spr_headquarters_flag_team_2"),
        (else_try),
          (eq, ":team_faction_no", "fac_culture_angle"),
          (assign, "$team_2_flag_scene_prop", "spr_headquarters_flag_team_3"),
        (else_try),
          (eq, ":team_faction_no", "fac_culture_welsh"),
          (assign, "$team_2_flag_scene_prop", "spr_headquarters_flag_team_4"),
        (else_try),
          (eq, ":team_faction_no", "fac_culture_irish"),
          (assign, "$team_2_flag_scene_prop", "spr_headquarters_flag_team_5"),
        (else_try),
          (eq, ":team_faction_no", "fac_culture_scotch"),
          (assign, "$team_2_flag_scene_prop", "spr_headquarters_flag_team_6"),
        (try_end),
        
        (try_begin),
          (eq, "$team_1_flag_scene_prop", "$team_2_flag_scene_prop"),
          (assign, "$team_2_flag_scene_prop", "spr_headquarters_flag_rebel"),
        (try_end),
      (try_end),
  ]),
  
  
  #script_calculate_flag_move_time
  # INPUT: arg1 = number_of_total_agents_around_flag, arg2 = dist_between_flag_and_its_pole
  # OUTPUT: reg0 = flag move time
  ("calculate_flag_move_time",
    [
      (store_script_param, ":number_of_total_agents_around_flag", 1),
      (store_script_param, ":dist_between_flag_and_its_target", 2),
      
      (try_begin), #(if no one is around flag it again moves to its current owner situation but 5 times slower than normal)
        (eq, ":number_of_total_agents_around_flag", 0),
        (store_mul, reg0, ":dist_between_flag_and_its_target", 2500),#5.00 * 1.00 * (500 stable) = 2000
      (else_try),
        (eq, ":number_of_total_agents_around_flag", 1),
        (store_mul, reg0, ":dist_between_flag_and_its_target", 500), #1.00 * (500 stable) = 500
      (else_try),
        (eq, ":number_of_total_agents_around_flag", 2),
        (store_mul, reg0, ":dist_between_flag_and_its_target", 300), #0.60(0.60) * (500 stable) = 300
      (else_try),
        (eq, ":number_of_total_agents_around_flag", 3),
        (store_mul, reg0, ":dist_between_flag_and_its_target", 195), #0.39(0.60 * 0.65) * (500 stable) = 195
      (else_try),
        (eq, ":number_of_total_agents_around_flag", 4),
        (store_mul, reg0, ":dist_between_flag_and_its_target", 137), #0.273(0.60 * 0.65 * 0.70) * (500 stable) = 136.5 >rounding> 137
      (else_try),
        (eq, ":number_of_total_agents_around_flag", 5),
        (store_mul, reg0, ":dist_between_flag_and_its_target", 102), #0.20475(0.60 * 0.65 * 0.70 * 0.75) * (500 stable) = 102.375 >rounding> 102
      (else_try),
        (eq, ":number_of_total_agents_around_flag", 6),
        (store_mul, reg0, ":dist_between_flag_and_its_target", 82),  #0.1638(0.60 * 0.65 * 0.70 * 0.75 * 0.80) * (500 stable) = 81.9 >rounding> 82
      (else_try),
        (eq, ":number_of_total_agents_around_flag", 7),
        (store_mul, reg0, ":dist_between_flag_and_its_target", 66),  #0.13104(0.60 * 0.65 * 0.70 * 0.75 * 0.80 * 0.85) * (500 stable) = 65.52 >rounding> 66
      (else_try),
        (eq, ":number_of_total_agents_around_flag", 8),
        (store_mul, reg0, ":dist_between_flag_and_its_target", 59),  #0.117936(0.60 * 0.65 * 0.70 * 0.75 * 0.80 * 0.85 * 0.90) * (500 stable) = 58.968 >rounding> 59
      (else_try),
        (store_mul, reg0, ":dist_between_flag_and_its_target", 56),  #0.1120392(0.60 * 0.65 * 0.70 * 0.75 * 0.80 * 0.85 * 0.90 * 0.95) * (500 stable) = 56.0196 >rounding> 56
      (try_end),
      
      (assign, ":number_of_players", 0),
      (get_max_players, ":num_players"),
      (try_for_range, ":cur_player", 0, ":num_players"),
        (player_is_active, ":cur_player"),
        (val_add, ":number_of_players", 1),
      (try_end),
      
      (try_begin),
        (lt, ":number_of_players", 10),
        (val_mul, reg0, 50),
      (else_try),
        (lt, ":number_of_players", 35),
        (store_sub, ":number_of_players_multipication", 35, ":number_of_players"),
        (val_mul, ":number_of_players_multipication", 2),
        (store_sub, ":number_of_players_multipication", 100, ":number_of_players_multipication"),
        (val_mul, reg0, ":number_of_players_multipication"),
      (else_try),
        (val_mul, reg0, 100),
      (try_end),
      
      (try_begin),
        (eq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
        (val_mul, reg0, 2),
      (try_end),
      
      (val_div, reg0, 10000), #100x for number of players around flag, 100x for number of players in game
  ]),
  
  #script_move_death_mode_flags_down
  # INPUT: none
  # OUTPUT: none
  ("move_death_mode_flags_down",
    [
      (try_begin),
        (scene_prop_get_instance, ":pole_1_id", "spr_headquarters_pole_code_only", 0),
        (prop_instance_get_position, pos0, ":pole_1_id"),
        (position_move_z, pos0, -2000),
        (prop_instance_set_position, ":pole_1_id", pos0),
      (try_end),
      
      (try_begin),
        (scene_prop_get_instance, ":pole_2_id", "spr_headquarters_pole_code_only", 1),
        (prop_instance_get_position, pos1, ":pole_2_id"),
        (position_move_z, pos1, -2000),
        (prop_instance_set_position, ":pole_2_id", pos1),
      (try_end),
      
      (try_begin),
        (scene_prop_get_instance, ":pole_1_id", "spr_headquarters_pole_code_only", 0),
        (prop_instance_get_position, pos0, ":pole_1_id"),
        (scene_prop_get_instance, ":flag_1_id", "$team_1_flag_scene_prop", 0),
        (prop_instance_stop_animating, ":flag_1_id"),
        (position_move_z, pos0, multi_headquarters_flag_initial_height),
        (prop_instance_set_position, ":flag_1_id", pos0),
      (try_end),
      
      (try_begin),
        (scene_prop_get_instance, ":pole_2_id", "spr_headquarters_pole_code_only", 1),
        (prop_instance_get_position, pos1, ":pole_2_id"),
        (scene_prop_get_instance, ":flag_2_id", "$team_2_flag_scene_prop", 0),
        (prop_instance_stop_animating, ":flag_2_id"),
        (position_move_z, pos1, multi_headquarters_flag_initial_height),
        (prop_instance_set_position, ":flag_2_id", pos1),
      (try_end),
  ]),
  
  #script_move_flag
  # INPUT: arg1 = shown_flag_id, arg2 = move time in seconds, pos0 = target position
  # OUTPUT: none
  ("move_flag",
    [
      (store_script_param, ":shown_flag_id", 1),
      (store_script_param, ":shown_flag_move_time", 2),
      
      (try_begin),
        (multiplayer_is_server), #added after auto-animating
        
        (try_begin),
          (eq, ":shown_flag_move_time", 0), #stop
          (prop_instance_stop_animating, ":shown_flag_id"),
        (else_try),
          (prop_instance_animate_to_position, ":shown_flag_id", pos0, ":shown_flag_move_time"),
        (try_end),
      (try_end),
  ]),
  
  #script_move_headquarters_flags
  # INPUT: arg1 = current_owner, arg2 = number_of_agents_around_flag_team_1, arg3 = number_of_agents_around_flag_team_2
  # OUTPUT: none
  ("move_headquarters_flags",
    [
      (store_script_param, ":flag_no", 1),
      (store_script_param, ":number_of_agents_around_flag_team_1", 2),
      (store_script_param, ":number_of_agents_around_flag_team_2", 3),
      
      (store_add, ":cur_flag_slot", multi_data_flag_owner_begin, ":flag_no"),
      (troop_get_slot, ":current_owner", "trp_multiplayer_data", ":cur_flag_slot"),
      
      (scene_prop_get_num_instances, ":num_instances", "spr_headquarters_flag_gray_code_only"),
      (try_begin),
        (assign, ":visibility", 0),
        (lt, ":flag_no", ":num_instances"),
        (scene_prop_get_instance, ":flag_id", "spr_headquarters_flag_gray_code_only", ":flag_no"),
        (scene_prop_get_visibility, ":visibility", ":flag_id"),
      (try_end),
      
      (try_begin),
        (eq, ":visibility", 1),
        (assign, ":shown_flag", 0),
        (assign, ":shown_flag_id", ":flag_id"),
      (else_try),
        (scene_prop_get_num_instances, ":num_instances", "$team_1_flag_scene_prop"),
        (try_begin),
          (assign, ":visibility", 0),
          (lt, ":flag_no", ":num_instances"),
          (scene_prop_get_instance, ":flag_id", "$team_1_flag_scene_prop", ":flag_no"),
          (scene_prop_get_visibility, ":visibility", ":flag_id"),
        (try_end),
        
        #(scene_prop_get_instance, ":flag_id", "$team_1_flag_scene_prop", ":flag_no"),
        #(scene_prop_get_visibility, ":visibility", ":flag_id"),
        (try_begin),
          (eq, ":visibility", 1),
          (assign, ":shown_flag", 1),
          (assign, ":shown_flag_id", ":flag_id"),
        (else_try),
          (scene_prop_get_num_instances, ":num_instances", "$team_2_flag_scene_prop"),
          (try_begin),
            (assign, ":visibility", 0),
            (lt, ":flag_no", ":num_instances"),
            (scene_prop_get_instance, ":flag_id", "$team_2_flag_scene_prop", ":flag_no"),
            (scene_prop_get_visibility, ":visibility", ":flag_id"),
          (try_end),
          
          #(scene_prop_get_instance, ":flag_id", "$team_2_flag_scene_prop", ":flag_no"),
          #(scene_prop_get_visibility, ":visibility", ":flag_id"),
          (try_begin),
            (eq, ":visibility", 1),
            (assign, ":shown_flag", 2),
            (assign, ":shown_flag_id", ":flag_id"),
          (try_end),
        (try_end),
      (try_end),
      
      (try_begin),
        (scene_prop_get_instance, ":pole_id", "spr_headquarters_pole_code_only", ":flag_no"),
      (try_end),
      
      (try_begin),
        (eq, ":shown_flag", ":current_owner"), #situation 1 : (current owner is equal shown flag)
        (try_begin),
          (ge, ":number_of_agents_around_flag_team_1", 1),
          (ge, ":number_of_agents_around_flag_team_2", 1),
          (assign, ":flag_movement", 0), #0:stop
        (else_try),
          (eq, ":number_of_agents_around_flag_team_1", 0),
          (eq, ":number_of_agents_around_flag_team_2", 0),
          (assign, ":flag_movement", 1), #1:rise (slow)
        (else_try),
          (try_begin),
            (ge, ":number_of_agents_around_flag_team_1", 1),
            (eq, ":number_of_agents_around_flag_team_2", 0),
            (eq, ":current_owner", 1),
            (assign, ":flag_movement", 1), #1:rise (fast)
          (else_try),
            (eq, ":number_of_agents_around_flag_team_1", 0),
            (ge, ":number_of_agents_around_flag_team_2", 1),
            (eq, ":current_owner", 2),
            (assign, ":flag_movement", 1), #1:rise (fast)
          (else_try),
            (assign, ":flag_movement", -1), #-1:drop (fast)
          (try_end),
        (try_end),
      (else_try), #situation 2 : (current owner is different than shown flag)
        (try_begin),
          (ge, ":number_of_agents_around_flag_team_1", 1),
          (ge, ":number_of_agents_around_flag_team_2", 1),
          (assign, ":flag_movement", 0), #0:stop
        (else_try),
          (eq, ":number_of_agents_around_flag_team_1", 0),
          (eq, ":number_of_agents_around_flag_team_2", 0),
          (assign, ":flag_movement", -1), #-1:drop (slow)
        (else_try),
          (try_begin),
            (ge, ":number_of_agents_around_flag_team_1", 1),
            (eq, ":number_of_agents_around_flag_team_2", 0),
            (try_begin),
              (eq, ":shown_flag", 1),
              (assign, ":flag_movement", 1), #1:rise (fast)
            (else_try),
              (neq, ":current_owner", 1),
              (assign, ":flag_movement", -1), #-1:drop (fast)
            (try_end),
          (else_try),
            (eq, ":number_of_agents_around_flag_team_1", 0),
            (ge, ":number_of_agents_around_flag_team_2", 1),
            (try_begin),
              (eq, ":shown_flag", 2),
              (assign, ":flag_movement", 1), #1:rise (fast)
            (else_try),
              (neq, ":current_owner", 2),
              (assign, ":flag_movement", -1), #-1:drop (fast)
            (try_end),
          (try_end),
        (try_end),
      (try_end),
      
      (store_add, ":number_of_total_agents_around_flag", ":number_of_agents_around_flag_team_1", ":number_of_agents_around_flag_team_2"),
      
      (try_begin),
        (eq, ":flag_movement", 0),
        (assign, reg0, 0),
      (else_try),
        (eq, ":flag_movement", 1),
        (prop_instance_get_position, pos1, ":shown_flag_id"),
        (prop_instance_get_position, pos0, ":pole_id"),
        (position_move_z, pos0, multi_headquarters_pole_height),
        (get_distance_between_positions, ":dist_between_flag_and_its_target", pos0, pos1),
        (call_script, "script_calculate_flag_move_time", ":number_of_total_agents_around_flag", ":dist_between_flag_and_its_target"),
      (else_try),
        (eq, ":flag_movement", -1),
        (prop_instance_get_position, pos1, ":shown_flag_id"),
        (prop_instance_get_position, pos0, ":pole_id"),
        (get_distance_between_positions, ":dist_between_flag_and_its_target", pos0, pos1),
        (call_script, "script_calculate_flag_move_time", ":number_of_total_agents_around_flag", ":dist_between_flag_and_its_target"),
      (try_end),
      
      (call_script, "script_move_flag", ":shown_flag_id", reg0), #pos0 : target position
  ]),
  
  #script_set_num_agents_around_flag
  # INPUT: arg1 = flag_no, arg2 = owner_code
  # OUTPUT: none
  ("set_num_agents_around_flag",
    [
      (store_script_param, ":flag_no", 1),
      (store_script_param, ":current_owner_code", 2),
      
      (store_div, ":number_of_agents_around_flag_team_1", ":current_owner_code", 100),
      (store_mod, ":number_of_agents_around_flag_team_2", ":current_owner_code", 100),
      
      (store_add, ":cur_flag_owner_counts_slot", multi_data_flag_players_around_begin, ":flag_no"),
      (troop_set_slot, "trp_multiplayer_data", ":cur_flag_owner_counts_slot", ":current_owner_code"),
      
      (call_script, "script_move_headquarters_flags", ":flag_no", ":number_of_agents_around_flag_team_1", ":number_of_agents_around_flag_team_2"),
  ]),
  
  #script_change_flag_owner
  # INPUT: arg1 = flag_no, arg2 = owner_code
  # OUTPUT: none
  ("change_flag_owner",
    [
      (store_script_param, ":flag_no", 1),
      (store_script_param, ":owner_code", 2),
      
      (try_begin),
        (lt, ":owner_code", 0),
        (val_add, ":owner_code", 1),
        (val_mul, ":owner_code", -1),
      (try_end),
      
      (store_div, ":owner_team_no", ":owner_code", 100),
      (store_mod, ":shown_flag_no", ":owner_code", 100),
      
      (store_add, ":cur_flag_slot", multi_data_flag_owner_begin, ":flag_no"),
      (troop_get_slot, ":older_owner_team_no", "trp_multiplayer_data", ":cur_flag_slot"),
      
      (store_add, ":cur_flag_slot", multi_data_flag_owner_begin, ":flag_no"),
      (troop_set_slot, "trp_multiplayer_data", ":cur_flag_slot", ":owner_team_no"),
      
      #senchronizing flag positions
      (try_begin),
        #(this_or_next|eq, ":initial_flags", 0), #moved after auto-animating
        (multiplayer_is_server),
        
        (scene_prop_get_instance, ":pole_id", "spr_headquarters_pole_code_only", ":flag_no"),
        (try_begin),
          (eq, ":owner_team_no", 0), #if new owner team is 0 then flags are at bottom
          (neq, ":older_owner_team_no", -1), #clients
          (assign, ":continue", 1),
          (try_begin),
            (multiplayer_is_server),
            (eq, "$g_placing_initial_flags", 1),
            (assign, ":continue", 0),
          (try_end),
          (eq, ":continue", 1),
          (prop_instance_get_position, pos0, ":pole_id"),
          (position_move_z, pos0, multi_headquarters_distance_to_change_flag),
        (else_try),
          (prop_instance_get_position, pos0, ":pole_id"), #if new owner team is not 0 then flags are at top
          (position_move_z, pos0, multi_headquarters_pole_height),
        (try_end),
        
        (scene_prop_get_instance, ":flag_id", "$team_1_flag_scene_prop", ":flag_no"),
        (prop_instance_stop_animating, ":flag_id"),
        (prop_instance_set_position, ":flag_id", pos0),
        
        (scene_prop_get_instance, ":flag_id", "$team_2_flag_scene_prop", ":flag_no"),
        (prop_instance_stop_animating, ":flag_id"),
        (prop_instance_set_position, ":flag_id", pos0),
        
        (scene_prop_get_instance, ":flag_id", "spr_headquarters_flag_gray_code_only", ":flag_no"),
        (prop_instance_stop_animating, ":flag_id"),
        (prop_instance_set_position, ":flag_id", pos0),
      (try_end),
      
      #setting visibilities of flags
      (try_begin),
        (eq, ":shown_flag_no", 0),
        (scene_prop_get_instance, ":flag_id", "$team_1_flag_scene_prop", ":flag_no"),
        (scene_prop_set_visibility, ":flag_id", 0),
        (scene_prop_get_instance, ":flag_id", "$team_2_flag_scene_prop", ":flag_no"),
        (scene_prop_set_visibility, ":flag_id", 0),
        (scene_prop_get_instance, ":flag_id", "spr_headquarters_flag_gray_code_only", ":flag_no"),
        (scene_prop_set_visibility, ":flag_id", 1),
      (else_try),
        (eq, ":shown_flag_no", 1),
        (scene_prop_get_instance, ":flag_id", "$team_1_flag_scene_prop", ":flag_no"),
        (scene_prop_set_visibility, ":flag_id", 1),
        (scene_prop_get_instance, ":flag_id", "$team_2_flag_scene_prop", ":flag_no"),
        (scene_prop_set_visibility, ":flag_id", 0),
        (scene_prop_get_instance, ":flag_id", "spr_headquarters_flag_gray_code_only", ":flag_no"),
        (scene_prop_set_visibility, ":flag_id", 0),
      (else_try),
        (eq, ":shown_flag_no", 2),
        (scene_prop_get_instance, ":flag_id", "$team_1_flag_scene_prop", ":flag_no"),
        (scene_prop_set_visibility, ":flag_id", 0),
        (scene_prop_get_instance, ":flag_id", "$team_2_flag_scene_prop", ":flag_no"),
        (scene_prop_set_visibility, ":flag_id", 1),
        (scene_prop_get_instance, ":flag_id", "spr_headquarters_flag_gray_code_only", ":flag_no"),
        (scene_prop_set_visibility, ":flag_id", 0),
      (try_end),
      
      #other
      (store_add, ":cur_flag_players_around_slot", multi_data_flag_players_around_begin, ":flag_no"),
      (troop_get_slot, ":players_around_code", "trp_multiplayer_data", ":cur_flag_players_around_slot"),
      
      (store_div, ":number_of_agents_around_flag_team_1", ":players_around_code", 100),
      (store_mod, ":number_of_agents_around_flag_team_2", ":players_around_code", 100),
      
      (call_script, "script_move_headquarters_flags", ":flag_no", ":number_of_agents_around_flag_team_1", ":number_of_agents_around_flag_team_2"),
  ]),
  
  #script_move_object_to_nearest_entry_point
  # INPUT: none
  # OUTPUT: none
  ("move_object_to_nearest_entry_point",
    [
      (store_script_param, ":scene_prop_no", 1),
      
      (scene_prop_get_num_instances, ":num_instances", ":scene_prop_no"),
      
      (try_for_range, ":instance_no", 0, ":num_instances"),
        (scene_prop_get_instance, ":instance_id", ":scene_prop_no", ":instance_no"),
        (prop_instance_get_position, pos0, ":instance_id"),
        
        (assign, ":smallest_dist", -1),
        (try_for_range, ":entry_point_no", multi_entry_points_for_usable_items_start, multi_entry_points_for_usable_items_end),
          (entry_point_get_position, pos1, ":entry_point_no"),
          (get_sq_distance_between_positions, ":dist", pos0, pos1),
          (this_or_next|eq, ":smallest_dist", -1),
          (lt, ":dist", ":smallest_dist"),
          (assign, ":smallest_dist", ":dist"),
          (assign, ":nearest_entry_point", ":entry_point_no"),
        (try_end),
        
        (try_begin),
          (ge, ":smallest_dist", 0),
          (lt, ":smallest_dist", 22500), #max 15m distance
          (entry_point_get_position, pos1, ":nearest_entry_point"),
          (position_rotate_x, pos1, -90),
          (prop_instance_animate_to_position, ":instance_id", pos1, 1),
        (try_end),
      (try_end),
  ]),
  
  
  #script_multiplayer_server_on_agent_spawn_common
  # INPUT: arg1 = agent_no
  # OUTPUT: none
  ("multiplayer_server_on_agent_spawn_common",
    [
      (store_script_param, ":agent_no", 1),
      (agent_set_slot, ":agent_no", slot_agent_in_duel_with, -1),
      (try_begin),
        (agent_is_non_player, ":agent_no"),
        (assign, "$g_multiplayer_ready_for_spawning_agent", 1),
      (try_end),
  ]),
  
  #script_multiplayer_server_player_joined_common
  # INPUT: arg1 = player_no
  # OUTPUT: none
  ("multiplayer_server_player_joined_common",
    [
      (store_script_param, ":player_no", 1),
      (try_begin),
        (this_or_next|player_is_active, ":player_no"),
        (eq, ":player_no", 0),
        (call_script, "script_multiplayer_init_player_slots", ":player_no"),
        (store_mission_timer_a, ":player_join_time"),
        (player_set_slot, ":player_no", slot_player_join_time, ":player_join_time"),
        (player_set_slot, ":player_no", slot_player_first_spawn, 1),
        #fight and destroy only
        (player_set_slot, ":player_no", slot_player_damage_given_to_target_1, 0),
        (player_set_slot, ":player_no", slot_player_damage_given_to_target_2, 0),
        #fight and destroy only end
        (try_begin),
          (multiplayer_is_server),
          (assign, ":initial_gold", multi_initial_gold_value),
          (val_mul, ":initial_gold", "$g_multiplayer_initial_gold_multiplier"),
          (val_div, ":initial_gold", 100),
          (player_set_gold, ":player_no", ":initial_gold"),
          (call_script, "script_multiplayer_send_initial_information", ":player_no"),
        (try_end),
      (try_end),
  ]),
  
  #script_multiplayer_server_before_mission_start_common
  # INPUT: none
  # OUTPUT: none
  ("multiplayer_server_before_mission_start_common",
    [
      (try_begin),
        (scene_allows_mounted_units),
        (assign, "$g_horses_are_avaliable", 1),
      (else_try),
        (assign, "$g_horses_are_avaliable", 0),
      (try_end),
      #(scene_set_day_time, 15),
      (assign, "$g_multiplayer_mission_end_screen", 0),
      
      (get_max_players, ":num_players"),
      (try_for_range, ":player_no", 0, ":num_players"),
        (player_is_active, ":player_no"),
        (call_script, "script_multiplayer_init_player_slots", ":player_no"),
        (assign, ":initial_gold", multi_initial_gold_value),
        (val_mul, ":initial_gold", "$g_multiplayer_initial_gold_multiplier"),
        (val_div, ":initial_gold", 100),
        (player_set_gold, ":player_no", ":initial_gold"),
        (player_set_slot, ":player_no", slot_player_first_spawn, 1), #not required in siege, bt, fd
      (try_end),
  ]),
  
  #script_multiplayer_server_on_agent_killed_or_wounded_common
  # INPUT: arg1 = dead_agent_no, arg2 = killer_agent_no
  # OUTPUT: none
  ("multiplayer_server_on_agent_killed_or_wounded_common",
    [
      (store_script_param, ":dead_agent_no", 1),
      (store_script_param, ":killer_agent_no", 2),
      
      (call_script, "script_multiplayer_event_agent_killed_or_wounded", ":dead_agent_no", ":killer_agent_no"),
      #adding 1 score points to agent which kills enemy agent at server
      (try_begin),
        (multiplayer_is_server),
        (try_begin), #killing myself because of some reason (friend hit, fall, team change)
          (lt, ":killer_agent_no", 0),
          (ge, ":dead_agent_no", 0),
          (neg|agent_is_non_player, ":dead_agent_no"),
          (agent_get_player_id, ":dead_agent_player_id", ":dead_agent_no"),
          (player_is_active, ":dead_agent_player_id"),
          (player_get_score, ":dead_agent_player_score", ":dead_agent_player_id"),
          (val_add, ":dead_agent_player_score", -1),
          (player_set_score, ":dead_agent_player_id", ":dead_agent_player_score"),
        (else_try), #killing teammate
          (ge, ":killer_agent_no", 0),
          (ge, ":dead_agent_no", 0),
          (agent_get_team, ":killer_team_no", ":killer_agent_no"),
          (agent_get_team, ":dead_team_no", ":dead_agent_no"),
          (eq, ":killer_team_no", ":dead_team_no"),
          (neg|agent_is_non_player, ":killer_agent_no"),
          (agent_get_player_id, ":killer_agent_player_id", ":killer_agent_no"),
          (player_is_active, ":killer_agent_player_id"),
          (player_get_score, ":killer_agent_player_score", ":killer_agent_player_id"),
          (val_add, ":killer_agent_player_score", -1),
          (player_set_score, ":killer_agent_player_id", ":killer_agent_player_score"),
          #(player_get_kill_count, ":killer_agent_player_kill_count", ":killer_agent_player_id"),
          #(val_add, ":killer_agent_player_kill_count", -2),
          #(player_set_kill_count, ":killer_agent_player_id", ":killer_agent_player_kill_count"),
        (else_try), #killing enemy
          (ge, ":killer_agent_no", 0),
          (ge, ":dead_agent_no", 0),
          (agent_is_human, ":dead_agent_no"),
          (agent_is_human, ":killer_agent_no"),
          (try_begin),
            (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_sea),#naval battles
            (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_invasion),
            (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_raid),
            (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle), #capitan chief
            (eq, "$g_multiplayer_game_type", multiplayer_game_type_battle),
            (try_begin),
              (eq, "$g_battle_death_mode_started", 1),
              (neq, ":dead_agent_no", ":killer_agent_no"),
              (call_script, "script_calculate_new_death_waiting_time_at_death_mod"),
            (try_end),
          (try_end),
          (try_begin),
            (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_invasion),
            (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_raid),
            (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_sea),#naval battles
            (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle), #capitan chief
            (eq, "$g_multiplayer_game_type", multiplayer_game_type_battle),
            (agent_get_player_id, ":dead_player_no", ":dead_agent_no"),
            (try_begin),
              (ge, ":dead_player_no", 0),
              (player_is_active, ":dead_player_no"),
              (neg|agent_is_non_player, ":dead_agent_no"),
              (try_for_agents, ":cur_agent"),
                (agent_is_non_player, ":cur_agent"),
                (agent_is_human, ":cur_agent"),
                (agent_is_alive, ":cur_agent"),
                (agent_get_group, ":agent_group", ":cur_agent"),
                (try_begin),
                  (eq, ":dead_player_no", ":agent_group"),
                  (agent_set_group, ":cur_agent", -1),
                (try_end),
              (try_end),
            (try_end),
          (try_end),
          (neg|agent_is_non_player, ":killer_agent_no"),
          (agent_get_player_id, ":killer_agent_player_id", ":killer_agent_no"),
          (player_is_active, ":killer_agent_player_id"),
          (player_get_score, ":killer_agent_player_score", ":killer_agent_player_id"),
          (agent_get_team, ":killer_agent_team", ":killer_agent_no"),
          (agent_get_team, ":dead_agent_team", ":dead_agent_no"),
          (try_begin),
            (neq, ":killer_agent_team", ":dead_agent_team"),
            (val_add, ":killer_agent_player_score", 1),
          (else_try),
            (val_add, ":killer_agent_player_score", -1),
          (try_end),
          (player_set_score, ":killer_agent_player_id", ":killer_agent_player_score"),
        (try_end),
      (try_end),
      
      (call_script, "script_add_kill_death_counts", ":killer_agent_no", ":dead_agent_no"),
      #money management
      (try_begin), #chief capitan
        (neq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle), #capitan chief
        (call_script, "script_money_management_after_agent_death", ":killer_agent_no", ":dead_agent_no"),
      (try_end), #chief capitan acaba
  ]),
  
  #script_multiplayer_close_gate_if_it_is_open
  # INPUT: none
  # OUTPUT: none
  ("multiplayer_close_gate_if_it_is_open",
    [
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_winch_b"),
      (try_for_range, ":cur_prop_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":prop_instance_id", "spr_winch_b", ":cur_prop_instance"),
        (scene_prop_slot_eq, ":prop_instance_id", scene_prop_open_or_close_slot, 1),
        (scene_prop_get_instance, ":effected_object_instance_id", "spr_portcullis", ":cur_prop_instance"),
        (prop_instance_get_starting_position, pos0, ":effected_object_instance_id"),
        (prop_instance_animate_to_position, ":effected_object_instance_id", pos0, 1),
      (try_end),
  ]),
  
  #script_multiplayer_move_moveable_objects_initial_positions
  # INPUT: none
  # OUTPUT: none
  ("multiplayer_move_moveable_objects_initial_positions",
    [
      (call_script, "script_move_object_to_nearest_entry_point", "spr_siege_ladder_move_6m"),
      (call_script, "script_move_object_to_nearest_entry_point", "spr_siege_ladder_move_8m"),
      (call_script, "script_move_object_to_nearest_entry_point", "spr_siege_ladder_move_10m"),
      (call_script, "script_move_object_to_nearest_entry_point", "spr_siege_ladder_move_12m"),
      (call_script, "script_move_object_to_nearest_entry_point", "spr_siege_ladder_move_14m"),
  ]),
  
  #script_move_belfries_to_their_first_entry_point
  # INPUT: none
  # OUTPUT: none
  ("move_belfries_to_their_first_entry_point",
    [
      (store_script_param, ":belfry_body_scene_prop", 1),
      
      (set_fixed_point_multiplier, 100),
      (scene_prop_get_num_instances, ":num_belfries", ":belfry_body_scene_prop"),
      
      (try_for_range, ":belfry_no", 0, ":num_belfries"),
        #belfry
        (scene_prop_get_instance, ":belfry_scene_prop_id", ":belfry_body_scene_prop", ":belfry_no"),
        (prop_instance_get_position, pos0, ":belfry_scene_prop_id"),
        
        (try_begin),
          (eq, ":belfry_body_scene_prop", "spr_belfry_a"),
          #belfry platform_a
          (scene_prop_get_instance, ":belfry_platform_a_scene_prop_id", "spr_belfry_platform_a", ":belfry_no"),
          #belfry platform_b
          (scene_prop_get_instance, ":belfry_platform_b_scene_prop_id", "spr_belfry_platform_b", ":belfry_no"),
        (else_try),
          #belfry platform_a
          (scene_prop_get_instance, ":belfry_platform_a_scene_prop_id", "spr_belfry_b_platform_a", ":belfry_no"),
        (try_end),
        
        #belfry wheel_1
        (store_mul, ":wheel_no", ":belfry_no", 3),
        (try_begin),
          (eq, ":belfry_body_scene_prop", "spr_belfry_b"),
          (scene_prop_get_num_instances, ":number_of_belfry_a", "spr_belfry_a"),
          (store_mul, ":number_of_belfry_a_wheels", ":number_of_belfry_a", 3),
          (val_add, ":wheel_no", ":number_of_belfry_a_wheels"),
        (try_end),
        (scene_prop_get_instance, ":belfry_wheel_1_scene_prop_id", "spr_belfry_wheel", ":wheel_no"),
        #belfry wheel_2
        (val_add, ":wheel_no", 1),
        (scene_prop_get_instance, ":belfry_wheel_2_scene_prop_id", "spr_belfry_wheel", ":wheel_no"),
        #belfry wheel_3
        (val_add, ":wheel_no", 1),
        (scene_prop_get_instance, ":belfry_wheel_3_scene_prop_id", "spr_belfry_wheel", ":wheel_no"),
        
        (store_add, ":belfry_first_entry_point_id", 11, ":belfry_no"), #belfry entry points are 110..119 and 120..129 and 130..139
        (try_begin),
          (eq, ":belfry_body_scene_prop", "spr_belfry_b"),
          (scene_prop_get_num_instances, ":number_of_belfry_a", "spr_belfry_a"),
          (val_add, ":belfry_first_entry_point_id", ":number_of_belfry_a"),
        (try_end),
        (val_mul, ":belfry_first_entry_point_id", 10),
        (entry_point_get_position, pos1, ":belfry_first_entry_point_id"),
        
        #this code block is taken from module_mission_templates.py (multiplayer_server_check_belfry_movement)
        #up down rotation of belfry's next entry point
        (init_position, pos9),
        (position_set_y, pos9, -500), #go 5.0 meters back
        (position_set_x, pos9, -300), #go 3.0 meters left
        (position_transform_position_to_parent, pos10, pos1, pos9),
        (position_get_distance_to_terrain, ":height_to_terrain_1", pos10), #learn distance between 5 meters back of entry point(pos10) and ground level at left part of belfry
        
        (init_position, pos9),
        (position_set_y, pos9, -500), #go 5.0 meters back
        (position_set_x, pos9, 300), #go 3.0 meters right
        (position_transform_position_to_parent, pos10, pos1, pos9),
        (position_get_distance_to_terrain, ":height_to_terrain_2", pos10), #learn distance between 5 meters back of entry point(pos10) and ground level at right part of belfry
        
        (store_add, ":height_to_terrain", ":height_to_terrain_1", ":height_to_terrain_2"),
        (val_mul, ":height_to_terrain", 100), #because of fixed point multiplier
        
        (store_div, ":rotate_angle_of_next_entry_point", ":height_to_terrain", 24), #if there is 1 meters of distance (100cm) then next target position will rotate by 2 degrees. #ac sonra
        (init_position, pos20),
        (position_rotate_x_floating, pos20, ":rotate_angle_of_next_entry_point"),
        (position_transform_position_to_parent, pos23, pos1, pos20),
        
        #right left rotation of belfry's next entry point
        (init_position, pos9),
        (position_set_x, pos9, -300), #go 3.0 meters left
        (position_transform_position_to_parent, pos10, pos1, pos9), #applying 3.0 meters in -x to position of next entry point target, final result is in pos10
        (position_get_distance_to_terrain, ":height_to_terrain_at_left", pos10), #learn distance between 3.0 meters left of entry point(pos10) and ground level
        (init_position, pos9),
        (position_set_x, pos9, 300), #go 3.0 meters left
        (position_transform_position_to_parent, pos10, pos1, pos9), #applying 3.0 meters in x to position of next entry point target, final result is in pos10
        (position_get_distance_to_terrain, ":height_to_terrain_at_right", pos10), #learn distance between 3.0 meters right of entry point(pos10) and ground level
        (store_sub, ":height_to_terrain_1", ":height_to_terrain_at_left", ":height_to_terrain_at_right"),
        
        (init_position, pos9),
        (position_set_x, pos9, -300), #go 3.0 meters left
        (position_set_y, pos9, -500), #go 5.0 meters forward
        (position_transform_position_to_parent, pos10, pos1, pos9), #applying 3.0 meters in -x to position of next entry point target, final result is in pos10
        (position_get_distance_to_terrain, ":height_to_terrain_at_left", pos10), #learn distance between 3.0 meters left of entry point(pos10) and ground level
        (init_position, pos9),
        (position_set_x, pos9, 300), #go 3.0 meters left
        (position_set_y, pos9, -500), #go 5.0 meters forward
        (position_transform_position_to_parent, pos10, pos1, pos9), #applying 3.0 meters in x to position of next entry point target, final result is in pos10
        (position_get_distance_to_terrain, ":height_to_terrain_at_right", pos10), #learn distance between 3.0 meters right of entry point(pos10) and ground level
        (store_sub, ":height_to_terrain_2", ":height_to_terrain_at_left", ":height_to_terrain_at_right"),
        
        (store_add, ":height_to_terrain", ":height_to_terrain_1", ":height_to_terrain_2"),
        (val_mul, ":height_to_terrain", 100), #100 is because of fixed_point_multiplier
        (store_div, ":rotate_angle_of_next_entry_point", ":height_to_terrain", 24), #if there is 1 meters of distance (100cm) then next target position will rotate by 25 degrees.
        (val_mul, ":rotate_angle_of_next_entry_point", -1),
        
        (init_position, pos20),
        (position_rotate_y_floating, pos20, ":rotate_angle_of_next_entry_point"),
        (position_transform_position_to_parent, pos22, pos23, pos20),
        
        (copy_position, pos1, pos22),
        #end of code block
        
        #belfry
        (prop_instance_stop_animating, ":belfry_scene_prop_id"),
        (prop_instance_set_position, ":belfry_scene_prop_id", pos1),
        
        #belfry platforms
        (try_begin),
          (eq, ":belfry_body_scene_prop", "spr_belfry_a"),
          
          #belfry platform_a
          (prop_instance_get_position, pos6, ":belfry_platform_a_scene_prop_id"),
          (position_transform_position_to_local, pos7, pos0, pos6),
          (position_transform_position_to_parent, pos8, pos1, pos7),
          (try_begin),
            (neg|scene_prop_slot_eq, ":belfry_scene_prop_id", scene_prop_belfry_platform_moved, 0),
            
            (init_position, pos20),
            (position_rotate_x, pos20, 90),
            (position_transform_position_to_parent, pos8, pos8, pos20),
          (try_end),
          (prop_instance_stop_animating, ":belfry_platform_a_scene_prop_id"),
          (prop_instance_set_position, ":belfry_platform_a_scene_prop_id", pos8),
          #belfry platform_b
          (prop_instance_get_position, pos6, ":belfry_platform_b_scene_prop_id"),
          (position_transform_position_to_local, pos7, pos0, pos6),
          (position_transform_position_to_parent, pos8, pos1, pos7),
          (prop_instance_stop_animating, ":belfry_platform_b_scene_prop_id"),
          (prop_instance_set_position, ":belfry_platform_b_scene_prop_id", pos8),
        (else_try),
          #belfry platform_a
          (prop_instance_get_position, pos6, ":belfry_platform_a_scene_prop_id"),
          (position_transform_position_to_local, pos7, pos0, pos6),
          (position_transform_position_to_parent, pos8, pos1, pos7),
          (try_begin),
            (neg|scene_prop_slot_eq, ":belfry_scene_prop_id", scene_prop_belfry_platform_moved, 0),
            
            (init_position, pos20),
            (position_rotate_x, pos20, 50),
            (position_transform_position_to_parent, pos8, pos8, pos20),
          (try_end),
          (prop_instance_stop_animating, ":belfry_platform_a_scene_prop_id"),
          (prop_instance_set_position, ":belfry_platform_a_scene_prop_id", pos8),
        (try_end),
        
        #belfry wheel_1
        (store_mul, ":wheel_no", ":belfry_no", 3),
        (try_begin),
          (eq, ":belfry_body_scene_prop", "spr_belfry_b"),
          (scene_prop_get_num_instances, ":number_of_belfry_a", "spr_belfry_a"),
          (store_mul, ":number_of_belfry_a_wheels", ":number_of_belfry_a", 3),
          (val_add, ":wheel_no", ":number_of_belfry_a_wheels"),
        (try_end),
        (prop_instance_get_position, pos6, ":belfry_wheel_1_scene_prop_id"),
        (position_transform_position_to_local, pos7, pos0, pos6),
        (position_transform_position_to_parent, pos8, pos1, pos7),
        (prop_instance_stop_animating, ":belfry_wheel_1_scene_prop_id"),
        (prop_instance_set_position, ":belfry_wheel_1_scene_prop_id", pos8),
        #belfry wheel_2
        (prop_instance_get_position, pos6, ":belfry_wheel_2_scene_prop_id"),
        (position_transform_position_to_local, pos7, pos0, pos6),
        (position_transform_position_to_parent, pos8, pos1, pos7),
        (prop_instance_stop_animating, ":belfry_wheel_2_scene_prop_id"),
        (prop_instance_set_position, ":belfry_wheel_2_scene_prop_id", pos8),
        #belfry wheel_3
        (prop_instance_get_position, pos6, ":belfry_wheel_3_scene_prop_id"),
        (position_transform_position_to_local, pos7, pos0, pos6),
        (position_transform_position_to_parent, pos8, pos1, pos7),
        (prop_instance_stop_animating, ":belfry_wheel_3_scene_prop_id"),
        (prop_instance_set_position, ":belfry_wheel_3_scene_prop_id", pos8),
      (try_end),
  ]),
  
  #script_team_set_score
  # INPUT: arg1 = team_no, arg2 = score
  # OUTPUT: none
  ("team_set_score",
    [
      (store_script_param, ":team_no", 1),
      (store_script_param, ":score", 2),
      
      (team_set_score, ":team_no", ":score"),
  ]),
  
  #script_player_set_score
  # INPUT: arg1 = player_no, arg2 = score
  # OUTPUT: none
  ("player_set_score",
    [
      (store_script_param, ":player_no", 1),
      (store_script_param, ":score", 2),
      
      (player_set_score, ":player_no", ":score"),
  ]),
  
  #script_player_set_kill_count
  # INPUT: arg1 = player_no, arg2 = score
  # OUTPUT: none
  ("player_set_kill_count",
    [
      (store_script_param, ":player_no", 1),
      (store_script_param, ":score", 2),
      
      (player_set_kill_count, ":player_no", ":score"),
  ]),
  
  #script_player_set_death_count
  # INPUT: arg1 = player_no, arg2 = score
  # OUTPUT: none
  ("player_set_death_count",
    [
      (store_script_param, ":player_no", 1),
      (store_script_param, ":score", 2),
      
      (player_set_death_count, ":player_no", ":score"),
  ]),
  
  #script_set_attached_scene_prop
  # INPUT: arg1 = agent_id, arg2 = flag_id
  # OUTPUT: none
  ("set_attached_scene_prop",
    [
      (store_script_param, ":agent_id", 1),
      (store_script_param, ":flag_id", 2),
      
      (try_begin), #if current mod is capture the flag and attached scene prop is flag then change flag situation of flag owner team.
        (scene_prop_get_instance, ":red_flag_id", "spr_tutorial_flag_red", 0),
        (scene_prop_get_instance, ":blue_flag_id", "spr_tutorial_flag_blue", 0),
        (assign, ":flag_owner_team", -1),
        (try_begin),
          (ge, ":red_flag_id", 0),
          (eq, ":flag_id", ":red_flag_id"),
          (assign, ":flag_owner_team", 0),
        (else_try),
          (ge, ":blue_flag_id", 0),
          (eq, ":flag_id", ":blue_flag_id"),
          (assign, ":flag_owner_team", 1),
        (try_end),
        (ge, ":flag_owner_team", 0),
        (team_set_slot, ":flag_owner_team", slot_team_flag_situation, 1), #1-stolen flag
      (try_end),
      
      (agent_set_attached_scene_prop, ":agent_id", ":flag_id"),
      (agent_set_attached_scene_prop_x, ":agent_id", 20),
      (agent_set_attached_scene_prop_z, ":agent_id", 50),
  ]),
  
  #script_set_team_flag_situation
  # INPUT: arg1 = team_no, arg2 = score
  # OUTPUT: none
  ("set_team_flag_situation",
    [
      (store_script_param, ":team_no", 1),
      (store_script_param, ":flag_situation", 2),
      
      (team_set_slot, ":team_no", slot_team_flag_situation, ":flag_situation"),
  ]),
  
  #script_start_death_mode
  # INPUT: none
  # OUTPUT: none
  ("start_death_mode",
    [
      (assign, "$g_multiplayer_message_type", multiplayer_message_type_start_death_mode),
      (start_presentation, "prsnt_multiplayer_message_1"),
  ]),
  
  #capitan chief capitan mensaje exploracion
  ("start_capitan_mode",
    [
      (assign, "$g_multiplayer_message_type", multiplayer_message_type_start_capitan_mode),
      (start_presentation, "prsnt_multiplayer_message_1"),
  ]),
  
  #script_calculate_new_death_waiting_time_at_death_mod
  # INPUT: none
  # OUTPUT: none
  ("calculate_new_death_waiting_time_at_death_mod",
    [
      (assign, ":num_living_players", 0), #count number of living players to find out death wait time
      (try_begin),
        (try_for_agents, ":agent_no"),
          (agent_is_human, ":agent_no"),
          (agent_is_alive, ":agent_no"),
          (val_add, ":num_living_players", 1),
        (try_end),
      (try_end),
      
      (val_add, ":num_living_players", multiplayer_battle_formula_value_a),
      (set_fixed_point_multiplier, 100),
      (store_mul, ":num_living_players", ":num_living_players", 100),
      (store_sqrt, ":sqrt_num_living_players", ":num_living_players"),
      (store_div, "$g_battle_waiting_seconds", multiplayer_battle_formula_value_b, ":sqrt_num_living_players"),
      (store_mission_timer_a, "$g_death_mode_part_1_start_time"),
  ]),
  
  # #script_calculate_number_of_targets_destroyed
  # # INPUT: none
  # # OUTPUT: none
  
  # ("calculate_number_of_targets_destroyed",
    # [
      # (assign, "$g_number_of_targets_destroyed", 0),
      # (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_catapult_destructible"),
      # (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        # (scene_prop_get_instance, ":cur_instance_id", "spr_catapult_destructible", ":cur_instance"),
        # (prop_instance_get_starting_position, pos0, ":cur_instance_id"),
        # (prop_instance_get_position, pos1, ":cur_instance_id"),
        # (get_sq_distance_between_positions_in_meters, ":dist", pos0, pos1),
        # (gt, ":dist", 2), #this can be 0 or 1 too.
        # (val_add, "$g_number_of_targets_destroyed", 1),
      # (try_end),
      
      # (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_trebuchet_destructible"),
      # (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        # (scene_prop_get_instance, ":cur_instance_id", "spr_trebuchet_destructible", ":cur_instance"),
        # (prop_instance_get_starting_position, pos0, ":cur_instance_id"),
        # (prop_instance_get_position, pos1, ":cur_instance_id"),
        # (get_sq_distance_between_positions_in_meters, ":dist", pos0, pos1),
        # (gt, ":dist", 2), #this can be 0 or 1 too.
        # (val_add, "$g_number_of_targets_destroyed", 1),
      # (try_end),
  # ]),
  
  #script_initialize_objects
  # INPUT: none
  # OUTPUT: none
  ("initialize_objects",
    [
      (assign, ":number_of_players", 0),
      (get_max_players, ":num_players"),
      (try_for_range, ":player_no", 0, ":num_players"),
        (player_is_active, ":player_no"),
        (val_add, ":number_of_players", 1),
      (try_end),
      
      #1 player = (Sqrt(1) - 1) * 200 + 1200 = 1200, 1800 (minimum)
      #4 player = (Sqrt(4) - 1) * 200 + 1200 = 1400, 2100
      #9 player = (Sqrt(9) - 1) * 200 + 1200 = 1600, 2400
      #16 player = (Sqrt(16) - 1) * 200 + 1200 = 1800, 2700 (general used)
      #25 player = (Sqrt(25) - 1) * 200 + 1200 = 2000, 3000 (average)
      #36 player = (Sqrt(36) - 1) * 200 + 1200 = 2200, 3300
      #49 player = (Sqrt(49) - 1) * 200 + 1200 = 2400, 3600
      #64 player = (Sqrt(49) - 1) * 200 + 1200 = 2600, 3900
      
      (set_fixed_point_multiplier, 100),
      (val_mul, ":number_of_players", 100),
      (store_sqrt, ":number_of_players", ":number_of_players"),
      (val_sub, ":number_of_players", 100),
      (val_max, ":number_of_players", 0),
      (store_mul, ":effect_of_number_of_players", ":number_of_players", 2),
      (store_add, ":health_catapult", multi_minimum_target_health, ":effect_of_number_of_players"),
      (store_mul, ":health_trebuchet", ":health_catapult", 15), #trebuchet's health is 1.5x of catapult's
      (val_div, ":health_trebuchet", 10),
      (store_mul, ":health_sally_door", ":health_catapult", 18), #sally door's health is 1.8x of catapult's
      (val_div, ":health_sally_door", 10),
      (store_mul, ":health_sally_door_double", ":health_sally_door", 2),
      
      (assign, "$g_number_of_targets_destroyed", 0),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_catapult_destructible"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_catapult_destructible", ":cur_instance"),
        (prop_instance_get_starting_position, pos0, ":cur_instance_id"),
        (prop_instance_stop_animating, ":cur_instance_id"),
        (prop_instance_set_position, ":cur_instance_id", pos0),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_catapult"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_trebuchet_destructible"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_trebuchet_destructible", ":cur_instance"),
        (prop_instance_get_starting_position, pos0, ":cur_instance_id"),
        (prop_instance_stop_animating, ":cur_instance_id"),
        (prop_instance_set_position, ":cur_instance_id", pos0),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_trebuchet"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_castle_e_sally_door_a"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_castle_e_sally_door_a", ":cur_instance"),
        (prop_instance_get_starting_position, pos0, ":cur_instance_id"),
        (prop_instance_stop_animating, ":cur_instance_id"),
        (prop_instance_set_position, ":cur_instance_id", pos0),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_castle_f_sally_door_a"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_castle_f_sally_door_a", ":cur_instance"),
        (prop_instance_get_starting_position, pos0, ":cur_instance_id"),
        (prop_instance_stop_animating, ":cur_instance_id"),
        (prop_instance_set_position, ":cur_instance_id", pos0),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_earth_sally_gate_left"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_earth_sally_gate_left", ":cur_instance"),
        (prop_instance_get_starting_position, pos0, ":cur_instance_id"),
        (prop_instance_stop_animating, ":cur_instance_id"),
        (prop_instance_set_position, ":cur_instance_id", pos0),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door_double"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_earth_sally_gate_right"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_earth_sally_gate_right", ":cur_instance"),
        (prop_instance_get_starting_position, pos0, ":cur_instance_id"),
        (prop_instance_stop_animating, ":cur_instance_id"),
        (prop_instance_set_position, ":cur_instance_id", pos0),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door_double"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_viking_keep_destroy_sally_door_left"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_viking_keep_destroy_sally_door_left", ":cur_instance"),
        (prop_instance_get_starting_position, pos0, ":cur_instance_id"),
        (prop_instance_stop_animating, ":cur_instance_id"),
        (prop_instance_set_position, ":cur_instance_id", pos0),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_viking_keep_destroy_sally_door_right"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_viking_keep_destroy_sally_door_right", ":cur_instance"),
        (prop_instance_get_starting_position, pos0, ":cur_instance_id"),
        (prop_instance_stop_animating, ":cur_instance_id"),
        (prop_instance_set_position, ":cur_instance_id", pos0),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door"),
      (try_end),
      
      (store_div, ":health_sally_door_div_3", ":health_sally_door", 3),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_castle_f_door_a"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_castle_f_door_a", ":cur_instance"),
        (prop_instance_get_starting_position, pos0, ":cur_instance_id"),
        (prop_instance_stop_animating, ":cur_instance_id"),
        (prop_instance_set_position, ":cur_instance_id", pos0),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door_div_3"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_castle_f_door_b"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_castle_f_door_b", ":cur_instance"),
        (prop_instance_get_starting_position, pos0, ":cur_instance_id"),
        (prop_instance_stop_animating, ":cur_instance_id"),
        (prop_instance_set_position, ":cur_instance_id", pos0),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door_div_3"),
      (try_end),
  ]),
  
  #script_initialize_objects_clients
  # INPUT: none
  # OUTPUT: none
  ("initialize_objects_clients",
    [
      (assign, ":number_of_players", 0),
      (get_max_players, ":num_players"),
      (try_for_range, ":player_no", 0, ":num_players"),
        (player_is_active, ":player_no"),
        (val_add, ":number_of_players", 1),
      (try_end),
      
      #1 player = (Sqrt(1) - 1) * 200 + 1200 = 1200, 1800 (minimum)
      #4 player = (Sqrt(4) - 1) * 200 + 1200 = 1400, 2100
      #9 player = (Sqrt(9) - 1) * 200 + 1200 = 1600, 2400
      #16 player = (Sqrt(16) - 1) * 200 + 1200 = 1800, 2700 (general used)
      #25 player = (Sqrt(25) - 1) * 200 + 1200 = 2000, 3000 (average)
      #36 player = (Sqrt(36) - 1) * 200 + 1200 = 2200, 3300
      #49 player = (Sqrt(49) - 1) * 200 + 1200 = 2400, 3600
      #64 player = (Sqrt(49) - 1) * 200 + 1200 = 2600, 3900
      
      (set_fixed_point_multiplier, 100),
      (val_mul, ":number_of_players", 100),
      (store_sqrt, ":number_of_players", ":number_of_players"),
      (val_sub, ":number_of_players", 100),
      (val_max, ":number_of_players", 0),
      (store_mul, ":effect_of_number_of_players", ":number_of_players", 2),
      (store_add, ":health_catapult", multi_minimum_target_health, ":effect_of_number_of_players"),
      (store_mul, ":health_trebuchet", ":health_catapult", 15), #trebuchet's health is 1.5x of catapult's
      (val_div, ":health_trebuchet", 10),
      (store_mul, ":health_sally_door", ":health_catapult", 18), #trebuchet's health is 1.8x of trebuchet's
      (val_div, ":health_sally_door", 10),
      (store_mul, ":health_sally_door_double", ":health_sally_door", 2),
      
      (assign, "$g_number_of_targets_destroyed", 0),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_catapult_destructible"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_catapult_destructible", ":cur_instance"),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_catapult"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_trebuchet_destructible"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_trebuchet_destructible", ":cur_instance"),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_trebuchet"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_castle_e_sally_door_a"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_castle_e_sally_door_a", ":cur_instance"),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_castle_f_sally_door_a"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_castle_f_sally_door_a", ":cur_instance"),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_earth_sally_gate_left"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_earth_sally_gate_left", ":cur_instance"),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door_double"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_earth_sally_gate_right"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_earth_sally_gate_right", ":cur_instance"),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door_double"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_viking_keep_destroy_sally_door_left"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_viking_keep_destroy_sally_door_left", ":cur_instance"),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_viking_keep_destroy_sally_door_right"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_viking_keep_destroy_sally_door_right", ":cur_instance"),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door"),
      (try_end),
      
      (store_div, ":health_sally_door_div_3", ":health_sally_door", 3),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_castle_f_door_a"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_castle_f_door_a", ":cur_instance"),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door_div_3"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_instances_of_scene_prop", "spr_castle_f_door_b"),
      (try_for_range, ":cur_instance", 0, ":num_instances_of_scene_prop"),
        (scene_prop_get_instance, ":cur_instance_id", "spr_castle_f_door_b", ":cur_instance"),
        (prop_instance_enable_physics, ":cur_instance_id", 1),
        (scene_prop_set_hit_points, ":cur_instance_id", ":health_sally_door_div_3"),
      (try_end),
  ]),
  
  #script_show_multiplayer_message
  # INPUT: arg1 = multiplayer_message_type
  # OUTPUT: none
  ("show_multiplayer_message",
    [
      (store_script_param, ":multiplayer_message_type", 1),
      (store_script_param, ":value", 2),
      
      (assign, "$g_multiplayer_message_type", ":multiplayer_message_type"),
      
      (try_begin),
        (eq, ":multiplayer_message_type", multiplayer_message_type_round_result_in_battle_mode),
        (assign, "$g_multiplayer_message_value_1", ":value"),
        (start_presentation, "prsnt_multiplayer_message_1"),
        
        (try_begin), #end of round in clients
          (neg|multiplayer_is_server),
          (assign, "$g_battle_death_mode_started", 0),
        (try_end),
      (else_try),
        (eq, ":multiplayer_message_type", multiplayer_message_type_auto_team_balance_done),
        (assign, "$g_multiplayer_message_value_1", ":value"),
        (start_presentation, "prsnt_multiplayer_message_2"),
        (assign, "$g_team_balance_next_round", 0),
      (else_try),
        (eq, ":multiplayer_message_type", multiplayer_message_type_auto_team_balance_next),
        (assign, "$g_team_balance_next_round", 1),
        (call_script, "script_warn_player_about_auto_team_balance"),
      (else_try),
        (eq, ":multiplayer_message_type", multiplayer_message_type_auto_team_balance_no_need),
        (assign, "$g_team_balance_next_round", 0),
      (else_try),
        (eq, ":multiplayer_message_type", multiplayer_message_type_capture_the_flag_score),
        (assign, "$g_multiplayer_message_value_1", ":value"),
        (start_presentation, "prsnt_multiplayer_message_1"),
      (else_try),
        (eq, ":multiplayer_message_type", multiplayer_message_type_flag_returned_home),
        (assign, "$g_multiplayer_message_value_1", ":value"),
        (start_presentation, "prsnt_multiplayer_message_1"),
      (else_try),
        (eq, ":multiplayer_message_type", multiplayer_message_type_capture_the_flag_stole),
        (assign, "$g_multiplayer_message_value_1", ":value"),
        (start_presentation, "prsnt_multiplayer_message_1"),
      (else_try),
        (eq, ":multiplayer_message_type", multiplayer_message_type_poll_result),
        (assign, "$g_multiplayer_message_value_3", ":value"),
        (start_presentation, "prsnt_multiplayer_message_3"),
      (else_try),
        (eq, ":multiplayer_message_type", multiplayer_message_type_flag_neutralized),
        (assign, "$g_multiplayer_message_value_1", ":value"),
        (start_presentation, "prsnt_multiplayer_message_1"),
      (else_try),
        (eq, ":multiplayer_message_type", multiplayer_message_type_flag_captured),
        (assign, "$g_multiplayer_message_value_1", ":value"),
        (start_presentation, "prsnt_multiplayer_message_1"),
      (else_try),
        (eq, ":multiplayer_message_type", multiplayer_message_type_flag_is_pulling),
        (assign, "$g_multiplayer_message_value_1", ":value"),
        (start_presentation, "prsnt_multiplayer_message_1"),
      (else_try),
        (eq, ":multiplayer_message_type", multiplayer_message_type_round_draw),
        (start_presentation, "prsnt_multiplayer_message_1"),
      (else_try),
        (eq, ":multiplayer_message_type", multiplayer_message_type_target_destroyed),
        
        # (try_begin), #destroy score (condition : a target destroyed)
        # (eq, "$g_defender_team", 0),
        # (assign, ":attacker_team_no", 1),
        # (else_try),
        # (assign, ":attacker_team_no", 0),
        # (try_end),
        
        # (team_get_score, ":team_score", ":attacker_team_no"),
        # (val_add, ":team_score", 1),
        # (call_script, "script_team_set_score", ":attacker_team_no", ":team_score"), #destroy score end
        
        (assign, "$g_multiplayer_message_value_1", ":value"),
        (start_presentation, "prsnt_multiplayer_message_1"),
      (try_end),
  ]),
  
  #script_draw_this_round
  # INPUT: arg1 = value
  ("draw_this_round",
    [
      (store_script_param, ":value", 1),
      (try_begin),
        (eq, ":value", -9), #destroy mod round end
        (assign, "$g_round_ended", 1),
        (store_mission_timer_a, "$g_round_finish_time"),
        #(assign, "$g_multiplayer_message_value_1", -1),
        #(assign, "$g_multiplayer_message_type", multiplayer_message_type_round_draw),
        #(start_presentation, "prsnt_multiplayer_message_1"),
      (else_try),
        (eq, ":value", -1), #draw
        (assign, "$g_round_ended", 1),
        (store_mission_timer_a, "$g_round_finish_time"),
        (assign, "$g_multiplayer_message_value_1", -1),
        (assign, "$g_multiplayer_message_type", multiplayer_message_type_round_draw),
        (start_presentation, "prsnt_multiplayer_message_1"),
      (else_try),
        (eq, ":value", 0), #defender wins
        #THIS_IS_OUR_LAND achievement
        (try_begin),
          (eq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
          (multiplayer_get_my_player, ":my_player_no"),
          (is_between, ":my_player_no", 0, multiplayer_max_possible_player_id),
          (player_get_agent_id, ":my_player_agent", ":my_player_no"),
          (ge, ":my_player_agent", 0),
          (agent_is_alive, ":my_player_agent"),
          (agent_get_team, ":my_player_agent_team_no", ":my_player_agent"),
          (eq, ":my_player_agent_team_no", 0), #defender
          (unlock_achievement, ACHIEVEMENT_THIS_IS_OUR_LAND),
        (try_end),
        #THIS_IS_OUR_LAND achievement end
        (assign, "$g_round_ended", 1),
        (store_mission_timer_a, "$g_round_finish_time"),
        
        (team_get_faction, ":faction_of_winner_team", 0),
        (team_get_score, ":team_1_score", 0),
        (val_add, ":team_1_score", 1),
        (team_set_score, 0, ":team_1_score"),
        (assign, "$g_winner_team", 0),
        (str_store_faction_name, s1, ":faction_of_winner_team"),
        
        (assign, "$g_multiplayer_message_value_1", ":value"),
        (try_begin),
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_battle),
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_invasion),
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_raid),
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_sea),#naval battles
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle), #capitan chief
          (assign, "$g_multiplayer_message_type", multiplayer_message_type_round_result_in_siege_mode),
        (else_try),
          (assign, "$g_multiplayer_message_type", multiplayer_message_type_round_result_in_battle_mode),
        (try_end),
        (start_presentation, "prsnt_multiplayer_message_1"),
      (else_try),
        (eq, ":value", 1), #attacker wins
        (assign, "$g_round_ended", 1),
        (store_mission_timer_a, "$g_round_finish_time"),
        
        (team_get_faction, ":faction_of_winner_team", 1),
        (team_get_score, ":team_2_score", 1),
        (val_add, ":team_2_score", 1),
        (team_set_score, 1, ":team_2_score"),
        (assign, "$g_winner_team", 1),
        (str_store_faction_name, s1, ":faction_of_winner_team"),
        
        (assign, "$g_multiplayer_message_value_1", ":value"),
        (try_begin),
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_battle),
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_invasion),
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_raid),
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_sea),#naval battles
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle), #capitan chief
          (assign, "$g_multiplayer_message_type", multiplayer_message_type_round_result_in_siege_mode),
        (else_try),
          (assign, "$g_multiplayer_message_type", multiplayer_message_type_round_result_in_battle_mode),
        (try_end),
        (start_presentation, "prsnt_multiplayer_message_1"),
      (try_end),
      #LAST_MAN_STANDING achievement
      (try_begin),
        (is_between, ":value", 0, 2), #defender or attacker wins
        (try_begin),
          (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_invasion),
          (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_raid),
          (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_sea),#naval battles
          (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle), #capitan chief
          (eq, "$g_multiplayer_game_type", multiplayer_game_type_battle),
          (multiplayer_get_my_player, ":my_player_no"),
          (is_between, ":my_player_no", 0, multiplayer_max_possible_player_id),
          (player_get_agent_id, ":my_player_agent", ":my_player_no"),
          (ge, ":my_player_agent", 0),
          (agent_is_alive, ":my_player_agent"),
          (agent_get_team, ":my_player_agent_team_no", ":my_player_agent"),
          (eq, ":my_player_agent_team_no", ":value"), #winner team
          (unlock_achievement, ACHIEVEMENT_LAST_MAN_STANDING),
        (try_end),
      (try_end),
      #LAST_MAN_STANDING achievement end
  ]),
  
  #script_check_achievement_last_man_standing
  #INPUT: arg1 = value
  ("check_achievement_last_man_standing",
    [
      #LAST_MAN_STANDING achievement
      (try_begin),
        (store_script_param, ":value", 1),
        (is_between, ":value", 0, 2), #defender or attacker wins
        (try_begin),
          (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_invasion),
          (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_raid),
          (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_sea),#naval battles
          (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle), #capitan chief
          (eq, "$g_multiplayer_game_type", multiplayer_game_type_battle),
          (multiplayer_get_my_player, ":my_player_no"),
          (is_between, ":my_player_no", 0, multiplayer_max_possible_player_id),
          (player_get_agent_id, ":my_player_agent", ":my_player_no"),
          (ge, ":my_player_agent", 0),
          (agent_is_alive, ":my_player_agent"),
          (agent_get_team, ":my_player_agent_team_no", ":my_player_agent"),
          (eq, ":my_player_agent_team_no", ":value"), #winner team
          (unlock_achievement, ACHIEVEMENT_LAST_MAN_STANDING),
        (try_end),
      (try_end),
      #LAST_MAN_STANDING achievement end
  ]),
  
  #script_find_most_suitable_bot_to_control
  # INPUT: arg1 = value
  ("find_most_suitable_bot_to_control",
    [
      (set_fixed_point_multiplier, 100),
      (store_script_param, ":player_no", 1),
      (player_get_team_no, ":player_team", ":player_no"),
      
      (player_get_slot, ":x_coor", ":player_no", slot_player_death_pos_x),
      (player_get_slot, ":y_coor", ":player_no", slot_player_death_pos_y),
      (player_get_slot, ":z_coor", ":player_no", slot_player_death_pos_z),
      
      (init_position, pos0),
      (position_set_x, pos0, ":x_coor"),
      (position_set_y, pos0, ":y_coor"),
      (position_set_z, pos0, ":z_coor"),
      
      (assign, ":most_suitable_bot", -1),
      (assign, ":max_bot_score", -1),
      
      (try_for_agents, ":cur_agent"),
        (agent_is_alive, ":cur_agent"),
        (agent_is_human, ":cur_agent"),
        (agent_is_non_player, ":cur_agent"),
        (agent_get_team ,":cur_team", ":cur_agent"),
        (eq, ":cur_team", ":player_team"),
        (agent_get_position, pos1, ":cur_agent"),
        
        #getting score for distance of agent to death point (0..3000)
        (get_distance_between_positions_in_meters, ":dist", pos0, pos1),
        
        (try_begin),
          (lt, ":dist", 500),
          (store_sub, ":bot_score", 500, ":dist"),
        (else_try),
          (assign, ":bot_score", 0),
        (try_end),
        (val_mul, ":bot_score", 6),
        
        #getting score for distance of agent to enemy & friend agents (0..300 x agents)
        (try_for_agents, ":cur_agent_2"),
          (agent_is_alive, ":cur_agent_2"),
          (agent_is_human, ":cur_agent_2"),
          (neq, ":cur_agent", ":cur_agent_2"),
          (agent_get_team ,":cur_team_2", ":cur_agent_2"),
          (try_begin),
            (neq, ":cur_team_2", ":player_team"),
            (agent_get_position, pos1, ":cur_agent_2"),
            (get_distance_between_positions, ":dist_2", pos0, pos1),
            (try_begin),
              (lt, ":dist_2", 300),
              (assign, ":enemy_near_score", ":dist_2"),
            (else_try),
              (assign, ":enemy_near_score", 300),
            (try_end),
            (val_add, ":bot_score", ":enemy_near_score"),
          (else_try),
            (agent_get_position, pos1, ":cur_agent_2"),
            (get_distance_between_positions, ":dist_2", pos0, pos1),
            (try_begin),
              (lt, ":dist_2", 300),
              (assign, ":friend_near_score", 300, ":dist_2"),
            (else_try),
              (assign, ":friend_near_score", 0),
            (try_end),
            (val_add, ":bot_score", ":friend_near_score"),
          (try_end),
        (try_end),
        
        #getting score for health (0..200)
        (store_agent_hit_points, ":agent_hit_points", ":cur_agent"),
        (val_mul, ":agent_hit_points", 2),
        (val_add, ":bot_score", ":agent_hit_points"),
        
        (ge, ":bot_score", ":max_bot_score"),
        (assign, ":max_bot_score", ":bot_score"),
        (assign, ":most_suitable_bot", ":cur_agent"),
      (try_end),
      
      (assign, reg0, ":most_suitable_bot"),
  ]),
  
  #script_game_receive_url_response
  #response format should be like this:
  #  [a number or a string]|[another number or a string]|[yet another number or a string] ...
  # here is an example response:
  # 12|Player|100|another string|142|323542|34454|yet another string
  # INPUT: arg1 = num_integers, arg2 = num_strings
  # reg0, reg1, reg2, ... up to 128 registers contain the integer values
  # s0, s1, s2, ... up to 128 strings contain the string values
  ("game_receive_url_response",
    [
      #here is an example usage
      ##      (store_script_param, ":num_integers", 1),
      ##      (store_script_param, ":num_strings", 2),
      ##      (try_begin),
      ##        (gt, ":num_integers", 4),
      ##        (display_message, "@{reg0}, {reg1}, {reg2}, {reg3}, {reg4}"),
      ##      (try_end),
      ##      (try_begin),
      ##        (gt, ":num_strings", 4),
      ##        (display_message, "@{s0}, {s1}, {s2}, {s3}, {s4}"),
      ##      (try_end),
  ]),
  
  ("game_get_cheat_mode",
    [
      (assign, reg0, "$cheat_mode"),
  ]),
  
  #script_game_receive_network_message
  # This script is called from the game engine when a new network message is received.
  # INPUT: arg1 = player_no, arg2 = event_type, arg3 = value, arg4 = value_2, arg5 = value_3, arg6 = value_4
  ("game_receive_network_message",
    [
      (store_script_param, ":player_no", 1),
      (store_script_param, ":event_type", 2),
      (try_begin),
        ###dedal begin
        (eq, ":event_type", multiplayer_event_ship),
        (store_script_param, ":sub", 3),
        #(store_script_param, ":instance", 4),
        (try_begin),
          (is_between,":sub", me_ship_inrease_speed, me_ship_set_sail+1),
          (player_is_active,":player_no"),
          (player_get_slot,":player_ship",":player_no",slot_player_ship),
          (gt,":player_ship",-1),
          (player_get_agent_id,":agent",":player_no"),
          (agent_is_active,":agent"),
          (agent_is_alive,":agent"),
          (scene_prop_get_instance,":ship_instance","spr_dyn_ship_substrate",":player_ship"),
          (scene_prop_slot_eq,":ship_instance",scene_prop_lead_player,":agent"),
          (try_begin),
            (eq,":sub",me_ship_inrease_speed),
            (scene_prop_get_slot,":rowing",":ship_instance",scene_prop_rowing),
            (try_begin),
              (lt, ":rowing", 100),
              (val_add, ":rowing", 50),
              (scene_prop_set_slot, ":ship_instance", scene_prop_rowing, ":rowing"),
              (try_for_players,":player",1),
                (multiplayer_send_3_int_to_player, ":player", multiplayer_event_ship, me_ship_return_ship_rowing, ":ship_instance", ":rowing"),
              (try_end),
            (try_end),
            (try_begin),
              (neq,":rowing",0),
              (neg|scene_prop_slot_eq, ":ship_instance", scene_prop_sail, 0),
              (scene_prop_set_slot, ":ship_instance", scene_prop_sail, 0),
              (scene_prop_get_slot, ":ship_boom_instance", ":ship_instance", scene_prop_boom_instance),
              (scene_prop_set_visibility, ":ship_boom_instance",0),
              (try_for_players,":player",1),
                (multiplayer_send_2_int_to_player,":player", multiplayer_event_ship, me_ship_return_ship_sail, ":ship_instance"),
              (try_end),
            (try_end),
          (else_try),
            (eq,":sub",me_ship_decrease_speed),
            (scene_prop_get_slot,":rowing",":ship_instance",scene_prop_rowing),
            (try_begin),
              (gt, ":rowing", -50),
              (val_add, ":rowing", -50),
              (scene_prop_set_slot, ":ship_instance", scene_prop_rowing, ":rowing"),
              (try_for_players,":player",1),
                (multiplayer_send_3_int_to_player, ":player", multiplayer_event_ship, me_ship_return_ship_rowing, ":ship_instance", ":rowing"),
              (try_end),
            (try_end),
            (try_begin),
              (neq,":rowing",0),
              (neg|scene_prop_slot_eq, ":ship_instance", scene_prop_sail, 0),
              (scene_prop_set_slot, ":ship_instance", scene_prop_sail, 0),
              (scene_prop_get_slot, ":ship_boom_instance", ":ship_instance", scene_prop_boom_instance),
              (scene_prop_set_visibility, ":ship_boom_instance",0),
              (try_for_players,":player",1),
                (multiplayer_send_2_int_to_player,":player", multiplayer_event_ship, me_ship_return_ship_sail, ":ship_instance"),
              (try_end),
            (try_end),
          (else_try),
            (eq,":sub",me_ship_turn_right),
            (scene_prop_get_slot, ":rudder", ":ship_instance", scene_prop_rudder),
            (try_begin),
              (gt, ":rudder", -100),
              (val_add, ":rudder", -50),
              (scene_prop_set_slot, ":ship_instance", scene_prop_rudder, ":rudder"),
              (multiplayer_send_3_int_to_player, ":player_no", multiplayer_event_ship, me_ship_return_ship_rudder, ":ship_instance", ":rudder"),
            (try_end),
          (else_try),
            (eq,":sub",me_ship_turn_left),
            (scene_prop_get_slot, ":rudder", ":ship_instance", scene_prop_rudder),
            (try_begin),
              (lt, ":rudder", 100),
              (val_add, ":rudder", 50),
              (scene_prop_set_slot, ":ship_instance", scene_prop_rudder, ":rudder"),
              (multiplayer_send_3_int_to_player, ":player_no", multiplayer_event_ship, me_ship_return_ship_rudder, ":ship_instance", ":rudder"),
            (try_end),
          (else_try),
            (eq,":sub",me_ship_set_sail),
            (scene_prop_get_slot, ":sail", ":ship_instance", scene_prop_sail),
            (try_begin),
              (eq, ":sail", 0),
              (assign, ":sail", 1),
              (scene_prop_set_slot, ":ship_instance", scene_prop_rowing, 0),
            (else_try),
              (assign, ":sail", 0),
            (try_end),
            (scene_prop_set_slot, ":ship_instance", scene_prop_sail, ":sail"),
            (scene_prop_get_slot, ":ship_boom_instance", ":ship_instance", scene_prop_boom_instance),
            (scene_prop_set_visibility, ":ship_boom_instance",":sail"),
            
            (try_for_players,":player",1),
              (multiplayer_send_3_int_to_player,":player", multiplayer_event_ship, me_ship_return_ship_sail, ":ship_instance", ":sail"),
            (try_end),
          (try_end),
        (else_try),
          (neg|multiplayer_is_dedicated_server),
          (try_begin),
            (eq,":sub",me_ship_return_player_ship),
            (store_script_param, ":instance", 4),
            (assign,"$player_ship_number",":instance"),
            (assign,"$tutorial_2_msg_1_displayed",-1),#reset presentation
            (try_begin),
              (neq,":instance",-1),
              (store_script_param, ":value", 5),
              (assign,"$player_ship_captain",":value"),
            (else_try),
              (assign,"$player_ship_captain",-1),
            (try_end),
          (else_try),
            (eq,":sub",me_ship_return_ship_sail),
            (store_script_param, ":ship", 4),
            (store_script_param, ":sail", 5),
            (try_begin),
              (prop_instance_is_valid, ":instance"),
              (scene_prop_get_slot, ":instance", ":ship", scene_prop_boom_instance),
              (scene_prop_set_slot, ":ship", scene_prop_sail, ":sail"),
              (scene_prop_set_visibility, ":instance",":sail"),
            (try_end),
          (else_try),
            (eq,":sub",me_ship_return_ship_rowing),
            (store_script_param, ":instance", 4),
            (store_script_param, ":value", 5),
            (try_begin),
              (prop_instance_is_valid, ":instance"),
              (scene_prop_set_slot, ":instance", scene_prop_rowing, ":value"),
            (try_end),
          (else_try),
            (eq,":sub",me_ship_return_ship_rudder),
            (store_script_param, ":instance", 4),
            (store_script_param, ":value", 5),
            (try_begin),
              (prop_instance_is_valid, ":instance"),
              (scene_prop_set_slot, ":instance", scene_prop_rudder, ":value"),
            (try_end),
          (else_try),
            (eq,":sub",me_ship_return_ship_speed),
            (store_script_param, ":speed", 4),
            (store_script_param, ":max", 5),
            (store_script_param, ":quality", 6),
            (try_begin),
              (scene_prop_get_instance, ":instance", "spr_dyn_ship_substrate", "$player_ship_number"),
              (scene_prop_set_slot, ":instance", scene_prop_last_speed, ":speed"),
              (try_begin),
                (gt,":max",0),
                (scene_prop_set_slot, ":instance", scene_prop_max_speed, ":max"),
                (scene_prop_set_slot, ":instance", scene_prop_quality, ":quality"),
              (try_end),
            (try_end),
          (else_try),
            (eq,":sub",me_ship_return_ship_crew),
            (store_script_param, ":instance", 4),
            (store_script_param, ":value", 5),
            (try_begin),
              (prop_instance_is_valid, ":instance"),
              (scene_prop_set_slot, ":instance", scene_prop_crew_number, ":value"),
            (try_end),
          (else_try),
            (eq,":sub",me_ship_return_ship_landed),
            (store_script_param, ":instance", 4),
            (try_begin),
              (prop_instance_is_valid, ":instance"),
              (scene_prop_set_slot, ":instance", scene_prop_boarding_progress, -1),
            (try_end),
          (else_try),
            (eq,":sub",me_ship_return_ship_sail_and_rowing),
            (store_script_param, ":ship", 4),
            (store_script_param, ":sail", 5),
            (store_script_param, ":value", 6),
            (try_begin),
              (prop_instance_is_valid, ":ship"),
              (scene_prop_get_slot, ":instance", ":ship", scene_prop_boom_instance),
              (scene_prop_set_slot, ":ship", scene_prop_sail, ":sail"),
              (scene_prop_set_visibility, ":instance",":sail"),
              (scene_prop_set_slot, ":ship", scene_prop_rowing, ":value"),
            (try_end),
          (else_try),
            (eq,":sub",me_ship_return_wind),
            (store_script_param, "$WindStrength_variable", 4),
            (store_script_param, "$WindDirection_variable", 5),
            (set_fixed_point_multiplier,1),
            (set_shader_param_float, "@vWindStrength", "$WindStrength_variable"),
            (set_shader_param_float, "@vWindDirection", "$WindDirection_variable"),
            (set_fixed_point_multiplier,100),
            
            ##			(assign,reg1,"$WindStrength_variable"),
            ##			(assign,reg2,"$WindDirection_variable"),
            ##			(display_message,"@{!}DEBUG - direction of the wind has changed ({reg1}/{reg2})"),
          (else_try),
            (eq,":sub",me_ship_do_thunder),
            (assign,"$lightning_cycle",1),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_ship_send_data),
        (try_begin),
          (neg|multiplayer_is_dedicated_server),
          (store_script_param, ":instance", 3),
          (prop_instance_is_valid, ":instance"),
          (store_script_param, ":value", 4),
          (store_script_param, ":sail", 5),
          (store_script_param, ":type", 6),
          (scene_prop_set_slot, ":instance", scene_prop_main_instance, ":value"),
          (scene_prop_set_slot, ":instance", scene_prop_boom_instance, ":sail"),
          (scene_prop_set_slot, ":instance", scene_prop_ship_type, ":type"),
        (try_end),
        
      (else_try),
        (eq, ":event_type", multiplayer_event_agent),
        (store_script_param, ":sub", 3),
        (try_begin),
          (eq,":sub",me_agent_play_animation),
          (try_begin),
            (neg|multiplayer_is_server),
            (store_script_param, ":agent", 4),
            (store_script_param, ":anim", 5),
            (store_script_param, ":flag", 6),
            (agent_is_active,":agent"),
            (agent_is_alive,":agent"),
            (agent_set_animation,":agent",":anim",":flag"),
          (try_end),
        (else_try),
          (eq,":sub",me_agent_use_horn),
          (try_begin),
            (player_is_active,":player_no"),
            (neg|player_is_busy_with_menus,":player_no"),
            (player_get_agent_id,":agent",":player_no"),
            (agent_is_active,":agent"),
            (agent_is_alive,":agent"),
            (agent_get_wielded_item,":item",":agent"),
            (this_or_next|eq,":item","itm_horn"),
            (eq,":item","itm_horn_multi"),
            (try_begin),
              (agent_slot_eq,":agent",slot_agent_horn_use_cooldown,0),
              (agent_set_slot,":agent",slot_agent_horn_use_cooldown,30),
              (agent_play_sound,":agent","snd_horn"),
              (agent_get_horse,":horse",":agent"),
              (try_begin),
                (agent_is_active,":horse"),
                (assign,":flag",1),
              (else_try),
                (assign,":flag",0),
              (try_end),
              (agent_set_animation,":agent","anim_horn",":flag"),
              
              (try_for_players,":player",1),
                (multiplayer_send_4_int_to_player,":player", multiplayer_event_agent, me_agent_play_animation, ":agent", "anim_horn", ":flag"),
              (try_end),
              
              (try_begin),
                (neq, "$g_multiplayer_game_type", multiplayer_game_type_thor),
                (neq, "$g_multiplayer_game_type", multiplayer_game_type_deathmatch),
                (neq, "$g_multiplayer_game_type", multiplayer_game_type_duel),
                (agent_get_position,pos1,":agent"),
                (agent_get_team,":team",":agent"),
                (assign,":num_affected",0),
                (set_fixed_point_multiplier,100),
                (try_for_agents,":i",pos1,5000),
                  (neq,":i",":agent"),
                  (agent_is_active,":i"),
                  (agent_is_alive,":i"),
                  (agent_slot_eq,":i",slot_agent_horn_cooldown,0),
                  (agent_slot_eq,":i",slot_agent_berserk_cooldown,0),###no boost for berserker
                  (agent_slot_eq,":agent",slot_agent_is_chosen,0),###no boost for chosen one
                  (agent_get_team,":i_team",":i"),
                  (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_thor),
                  (eq,":i_team",":team"),
                  (agent_set_slot,":i",slot_agent_horn_cooldown,120),#2min
                  (agent_set_slot,":i",slot_agent_horn_bonus_applied,1),
                  (call_script,"script_reset_bonus_effect",":i"),
                  (store_agent_hit_points,":hp",":i",0),
                  (val_add,":hp",10),
                  (val_min,":hp",100),
                  (agent_set_hit_points,":i",":hp",0),
                  (agent_get_player_id,":player",":i"),
                  (try_begin),
                    (player_is_active,":player"),
                    (multiplayer_send_int_to_player,":player", multiplayer_event_display_message, me_message_horn_ally),
                  (try_end),
                  (val_add,":num_affected",1),
                (try_end),
                (multiplayer_send_2_int_to_player,":player_no", multiplayer_event_display_message, me_message_horn_player_ally, ":num_affected"),
              (try_end),
            (else_try),
              (multiplayer_send_int_to_player,":player_no", multiplayer_event_display_message, me_message_horn_cooldown),
            (try_end),
          (try_end),
        (else_try),
          (eq,":sub",me_agent_berserk),
          (try_begin),
            (player_is_active,":player_no"),
            (player_get_agent_id,":agent",":player_no"),
            (agent_is_active,":agent"),
            (agent_is_alive,":agent"),
            (call_script,"script_cf_agent_is_not_peasant",":agent"),
            (agent_slot_eq,":agent",slot_agent_is_chosen,0),###no boost for chosen one
            (try_begin),
              (agent_slot_eq,":agent",slot_agent_berserk_use_cooldown,0),
              (player_get_gender,":is_female",":player_no"),
              (try_begin),
                (eq, ":is_female", 1),
                (agent_play_sound,":agent","snd_woman_yell"),###WARCRY SOUND
              (else_try),
                (agent_play_sound,":agent","snd_man_warcry"),###WARCRY SOUND
              (try_end),
              #(agent_play_sound,":agent","snd_berserker_trance"),###WARCRY SOUND
              (agent_set_damage_modifier,":agent",125),
              (call_script, "script_advanced_agent_set_speed_modifier",":agent",110),
              (agent_set_accuracy_modifier,":agent",50),
              (agent_set_reload_speed_modifier,":agent",50),
              (agent_set_slot,":agent",slot_agent_berserk_use_cooldown,180),
              (agent_set_slot,":agent",slot_agent_berserk_cooldown,50),
              (agent_set_slot,":agent",slot_agent_horn_cooldown,0),
              (multiplayer_send_int_to_player, ":player_no", multiplayer_event_display_message, me_message_berserk_begin),
            (else_try),
              (multiplayer_send_int_to_player,":player_no", multiplayer_event_display_message, me_message_berserk_cooldown),
            (try_end),
          (try_end),
          
        (else_try),
          (eq,":sub",me_agent_shield_taunt),
          (try_begin),
            (multiplayer_is_server),
            (try_begin),
              (player_is_active,":player_no"),
              (player_get_agent_id,":agent",":player_no"),
              (agent_is_active,":agent"),
              (agent_is_alive,":agent"),
              (agent_set_animation, ":agent", "anim_shield_taunt", 1),
              (agent_play_sound, ":agent", "snd_shield_taunt"),
              (try_for_players,":i",1),
                (multiplayer_send_2_int_to_player,":i",multiplayer_event_agent, me_agent_shield_taunt,":agent"),
              (try_end),
            (try_end),
          (else_try),
            (store_script_param, ":agent", 4),
            (try_begin),
              (agent_is_active,":agent"),
              (agent_is_alive,":agent"),
              (agent_set_animation, ":agent", "anim_shield_taunt", 1),
            (try_end),
          (try_end),
        (else_try),
          (eq,":sub",me_agent_shield_bash),
          (try_begin),
            (multiplayer_is_server),
            (player_is_active,":player_no"),
            (player_get_agent_id,":agent",":player_no"),
            (agent_is_active,":agent"),
            (call_script,"script_shield_bash_script",0,":agent",":player_no"),
          (try_end),
        (else_try),
          (neg|multiplayer_is_dedicated_server),
          (try_begin),
            (eq,":sub",me_agent_decapitate),
            (store_script_param, ":agent", 4),
            (try_begin),
              (agent_is_active,":agent"),
              (agent_is_alive,":agent"),
              (agent_get_item_slot,":item",":agent",4),#head slot
              (try_begin),
                (ge,":item",1),
                (agent_unequip_item,":agent",":item"),
              (try_end),
              (agent_equip_item,":agent","itm_sarranid_horseman_helmet"),
              (agent_get_bone_position,pos0,":agent",hb_head,1),
              (particle_system_burst_no_sync,"psys_game_blood_3",pos0,125),
            (try_end),
          (else_try),
            (eq,":sub",me_agent_critical_recieved),
            (store_script_param, reg5, 4),#damage
            (display_message, "@You have received {reg5} critical damage!", 0xFF0000),
          (else_try),
            (eq,":sub",me_agent_critical_dealt),
            (store_script_param, reg5, 4),#damage
            (display_message, "@You have dealt {reg5} critical damage!", 0xFF0000),
          (else_try),
            (eq,":sub",me_agent_blinded),
            (store_script_param, reg5, 4),#damage
            (store_mul, ":time", reg5, 100),
            (display_message, "@You have received {reg5} critical damage to your head and lose vision for a second!", 0xFF0000),
            (mission_cam_set_screen_color, 0x99660000),#player lose vision a seconds
            (mission_cam_animate_to_screen_color, 0x00000000, ":time"),
          (else_try),
            (eq,":sub",me_agent_is_chosen),#client only
            (try_begin),
              (neg|multiplayer_is_server),
              (store_script_param, ":agent", 4),
              (store_script_param, ":val", 5),
              (try_begin),
                (agent_is_active,":agent"),
                (agent_set_slot,":agent",slot_agent_is_chosen,1),
                ##armors (have to run this for all players or they won't see changes)
                (try_for_range,":slot",4,8),
                  (agent_get_item_slot,":item",":agent",":slot"),
                  (gt,":item",0),
                  (agent_unequip_item,":agent",":item"),
                (try_end),
                (agent_equip_item,":agent","itm_thors_armor"),
                (agent_equip_item,":agent","itm_thors_helmet"),
                (agent_equip_item,":agent","itm_thors_boots"),
              (try_end),
              (assign,"$gThorsChampionAgent",":agent"),
              (val_mul, "$tutorial_2_msg_1_displayed", -1),
              (play_sound,"snd_thunder_close"),
              (assign, "$temp", ":val"),
              (assign, "$g_multiplayer_message_type", ":agent"),
              (start_presentation, "prsnt_multiplayer_message_champion"),
            (try_end),
          (try_end),
        (try_end),
        
      (else_try),
        (eq, ":event_type", multiplayer_event_display_string),
        (try_begin),
          (neg|multiplayer_is_dedicated_server),
          (display_message,s0),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_display_message),
        (try_begin),
          (neg|multiplayer_is_dedicated_server),
          (store_script_param,":sub",3),
          (get_player_agent_no,":agent"),
          (try_begin),
            (eq,":sub",me_message_berserk_begin),
            (display_message,"@You unleash your battle rage!",0x000000),
            # (mission_cam_set_screen_color, 0x77FF0000),
            # (mission_cam_animate_to_screen_color, 0x22ff0000, 2000),
            (assign,"$is_berserk",1),
          (else_try),
            (eq,":sub",me_message_berserk_loss),
            (display_message,"@You feel exhausted after the rage.",0xff3333),
            (assign,"$is_berserk",-1),
            (mission_cam_animate_to_screen_color, 0x55000000, 2000),
          (else_try),
            (eq,":sub",me_message_berserk_end),
            (display_message,"@Your strength is back.",0x88FF88),
            (assign,"$is_berserk",0),
            (mission_cam_animate_to_screen_color, 0x00000000, 2000),
          (else_try),
            (eq,":sub",me_message_berserk_cooldown),
            (display_message,"@You are too exhausted to go into a battle rage.",0x000000),
          (else_try),
            (eq,":sub",me_message_horn_ally),
            (try_begin),
              (agent_is_active,":agent"),
              (agent_is_alive,":agent"),
              (assign,"$gRefreshBonusIndicator",1),
              (agent_set_slot,":agent",slot_agent_horn_bonus_applied,1),
              (display_message,"@The sound of your side's horn makes you feel stronger!",0x88FF88),
            (try_end),
          (else_try),
            (eq,":sub",me_message_horn_enemy),
            (display_message,"@The sound of the enemy's horn makes you feel doubtful.",0xff3333),
          (else_try),
            (eq,":sub",me_message_horn_end),
            (display_message,"@You are no longer affected by having heard the horn."),
            (try_begin),
              (agent_is_active,":agent"),
              (agent_is_alive,":agent"),
              (assign,"$gRefreshBonusIndicator",1),
              (agent_set_slot,":agent",slot_agent_horn_bonus_applied,0),
            (try_end),
          (else_try),
            (eq,":sub",me_message_horn_player_ally),
            (store_script_param,reg1,4),
            (try_begin),
              (gt,reg1,0),
              (display_message,"@{reg1} ally troops were affected by your horn!",0x88FF88),
            (else_try),
              (display_message,"@No ally troops were affected by your horn.",0xff7777),
            (try_end),
          (else_try),
            (eq,":sub",me_message_horn_cooldown),
            (display_message,"@You need to catch your breath before blowing the horn again.",0x000000),
          (else_try),
            (eq,":sub",me_message_supply_box_used),
            (display_message,"@Your supplies are replenished.",0x88FF88),
          (else_try),
            (eq,":sub",me_message_banner_bonus_active),
            (try_begin),
              (agent_is_active,":agent"),
              (agent_is_alive,":agent"),
              (assign,"$gRefreshBonusIndicator",1),
              (agent_set_slot,":agent",slot_agent_banner_bonus_applied,1),
            (try_end),
          (else_try),
            (eq,":sub",me_message_banner_bonus_end),
            (try_begin),
              (agent_is_active,":agent"),
              (agent_is_alive,":agent"),
              (assign,"$gRefreshBonusIndicator",1),
              (agent_set_slot,":agent",slot_agent_banner_bonus_applied,0),
            (try_end),
          (else_try),
            (eq,":sub",me_message_shieldwall_bonus),
            (store_script_param,":value",4),
            (try_begin),
              (agent_is_active,":agent"),
              (agent_is_alive,":agent"),
              (assign,"$gRefreshBonusIndicator",1),
              (agent_set_slot,":agent",slot_agent_shieldwall_bonus,":value"),
            (try_end),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_sync_shader),
        (try_begin),
          (neg|multiplayer_is_dedicated_server),
          (store_script_param,":val",3),
          (try_begin),
            (ge,":val",0),
            #(assign,"$wave_timer",":val"),
            (store_mission_timer_a_msec,":my_timer"),
            (store_sub,"$timer_diff",":val",":my_timer"),
          (else_try),
            (eq,":val",-1),
            (store_script_param,"$wavespeed_y",4),
            (store_script_param,"$Amplitude_x",5),
            (store_script_param,"$Amplitude_y",6),
          (else_try),
            (eq,":val",-2),
            (store_script_param,":timer",4),
            (store_script_param,"$lamda_x",5),
            (store_script_param,"$lamda_y",6),
            
            (store_mission_timer_a_msec,":my_timer"),
            (store_sub,"$timer_diff",":timer",":my_timer"),
            
            (assign, "$schader_timer_on", 1),
            (call_script,"script_set_wave_shader"),
          (try_end),
        (try_end),
        
      (else_try),
        ###############
        #SERVER EVENTS#
        ###############
        (eq, ":event_type", multiplayer_event_set_item_selection),
        (store_script_param, ":slot_no", 3),
        (store_script_param, ":value", 4),
        (try_begin),
          #valid slot check
          (is_between, ":slot_no", slot_player_selected_item_indices_begin, slot_player_selected_item_indices_end),
          #valid item check
          (assign, ":valid_item", 0),
          (try_begin),
            (eq, ":value", -1),
            (assign, ":valid_item", 1),
          (else_try),
            (this_or_next|eq, ":value", "itm_deadly_bow"),
            (eq, ":value", "itm_deadly_arrows"),
            (ge,"$g_multiplayer_deadly_bow",1),
            (assign, ":valid_item", 1),
          (else_try),
            (ge, ":value", 0),
            (player_get_troop_id, ":player_troop_no", ":player_no"),
            (this_or_next|is_between, ":player_troop_no", multiplayer_addon_troops_begin, multiplayer_addon_troops_end),
            (is_between, ":player_troop_no", multiplayer_troops_begin, multiplayer_troops_end),
            (store_sub, ":troop_index", ":player_troop_no", multiplayer_troops_begin),
            (val_add, ":troop_index", slot_item_multiplayer_availability_linked_list_begin),
            (item_get_slot, ":prev_next_item_ids", ":value", ":troop_index"),
            (gt, ":prev_next_item_ids", 0), #0 if the item is not valid for the multiplayer mode
            (assign, ":valid_item", 1),
            (try_begin),
              (neq, "$g_horses_are_avaliable", 1),
              (item_get_slot, ":item_class", ":value", slot_item_multiplayer_item_class),
              (is_between, ":item_class", multi_item_class_type_horses_begin, multi_item_class_type_horses_end),
              (assign, ":valid_item", 0),
            (try_end),
            (try_begin),
              (eq, "$g_multiplayer_disallow_ranged_weapons", 1),
              (item_get_slot, ":item_class", ":value", slot_item_multiplayer_item_class),
              (is_between, ":item_class", multi_item_class_type_ranged_weapons_begin, multi_item_class_type_ranged_weapons_end),
              (assign, ":valid_item", 0),
            (try_end),
          (try_end),
          (eq, ":valid_item", 1),
          #condition checks are done
          (player_set_slot, ":player_no", ":slot_no", ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_set_bot_selection),
        (store_script_param, ":slot_no", 3),
        (store_script_param, ":value", 4),
        (try_begin),
          #condition check
          (is_between, ":slot_no", slot_player_bot_type_1_wanted, slot_player_bot_type_15_wanted + 1),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (player_set_slot, ":player_no", ":slot_no", ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_change_team_no),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_get_team_no, ":player_team", ":player_no"),
          (neq, ":player_team", ":value"),
          
          #condition checks are done
          (try_begin),
            #check if available
            (call_script, "script_cf_multiplayer_team_is_available", ":player_no", ":value"),
            #reset troop_id to -1
            (player_set_troop_id, ":player_no", -1),
            (player_set_team_no, ":player_no", ":value"),
            (player_set_slot,":player_no",slot_player_forced_team_change,0),
            (try_begin),
              (neq, ":value", multi_team_spectator),
              (neq, ":value", multi_team_unassigned),
              
              (store_mission_timer_a, ":player_last_team_select_time"),
              (player_set_slot, ":player_no", slot_player_last_team_select_time, ":player_last_team_select_time"),
              
              (multiplayer_send_message_to_player, ":player_no", multiplayer_event_return_confirmation),
            (try_end),
          (else_try),
            #reject request
            (multiplayer_send_message_to_player, ":player_no", multiplayer_event_return_rejection),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_change_troop_id),
        (store_script_param, ":value", 3),
        #troop-faction validity check
        (player_get_team_no, ":player_team", ":player_no"),
        (try_begin),
          (eq, ":value", -1),
          (player_set_troop_id, ":player_no", -1),
        (else_try),
          (eq,"$g_multiplayer_game_type",multiplayer_game_type_raid),
          (eq,":player_team",0),
          (is_between, ":value",multiplayer_peasant_troops_begin,multiplayer_peasant_troops_end),
          (team_get_faction, ":team_faction", ":player_team"),
          (store_troop_faction, ":new_troop_faction", ":value"),
          (eq, ":new_troop_faction", ":team_faction"),
          (player_set_troop_id, ":player_no", ":value"),
          (call_script, "script_multiplayer_clear_player_selected_items", ":player_no"),
        (else_try),
          (this_or_next|is_between, ":value", multiplayer_addon_troops_begin, multiplayer_addon_troops_end),
          (is_between, ":value", multiplayer_troops_begin, multiplayer_troops_end),
          (is_between, ":player_team", 0, multi_team_spectator),
          (team_get_faction, ":team_faction", ":player_team"),
          (store_troop_faction, ":new_troop_faction", ":value"),
          (eq, ":new_troop_faction", ":team_faction"),
          (player_set_troop_id, ":player_no", ":value"),
          (call_script, "script_multiplayer_clear_player_selected_items", ":player_no"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_start_map),
        (store_script_param, ":value", 3),
        (store_script_param, ":value_2", 4),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (this_or_next|is_between, ":value", multiplayer_addon_scenes_begin, multiplayer_addon_scenes_end),
          (is_between, ":value", multiplayer_scenes_begin, multiplayer_scenes_end),
          (is_between, ":value_2", 0, multiplayer_num_game_types),
          (server_get_changing_game_type_allowed, "$g_multiplayer_changing_game_type_allowed"),
          (this_or_next|eq, "$g_multiplayer_changing_game_type_allowed", 1),
          (eq, "$g_multiplayer_game_type", ":value_2"),
          (call_script, "script_multiplayer_fill_map_game_types", ":value_2"),
          (assign, ":num_maps", reg0),
          (assign, ":is_valid", 0),
          (store_add, ":end_cond", multi_data_maps_for_game_type_begin, ":num_maps"),
          (try_for_range, ":i_map", multi_data_maps_for_game_type_begin, ":end_cond"),
            (troop_slot_eq, "trp_multiplayer_data", ":i_map", ":value"),
            (assign, ":is_valid", 1),
            (assign, ":end_cond", 0),
          (try_end),
          (eq, ":is_valid", 1),
          #condition checks are done
          (assign, "$g_multiplayer_game_type", ":value_2"),
          (assign, "$g_multiplayer_selected_map", ":value"),
          (team_set_faction, 0, "$g_multiplayer_next_team_1_faction"),
          (team_set_faction, 1, "$g_multiplayer_next_team_2_faction"),
          (call_script, "script_game_multiplayer_get_game_type_mission_template", "$g_multiplayer_game_type"),
          (start_multiplayer_mission, reg0, "$g_multiplayer_selected_map", 1),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_max_num_players),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 2, 201),
          #condition checks are done
          (server_set_max_num_players, ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_num_bots_in_team),
        (store_script_param, ":value", 3),
        (store_script_param, ":value_2", 4),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 1, 3),
          (is_between, ":value_2", 0, "$g_multiplayer_max_num_bots"),
          #condition checks are done
          (try_begin),
            (eq, ":value", 1),
            (assign, "$g_multiplayer_num_bots_team_1", ":value_2"),
          (else_try),
            (assign, "$g_multiplayer_num_bots_team_2", ":value_2"),
          (try_end),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_2_int_to_player, ":cur_player", multiplayer_event_return_num_bots_in_team, ":value", ":value_2"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_anti_cheat),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (server_set_anti_cheat, ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_friendly_fire),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (server_set_friendly_fire, ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_melee_friendly_fire),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (server_set_melee_friendly_fire, ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_friendly_fire_damage_self_ratio),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 101),
          #condition checks are done
          (server_set_friendly_fire_damage_self_ratio, ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_friendly_fire_damage_friend_ratio),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 101),
          #condition checks are done
          (server_set_friendly_fire_damage_friend_ratio, ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_ghost_mode),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 4),
          #condition checks are done
          (server_set_ghost_mode, ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_control_block_dir),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (server_set_control_block_dir, ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_combat_speed),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 5),
          #condition checks are done
          (server_set_combat_speed, ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_respawn_count),
        (store_script_param, ":value", 3),
        #validity check
        (player_is_admin, ":player_no"),
        (is_between, ":value", 0, 6),
        #condition checks are done
        (assign, "$g_multiplayer_number_of_respawn_count", ":value"),
        (get_max_players, ":num_players"),
        (try_for_range, ":cur_player", 1, ":num_players"),
          (player_is_active, ":cur_player"),
          (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_respawn_count, ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_add_to_servers_list),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          #condition checks are done
          (server_set_add_to_game_servers_list, ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_respawn_period),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 3, 31),
          #condition checks are done
          (assign, "$g_multiplayer_respawn_period", ":value"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_respawn_period, ":value"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_game_max_minutes),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 5, 121),
          #condition checks are done
          (assign, "$g_multiplayer_game_max_minutes", ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_round_max_seconds),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 60, 901),
          #condition checks are done
          (assign, "$g_multiplayer_round_max_seconds", ":value"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_round_max_seconds, ":value"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_player_respawn_as_bot),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (assign, "$g_multiplayer_player_respawn_as_bot", ":value"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_player_respawn_as_bot, ":value"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_game_max_points),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 3, 1001),
          #condition checks are done
          (assign, "$g_multiplayer_game_max_points", ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_point_gained_from_flags),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 25, 401),
          #condition checks are done
          (assign, "$g_multiplayer_point_gained_from_flags", ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_point_gained_from_capturing_flag),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 11),
          #condition checks are done
          (assign, "$g_multiplayer_point_gained_from_capturing_flag", ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_initial_gold_multiplier),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 1001),
          #condition checks are done
          (assign, "$g_multiplayer_initial_gold_multiplier", ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_battle_earnings_multiplier),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 1001),
          #condition checks are done
          (assign, "$g_multiplayer_battle_earnings_multiplier", ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_round_earnings_multiplier),
        (store_script_param, ":value", 3),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 1001),
          #condition checks are done
          (assign, "$g_multiplayer_round_earnings_multiplier", ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_server_name),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (server_get_renaming_server_allowed, "$g_multiplayer_renaming_server_allowed"),
          (eq, "$g_multiplayer_renaming_server_allowed", 1),
          #condition checks are done
          (server_set_name, s0), #validity is checked inside this function
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_game_password),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          #condition checks are done
          (server_set_password, s0), #validity is checked inside this function
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_welcome_message),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          #condition checks are done
          (server_set_welcome_message, s0), #validity is checked inside this function
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_team_faction),
        (store_script_param, ":value", 3),
        (store_script_param, ":value_2", 4),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 1, 3),
          (is_between, ":value_2", mp_factions_begin, mp_factions_end),
          ##          (assign, ":is_valid", 0),
          ##          (try_begin),
          ##            (eq, ":value", 1),
          ##            (neq, ":value_2", "$g_multiplayer_next_team_2_faction"),
          ##            (assign, ":is_valid", 1),
          ##          (else_try),
          ##            (neq, ":value_2", "$g_multiplayer_next_team_1_faction"),
          ##            (assign, ":is_valid", 1),
          ##          (try_end),
          ##          (eq, ":is_valid", 1),
          #condition checks are done
          (try_begin),
            (eq, ":value", 1),
            (assign, "$g_multiplayer_next_team_1_faction", ":value_2"),
          (else_try),
            (assign, "$g_multiplayer_next_team_2_faction", ":value_2"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_open_game_rules),
        (try_begin),
          #no validity check
          (server_get_max_num_players, ":max_num_players"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_max_num_players, ":max_num_players"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return_next_team_faction, 1, "$g_multiplayer_next_team_1_faction"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return_next_team_faction, 2, "$g_multiplayer_next_team_2_faction"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return_num_bots_in_team, 1, "$g_multiplayer_num_bots_team_1"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return_num_bots_in_team, 2, "$g_multiplayer_num_bots_team_2"),
          (server_get_anti_cheat, ":server_anti_cheat"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_anti_cheat, ":server_anti_cheat"),
          (server_get_friendly_fire, ":server_friendly_fire"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_friendly_fire, ":server_friendly_fire"),
          (server_get_melee_friendly_fire, ":server_melee_friendly_fire"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_melee_friendly_fire, ":server_melee_friendly_fire"),
          (server_get_friendly_fire_damage_self_ratio, ":friendly_fire_damage_self_ratio"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_friendly_fire_damage_self_ratio, ":friendly_fire_damage_self_ratio"),
          (server_get_friendly_fire_damage_friend_ratio, ":friendly_fire_damage_friend_ratio"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_friendly_fire_damage_friend_ratio, ":friendly_fire_damage_friend_ratio"),
          (server_get_ghost_mode, ":server_ghost_mode"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_ghost_mode, ":server_ghost_mode"),
          (server_get_control_block_dir, ":server_control_block_dir"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_control_block_dir, ":server_control_block_dir"),
          (server_get_combat_speed, ":server_combat_speed"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_combat_speed, ":server_combat_speed"),
          (server_get_add_to_game_servers_list, ":server_add_to_servers_list"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_add_to_servers_list, ":server_add_to_servers_list"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_respawn_period, "$g_multiplayer_respawn_period"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_game_max_minutes, "$g_multiplayer_game_max_minutes"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_round_max_seconds, "$g_multiplayer_round_max_seconds"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_player_respawn_as_bot, "$g_multiplayer_player_respawn_as_bot"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_game_max_points, "$g_multiplayer_game_max_points"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_point_gained_from_flags, "$g_multiplayer_point_gained_from_flags"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_point_gained_from_capturing_flag, "$g_multiplayer_point_gained_from_capturing_flag"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_initial_gold_multiplier, "$g_multiplayer_initial_gold_multiplier"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_battle_earnings_multiplier, "$g_multiplayer_battle_earnings_multiplier"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_round_earnings_multiplier, "$g_multiplayer_round_earnings_multiplier"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_valid_vote_ratio, "$g_multiplayer_valid_vote_ratio"),
          (str_store_server_name, s0),
          (multiplayer_send_string_to_player, ":player_no", multiplayer_event_return_server_name, s0),
          (multiplayer_send_message_to_player, ":player_no", multiplayer_event_return_open_game_rules),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_open_admin_panel),
        (try_begin),
          #validity check
          (player_is_admin, ":player_no"),
          #condition checks are done
          (server_get_renaming_server_allowed, "$g_multiplayer_renaming_server_allowed"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_renaming_server_allowed, "$g_multiplayer_renaming_server_allowed"),
          (server_get_changing_game_type_allowed, "$g_multiplayer_changing_game_type_allowed"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_changing_game_type_allowed, "$g_multiplayer_changing_game_type_allowed"),
          (server_get_max_num_players, ":max_num_players"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_max_num_players, ":max_num_players"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return_next_team_faction, 1, "$g_multiplayer_next_team_1_faction"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return_next_team_faction, 2, "$g_multiplayer_next_team_2_faction"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return_num_bots_in_team, 1, "$g_multiplayer_num_bots_team_1"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return_num_bots_in_team, 2, "$g_multiplayer_num_bots_team_2"),
          (server_get_anti_cheat, ":server_anti_cheat"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_anti_cheat, ":server_anti_cheat"),
          (server_get_friendly_fire, ":server_friendly_fire"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_friendly_fire, ":server_friendly_fire"),
          (server_get_melee_friendly_fire, ":server_melee_friendly_fire"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_melee_friendly_fire, ":server_melee_friendly_fire"),
          (server_get_friendly_fire_damage_self_ratio, ":friendly_fire_damage_self_ratio"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_friendly_fire_damage_self_ratio, ":friendly_fire_damage_self_ratio"),
          (server_get_friendly_fire_damage_friend_ratio, ":friendly_fire_damage_friend_ratio"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_friendly_fire_damage_friend_ratio, ":friendly_fire_damage_friend_ratio"),
          (server_get_ghost_mode, ":server_ghost_mode"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_ghost_mode, ":server_ghost_mode"),
          (server_get_control_block_dir, ":server_control_block_dir"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_control_block_dir, ":server_control_block_dir"),
          (server_get_combat_speed, ":server_combat_speed"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_combat_speed, ":server_combat_speed"),
          (server_get_add_to_game_servers_list, ":server_add_to_servers_list"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_add_to_servers_list, ":server_add_to_servers_list"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_respawn_period, "$g_multiplayer_respawn_period"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_game_max_minutes, "$g_multiplayer_game_max_minutes"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_round_max_seconds, "$g_multiplayer_round_max_seconds"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_player_respawn_as_bot, "$g_multiplayer_player_respawn_as_bot"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_game_max_points, "$g_multiplayer_game_max_points"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_point_gained_from_flags, "$g_multiplayer_point_gained_from_flags"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_point_gained_from_capturing_flag, "$g_multiplayer_point_gained_from_capturing_flag"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_initial_gold_multiplier, "$g_multiplayer_initial_gold_multiplier"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_battle_earnings_multiplier, "$g_multiplayer_battle_earnings_multiplier"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_round_earnings_multiplier, "$g_multiplayer_round_earnings_multiplier"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_valid_vote_ratio, "$g_multiplayer_valid_vote_ratio"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_max_num_bots, "$g_multiplayer_max_num_bots"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_allow_player_banners, "$g_multiplayer_allow_player_banners"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_force_default_armor, "$g_multiplayer_force_default_armor"),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_disallow_ranged_weapons, "$g_multiplayer_disallow_ranged_weapons"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return, me_return_allow_fall_from_horse, "$g_multiplayer_allow_fall_from_horse"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return, me_return_allow_shieldwall, "$g_multiplayer_allow_shieldwalls"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return, me_return_remove_stray_horses, "$g_multiplayer_remove_stray_horses"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return, me_return_god_mode, "$g_multiplayer_god_mode"),
          #(multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return, me_return_troop_classes, "$g_multiplayer_troop_classes"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return, me_return_poll_cooldown, "$g_multiplayer_poll_cooldown"),
          #
          (str_store_server_name, s0),
          (multiplayer_send_string_to_player, ":player_no", multiplayer_event_return_server_name, s0),
          (str_store_server_password, s0),
          (multiplayer_send_string_to_player, ":player_no", multiplayer_event_return_game_password, s0),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_start_new_poll),
        (try_begin),
          (store_script_param, ":value", 3),
          (store_script_param, ":value_2", 4),
          #validity check
          (eq, "$g_multiplayer_poll_running", 0),
          (try_begin),
            (gt, "$g_multiplayer_poll_cooldown_counter",0),
            (assign,reg1,"$g_multiplayer_poll_cooldown_counter"),
            (str_store_string,s1,"str_polls_disabled_for_reg1"),
            (player_is_active,":player_no"),
            (multiplayer_send_string_to_player,":player_no",multiplayer_event_display_string,s1),
            (store_mission_timer_a, ":mission_timer"),
            (player_get_slot, ":poll_disable_time", ":player_no", slot_player_poll_disabled_until_time),
            (ge, ":mission_timer", ":poll_disable_time"),
            (multiplayer_send_int_to_player,":player_no",multiplayer_event_return, me_return_poll_cooldown_failed),
          (try_end),
          (le, "$g_multiplayer_poll_cooldown_counter",0),
          (store_mission_timer_a, ":mission_timer"),
          (player_get_slot, ":poll_disable_time", ":player_no", slot_player_poll_disabled_until_time),
          (ge, ":mission_timer", ":poll_disable_time"),
          (assign, "$g_multiplayer_poll_cooldown_counter", "$g_multiplayer_poll_cooldown"),
          (assign, ":continue", 0),
          (try_begin),
            (eq, ":value", 1), # kicking a player
            (try_begin),
              (eq, "$g_multiplayer_kick_voteable", 1),
              (player_is_active, ":value_2"),
              (assign, ":continue", 1),
            (try_end),
          (else_try),
            (eq, ":value", 2), # banning a player
            (try_begin),
              (eq, "$g_multiplayer_ban_voteable", 1),
              (player_is_active, ":value_2"),
              (save_ban_info_of_player, ":value_2"),
              (assign, ":continue", 1),
            (try_end),
          (else_try), # vote for map
            (eq, ":value", 0),
            (try_begin),
              (eq, "$g_multiplayer_maps_voteable", 1),
              (call_script, "script_multiplayer_fill_map_game_types", "$g_multiplayer_game_type"),
              (assign, ":num_maps", reg0),
              (try_for_range, ":i_map", 0, ":num_maps"),
                (store_add, ":map_slot", ":i_map", multi_data_maps_for_game_type_begin),
                (troop_slot_eq, "trp_multiplayer_data", ":map_slot", ":value_2"),
                (assign, ":continue", 1),
                (assign, ":num_maps", 0), #break
              (try_end),
            (try_end),
          (else_try),
            (eq, ":value", 3), #vote for map and factions
            (try_begin),
              (eq, "$g_multiplayer_factions_voteable", 1),
              (store_script_param, ":value_3", 5),
              (store_script_param, ":value_4", 6),
              (call_script, "script_multiplayer_fill_map_game_types", "$g_multiplayer_game_type"),
              (assign, ":num_maps", reg0),
              (try_for_range, ":i_map", 0, ":num_maps"),
                (store_add, ":map_slot", ":i_map", multi_data_maps_for_game_type_begin),
                (troop_slot_eq, "trp_multiplayer_data", ":map_slot", ":value_2"),
                (assign, ":continue", 1),
                (assign, ":num_maps", 0), #break
              (try_end),
              (try_begin),
                (eq, ":continue", 1),
                (this_or_next|neg|is_between, ":value_3", mp_factions_begin, mp_factions_end),
                (this_or_next|neg|is_between, ":value_4", mp_factions_begin, mp_factions_end),
                (eq, ":value_3", ":value_4"),
                (assign, ":continue", 0),
              (try_end),
            (try_end),
          (else_try),
            (eq, ":value", 4), #vote for number of bots
            (store_script_param, ":value_3", 5),
            (store_add, ":upper_limit", "$g_multiplayer_num_bots_voteable", 1),
            (is_between, ":value_2", 0, ":upper_limit"),
            (is_between, ":value_3", 0, ":upper_limit"),
            (assign, ":continue", 1),
          (else_try),
            (eq, ":value", 5), #vote for game type
            (eq, "$g_multiplayer_gametype_voteable", 1),
            (is_between,":value_2",0, multiplayer_num_game_types),
            (assign, ":continue", 1),
          (try_end),
          (eq, ":continue", 1),
          #condition checks are done
          (str_store_player_username, s0, ":player_no"),
          (try_begin),
            (eq, ":value", 1), #kicking a player
            (str_store_player_username, s1, ":value_2"),
            (server_add_message_to_log, "str_poll_kick_player_s1_by_s0"),
          (else_try),
            (eq, ":value", 2), #banning a player
            (str_store_player_username, s1, ":value_2"),
            (server_add_message_to_log, "str_poll_ban_player_s1_by_s0"),
          (else_try),
            (eq, ":value", 0), #vote for map
            (try_begin),
              (is_between,":value_2",multiplayer_addon_scenes_begin,multiplayer_addon_scenes_end),
              (store_sub, ":string_index", ":value_2", multiplayer_addon_scenes_begin),
              (val_add, ":string_index", multiplayer_addon_scene_names_begin),
            (else_try),
              (store_sub, ":string_index", ":value_2", multiplayer_scenes_begin),
              (val_add, ":string_index", multiplayer_scene_names_begin),
            (try_end),
            (str_store_string, s1, ":string_index"),
            (server_add_message_to_log, "str_poll_change_map_to_s1_by_s0"),
          (else_try),
            (eq, ":value", 3), #vote for map and factions
            (try_begin),
              (is_between,":value_2",multiplayer_addon_scenes_begin,multiplayer_addon_scenes_end),
              (store_sub, ":string_index", ":value_2", multiplayer_addon_scenes_begin),
              (val_add, ":string_index", multiplayer_addon_scene_names_begin),
            (else_try),
              (store_sub, ":string_index", ":value_2", multiplayer_scenes_begin),
              (val_add, ":string_index", multiplayer_scene_names_begin),
            (try_end),
            (str_store_string, s1, ":string_index"),
            (str_store_faction_name, s2, ":value_3"),
            (str_store_faction_name, s3, ":value_4"),
            (server_add_message_to_log, "str_poll_change_map_to_s1_and_factions_to_s2_and_s3_by_s0"),
          (else_try),
            (eq, ":value", 4), #vote for number of bots
            (assign, reg0, ":value_2"),
            (assign, reg1, ":value_3"),
            (server_add_message_to_log, "str_poll_change_number_of_bots_to_reg0_and_reg1_by_s0"),
          (else_try),
            (eq, ":value", 5), #vote for game type
            (store_add, ":string", "str_multi_game_type_1", ":value_2"),
            (str_store_string,s1,":string"),
            (server_add_message_to_log, "str_poll_change_number_of_bots_to_reg0_and_reg1_by_s0"),
          (try_end),
          (assign, "$g_multiplayer_poll_running", 1),
          (assign, "$g_multiplayer_poll_ended", 0),
          (assign, "$g_multiplayer_poll_num_sent", 0),
          (assign, "$g_multiplayer_poll_yes_count", 0),
          (assign, "$g_multiplayer_poll_no_count", 0),
          (assign, "$g_multiplayer_poll_to_show", ":value"),
          (assign, "$g_multiplayer_poll_value_to_show", ":value_2"),
          (try_begin),
            (eq, ":value", 3),
            (assign, "$g_multiplayer_poll_value_2_to_show", ":value_3"),
            (assign, "$g_multiplayer_poll_value_3_to_show", ":value_4"),
          (else_try),
            (eq, ":value", 4),
            (assign, "$g_multiplayer_poll_value_2_to_show", ":value_3"),
            (assign, "$g_multiplayer_poll_value_3_to_show", -1),
          (else_try),
            (assign, "$g_multiplayer_poll_value_2_to_show", -1),
            (assign, "$g_multiplayer_poll_value_3_to_show", -1),
          (try_end),
          (store_add, ":poll_disable_until", ":mission_timer", multiplayer_poll_disable_period),
          (player_set_slot, ":player_no", slot_player_poll_disabled_until_time, ":poll_disable_until"),
          (store_add, "$g_multiplayer_poll_end_time", ":mission_timer", 60),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 0, ":num_players"),
            (player_is_active, ":cur_player"),
            (player_set_slot, ":cur_player", slot_player_can_answer_poll, 1),
            (val_add, "$g_multiplayer_poll_num_sent", 1),
            (multiplayer_send_4_int_to_player, ":cur_player", multiplayer_event_ask_for_poll, "$g_multiplayer_poll_to_show", "$g_multiplayer_poll_value_to_show", "$g_multiplayer_poll_value_2_to_show", "$g_multiplayer_poll_value_3_to_show"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_answer_to_poll),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (eq, "$g_multiplayer_poll_running", 1),
          (is_between, ":value", 0, 2),
          (player_slot_eq, ":player_no", slot_player_can_answer_poll, 1),
          #condition checks are done
          (player_set_slot, ":player_no", slot_player_can_answer_poll, 0),
          (try_begin),
            (eq, ":value", 0),
            (val_add, "$g_multiplayer_poll_no_count", 1),
          (else_try),
            (eq, ":value", 1),
            (val_add, "$g_multiplayer_poll_yes_count", 1),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_kick_player),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (player_is_admin, ":player_no"),
          (player_is_active, ":value"),
          #condition checks are done
          (kick_player, ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_ban_player),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (player_is_admin, ":player_no"),
          (player_is_active, ":value"),
          #condition checks are done
          (ban_player, ":value", 0, ":player_no"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_valid_vote_ratio),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 50, 101),
          #condition checks are done
          (assign, "$g_multiplayer_valid_vote_ratio", ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_auto_team_balance_limit),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (player_is_admin, ":player_no"),
          (this_or_next|is_between, ":value", 2, 7),
          (eq, ":value", 1000),
          #condition checks are done
          (assign, "$g_multiplayer_auto_team_balance_limit", ":value"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_auto_team_balance_limit, ":value"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_num_bots_voteable),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 51),
          (is_between, ":value", 0, "$g_multiplayer_max_num_bots"),
          #condition checks are done
          (assign, "$g_multiplayer_num_bots_voteable", ":value"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_num_bots_voteable, ":value"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_factions_voteable),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (assign, "$g_multiplayer_factions_voteable", ":value"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_factions_voteable, ":value"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_maps_voteable),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (assign, "$g_multiplayer_maps_voteable", ":value"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_maps_voteable, ":value"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_kick_voteable),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (assign, "$g_multiplayer_kick_voteable", ":value"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_kick_voteable, ":value"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_ban_voteable),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (assign, "$g_multiplayer_ban_voteable", ":value"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_int_to_player, ":cur_player", multiplayer_event_return_ban_voteable, ":value"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_allow_player_banners),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (assign, "$g_multiplayer_allow_player_banners", ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_force_default_armor),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (assign, "$g_multiplayer_force_default_armor", ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin),
        (store_script_param, ":sub_type", 3),
        (try_begin),
          (player_is_active,":player_no"),
          (player_is_admin,":player_no"),
          (try_begin),
            (eq,":sub_type",me_admin_set_deadly_bow),
            (store_script_param, ":value", 4),
            (try_begin),
              (is_between, ":value", 0, 2),
              (assign, "$g_multiplayer_deadly_bow", ":value"),
              (try_for_players,":i",1),
                (multiplayer_send_2_int_to_player,":i", multiplayer_event_return, me_return_deadly_bow, "$g_multiplayer_deadly_bow"),
              (try_end),
            (try_end),
          (else_try),
            (eq,":sub_type",me_admin_set_invasion_difficulty),
            (store_script_param, ":value", 4),
            (try_begin),
              (is_between, ":value", 1, 300),
              (assign, "$g_multiplayer_invasion_difficulty", ":value"),
              (try_for_players,":i",1),
                (multiplayer_send_2_int_to_player,":i", multiplayer_event_return, me_return_invasion_difficulty, "$g_multiplayer_invasion_difficulty"),
              (try_end),
            (try_end),
          (else_try),
            (eq,":sub_type",me_admin_reset_map),
            (call_script,"script_multi_reset_map",":player_no"),
          (else_try),
            (eq,":sub_type",me_admin_slay_horses),
            (try_for_agents,":horse"),
              (agent_is_alive,":horse"),
              (neg|agent_is_human,":horse"),
              (agent_get_rider,":agent",":horse"),
              (this_or_next|neg|agent_is_active,":agent"),
              (neg|agent_is_alive,":agent"),
              (agent_deliver_damage_to_agent,":horse",":horse",250),
            (try_end),
            (str_store_string,s1,"@All stray horses were slain by an admin"),
            (display_message,s1,0xFFBBBBFF),
            (server_add_message_to_log,s1),
          (else_try),
            (eq,":sub_type",me_admin_slay_bots),
            (try_for_agents,":agent"),
              (agent_is_alive,":agent"),
              (agent_is_human,":agent"),
              (agent_is_non_player,":agent"),
              (agent_deliver_damage_to_agent,":agent",":agent",250),
            (try_end),
            (str_store_string,s1,"@AI warriors were slain by an admin"),
            (display_message,s1,0xFFBBBBFF),
            (server_add_message_to_log,s1),
          (else_try),
            (eq,":sub_type",me_admin_slay_player),
            (store_script_param, ":value", 4),
            (try_begin),
              (str_clear,s1),
              (try_begin),
                (eq,":value",-2),
                (try_for_players,":i"),
                  (player_is_active,":i"),
                  #(neg|player_is_admin,":i"),
                  (player_get_agent_id,":agent",":i"),
                  (agent_is_active,":agent"),
                  (agent_is_alive,":agent"),
                  (agent_deliver_damage_to_agent,":agent",":agent",250),
                (try_end),
                (str_store_string,s1,"@All players were slain by an admin"),
              (else_try),
                (player_is_active,":value"),
                #(neg|player_is_admin,":value"),
                (player_get_agent_id,":agent",":value"),
                (agent_is_active,":agent"),
                (agent_is_alive,":agent"),
                (agent_deliver_damage_to_agent,":agent",":agent",250),
                (str_store_player_username,s1,":value"),
                (str_store_string,s1,"@Player '{s1}' was slain by an admin"),
              (try_end),
              (neg|str_is_empty,s1),
              (display_message,s1,0xFFBBBBFF),
              (server_add_message_to_log,s1),
            (try_end),
          (else_try),
            (this_or_next|eq,":sub_type",me_admin_refill_player),
            (this_or_next|eq,":sub_type",me_admin_fix_player_shield),
            (this_or_next|eq,":sub_type",me_admin_teleport_me),
            (this_or_next|eq,":sub_type",me_admin_teleport_him),
            (this_or_next|eq,":sub_type",me_admin_immortal),
            (eq,":sub_type",me_admin_heal_player),
            (store_script_param, ":value", 4),
            (try_begin),
              (str_clear,s1),
              (try_begin),
                (eq,":sub_type",me_admin_immortal),
                (eq,":value",-4),
                (try_for_players,":i"),
                  (player_is_active,":i"),
                  (player_slot_eq,":i",slot_player_is_immortal,1),
                  (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return, me_return_immortal_player, ":i"),
                (try_end),
              (else_try),
                (eq,":sub_type",me_admin_immortal),
                (le,":value",-2),
                (val_add,":value",3),
                (try_for_players,":i"),
                  (player_is_active,":i"),
                  (neg|player_slot_eq,":i",slot_player_is_immortal,":value"),
                  (player_set_slot,":i",slot_player_is_immortal,":value"),
                  (try_begin),
                    (eq,":value",1),
                    (multiplayer_is_dedicated_server),
                    (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return, me_return_immortal_player, ":i"),
                  (try_end),
                  (player_get_agent_id,":agent",":i"),
                  (agent_is_active,":agent"),
                  (agent_is_alive,":agent"),
                  (agent_set_no_death_knock_down_only,":agent",":value"),
                (try_end),
                (try_begin),
                  (eq,":value",1),
                  (str_store_string,s1,"@Admin made all players immortal"),
                (else_try),
                  (str_store_string,s1,"@Admin made all players mortal"),
                (try_end),
              (else_try),
                (eq,":value",-2),
                (try_for_players,":i"),
                  (player_is_active,":i"),
                  (player_get_agent_id,":agent",":i"),
                  (agent_is_active,":agent"),
                  (agent_is_alive,":agent"),
                  (try_begin),
                    (eq,":sub_type",me_admin_heal_player),
                    (agent_set_hit_points,":agent",100),
                  (else_try),
                    (eq,":sub_type",me_admin_refill_player),
                    (agent_refill_ammo,":agent"),
                  (else_try),
                    (eq,":sub_type",me_admin_fix_player_shield),
                    (agent_refill_wielded_shield_hit_points,":agent"),
                  (try_end),
                (try_end),
                (try_begin),
                  (eq,":sub_type",me_admin_heal_player),
                  (str_store_string,s1,"@All players were healed by an admin"),
                (else_try),
                  (eq,":sub_type",me_admin_refill_player),
                  (str_store_string,s1,"@Admin refilled ammo of all players"),
                (else_try),
                  (eq,":sub_type",me_admin_fix_player_shield),
                  (str_store_string,s1,"@Admin fixed shields of all players"),
                (try_end),
              (else_try),
                (player_is_active,":value"),
                (player_get_agent_id,":agent",":value"),
                (agent_is_active,":agent"),
                (agent_is_alive,":agent"),
                (str_store_player_username,s1,":value"),
                (try_begin),
                  (eq,":sub_type",me_admin_heal_player),
                  (agent_set_hit_points,":agent",100),
                  (str_store_string,s1,"@Player '{s1}' was healed by an admin"),
                (else_try),
                  (eq,":sub_type",me_admin_refill_player),
                  (agent_refill_ammo,":agent"),
                  (str_store_string,s1,"@Admin refilled ammo of player '{s1}'"),
                (else_try),
                  (eq,":sub_type",me_admin_fix_player_shield),
                  (agent_refill_wielded_shield_hit_points,":agent"),
                  (str_store_string,s1,"@Admin fixed shield of player '{s1}'"),
                (else_try),
                  (eq,":sub_type",me_admin_teleport_me),
                  (agent_get_position,pos1,":agent"),
                  (position_move_y,pos1,100),
                  (position_rotate_z,pos1,180),
                  (player_get_agent_id,":agent",":player_no"),
                  (try_begin),
                    (agent_is_active,":agent"),
                    (agent_is_alive,":agent"),
                    (agent_set_position,":agent",pos1),
                  (try_end),
                  (str_clear,s1),
                (else_try),
                  (eq,":sub_type",me_admin_teleport_him),
                  (player_get_agent_id,":i",":player_no"),
                  (try_begin),
                    (agent_is_active,":i"),
                    (agent_is_alive,":i"),
                    (agent_get_position,pos1,":i"),
                    (position_move_y,pos1,100),
                    (position_rotate_z,pos1,180),
                    (agent_set_position,":agent",pos1),
                  (try_end),
                  (str_clear,s1),
                (else_try),
                  (eq,":sub_type",me_admin_immortal),
                  (store_script_param, ":value_2", 5),
                  (neg|player_slot_eq,":value",slot_player_is_immortal,":value_2"),
                  (player_set_slot,":value",slot_player_is_immortal,":value_2"),
                  (player_get_agent_id,":agent",":value"),
                  (agent_is_active,":agent"),
                  (agent_is_alive,":agent"),
                  (agent_set_no_death_knock_down_only,":agent",":value_2"),
                  (try_begin),
                    (eq,":value_2",1),
                    (multiplayer_is_dedicated_server),
                    (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return, me_return_immortal_player, ":value"),
                  (try_end),
                  (assign,reg1,":value_2"),
                  (str_store_string,s1,"@Admin made player '{s1}' {reg1?im:}mortal"),
                (try_end),
              (try_end),
              (neg|str_is_empty,s1),
              (display_message,s1,0xFFBBBBFF),
              (server_add_message_to_log,s1),
            (try_end),
          (else_try),
            (eq,":sub_type",me_admin_swap_player_team),
            (store_script_param, ":value", 4),
            (try_begin),
              (str_clear,s1),
              (try_begin),
                (eq,":value",-2),
                (try_begin),
                  (eq, "$g_multiplayer_game_type", multiplayer_game_type_invasion),
                  (team_get_faction, ":faction_1", 0),
                  (team_get_faction, ":faction_2", 1),
                  (team_set_faction, 0, ":faction_2"),
                  (team_set_faction, 1, ":faction_1"),
                  (call_script, "script_game_multiplayer_get_game_type_mission_template", "$g_multiplayer_game_type"),
                  (start_multiplayer_mission, reg0, "$g_multiplayer_selected_map", 0),
                (else_try),
                  (try_for_players,":i"),
                    (player_is_active,":i"),
                    (player_get_team_no,":team",":i"),
                    (is_between,":team",0,2),
                    (val_add,":team",1),
                    (val_mod,":team",2),
                    (call_script,"script_refund_player_forced_team_change",":i"),
                    (player_set_troop_id,":i", -1),
                    (player_set_team_no,":i",":team"),
                    (multiplayer_send_message_to_player, ":i", multiplayer_event_force_start_team_selection),
                  (try_end),
                (try_end),
                (str_store_string,s1,"@Teams have been switched by an admin"),
              (else_try),
                (neq, "$g_multiplayer_game_type", multiplayer_game_type_invasion),
                (player_is_active,":value"),
                (player_get_team_no,":team",":value"),
                (is_between,":team",0,2),
                (val_add,":team",1),
                (val_mod,":team",2),
                (call_script,"script_refund_player_forced_team_change",":value"),
                (player_set_team_no,":value",":team"),
                (player_set_troop_id,":value", -1),
                (multiplayer_send_message_to_player, ":value", multiplayer_event_force_start_team_selection),
                (str_store_player_username,s1,":value"),
                (str_store_string,s1,"@Player '{s1}' was switched to the other team by an admin"),
              (try_end),
              (neg|str_is_empty,s1),
              (display_message,s1,0xFFBBBBFF),
              (server_add_message_to_log,s1),
            (try_end),
          (else_try),
            (eq,":sub_type",me_admin_swap_player_spectator),
            (store_script_param, ":value", 4),
            (try_begin),
              (str_clear,s1),
              (try_begin),
                (eq,":value",-2),
                (try_for_players,":i"),
                  (player_is_active,":i"),
                  (neg|player_is_admin,":i"),
                  (call_script,"script_refund_player_forced_team_change",":i"),
                  (player_set_troop_id,":i", -1),
                  (player_set_team_no,":i",multi_team_spectator),
                (try_end),
                (str_store_string,s1,"@All players were turned into spectators by an admin"),
              (else_try),
                (player_is_active,":value"),
                (call_script,"script_refund_player_forced_team_change",":value"),
                (player_set_troop_id,":value", -1),
                (player_set_team_no,":value",multi_team_spectator),
                (str_store_player_username,s1,":value"),
                (str_store_string,s1,"@Player '{s1}' was turned into spectator by an admin"),
              (try_end),
              (neg|str_is_empty,s1),
              (display_message,s1,0xFFBBBBFF),
              (server_add_message_to_log,s1),
            (try_end),
          (else_try),
            (eq,":sub_type",me_admin_give_thors_hammer),
            (store_script_param, ":value", 4),
            (try_begin),
              (player_is_active,":value"),
              (player_get_agent_id,":agent",":value"),
              (agent_is_active,":agent"),
              (agent_is_alive,":agent"),
              (agent_play_sound,":agent","snd_thunder_close"),
              (str_store_player_username,s1,":value"),
              (try_for_range,":slot",0,4),
                (agent_get_item_slot,":item",":agent",":slot"),
                (gt,":item",0),
                (agent_unequip_item,":agent",":item"),
              (try_end),
              (agent_equip_item,":agent","itm_thors_hammer"),
              (agent_set_wielded_item,":agent","itm_thors_hammer"),
              (str_store_string,s1,"@{s1} received a Thor's Hammer from an admin"),
              (display_message,s1,0xFFBBBBFF),
              (server_add_message_to_log,s1),
            (try_end),
          (else_try),
            (eq,":sub_type",me_admin_beacon_player),
            (store_script_param, ":value", 4),
            (try_begin),
              (player_is_active,":value"),
              (player_get_agent_id,":agent",":value"),
              (agent_is_active,":agent"),
              (agent_is_alive,":agent"),
              (try_for_players,":i",1),
                (neq,":i",":value"),
                (multiplayer_send_2_int_to_player,":i", multiplayer_event_return, me_return_beaconed_player, ":value"),
              (try_end),
              (neg|multiplayer_is_dedicated_server),
              (assign,"$tutorialmq_1_state",":value"),
              (start_presentation,"prsnt_multiplayer_beacon_player"),
            (try_end),
          (else_try),
            (eq,":sub_type",me_admin_set_allow_shield_bash),
            (store_script_param,":value",4),
            (try_begin),
              (is_between,":value",0,2),
              (assign,"$g_multiplayer_allow_shield_bash",":value"),
              (try_for_players,":i",1),
                (multiplayer_send_2_int_to_player,":i", multiplayer_event_return, me_return_allow_shield_bash, ":value"),
              (try_end),
            (try_end),
          (else_try),
            (eq,":sub_type",me_admin_set_allow_fall_from_horse),
            (store_script_param,":value",4),
            (try_begin),
              (is_between,":value",0,2),
              (assign,"$g_multiplayer_allow_fall_from_horse",":value"),
            (try_end),
          (else_try),
            (eq,":sub_type",me_admin_set_allow_shieldwall),
            (store_script_param,":value",4),
            (try_begin),
              (is_between,":value",0,2),
              (assign,"$g_multiplayer_allow_shieldwalls",":value"),
            (try_end),
          (else_try),
            (eq,":sub_type",me_admin_set_remove_stray_horses),
            (store_script_param,":value",4),
            (try_begin),
              (is_between,":value",0,1000),
              (assign,"$g_multiplayer_remove_stray_horses",":value"),
            (try_end),
          (else_try),
            (eq,":sub_type",me_admin_set_god_mode),
            (store_script_param,":value",4),
            (try_begin),
              (is_between,":value",0,2),
              (assign,"$g_multiplayer_god_mode",":value"),
              (try_for_agents,":agent"),
                (agent_is_alive,":agent"),
                (agent_set_no_death_knock_down_only,":agent",":value"),
              (try_end),
            (try_end),
          (else_try),
            (eq,":sub_type",me_admin_set_troop_classes),
            # (store_script_param,":value",4),
            # (try_begin),
            # (is_between,":value",0,2),
            # (assign,"$g_multiplayer_troop_classes",":value"),
            # (try_for_players,":i",1),
            # (multiplayer_send_2_int_to_player,":i", multiplayer_event_return, me_return_troop_classes, ":value"),
            # (try_end),
            # (try_end),
          (else_try),
            (eq,":sub_type",me_admin_force_master),
            (assign, "$g_battle_death_mode_started", 2),
            (call_script,"script_start_death_mode"),
            (try_for_players,":i",1),
              (multiplayer_send_int_to_player,":i",multiplayer_event_start_death_mode),
            (try_end),
            (scene_prop_get_instance, ":pole_1_id", "spr_headquarters_pole_code_only", 0),
            (scene_prop_get_instance, ":pole_2_id", "spr_headquarters_pole_code_only", 1),
            (scene_prop_get_instance, ":flag_1_id", "$team_1_flag_scene_prop", 0),
            (scene_prop_get_instance, ":flag_2_id", "$team_2_flag_scene_prop", 0),
            (store_random_in_range, "$g_random_entry_point", 0, 3),
            (val_add, "$g_random_entry_point", multi_death_mode_point),
            (entry_point_get_position, pos0, "$g_random_entry_point"),
            (position_set_z_to_ground_level, pos0),
            (position_move_x, pos0, 100),
            (prop_instance_set_position, ":pole_1_id", pos0),
            (position_move_x, pos0, -200),
            (prop_instance_set_position, ":pole_2_id", pos0),
            (prop_instance_get_position, pos0, ":pole_1_id"),
            (position_move_z, pos0, multi_headquarters_flag_initial_height),
            (prop_instance_set_position, ":flag_1_id", pos0),
            (prop_instance_get_position, pos0, ":pole_2_id"),
            (position_move_z, pos0, multi_headquarters_flag_initial_height),
            (prop_instance_set_position, ":flag_2_id", pos0),
            (start_presentation, "prsnt_multiplayer_flag_projection_display_bt"),
          (else_try),
            (eq,":sub_type",me_admin_set_gametype_voteable),
            (try_begin),
              (store_script_param, ":value", 4),
              (player_is_admin, ":player_no"),
              (is_between, ":value", 0, 2),
              (assign, "$g_multiplayer_gametype_voteable", ":value"),
              (try_for_players,":cur_player",1),
                (multiplayer_send_2_int_to_player, ":cur_player", multiplayer_event_return, me_return_gametype_voteable, ":value"),
              (try_end),
            (try_end),
          (else_try),
            (eq,":sub_type",me_admin_set_poll_cooldown),
            (try_begin),
              (store_script_param, ":value", 4),
              (player_is_admin, ":player_no"),
              (is_between, ":value", 0, 1000),
              (assign, "$g_multiplayer_poll_cooldown", ":value"),
              (try_begin),
                (gt,"$g_multiplayer_poll_cooldown_counter",":value"),
                (assign,"$g_multiplayer_poll_cooldown_counter",":value"),
              (try_end),
              (try_for_players,":cur_player",1),
                (multiplayer_send_2_int_to_player, ":cur_player", multiplayer_event_return, me_return_poll_cooldown, ":value"),
              (try_end),
            (try_end),
            
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_offer_duel),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (eq, "$g_multiplayer_game_type", multiplayer_game_type_duel),
          (agent_is_active, ":value"),
          (agent_is_alive, ":value"),
          (agent_is_human, ":value"),
          (player_get_agent_id, ":player_agent_no", ":player_no"),
          (agent_is_active, ":player_agent_no"),
          (agent_is_alive, ":player_agent_no"),
          (agent_get_position, pos0, ":player_agent_no"),
          (agent_get_position, pos1, ":value"),
          (get_sq_distance_between_positions_in_meters, ":agent_dist_sq", pos0, pos1),
          (le, ":agent_dist_sq", 49),
          #allow duelists to receive new offers
          (this_or_next|agent_check_offer_from_agent, ":player_agent_no", ":value"),
          (agent_slot_eq, ":player_agent_no", slot_agent_in_duel_with, -1),
          (neg|agent_slot_eq, ":player_agent_no", slot_agent_in_duel_with, ":value"), #don't allow spamming duel offers during countdown
          #condition checks are done
          (try_begin),
            #accepting a duel
            (agent_check_offer_from_agent, ":player_agent_no", ":value"),
            (call_script, "script_multiplayer_accept_duel", ":player_agent_no", ":value"),
          (else_try),
            #sending a duel request
            (assign, ":display_notification", 1),
            (try_begin),
              (agent_check_offer_from_agent, ":value", ":player_agent_no"),
              (assign, ":display_notification", 0),
            (try_end),
            (agent_add_offer_with_timeout, ":value", ":player_agent_no", 10000), #10 second timeout
            (agent_get_player_id, ":value_player", ":value"),
            (try_begin),
              (player_is_active, ":value_player"), #might be AI
              (try_begin),
                (eq, ":display_notification", 1),
                (multiplayer_send_int_to_player, ":value_player", multiplayer_event_show_duel_request, ":player_agent_no"),
              (try_end),
            (else_try),
              (call_script, "script_multiplayer_accept_duel", ":value", ":player_agent_no"),
            (try_end),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_set_disallow_ranged_weapons),
        (try_begin),
          (store_script_param, ":value", 3),
          #validity check
          (player_is_admin, ":player_no"),
          (is_between, ":value", 0, 2),
          #condition checks are done
          (assign, "$g_multiplayer_disallow_ranged_weapons", ":value"),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_chat),
        (try_begin),
          (multiplayer_is_server),
          (try_begin),
            (player_is_active, ":player_no"),
            (player_is_admin, ":player_no"),
            (str_store_player_username,s1,":player_no"),
            (str_store_string,s0,"@*ADMIN CHAT* [{s1}]: {s0}"),
            (try_for_players,":i",1),
              (player_is_admin,":i"),
              (multiplayer_send_string_to_player,":i",multiplayer_event_admin_chat, s0),
            (try_end),
            (neg|multiplayer_is_dedicated_server),
            (display_message, s0, 0xFFBBBBFF),
          (try_end),
        (else_try),
          (neg|multiplayer_is_dedicated_server),
          (display_message, s0, 0xFFBBBBFF),
        (try_end),
      (else_try),
        (eq, ":event_type", multiplayer_event_admin_message),
        (try_begin),
          (multiplayer_is_server),
          (try_begin),
            (player_is_active, ":player_no"),
            (player_is_admin, ":player_no"),
            (try_for_players,":i",1),
              (multiplayer_send_string_to_player,":i",multiplayer_event_admin_message, s0),
            (try_end),
            (neg|multiplayer_is_dedicated_server),
            (start_presentation,"prsnt_multiplayer_display_admin_message"),
          (try_end),
        (else_try),
          (neg|multiplayer_is_dedicated_server),
          (start_presentation,"prsnt_multiplayer_display_admin_message"),
        (try_end),
      (else_try),#CLASS GIVE ORDER
        (eq, ":event_type", multiplayer_event_give_order_to_class),
        (try_begin),
          (multiplayer_is_server),
          (player_is_active,":player_no"),
          (player_is_admin,":player_no"),
          (store_script_param,":val",3),#class
          (store_script_param,":x",4),#x or order
          (store_script_param,":y",5),
          (store_script_param,":z",6),
          
          (try_begin),
            (ge,":val",20),
            (init_position,pos2),
            (set_fixed_point_multiplier,1000),
            (position_set_x,pos2,":x"),
            (position_set_y,pos2,":y"),
            (position_set_z,pos2,":z"),
            (try_begin),
              (ge,":val",20),
              (val_sub,":val",20),
              (team_give_order,":player_no",":val",mordr_hold),
              (team_set_order_position,":player_no",":val",pos2),
            (try_end),
          (else_try),
            (team_give_order,":player_no",":val",":x"),
          (try_end),
        (try_end),
      (else_try),###SQUAD ORDERS
        (eq, ":event_type", multiplayer_event_class_orders),
        (store_script_param,":subtype",3),
        (try_begin),
          (eq,":subtype",me_class_order_select),
          (try_begin),
            (multiplayer_is_server),
            (player_is_active,":player_no"),
            (player_is_admin,":player_no"),
            (store_script_param,":selected_class",4),
            (try_for_agents,":bot"),
              (agent_is_alive,":bot"),
              (agent_is_human,":bot"),
              (agent_get_team,":team",":bot"),
              (eq,":team",1),
              (agent_is_non_player,":bot"),
              (agent_get_class,":class",":bot"),
              (this_or_next|eq,":selected_class",grc_everyone),
              (eq,":selected_class",":class"),
              (agent_set_group,":bot",":player_no"),
            (try_end),
            
            (team_get_movement_order,":move",":player_no",":selected_class"),
            (team_get_weapon_usage_order,":weapon",":player_no",":selected_class"),
            (multiplayer_send_3_int_to_player,":player_no",multiplayer_event_class_orders,me_class_order_return,":move",":weapon"),
          (try_end),
        (else_try),
          (eq,":subtype",me_class_order_return),
          (try_begin),
            (neg|multiplayer_is_dedicated_server),
            (is_presentation_active,"prsnt_invasion_commander"),
            (store_script_param,":move",4),
            (store_script_param,":weapon",5),
            
            (try_begin),
              (eq,":move",mordr_charge),
              (assign,"$g_multiplayer_invasion_order_move_val",0),
            (else_try),
              (eq,":move",mordr_hold),
              (assign,"$g_multiplayer_invasion_order_move_val",1),
            (try_end),
            
            (try_begin),
              (eq,":weapon",wordr_use_any_weapon),
              (assign,"$g_multiplayer_invasion_order_weapons_val",0),
            (else_try),
              (eq,":weapon",wordr_use_melee_weapons),
              (assign,"$g_multiplayer_invasion_order_weapons_val",1),
            (else_try),
              (eq,":weapon",wordr_use_ranged_weapons),
              (assign,"$g_multiplayer_invasion_order_weapons_val",2),
            (try_end),
            (overlay_set_val,"$g_multiplayer_invasion_order_move","$g_multiplayer_invasion_order_move_val"),
            (overlay_set_val,"$g_multiplayer_invasion_order_weapons","$g_multiplayer_invasion_order_weapons_val"),
          (try_end),
        (try_end),
      (else_try),
        ###############
        #CLIENT EVENTS#
        ###############
        (neg|multiplayer_is_dedicated_server),
        (try_begin),
          (eq, ":event_type", multiplayer_event_return_renaming_server_allowed),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_renaming_server_allowed", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_changing_game_type_allowed),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_changing_game_type_allowed", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_max_num_players),
          (store_script_param, ":value", 3),
          (server_set_max_num_players, ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_next_team_faction),
          (store_script_param, ":value", 3),
          (store_script_param, ":value_2", 4),
          (try_begin),
            (eq, ":value", 1),
            (assign, "$g_multiplayer_next_team_1_faction", ":value_2"),
          (else_try),
            (assign, "$g_multiplayer_next_team_2_faction", ":value_2"),
          (try_end),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_num_bots_in_team),
          (store_script_param, ":value", 3),
          (store_script_param, ":value_2", 4),
          (try_begin),
            (eq, ":value", 1),
            (assign, "$g_multiplayer_num_bots_team_1", ":value_2"),
          (else_try),
            (assign, "$g_multiplayer_num_bots_team_2", ":value_2"),
          (try_end),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_anti_cheat),
          (store_script_param, ":value", 3),
          (server_set_anti_cheat, ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_friendly_fire),
          (store_script_param, ":value", 3),
          (server_set_friendly_fire, ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_melee_friendly_fire),
          (store_script_param, ":value", 3),
          (server_set_melee_friendly_fire, ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_friendly_fire_damage_self_ratio),
          (store_script_param, ":value", 3),
          (server_set_friendly_fire_damage_self_ratio, ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_friendly_fire_damage_friend_ratio),
          (store_script_param, ":value", 3),
          (server_set_friendly_fire_damage_friend_ratio, ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_ghost_mode),
          (store_script_param, ":value", 3),
          (server_set_ghost_mode, ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_control_block_dir),
          (store_script_param, ":value", 3),
          (server_set_control_block_dir, ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_add_to_servers_list),
          (store_script_param, ":value", 3),
          (server_set_add_to_game_servers_list, ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_respawn_period),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_respawn_period", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_game_max_minutes),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_game_max_minutes", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_round_max_seconds),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_round_max_seconds", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_player_respawn_as_bot),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_player_respawn_as_bot", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_game_max_points),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_game_max_points", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_point_gained_from_flags),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_point_gained_from_flags", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_point_gained_from_capturing_flag),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_point_gained_from_capturing_flag", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_initial_gold_multiplier),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_initial_gold_multiplier", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_battle_earnings_multiplier),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_battle_earnings_multiplier", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_round_earnings_multiplier),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_round_earnings_multiplier", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_respawn_count),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_number_of_respawn_count", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_server_name),
          (server_set_name, s0),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_game_password),
          (server_set_password, s0),
          #this is the last option in admin panel, so start the presentation
          (start_presentation, "prsnt_game_multiplayer_admin_panel"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_open_game_rules),
          #this is the last message for game rules, so start the presentation
          (assign, "$g_multiplayer_show_server_rules", 1),
          (start_presentation, "prsnt_multiplayer_welcome_message"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_game_type),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_game_type", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_valid_vote_ratio),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_valid_vote_ratio", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_max_num_bots),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_max_num_bots", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_server_mission_timer_while_player_joined),
          (store_script_param, ":value", 3),
          (assign, "$server_mission_timer_while_player_joined", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_auto_team_balance_limit),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_auto_team_balance_limit", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_num_bots_voteable),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_num_bots_voteable", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_factions_voteable),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_factions_voteable", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_maps_voteable),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_maps_voteable", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_kick_voteable),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_kick_voteable", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_ban_voteable),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_ban_voteable", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_allow_player_banners),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_allow_player_banners", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_force_default_armor),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_force_default_armor", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_disallow_ranged_weapons),
          (store_script_param, ":value", 3),
          (assign, "$g_multiplayer_disallow_ranged_weapons", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_confirmation),
          (assign, "$g_confirmation_result", 1),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_rejection),
          (assign, "$g_confirmation_result", -1),
        (else_try),
          (eq, ":event_type", multiplayer_event_return),
          (store_script_param, ":sub_type", 3),
          (try_begin),
            (eq,":sub_type",me_return_deadly_bow),
            (store_script_param, "$g_multiplayer_deadly_bow", 4),
          (else_try),
            (eq,":sub_type",me_return_invasion_data),
            (store_script_param, ":wave_number", 4),
            (store_script_param, ":wave_time", 5),
            (store_script_param, ":bots", 6),
            (try_begin),
              (gt, ":bots", 0),
              (assign, "$g_multiplayer_invasion_bots_left", ":bots"),
            (try_end),
            (assign, "$g_multiplayer_invasion_wave_number", ":wave_number"),
            (try_begin),
              (le, ":wave_time", 0),
              (assign, "$g_multiplayer_invasion_wave_time", ":wave_time"),
            (else_try),
              (store_mission_timer_a, ":round_time"),
              (store_add, "$g_multiplayer_invasion_wave_time", ":round_time", ":wave_time"),
            (try_end),
          (else_try),
            (eq,":sub_type",me_return_invasion_difficulty),
            (store_script_param, ":value", 4),
            (try_begin),
              (is_between, ":value", 1, 300),
              (assign, "$g_multiplayer_invasion_difficulty", ":value"),
            (try_end),
          (else_try),
            (eq,":sub_type",me_return_beaconed_player),
            (store_script_param, ":value", 4),
            (assign,"$tutorialmq_1_state",":value"),
            (start_presentation,"prsnt_multiplayer_beacon_player"),
          (else_try),
            (eq,":sub_type",me_return_allow_shield_bash),
            (store_script_param, ":value", 4),
            (assign, "$g_multiplayer_allow_shield_bash", ":value"),
          (else_try),
            (eq,":sub_type",me_return_allow_fall_from_horse),
            (store_script_param, ":value", 4),
            (assign, "$g_multiplayer_allow_fall_from_horse", ":value"),
          (else_try),
            (eq,":sub_type",me_return_allow_shieldwall),
            (store_script_param, ":value", 4),
            (assign, "$g_multiplayer_allow_shieldwalls", ":value"),
          (else_try),
            (eq,":sub_type",me_return_thors_champion_agent),
            (store_script_param, ":value", 4),
            (assign, "$gThorsChampionAgent", ":value"),
            (val_mul, "$tutorial_2_msg_1_displayed", -1),
          (else_try),
            (eq,":sub_type",me_return_remove_stray_horses),
            (store_script_param,":value",4),
            (assign,"$g_multiplayer_remove_stray_horses",":value"),
          (else_try),
            (eq,":sub_type",me_return_god_mode),
            (store_script_param,":value",4),
            (assign,"$g_multiplayer_god_mode",":value"),
          (else_try),
            (eq,":sub_type",me_return_troop_classes),
            # (store_script_param,":value",4),
            # (assign,"$g_multiplayer_troop_classes",":value"),
          (else_try),
            (eq,":sub_type",me_return_immortal_player),
            (store_script_param,":value",4),
            (try_begin),
              (player_is_active,":value"),
              (player_set_slot,":value",slot_player_is_immortal,1),
            (try_end),
          (else_try),
            (eq,":sub_type",me_return_gametype_voteable),
            (store_script_param, ":value", 4),
            (assign, "$g_multiplayer_gametype_voteable", ":value"),
          (else_try),
            (eq,":sub_type",me_return_poll_cooldown),
            (store_script_param, ":value", 4),
            (assign, "$g_multiplayer_poll_cooldown", ":value"),
          (else_try),
            (eq,":sub_type",me_return_poll_cooldown_failed),
            (multiplayer_get_my_player,":player"),
            (player_set_slot,":player",slot_player_poll_disabled_until_time,0),
          (try_end),
        (else_try),
          (eq, ":event_type", multiplayer_event_show_multiplayer_message),
          (store_script_param, ":value", 3),
          (store_script_param, ":value_2", 4),
          (call_script, "script_show_multiplayer_message", ":value", ":value_2"),
        (else_try),
          (eq, ":event_type", multiplayer_event_draw_this_round),
          (store_script_param, ":value", 3),
          (call_script, "script_draw_this_round", ":value"),
        (else_try),
          (eq, ":event_type", multiplayer_event_set_attached_scene_prop),
          (store_script_param, ":value", 3),
          (store_script_param, ":value_2", 4),
          (call_script, "script_set_attached_scene_prop", ":value", ":value_2"),
        (else_try),
          (eq, ":event_type", multiplayer_event_set_team_flag_situation),
          (store_script_param, ":value", 3),
          (store_script_param, ":value_2", 4),
          (call_script, "script_set_team_flag_situation", ":value", ":value_2"),
        (else_try),
          (eq, ":event_type", multiplayer_event_set_team_score),
          (store_script_param, ":value", 3),
          (store_script_param, ":value_2", 4),
          (call_script, "script_team_set_score", ":value", ":value_2"),
        (else_try),
          (eq, ":event_type", multiplayer_event_set_player_score_kill_death),
          (store_script_param, ":value", 3),
          (store_script_param, ":value_2", 4),
          (store_script_param, ":value_3", 5),
          (store_script_param, ":value_4", 6),
          (call_script, "script_player_set_score", ":value", ":value_2"),
          (call_script, "script_player_set_kill_count", ":value", ":value_3"),
          (call_script, "script_player_set_death_count", ":value", ":value_4"),
        (else_try),
          (eq, ":event_type", multiplayer_event_set_num_agents_around_flag),
          (store_script_param, ":flag_no", 3),
          (store_script_param, ":current_owner_code", 4),
          (call_script, "script_set_num_agents_around_flag", ":flag_no", ":current_owner_code"),
        (else_try),
          (eq, ":event_type", multiplayer_event_ask_for_poll),
          (store_script_param, ":value", 3),
          (store_script_param, ":value_2", 4),
          (store_script_param, ":value_3", 5),
          (store_script_param, ":value_4", 6),
          (assign, ":continue_to_poll", 0),
          (try_begin),
            (this_or_next|eq, ":value", 1),
            (eq, ":value", 2),
            (player_is_active, ":value_2"), #might go offline before here
            (assign, ":continue_to_poll", 1),
          (else_try),
            (assign, ":continue_to_poll", 1),
          (try_end),
          (try_begin),
            (eq, ":continue_to_poll", 1),
            (assign, "$g_multiplayer_poll_to_show", ":value"),
            (assign, "$g_multiplayer_poll_value_to_show", ":value_2"),
            (assign, "$g_multiplayer_poll_value_2_to_show", ":value_3"),
            (assign, "$g_multiplayer_poll_value_3_to_show", ":value_4"),
            (store_mission_timer_a, ":mission_timer"),
            (store_add, "$g_multiplayer_poll_client_end_time", ":mission_timer", 60),
            (start_presentation, "prsnt_multiplayer_poll"),
          (try_end),
        (else_try),
          (eq, ":event_type", multiplayer_event_change_flag_owner),
          (store_script_param, ":flag_no", 3),
          (store_script_param, ":owner_code", 4),
          (call_script, "script_change_flag_owner", ":flag_no", ":owner_code"),
        (else_try),
          (eq, ":event_type", multiplayer_event_use_item),
          (store_script_param, ":value", 3),
          (store_script_param, ":value_2", 4),
          (call_script, "script_use_item", ":value", ":value_2"),
        (else_try),
          (eq, ":event_type", multiplayer_event_set_scene_prop_open_or_close),
          (store_script_param, ":instance_id", 3),
          
          (scene_prop_set_slot, ":instance_id", scene_prop_open_or_close_slot, 1),
          
          (prop_instance_get_scene_prop_kind, ":scene_prop_id", ":instance_id"),
          
          (try_begin),
            (eq, ":scene_prop_id", "spr_winch_b"),
            (assign, ":effected_object", "spr_portcullis"),
          (else_try),
            (this_or_next|eq, ":scene_prop_id", "spr_castle_e_sally_door_a"),
            (this_or_next|eq, ":scene_prop_id", "spr_castle_f_sally_door_a"),
            (this_or_next|eq, ":scene_prop_id", "spr_earth_sally_gate_left"),
            (this_or_next|eq, ":scene_prop_id", "spr_earth_sally_gate_right"),
            (this_or_next|eq, ":scene_prop_id", "spr_viking_keep_destroy_sally_door_left"),
            (this_or_next|eq, ":scene_prop_id", "spr_viking_keep_destroy_sally_door_right"),
            (this_or_next|eq, ":scene_prop_id", "spr_castle_f_door_a"),
            (this_or_next|eq, ":scene_prop_id", "spr_door_destructible"),
            (this_or_next|eq, ":scene_prop_id", "spr_castle_f_door_b"),
            (this_or_next|eq, ":scene_prop_id", "spr_siege_ladder_move_6m"),
            (this_or_next|eq, ":scene_prop_id", "spr_siege_ladder_move_8m"),
            (this_or_next|eq, ":scene_prop_id", "spr_siege_ladder_move_10m"),
            (this_or_next|eq, ":scene_prop_id", "spr_siege_ladder_move_12m"),
            (eq, ":scene_prop_id", "spr_siege_ladder_move_14m"),
            (assign, ":effected_object", ":scene_prop_id"),
          (try_end),
          
          (try_begin),
            (eq, ":effected_object", "spr_portcullis"),
            
            (assign, ":smallest_dist", -1),
            (prop_instance_get_position, pos0, ":instance_id"),
            (scene_prop_get_num_instances, ":num_instances_of_effected_object", ":effected_object"),
            (try_for_range, ":cur_instance", 0, ":num_instances_of_effected_object"),
              (scene_prop_get_instance, ":cur_instance_id", ":effected_object", ":cur_instance"),
              (prop_instance_get_position, pos1, ":cur_instance_id"),
              (get_sq_distance_between_positions, ":dist", pos0, pos1),
              (this_or_next|eq, ":smallest_dist", -1),
              (lt, ":dist", ":smallest_dist"),
              (assign, ":smallest_dist", ":dist"),
              (assign, ":effected_object_instance_id", ":cur_instance_id"),
            (try_end),
            
            (ge, ":smallest_dist", 0),
            (prop_instance_is_animating, ":is_animating", ":effected_object_instance_id"),
            (eq, ":is_animating", 0),
            
            (prop_instance_get_starting_position, pos0, ":effected_object_instance_id"),
            (position_move_z, pos0, 375),
            (prop_instance_animate_to_position, ":effected_object_instance_id", pos0, 1),
          (else_try),
            (this_or_next|eq, ":scene_prop_id", "spr_castle_e_sally_door_a"),
            (this_or_next|eq, ":scene_prop_id", "spr_castle_f_sally_door_a"),
            (this_or_next|eq, ":scene_prop_id", "spr_earth_sally_gate_left"),
            (this_or_next|eq, ":scene_prop_id", "spr_earth_sally_gate_right"),
            (this_or_next|eq, ":scene_prop_id", "spr_viking_keep_destroy_sally_door_left"),
            (this_or_next|eq, ":scene_prop_id", "spr_viking_keep_destroy_sally_door_right"),
            (this_or_next|eq, ":scene_prop_id", "spr_castle_f_door_a"),
            (this_or_next|eq, ":scene_prop_id", "spr_door_destructible"),
            (eq, ":scene_prop_id", "spr_castle_f_door_b"),
            (assign, ":effected_object_instance_id", ":instance_id"),
            (prop_instance_get_starting_position, pos0, ":effected_object_instance_id"),
            (position_rotate_z, pos0, -80),
            (prop_instance_animate_to_position, ":effected_object_instance_id", pos0, 1),
          (else_try),
            (assign, ":effected_object_instance_id", ":instance_id"),
            (prop_instance_is_animating, ":is_animating", ":effected_object_instance_id"),
            (eq, ":is_animating", 0),
            (prop_instance_get_starting_position, pos0, ":effected_object_instance_id"),
            (prop_instance_animate_to_position, ":effected_object_instance_id", pos0, 1),
          (try_end),
        (else_try),
          (eq, ":event_type", multiplayer_event_set_round_start_time),
          (store_script_param, ":value", 3),
          
          (try_begin),
            (neq, ":value", -9999),
            (assign, "$g_round_start_time", ":value"),
          (else_try),
            (store_mission_timer_a, "$g_round_start_time"),
            
            #if round start time is assigning to current time (so new round is starting) then also initialize moveable object slots too.
            (call_script, "script_initialize_scene_prop_slots", "spr_siege_ladder_move_6m"),
            (call_script, "script_initialize_scene_prop_slots", "spr_siege_ladder_move_8m"),
            (call_script, "script_initialize_scene_prop_slots", "spr_siege_ladder_move_10m"),
            (call_script, "script_initialize_scene_prop_slots", "spr_siege_ladder_move_12m"),
            (call_script, "script_initialize_scene_prop_slots", "spr_siege_ladder_move_14m"),
            (call_script, "script_initialize_scene_prop_slots", "spr_winch_b"),
          (try_end),
        (else_try),
          (eq, ":event_type", multiplayer_event_force_start_team_selection),
          (try_begin),
            (is_presentation_active, "prsnt_multiplayer_item_select"),
            (assign, "$g_close_equipment_selection", 1),
          (try_end),
          #(start_presentation, "prsnt_multiplayer_troop_select"),
          (call_script,"script_replace_troop_presentation"),
        (else_try),
          (eq, ":event_type", multiplayer_event_start_death_mode),
          (assign, "$g_battle_death_mode_started", 2),
          #  (start_presentation, "prsnt_multiplayer_flag_projection_display_bt"), #quita presentacion de banner en master of the field chief
          (call_script, "script_start_death_mode"),
        (else_try), #chief capitan
          (eq, ":event_type", multiplayer_event_start_capitan_mode),
          (store_script_param,":value",3),
          (assign,"$g_round_tropas",":value"),
          (call_script, "script_start_capitan_mode"),
        (else_try),
          (eq, ":event_type", multiplayer_event_return_player_respawn_spent),
          (store_script_param, ":value", 3),
          (try_begin),
            (gt, "$g_my_spawn_count", 0),
            (store_add, "$g_my_spawn_count", "$g_my_spawn_count", ":value"),
          (else_try),
            (assign, "$g_my_spawn_count", ":value"),
          (try_end),
        (else_try),
          (eq, ":event_type", multiplayer_event_show_duel_request),
          (store_script_param, ":value", 3),
          (try_begin),
            (agent_is_active, ":value"),
            (agent_get_player_id, ":value_player_no", ":value"),
            (try_begin),
              (player_is_active, ":value_player_no"),
              (str_store_player_username, s0, ":value_player_no"),
            (else_try),
              (str_store_agent_name, s0, ":value"),
            (try_end),
            (display_message, "str_s0_offers_a_duel_with_you"),
            (try_begin),
              (get_player_agent_no, ":player_agent"),
              (agent_is_active, ":player_agent"),
              (agent_add_offer_with_timeout, ":player_agent", ":value", 10000), #10 second timeout
            (try_end),
          (try_end),
        (else_try),
          (eq, ":event_type", multiplayer_event_start_duel),
          (store_script_param, ":value", 3),
          
          (store_mission_timer_a, ":mission_timer"),
          (try_begin),
            (agent_is_active, ":value"),
            (get_player_agent_no, ":player_agent"),
            (agent_is_active, ":player_agent"),
            (agent_get_player_id, ":value_player_no", ":value"),
            (try_begin),
              (player_is_active, ":value_player_no"),
              (str_store_player_username, s0, ":value_player_no"),
            (else_try),
              (str_store_agent_name, s0, ":value"),
            (try_end),
            (display_message, "str_a_duel_between_you_and_s0_will_start_in_3_seconds"),
            (assign, "$g_multiplayer_duel_start_time", ":mission_timer"),
            (start_presentation, "prsnt_multiplayer_duel_start_counter"),
            (agent_set_slot, ":player_agent", slot_agent_in_duel_with, ":value"),
            (agent_set_slot, ":value", slot_agent_in_duel_with, ":player_agent"),
            (agent_set_slot, ":player_agent", slot_agent_duel_start_time, ":mission_timer"),
            (agent_set_slot, ":value", slot_agent_duel_start_time, ":mission_timer"),
            (agent_clear_relations_with_agents, ":player_agent"),
            (agent_clear_relations_with_agents, ":value"),
            ##            (agent_add_relation_with_agent, ":player_agent", ":value", -1),
          (try_end),
        (else_try),
          (eq, ":event_type", multiplayer_event_cancel_duel),
          (store_script_param, ":value", 3),
          (try_begin),
            (agent_is_active, ":value"),
            (agent_get_player_id, ":value_player_no", ":value"),
            (try_begin),
              (player_is_active, ":value_player_no"),
              (str_store_player_username, s0, ":value_player_no"),
            (else_try),
              (str_store_agent_name, s0, ":value"),
            (try_end),
            (display_message, "str_your_duel_with_s0_is_cancelled"),
          (try_end),
          (try_begin),
            (get_player_agent_no, ":player_agent"),
            (agent_is_active, ":player_agent"),
            (agent_set_slot, ":player_agent", slot_agent_in_duel_with, -1),
            (agent_clear_relations_with_agents, ":player_agent"),
          (try_end),
        (else_try),
          (eq, ":event_type", multiplayer_event_show_server_message),
          (display_message, "str_server_s0", 0xFFFF6666),
        (try_end),
      (try_end),
  ]),
  
  # script_cf_multiplayer_evaluate_poll
  # Input: none
  # Output: none (can fail)
  ("cf_multiplayer_evaluate_poll",
    [
      (assign, ":result", 0),
      (assign, "$g_multiplayer_poll_ended", 1),
      (store_add, ":total_votes", "$g_multiplayer_poll_yes_count", "$g_multiplayer_poll_no_count"),
      (store_sub, ":abstain_votes", "$g_multiplayer_poll_num_sent", ":total_votes"),
      (store_mul, ":nos_from_abstains", 3, ":abstain_votes"),
      (val_div, ":nos_from_abstains", 10), #30% of abstains are counted as no
      (val_add, ":total_votes", ":nos_from_abstains"),
      (val_max, ":total_votes", 1), #if someone votes and only 1-3 abstain occurs?
      (store_mul, ":vote_ratio", 100, "$g_multiplayer_poll_yes_count"),
      (val_div, ":vote_ratio", ":total_votes"),
      (try_begin),
        (ge, ":vote_ratio", "$g_multiplayer_valid_vote_ratio"),
        (assign, ":result", 1),
        (try_begin),
          (eq, "$g_multiplayer_poll_to_show", 1), #kick player
          (try_begin),
            (player_is_active, "$g_multiplayer_poll_value_to_show"),
            (kick_player, "$g_multiplayer_poll_value_to_show"),
          (try_end),
        (else_try),
          (eq, "$g_multiplayer_poll_to_show", 2), #ban player
          (ban_player_using_saved_ban_info), #already loaded at the beginning of the poll
        (else_try),
          (eq, "$g_multiplayer_poll_to_show", 3), #change map with factions
          (team_set_faction, 0, "$g_multiplayer_poll_value_2_to_show"),
          (team_set_faction, 1, "$g_multiplayer_poll_value_3_to_show"),
        (else_try),
          (eq, "$g_multiplayer_poll_to_show", 4), #change number of bots
          (assign, "$g_multiplayer_num_bots_team_1", "$g_multiplayer_poll_value_to_show"),
          (assign, "$g_multiplayer_num_bots_team_2", "$g_multiplayer_poll_value_2_to_show"),
          (get_max_players, ":num_players"),
          (try_for_range, ":cur_player", 1, ":num_players"),
            (player_is_active, ":cur_player"),
            (multiplayer_send_2_int_to_player, ":cur_player", multiplayer_event_return_num_bots_in_team, 1, "$g_multiplayer_num_bots_team_1"),
            (multiplayer_send_2_int_to_player, ":cur_player", multiplayer_event_return_num_bots_in_team, 2, "$g_multiplayer_num_bots_team_2"),
          (try_end),
        (else_try),
          (eq, "$g_multiplayer_poll_to_show", 5), #change gametype
          (assign,"$g_multiplayer_game_type","$g_multiplayer_poll_value_to_show"),
          (call_script, "script_multiplayer_fill_map_game_types","$g_multiplayer_game_type"),
          (troop_get_slot,":first_map","trp_multiplayer_data",multi_data_maps_for_game_type_begin),
          (assign,"$g_multiplayer_poll_value_to_show",":first_map"),
        (try_end),
      (else_try),
        (assign, "$g_multiplayer_poll_running", 0), #end immediately if poll fails. but end after some time if poll succeeds (apply the results first)
      (try_end),
      (get_max_players, ":num_players"),
      #for only server itself-----------------------------------------------------------------------------------------------
      (call_script, "script_show_multiplayer_message", multiplayer_message_type_poll_result, ":result"), #0 is useless here
      #for only server itself-----------------------------------------------------------------------------------------------
      (try_for_range, ":cur_player", 1, ":num_players"),
        (player_is_active, ":cur_player"),
        (multiplayer_send_2_int_to_player, ":cur_player", multiplayer_event_show_multiplayer_message, multiplayer_message_type_poll_result, ":result"),
      (try_end),
      (eq, ":result", 1),
  ]),
  
  # script_multiplayer_accept_duel
  # Input: arg1 = agent_no, arg2 = agent_no_offerer
  # Output: none
  ("multiplayer_accept_duel",
    [
      (store_script_param, ":agent_no", 1),
      (store_script_param, ":agent_no_offerer", 2),
      (try_begin),
        (agent_slot_ge, ":agent_no", slot_agent_in_duel_with, 0),
        (agent_get_slot, ":ex_duelist", ":agent_no", slot_agent_in_duel_with),
        (agent_is_active, ":ex_duelist"),
        (agent_clear_relations_with_agents, ":ex_duelist"),
        (agent_set_slot, ":ex_duelist", slot_agent_in_duel_with, -1),
        (agent_get_player_id, ":player_no", ":ex_duelist"),
        (try_begin),
          (player_is_active, ":player_no"), #might be AI
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_cancel_duel, ":agent_no"),
        (else_try),
          (agent_force_rethink, ":ex_duelist"),
        (try_end),
      (try_end),
      (try_begin),
        (agent_slot_ge, ":agent_no_offerer", slot_agent_in_duel_with, 0),
        (agent_get_slot, ":ex_duelist", ":agent_no_offerer", slot_agent_in_duel_with),
        (agent_is_active, ":ex_duelist"),
        (agent_clear_relations_with_agents, ":ex_duelist"),
        (agent_set_slot, ":ex_duelist", slot_agent_in_duel_with, -1),
        (try_begin),
          (player_is_active, ":player_no"), #might be AI
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_cancel_duel, ":agent_no_offerer"),
        (else_try),
          (agent_force_rethink, ":ex_duelist"),
        (try_end),
      (try_end),
      (agent_set_slot, ":agent_no", slot_agent_in_duel_with, ":agent_no_offerer"),
      (agent_set_slot, ":agent_no_offerer", slot_agent_in_duel_with, ":agent_no"),
      (agent_clear_relations_with_agents, ":agent_no"),
      (agent_clear_relations_with_agents, ":agent_no_offerer"),
      ##     (agent_add_relation_with_agent, ":agent_no", ":agent_no_offerer", -1),
      ##     (agent_add_relation_with_agent, ":agent_no_offerer", ":agent_no", -1),
      (agent_get_player_id, ":player_no", ":agent_no"),
      (store_mission_timer_a, ":mission_timer"),
      (try_begin),
        (player_is_active, ":player_no"), #might be AI
        (multiplayer_send_int_to_player, ":player_no", multiplayer_event_start_duel, ":agent_no_offerer"),
      (else_try),
        (agent_force_rethink, ":agent_no"),
      (try_end),
      (agent_set_slot, ":agent_no", slot_agent_duel_start_time, ":mission_timer"),
      (agent_get_player_id, ":agent_no_offerer_player", ":agent_no_offerer"),
      (try_begin),
        (player_is_active, ":agent_no_offerer_player"), #might be AI
        (multiplayer_send_int_to_player, ":agent_no_offerer_player", multiplayer_event_start_duel, ":agent_no"),
      (else_try),
        (agent_force_rethink, ":agent_no_offerer"),
      (try_end),
      (agent_set_slot, ":agent_no_offerer", slot_agent_duel_start_time, ":mission_timer"),
  ]),
  
  # script_game_get_multiplayer_server_option_for_mission_template
  # Input: arg1 = mission_template_id, arg2 = option_index
  # Output: trigger_result = 1 for option available, 0 for not available
  # reg0 = option_value
  ("game_get_multiplayer_server_option_for_mission_template",
    [
      (store_script_param, ":mission_template_id", 1),
      (store_script_param, ":option_index", 2),
      (try_begin),
        (eq, ":option_index", 0),
        (assign, reg0, "$g_multiplayer_team_1_faction"),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 1),
        (assign, reg0, "$g_multiplayer_team_2_faction"),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 2),
        (assign, reg0, "$g_multiplayer_num_bots_team_1"),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 3),
        (assign, reg0, "$g_multiplayer_num_bots_team_2"),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 4),
        (server_get_friendly_fire, reg0),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 5),
        (server_get_melee_friendly_fire, reg0),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 6),
        (server_get_friendly_fire_damage_self_ratio, reg0),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 7),
        (server_get_friendly_fire_damage_friend_ratio, reg0),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 8),
        (server_get_ghost_mode, reg0),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 9),
        (server_get_control_block_dir, reg0),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 10),
        (server_get_combat_speed, reg0),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 11),
        (assign, reg0, "$g_multiplayer_game_max_minutes"),
        (set_trigger_result, 1),
      (else_try),
        (try_begin),
          (neq, ":mission_template_id", "mt_multiplayer_bt"),
          (neq, ":mission_template_id", "mt_multiplayer_sea"), #naval battles
          (neq, ":mission_template_id", "mt_multiplayer_lbt"), #chief capitan
          (neq, ":mission_template_id", "mt_multiplayer_sg"),
          (val_add, ":option_index", 1), #max round time
        (try_end),
        (eq, ":option_index", 12),
        (assign, reg0, "$g_multiplayer_round_max_seconds"),
        (set_trigger_result, 1),
      (else_try),
        (try_begin),
          (neq, ":mission_template_id", "mt_multiplayer_bt"),
          (neq, ":mission_template_id", "mt_multiplayer_sea"), #naval battles
          (neq, ":mission_template_id", "mt_multiplayer_lbt"), #chief capitan
          (val_add, ":option_index", 1), #respawn as bot
        (try_end),
        (eq, ":option_index", 13),
        (assign, reg0, "$g_multiplayer_player_respawn_as_bot"),
        (set_trigger_result, 1),
      (else_try),
        (try_begin),
          (neq, ":mission_template_id", "mt_multiplayer_sg"),
          (val_add, ":option_index", 1), #respawn limit
        (try_end),
        (eq, ":option_index", 14),
        (assign, reg0, "$g_multiplayer_number_of_respawn_count"),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 15),
        (assign, reg0, "$g_multiplayer_game_max_points"),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 16),
        (assign, reg0, "$g_multiplayer_point_gained_from_flags"),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 17),
        (assign, reg0, "$g_multiplayer_point_gained_from_capturing_flag"),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 18),
        (assign, reg0, "$g_multiplayer_respawn_period"),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 19),
        (assign, reg0, "$g_multiplayer_initial_gold_multiplier"),
        (set_trigger_result, 1),
      (else_try),
        (eq, ":option_index", 20),
        (assign, reg0, "$g_multiplayer_battle_earnings_multiplier"),
        (set_trigger_result, 1),
      (else_try),
        (try_begin),
          (neq, ":mission_template_id", "mt_multiplayer_bt"),
          (neq, ":mission_template_id", "mt_multiplayer_sea"), #naval battles
          (neq, ":mission_template_id", "mt_multiplayer_lbt"), #chief capitan
          (neq, ":mission_template_id", "mt_multiplayer_sg"),
          (val_add, ":option_index", 1),
        (try_end),
        (eq, ":option_index", 21),
        (assign, reg0, "$g_multiplayer_round_earnings_multiplier"),
        (set_trigger_result, 1),
      (try_end),
  ]),
  
  # script_game_multiplayer_server_option_for_mission_template_to_string
  # Input: arg1 = mission_template_id, arg2 = option_index, arg3 = option_value
  # Output: s0 = option_text
  ("game_multiplayer_server_option_for_mission_template_to_string",
    [
      (store_script_param, ":mission_template_id", 1),
      (store_script_param, ":option_index", 2),
      (store_script_param, ":option_value", 3),
      (str_clear, s0),
      (try_begin),
        (eq, ":option_index", 0),
        (assign, reg1, 1),
        (str_store_string, s0, "str_team_reg1_faction"),
        (str_store_faction_name, s1, ":option_value"),
        (str_store_string, s0, "str_s0_s1"),
      (else_try),
        (eq, ":option_index", 1),
        (assign, reg1, 2),
        (str_store_string, s0, "str_team_reg1_faction"),
        (str_store_faction_name, s1, ":option_value"),
        (str_store_string, s0, "str_s0_s1"),
      (else_try),
        (eq, ":option_index", 2),
        (assign, reg1, 1),
        (str_store_string, s0, "str_number_of_bots_in_team_reg1"),
        (assign, reg0, ":option_value"),
        (str_store_string, s0, "str_s0_reg0"),
      (else_try),
        (eq, ":option_index", 3),
        (assign, reg1, 2),
        (str_store_string, s0, "str_number_of_bots_in_team_reg1"),
        (assign, reg0, ":option_value"),
        (str_store_string, s0, "str_s0_reg0"),
      (else_try),
        (eq, ":option_index", 4),
        (str_store_string, s0, "str_allow_friendly_fire"),
        (try_begin),
          (eq, ":option_value", 0),
          (str_store_string, s1, "str_no_wo_dot"),
        (else_try),
          (str_store_string, s1, "str_yes_wo_dot"),
        (try_end),
        (str_store_string, s0, "str_s0_s1"),
      (else_try),
        (eq, ":option_index", 5),
        (str_store_string, s0, "str_allow_melee_friendly_fire"),
        (try_begin),
          (eq, ":option_value", 0),
          (str_store_string, s1, "str_no_wo_dot"),
        (else_try),
          (str_store_string, s1, "str_yes_wo_dot"),
        (try_end),
        (str_store_string, s0, "str_s0_s1"),
      (else_try),
        (eq, ":option_index", 6),
        (str_store_string, s0, "str_friendly_fire_damage_self_ratio"),
        (assign, reg0, ":option_value"),
        (str_store_string, s0, "str_s0_reg0"),
      (else_try),
        (eq, ":option_index", 7),
        (str_store_string, s0, "str_friendly_fire_damage_friend_ratio"),
        (assign, reg0, ":option_value"),
        (str_store_string, s0, "str_s0_reg0"),
      (else_try),
        (eq, ":option_index", 8),
        (str_store_string, s0, "str_spectator_camera"),
        (try_begin),
          (eq, ":option_value", 0),
          (str_store_string, s1, "str_free"),
        (else_try),
          (eq, ":option_value", 1),
          (str_store_string, s1, "str_stick_to_any_player"),
        (else_try),
          (eq, ":option_value", 2),
          (str_store_string, s1, "str_stick_to_team_members"),
        (else_try),
          (str_store_string, s1, "str_stick_to_team_members_view"),
        (try_end),
        (str_store_string, s0, "str_s0_s1"),
      (else_try),
        (eq, ":option_index", 9),
        (str_store_string, s0, "str_control_block_direction"),
        (try_begin),
          (eq, ":option_value", 0),
          (str_store_string, s1, "str_automatic"),
        (else_try),
          (str_store_string, s1, "str_by_mouse_movement"),
        (try_end),
        (str_store_string, s0, "str_s0_s1"),
      (else_try),
        (eq, ":option_index", 10),
        (str_store_string, s0, "str_combat_speed"),
        (try_begin),
          (eq, ":option_value", 0),
          (str_store_string, s1, "str_combat_speed_0"),
        (else_try),
          (eq, ":option_value", 1),
          (str_store_string, s1, "str_combat_speed_1"),
        (else_try),
          (eq, ":option_value", 2),
          (str_store_string, s1, "str_combat_speed_2"),
        (else_try),
          (eq, ":option_value", 3),
          (str_store_string, s1, "str_combat_speed_3"),
        (else_try),
          (str_store_string, s1, "str_combat_speed_4"),
        (try_end),
        (str_store_string, s0, "str_s0_s1"),
      (else_try),
        (eq, ":option_index", 11),
        (str_store_string, s0, "str_map_time_limit"),
        (assign, reg0, ":option_value"),
        (str_store_string, s0, "str_s0_reg0"),
      (else_try),
        (try_begin),
          (neq, ":mission_template_id", "mt_multiplayer_bt"),
          (neq, ":mission_template_id", "mt_multiplayer_sea"), #naval battles
          (neq, ":mission_template_id", "mt_multiplayer_lbt"), #chief capitan
          (neq, ":mission_template_id", "mt_multiplayer_sg"),
          (val_add, ":option_index", 1), #max round time
        (try_end),
        (eq, ":option_index", 12),
        (str_store_string, s0, "str_round_time_limit"),
        (assign, reg0, ":option_value"),
        (str_store_string, s0, "str_s0_reg0"),
      (else_try),
        (try_begin),
          (neq, ":mission_template_id", "mt_multiplayer_bt"),
          (neq, ":mission_template_id", "mt_multiplayer_sea"), #naval battles
          (neq, ":mission_template_id", "mt_multiplayer_lbt"), #chief capitan
          (val_add, ":option_index", 1), #respawn as bot
        (try_end),
        (eq, ":option_index", 13),
        (str_store_string, s0, "str_players_take_control_of_a_bot_after_death"),
        (try_begin),
          (eq, ":option_value", 0),
          (str_store_string, s1, "str_no_wo_dot"),
        (else_try),
          (str_store_string, s1, "str_yes_wo_dot"),
        (try_end),
        (str_store_string, s0, "str_s0_s1"),
      (else_try),
        (try_begin),
          (neq, ":mission_template_id", "mt_multiplayer_sg"),
          (val_add, ":option_index", 1), #respawn limit
        (try_end),
        (eq, ":option_index", 14),
        (str_store_string, s0, "str_defender_spawn_count_limit"),
        (try_begin),
          (eq, ":option_value", 0),
          (str_store_string, s1, "str_unlimited"),
        (else_try),
          (assign, reg1, ":option_value"),
          (str_store_string, s1, "str_reg1"),
        (try_end),
        (str_store_string, s0, "str_s0_s1"),
      (else_try),
        (eq, ":option_index", 15),
        (str_store_string, s0, "str_team_points_limit"),
        (assign, reg0, ":option_value"),
        (str_store_string, s0, "str_s0_reg0"),
      (else_try),
        (eq, ":option_index", 16),
        (str_store_string, s0, "str_point_gained_from_flags"),
        (assign, reg0, ":option_value"),
        (str_store_string, s0, "str_s0_reg0"),
      (else_try),
        (eq, ":option_index", 17),
        (str_store_string, s0, "str_point_gained_from_capturing_flag"),
        (assign, reg0, ":option_value"),
        (str_store_string, s0, "str_s0_reg0"),
      (else_try),
        (eq, ":option_index", 18),
        (str_store_string, s0, "str_respawn_period"),
        (assign, reg0, ":option_value"),
        (str_store_string, s0, "str_s0_reg0"),
      (else_try),
        (eq, ":option_index", 19),
        (str_store_string, s0, "str_initial_gold_multiplier"),
        (assign, reg0, ":option_value"),
        (str_store_string, s0, "str_s0_reg0"),
      (else_try),
        (eq, ":option_index", 20),
        (str_store_string, s0, "str_battle_earnings_multiplier"),
        (assign, reg0, ":option_value"),
        (str_store_string, s0, "str_s0_reg0"),
      (else_try),
        (try_begin),
          (neq, ":mission_template_id", "mt_multiplayer_bt"),
          (neq, ":mission_template_id", "mt_multiplayer_sea"), #naval battles
          (neq, ":mission_template_id", "mt_multiplayer_lbt"), #chief capitan
          (neq, ":mission_template_id", "mt_multiplayer_sg"),
          (val_add, ":option_index", 1),
        (try_end),
        (eq, ":option_index", 21),
        (str_store_string, s0, "str_round_earnings_multiplier"),
        (assign, reg0, ":option_value"),
        (str_store_string, s0, "str_s0_reg0"),
      (try_end),
  ]),
  
  # script_cf_multiplayer_team_is_available
  # Input: arg1 = player_no, arg2 = team_no
  # Output: none, true or false
  ("cf_multiplayer_team_is_available",
    [
      (store_script_param, ":player_no", 1),
      (store_script_param, ":team_no", 2),
      (assign, ":continue_change_team", 1),
      (try_begin),
        (eq, "$g_multiplayer_game_type", multiplayer_game_type_invasion),
        (try_begin),
          (eq,":team_no",1),
          (neg|player_is_admin,":player_no"),
          (assign, ":continue_change_team",0),
        (try_end),
      (else_try),
        (neq, "$g_multiplayer_game_type", multiplayer_game_type_thor),
        (neq, "$g_multiplayer_game_type", multiplayer_game_type_deathmatch),
        (neq, "$g_multiplayer_game_type", multiplayer_game_type_duel),
        (is_between, ":team_no", 0, multi_team_spectator),
        (neg|teams_are_enemies, ":team_no", ":team_no"), #checking if it is a deathmatch or not
        (assign, ":continue_change_team", 0),
        #counting number of players for team balance checks
        (assign, ":number_of_players_at_team_1", 0),
        (assign, ":number_of_players_at_team_2", 0),
        (get_max_players, ":num_players"),
        (try_for_range, ":cur_player", 0, ":num_players"),
          (player_is_active, ":cur_player"),
          (neq, ":cur_player", ":player_no"),
          (player_get_team_no, ":player_team", ":cur_player"),
          (try_begin),
            (eq, ":player_team", 0),
            (val_add, ":number_of_players_at_team_1", 1),
          (else_try),
            (eq, ":player_team", 1),
            (val_add, ":number_of_players_at_team_2", 1),
          (try_end),
        (try_end),
        
        (try_begin),
          (eq, "$g_multiplayer_game_type", multiplayer_game_type_raid),# 1-3 ratio
          (val_mul,":number_of_players_at_team_2",3),
        (try_end),
        
        (store_sub, ":difference_of_number_of_players", ":number_of_players_at_team_1", ":number_of_players_at_team_2"),
        
        (try_begin),
          (eq, "$g_multiplayer_game_type", multiplayer_game_type_raid),# 1-3 ratio
          (try_begin),
            (eq, ":team_no", 0),
            (le, ":difference_of_number_of_players", 0),
            (assign, ":continue_change_team", 1),
          (else_try),
            (eq, ":team_no", 1),
            (this_or_next|eq,":number_of_players_at_team_2",0),
            (ge, ":difference_of_number_of_players", 0),
            (assign, ":continue_change_team", 1),
          (try_end),
        (else_try),
          (try_begin),
            (ge, ":difference_of_number_of_players", 0),
            (val_add, ":difference_of_number_of_players", 1),
          (else_try),
            (val_add, ":difference_of_number_of_players", -1),
          (try_end),
          (try_begin),
            (eq, ":team_no", 0),
            (lt, ":difference_of_number_of_players", "$g_multiplayer_auto_team_balance_limit"),
            (assign, ":continue_change_team", 1),
          (else_try),
            (eq, ":team_no", 1),
            (store_mul, ":checked_value", "$g_multiplayer_auto_team_balance_limit", -1),
            (gt, ":difference_of_number_of_players", ":checked_value"),
            (assign, ":continue_change_team", 1),
          (try_end),
        (try_end),
      (try_end),
      (eq, ":continue_change_team", 1),
  ]),
  
  # script_find_number_of_agents_constant
  # Input: none
  # Output: reg0 = 100xconstant (100..500)
  # ("find_number_of_agents_constant",
    # [
      # (assign, ":num_dead_or_alive_agents", 0),
      
      # (try_for_agents, ":cur_agent"),
        # (agent_is_human, ":cur_agent"),
        # (val_add, ":num_dead_or_alive_agents", 1),
      # (try_end),
      
      # (try_begin),
        # (le, ":num_dead_or_alive_agents", 2), #2
        # (assign, reg0, 100),
      # (else_try),
        # (le, ":num_dead_or_alive_agents", 4), #2+2
        # (assign, reg0, 140),
      # (else_try),
        # (le, ":num_dead_or_alive_agents", 7), #2+2+3
        # (assign, reg0, 180),
      # (else_try),
        # (le, ":num_dead_or_alive_agents", 11), #2+2+3+4
        # (assign, reg0, 220),
      # (else_try),
        # (le, ":num_dead_or_alive_agents", 17), #2+2+3+4+6
        # (assign, reg0, 260),
      # (else_try),
        # (le, ":num_dead_or_alive_agents", 25), #2+2+3+4+6+8
        # (assign, reg0, 300),
      # (else_try),
        # (le, ":num_dead_or_alive_agents", 36), #2+2+3+4+6+8+11
        # (assign, reg0, 340),
      # (else_try),
        # (le, ":num_dead_or_alive_agents", 50), #2+2+3+4+6+8+11+14
        # (assign, reg0, 380),
      # (else_try),
        # (le, ":num_dead_or_alive_agents", 68), #2+2+3+4+6+8+11+14+18
        # (assign, reg0, 420),
      # (else_try),
        # (le, ":num_dead_or_alive_agents", 91), #2+2+3+4+6+8+11+14+18+23
        # (assign, reg0, 460),
      # (else_try),
        # (assign, reg0, 500),
      # (try_end),
  # ]),
  
  # script_game_multiplayer_event_duel_offered
  # Input: arg1 = agent_no
  # Output: none
  ("game_multiplayer_event_duel_offered",
    [
      (store_script_param, ":agent_no", 1),
      (get_player_agent_no, ":player_agent_no"),
      (try_begin),
        (agent_is_active, ":player_agent_no"),
        (this_or_next|agent_slot_eq, ":player_agent_no", slot_agent_in_duel_with, -1),
        (agent_check_offer_from_agent, ":player_agent_no", ":agent_no"),
        (neg|agent_slot_eq, ":player_agent_no", slot_agent_in_duel_with, ":agent_no"), #don't allow spamming duel offers during countdown
        (multiplayer_send_int_to_server, multiplayer_event_offer_duel, ":agent_no"),
        (agent_get_player_id, ":player_no", ":agent_no"),
        (try_begin),
          (player_is_active, ":player_no"),
          (str_store_player_username, s0, ":player_no"),
        (else_try),
          (str_store_agent_name, s0, ":agent_no"),
        (try_end),
        (display_message, "str_a_duel_request_is_sent_to_s0"),
      (try_end),
  ]),
  
  # script_game_get_multiplayer_game_type_enum
  # Input: none
  # Output: reg0:first type, reg1:type count
  ("game_get_multiplayer_game_type_enum",
    [
      (assign, reg0, multiplayer_game_type_deathmatch),
      (assign, reg1, multiplayer_num_game_types),
  ]),
  
  # script_game_multiplayer_get_game_type_mission_template
  # Input: arg1 = game_type
  # Output: mission_template
  ("game_multiplayer_get_game_type_mission_template",
    [
      (assign, ":selected_mt", -1),
      (store_script_param, ":game_type", 1),
      (try_begin),
        (eq, ":game_type", multiplayer_game_type_deathmatch),
        (assign, ":selected_mt", "mt_multiplayer_dm"),
      (else_try),
        (eq, ":game_type", multiplayer_game_type_team_deathmatch),
        (assign, ":selected_mt", "mt_multiplayer_tdm"),
      (else_try),
        (eq, ":game_type", multiplayer_game_type_battle),
        (assign, ":selected_mt", "mt_multiplayer_bt"),
      (else_try),
        (eq, ":game_type", multiplayer_game_type_siege),
        (assign, ":selected_mt", "mt_multiplayer_sg"),
      (else_try),
        (eq, ":game_type", multiplayer_game_type_duel),
        (assign, ":selected_mt", "mt_multiplayer_duel"),
      (else_try),
        (eq, ":game_type", multiplayer_game_type_lords_battle), #chief capitan
        (assign, ":selected_mt", "mt_multiplayer_lbt"),
      (else_try),
        (eq, ":game_type", multiplayer_game_type_sea), #naval battles
        (assign, ":selected_mt", "mt_multiplayer_sea"),
      (else_try),
        (eq, ":game_type", multiplayer_game_type_thor), #thor's champion
        (assign, ":selected_mt", "mt_multiplayer_thor"),
      (else_try),
        (eq, ":game_type", multiplayer_game_type_invasion), #invasion
        (assign, ":selected_mt", "mt_multiplayer_inv"),
      (else_try),
        (eq, ":game_type", multiplayer_game_type_raid), #raid
        (assign, ":selected_mt", "mt_multiplayer_raid"),
      (try_end),
      (assign, reg0, ":selected_mt"),
  ]),
  
  # script_multiplayer_get_mission_template_game_type
  # Input: arg1 = mission_template_no
  # Output: game_type
  ("multiplayer_get_mission_template_game_type",
    [
      (store_script_param, ":mission_template_no", 1),
      (assign, ":game_type", -1),
      (try_begin),
        (eq, ":mission_template_no", "mt_multiplayer_dm"),
        (assign, ":game_type", multiplayer_game_type_deathmatch),
      (else_try),
        (eq, ":mission_template_no", "mt_multiplayer_tdm"),
        (assign, ":game_type", multiplayer_game_type_team_deathmatch),
      (else_try),
        (eq, ":mission_template_no", "mt_multiplayer_bt"),
        (assign, ":game_type", multiplayer_game_type_battle),
      (else_try),
        (eq, ":mission_template_no", "mt_multiplayer_sg"),
        (assign, ":game_type", multiplayer_game_type_siege),
      (else_try),
        (eq, ":mission_template_no", "mt_multiplayer_duel"),
        (assign, ":game_type", multiplayer_game_type_duel),
      (else_try),
        (eq, ":mission_template_no", "mt_multiplayer_lbt"),
        (assign, ":game_type", multiplayer_game_type_lords_battle),#chief capitan
      (else_try),
        (eq, ":mission_template_no", "mt_multiplayer_sea"),
        (assign, ":game_type", multiplayer_game_type_sea),#naval battles
      (else_try),
        (eq, ":mission_template_no", "mt_multiplayer_thor"),
        (assign, ":game_type", multiplayer_game_type_thor),#thor's champion
      (else_try),
        (eq, ":mission_template_no", "mt_multiplayer_inv"),
        (assign, ":game_type", multiplayer_game_type_invasion),
      (else_try),
        (eq, ":mission_template_no", "mt_multiplayer_raid"),
        (assign, ":game_type", multiplayer_game_type_raid),
      (try_end),
      (assign, reg0, ":game_type"),
  ]),
  
  # script_multiplayer_fill_available_factions_combo_button
  # Input: arg1 = overlay_id, arg2 = selected_faction_no, arg3 = opposite_team_selected_faction_no
  # Output: none
  ("multiplayer_fill_available_factions_combo_button",
    [
      (store_script_param, ":overlay_id", 1),
      (store_script_param, ":selected_faction_no", 2),
      ##     (store_script_param, ":opposite_team_selected_faction_no", 3),
      ##     (try_for_range, ":cur_faction", "fac_kingdom_1", "fac_kingdoms_end"),
      ##       (try_begin),
      ##         (eq, ":opposite_team_selected_faction_no", ":cur_faction"),
      ##         (try_begin),
      ##           (gt, ":selected_faction_no", ":opposite_team_selected_faction_no"),
      ##           (val_sub, ":selected_faction_no", 1),
      ##         (try_end),
      ##       (else_try),
      ##         (str_store_faction_name, s0, ":cur_faction"),
      ##         (overlay_add_item, ":overlay_id", s0),
      ##       (try_end),
      ##     (try_end),
      ##     (val_sub, ":selected_faction_no", "fac_kingdom_1"),
      ##     (overlay_set_val, ":overlay_id", ":selected_faction_no"),
      (try_for_range, ":cur_faction", mp_factions_begin, mp_factions_end),
        (str_store_faction_name, s0, ":cur_faction"),
        (overlay_add_item, ":overlay_id", s0),
      (try_end),
      (val_sub, ":selected_faction_no", mp_factions_begin),
      (overlay_set_val, ":overlay_id", ":selected_faction_no"),
  ]),
  
  
  # script_multiplayer_get_troop_class
  # Input: arg1 = troop_no
  # Output: reg0: troop_class
  ("multiplayer_get_troop_class",
    [
      # (store_script_param_1, ":troop_no"),
      (assign, ":troop_class", multi_troop_class_other),
      # (try_begin),
      # (this_or_next|eq, ":troop_no", "trp_saxon_level1_companion_multiplayer"),
      # (this_or_next|eq, ":troop_no", "trp_angle_level3_landed_multiplayer"),
      # (eq, ":troop_no", "trp_norse_level1_companion_multiplayer"),
      # (assign, ":troop_class", multi_troop_class_archer),
      # (else_try),
      # (this_or_next|eq, ":troop_no", "trp_briton_horseman_multiplayer"),
      # (this_or_next|eq, ":troop_no", "trp_nord_scout_multiplayer"),
      # (this_or_next|eq, ":troop_no", "trp_rhodok_horseman_multiplayer"),
      # (this_or_next|eq, ":troop_no", "trp_norse_level3_landed_multiplayer"),
      # (eq, ":troop_no", "trp_saxon_horseman_multiplayer"),
      # (assign, ":troop_class", multi_troop_class_cavalry),
      # (else_try),
      # (eq, ":troop_no", "trp_scotch_level1_landed_multiplayer"),
      # (assign, ":troop_class", multi_troop_class_mounted_archer),
      # #     (else_try),
      # #       (eq, ":troop_no", "trp_swadian_mounted_crossbowman_multiplayer"),
      # #       (assign, ":troop_class", multi_troop_class_mounted_crossbowman),
      # (else_try),
      # (this_or_next|eq, ":troop_no", "trp_briton_level2_landed_multiplayer"),
      # (eq, ":troop_no", "trp_irish_horseman_multiplayer"),
      # (assign, ":troop_class", multi_troop_class_crossbowman),
      # (else_try),
      # (this_or_next|eq, ":troop_no", "trp_briton_level0_companion_multiplayer"),
      # (this_or_next|eq, ":troop_no", "trp_norse_level0_landed_multiplayer"),
      # (eq, ":troop_no", "trp_angle_level2_landed_multiplayer"),
      # (assign, ":troop_class", multi_troop_class_infantry),
      # (else_try),
      # (eq, ":troop_no", "trp_vaegir_spearman_multiplayer"),
      # (assign, ":troop_class", multi_troop_class_spearman),
      # (try_end),
      (assign, reg0, ":troop_class"),
  ]),
  
  #script_multiplayer_clear_player_selected_items
  # Input: arg1 = player_no
  # Output: none
  ("multiplayer_clear_player_selected_items",
    [
      (store_script_param, ":player_no", 1),
      (try_for_range, ":slot_no", slot_player_selected_item_indices_begin, slot_player_selected_item_indices_end),
        (player_set_slot, ":player_no", ":slot_no", -1),
      (try_end),
  ]),
  
  #script_multiplayer_init_player_slots
  # Input: arg1 = player_no
  # Output: none
  ("multiplayer_init_player_slots",
    [
      (store_script_param, ":player_no", 1),
      (call_script, "script_multiplayer_clear_player_selected_items", ":player_no"),
      (player_set_slot, ":player_no", slot_player_spawned_this_round, 0),
      (player_set_slot, ":player_no", slot_player_last_rounds_used_item_earnings, 0),
      (player_set_slot, ":player_no", slot_player_poll_disabled_until_time, 0),
      
      (try_for_range,":slot", slot_player_bot_type_1_wanted, slot_player_bot_type_15_wanted+1),
        (player_set_slot, ":player_no", ":slot", 0),
      (try_end),
  ]),
  
  #script_multiplayer_initialize_belfry_wheel_rotations
  # Input: none
  # Output: none
  ("multiplayer_initialize_belfry_wheel_rotations",
    [
      ##    (scene_prop_get_num_instances, ":num_belfries", "spr_belfry_a"),
      ##    (try_for_range, ":belfry_no", 0, ":num_belfries"),
      ##      (store_mul, ":wheel_no", ":belfry_no", 3),
      ##      (scene_prop_get_instance, ":belfry_wheel_1_scene_prop_id", "spr_belfry_wheel", ":wheel_no"),
      ##      (prop_instance_initialize_rotation_angles, ":belfry_wheel_1_scene_prop_id"),
      ##      #belfry wheel_2
      ##      (val_add, ":wheel_no", 1),
      ##      (scene_prop_get_instance, ":belfry_wheel_2_scene_prop_id", "spr_belfry_wheel", ":wheel_no"),
      ##      (prop_instance_initialize_rotation_angles, ":belfry_wheel_2_scene_prop_id"),
      ##      #belfry wheel_3
      ##      (val_add, ":wheel_no", 1),
      ##      (scene_prop_get_instance, ":belfry_wheel_3_scene_prop_id", "spr_belfry_wheel", ":wheel_no"),
      ##      (prop_instance_initialize_rotation_angles, ":belfry_wheel_3_scene_prop_id"),
      ##    (try_end),
      ##
      ##    (scene_prop_get_num_instances, ":num_belfries_a", "spr_belfry_a"),
      ##
      ##    (scene_prop_get_num_instances, ":num_belfries", "spr_belfry_b"),
      ##    (try_for_range, ":belfry_no", 0, ":num_belfries"),
      ##      (store_add, ":wheel_no_plus_num_belfries_a", ":wheel_no", ":num_belfries_a"),
      ##      (store_mul, ":wheel_no_plus_num_belfries_a", ":belfry_no", 3),
      ##      (scene_prop_get_instance, ":belfry_wheel_1_scene_prop_id", "spr_belfry_wheel", ":wheel_no_plus_num_belfries_a"),
      ##      (prop_instance_initialize_rotation_angles, ":belfry_wheel_1_scene_prop_id"),
      ##      #belfry wheel_2
      ##      (val_add, ":wheel_no_plus_num_belfries_a", 1),
      ##      (scene_prop_get_instance, ":belfry_wheel_2_scene_prop_id", "spr_belfry_wheel", ":wheel_no_plus_num_belfries_a"),
      ##      (prop_instance_initialize_rotation_angles, ":belfry_wheel_2_scene_prop_id"),
      ##      #belfry wheel_3
      ##      (val_add, ":wheel_no_plus_num_belfries_a", 1),
      ##      (scene_prop_get_instance, ":belfry_wheel_3_scene_prop_id", "spr_belfry_wheel", ":wheel_no_plus_num_belfries_a"),
      ##      (prop_instance_initialize_rotation_angles, ":belfry_wheel_3_scene_prop_id"),
      ##    (try_end),
      
      (scene_prop_get_num_instances, ":num_wheel", "spr_belfry_wheel"),
      (try_for_range, ":wheel_no", 0, ":num_wheel"),
        (scene_prop_get_instance, ":wheel_id", "spr_belfry_wheel", ":wheel_no"),
        (prop_instance_initialize_rotation_angles, ":wheel_id"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_winch", "spr_winch"),
      (try_for_range, ":winch_no", 0, ":num_winch"),
        (scene_prop_get_instance, ":winch_id", "spr_winch", ":winch_no"),
        (prop_instance_initialize_rotation_angles, ":winch_id"),
      (try_end),
      
      (scene_prop_get_num_instances, ":num_winch_b", "spr_winch_b"),
      (try_for_range, ":winch_b_no", 0, ":num_winch_b"),
        (scene_prop_get_instance, ":winch_b_id", "spr_winch_b", ":winch_b_no"),
        (prop_instance_initialize_rotation_angles, ":winch_b_id"),
      (try_end),
  ]),
  
  #script_send_open_close_information_of_object
  # Input: arg1 = mission_object_id
  # Output: none
  ("send_open_close_information_of_object",
    [
      (store_script_param, ":player_no", 1),
      (store_script_param, ":scene_prop_no", 2),
      
      (scene_prop_get_num_instances, ":num_instances", ":scene_prop_no"),
      
      (try_for_range, ":instance_no", 0, ":num_instances"),
        (scene_prop_get_instance, ":instance_id", ":scene_prop_no", ":instance_no"),
        (scene_prop_get_slot, ":opened_or_closed", ":instance_id", scene_prop_open_or_close_slot),
        (try_begin),
          (eq, ":opened_or_closed", 1),
          (multiplayer_send_int_to_player, ":player_no", multiplayer_event_set_scene_prop_open_or_close, ":instance_id"),
        (try_end),
      (try_end),
  ]),
  
  #script_multiplayer_send_initial_information
  # Input: arg1 = player_no
  # Output: none
  ("multiplayer_send_initial_information",
    [
      (store_script_param, ":player_no", 1),
      
      (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return_num_bots_in_team, 1, "$g_multiplayer_num_bots_team_1"),
      (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return_num_bots_in_team, 2, "$g_multiplayer_num_bots_team_2"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_auto_team_balance_limit, "$g_multiplayer_auto_team_balance_limit"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_num_bots_voteable, "$g_multiplayer_num_bots_voteable"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_factions_voteable, "$g_multiplayer_factions_voteable"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_maps_voteable, "$g_multiplayer_maps_voteable"),
      (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return, me_return_gametype_voteable, "$g_multiplayer_gametype_voteable"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_kick_voteable, "$g_multiplayer_kick_voteable"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_ban_voteable, "$g_multiplayer_ban_voteable"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_allow_player_banners, "$g_multiplayer_allow_player_banners"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_force_default_armor, "$g_multiplayer_force_default_armor"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_disallow_ranged_weapons, "$g_multiplayer_disallow_ranged_weapons"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_respawn_period, "$g_multiplayer_respawn_period"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_round_max_seconds, "$g_multiplayer_round_max_seconds"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_game_type, "$g_multiplayer_game_type"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_player_respawn_as_bot, "$g_multiplayer_player_respawn_as_bot"),
      (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return, me_return_deadly_bow, "$g_multiplayer_deadly_bow"),
      (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return, me_return_allow_shield_bash, "$g_multiplayer_allow_shield_bash"),
      #(multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_return, me_return_troop_classes, "$g_multiplayer_troop_classes"),
      
      (store_mission_timer_a, ":mission_timer"),
      (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_server_mission_timer_while_player_joined, ":mission_timer"),
      
      (try_begin),
        (eq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
        (multiplayer_send_int_to_player, ":player_no", multiplayer_event_return_respawn_count, "$g_multiplayer_number_of_respawn_count"),
      (try_end),
      
      (try_for_agents, ":cur_agent"), #send if any agent is carrying any scene object
        (agent_is_human, ":cur_agent"),
        (agent_get_attached_scene_prop, ":attached_scene_prop", ":cur_agent"),
        (ge, ":attached_scene_prop", 0),
        (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_set_attached_scene_prop, ":cur_agent", ":attached_scene_prop"),
      (try_end),
      
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_siege_ladder_move_6m"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_siege_ladder_move_8m"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_siege_ladder_move_10m"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_siege_ladder_move_12m"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_siege_ladder_move_14m"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_winch_b"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_castle_e_sally_door_a"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_castle_f_sally_door_a"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_earth_sally_gate_left"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_earth_sally_gate_right"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_viking_keep_destroy_sally_door_left"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_viking_keep_destroy_sally_door_right"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_castle_f_door_a"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_door_destructible"),
      (call_script, "script_send_open_close_information_of_object", ":player_no", "spr_castle_f_door_b"),
      
      (try_begin),
        (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_battle),
        (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_invasion),
        (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_raid),
        (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_sea),#naval battles
        (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle), #capitan chief
        (eq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
        
        (store_mission_timer_a, ":current_time"),
        (val_sub, ":current_time", "$g_round_start_time"),
        (val_mul, ":current_time", -1),
        
        (multiplayer_send_int_to_player, ":player_no", multiplayer_event_set_round_start_time, ":current_time"),
      (else_try),
        (eq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
        #if game type is headquarters send number of agents placed around each pole's around to player.
        (try_for_range, ":flag_no", 0, "$g_number_of_flags"),
          (assign, ":number_of_agents_around_flag_team_1", 0),
          (assign, ":number_of_agents_around_flag_team_2", 0),
          
          (scene_prop_get_instance, ":pole_id", "spr_headquarters_pole_code_only", ":flag_no"),
          (prop_instance_get_position, pos0, ":pole_id"), #pos0 holds pole position.
          
          (try_for_agents, ":cur_agent"),
            (agent_is_human, ":cur_agent"),
            (agent_is_alive, ":cur_agent"),
            (neg|agent_is_non_player, ":cur_agent"),
            (agent_get_team, ":cur_agent_team", ":cur_agent"),
            (agent_get_position, pos1, ":cur_agent"), #pos1 holds agent's position.
            (get_sq_distance_between_positions, ":squared_dist", pos0, pos1),
            (get_sq_distance_between_position_heights, ":squared_height_dist", pos0, pos1),
            (val_add, ":squared_dist", ":squared_height_dist"),
            (lt, ":squared_dist", multi_headquarters_max_distance_sq_to_raise_flags),
            (try_begin),
              (eq, ":cur_agent_team", 0),
              (val_add, ":number_of_agents_around_flag_team_1", 1),
            (else_try),
              (eq, ":cur_agent_team", 1),
              (val_add, ":number_of_agents_around_flag_team_2", 1),
            (try_end),
          (try_end),
          
          (store_mul, ":current_owner_code", ":number_of_agents_around_flag_team_1", 100),
          (val_add, ":current_owner_code", ":number_of_agents_around_flag_team_2"),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_set_num_agents_around_flag, ":flag_no", ":current_owner_code"),
        (try_end),
        
        #if game type is headquarters send owners of each pole to player.
        (assign, "$g_placing_initial_flags", 1),
        (try_for_range, ":cur_flag", 0, "$g_number_of_flags"),
          (store_add, ":cur_flag_slot", multi_data_flag_owner_begin, ":cur_flag"),
          (troop_get_slot, ":cur_flag_owner", "trp_multiplayer_data", ":cur_flag_slot"),
          (store_mul, ":cur_flag_owner_code", ":cur_flag_owner", 100),
          (val_add, ":cur_flag_owner_code", ":cur_flag_owner"),
          (val_add, ":cur_flag_owner_code", 1),
          (val_mul, ":cur_flag_owner_code", -1),
          (multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_change_flag_owner, ":cur_flag", ":cur_flag_owner_code"),
        (try_end),
        (assign, "$g_placing_initial_flags", 0),
      (try_end),
      
      #(multiplayer_send_2_int_to_player, ":player_no", multiplayer_event_set_day_time, "$g_round_day_time"),
  ]),
  
  #script_multiplayer_remove_headquarters_flags
  # Input: none
  # Output: none
  ("multiplayer_remove_headquarters_flags",
    [
      (store_add, ":end_cond", "spr_headquarters_flag_gray", 1),
      (try_for_range, ":headquarters_flag_no", "spr_headquarters_flag_red", ":end_cond"),
        (replace_scene_props, ":headquarters_flag_no", "spr_empty"),
      (try_end),
  ]),
  
  #script_multiplayer_remove_destroy_mod_targets
  # Input: none
  # Output: none
  ("multiplayer_remove_destroy_mod_targets",
    [
      (replace_scene_props, "spr_catapult_destructible", "spr_empty"),
      (replace_scene_props, "spr_trebuchet_destructible", "spr_empty"),
  ]),
  
  #script_multiplayer_init_mission_variables
  ("multiplayer_init_mission_variables",
    [
      (assign, "$g_multiplayer_team_1_first_spawn", 1),
      (assign, "$g_multiplayer_team_2_first_spawn", 1),
      (assign, "$g_multiplayer_poll_running", 0),
      ##     (assign, "$g_multiplayer_show_poll_when_suitable", 0),
      (assign, "$g_waiting_for_confirmation_to_terminate", 0),
      (assign, "$g_confirmation_result", 0),
      (assign, "$g_team_balance_next_round", 0),
      (team_get_faction, "$g_multiplayer_team_1_faction", 0),
      (team_get_faction, "$g_multiplayer_team_2_faction", 1),
      (assign, "$g_multiplayer_next_team_1_faction", "$g_multiplayer_team_1_faction"),
      (assign, "$g_multiplayer_next_team_2_faction", "$g_multiplayer_team_2_faction"),
      
      (assign, "$g_multiplayer_bot_type_1_wanted", 0),
      (assign, "$g_multiplayer_bot_type_2_wanted", 0),
      (assign, "$g_multiplayer_bot_type_3_wanted", 0),
      (assign, "$g_multiplayer_bot_type_4_wanted", 0),
      (assign, "$g_multiplayer_bot_type_5_wanted", 0),
      (assign, "$g_multiplayer_bot_type_6_wanted", 0),
      (assign, "$g_multiplayer_bot_type_7_wanted", 0),
      (assign, "$g_multiplayer_bot_type_8_wanted", 0),
      (assign, "$g_multiplayer_bot_type_9_wanted", 0),
      (assign, "$g_multiplayer_bot_type_10_wanted", 0),
      (assign, "$g_multiplayer_bot_type_11_wanted", 0),
      (assign, "$g_multiplayer_bot_type_12_wanted", 0),
      (assign, "$g_multiplayer_bot_type_13_wanted", 0),
      (assign, "$g_multiplayer_bot_type_14_wanted", 0),
      (assign, "$g_multiplayer_bot_type_15_wanted", 0),
      
      (call_script, "script_music_set_situation_with_culture", mtf_sit_multiplayer_fight),
  ]),
  
  #script_multiplayer_event_mission_end
  # Input: none
  # Output: none
  ("multiplayer_event_mission_end",
    [
      #EVERY_BREATH_YOU_TAKE achievement
      (try_begin),
        (multiplayer_get_my_player, ":my_player_no"),
        (is_between, ":my_player_no", 0, multiplayer_max_possible_player_id),
        (player_get_kill_count, ":kill_count", ":my_player_no"),
        (player_get_death_count, ":death_count", ":my_player_no"),
        (gt, ":kill_count", ":death_count"),
        (unlock_achievement, ACHIEVEMENT_EVERY_BREATH_YOU_TAKE),
      (try_end),
      #EVERY_BREATH_YOU_TAKE achievement end
  ]),
  
  
  #script_multiplayer_event_agent_killed_or_wounded
  # Input: arg1 = dead_agent_no, arg2 = killer_agent_no
  # Output: none
  ("multiplayer_event_agent_killed_or_wounded",
    [
      (store_script_param, ":dead_agent_no", 1),
      (store_script_param, ":killer_agent_no", 2),
      
      (multiplayer_get_my_player, ":my_player_no"),
      (try_begin),
        (is_between, ":my_player_no", 0, multiplayer_max_possible_player_id),
        (player_get_agent_id, ":my_player_agent", ":my_player_no"),
        (ge, ":my_player_agent", 0),
        (try_begin),
          (eq, ":my_player_agent", ":dead_agent_no"),
          (store_mission_timer_a, "$g_multiplayer_respawn_start_time"),
        (try_end),
        (try_begin),
          (eq, ":my_player_agent", ":killer_agent_no"),
          (neq, ":my_player_agent", ":dead_agent_no"),
          (agent_is_human, ":dead_agent_no"),
          (agent_is_alive, ":my_player_agent"),
          (neg|agent_is_ally, ":dead_agent_no"),
          (agent_get_horse, ":my_horse_agent", ":my_player_agent"),
          (agent_get_wielded_item, ":my_wielded_item", ":my_player_agent", 0),
          (assign, ":my_item_class", -1),
          (try_begin),
            (ge, ":my_wielded_item", 0),
            (item_get_slot, ":my_item_class", ":my_wielded_item", slot_item_multiplayer_item_class),
          (try_end),
          #SPOIL_THE_CHARGE achievement
          (try_begin),
            (lt, ":my_horse_agent", 0),
            (agent_get_horse, ":dead_agent_horse_agent", ":dead_agent_no"),
            (ge, ":dead_agent_horse_agent", 0),
            (get_achievement_stat, ":achievement_stat", ACHIEVEMENT_SPOIL_THE_CHARGE, 0),
            (lt, ":achievement_stat", 50),
            (val_add, ":achievement_stat", 1),
            (set_achievement_stat, ACHIEVEMENT_SPOIL_THE_CHARGE, 0, ":achievement_stat"),
            (ge, ":achievement_stat", 50),
            (unlock_achievement, ACHIEVEMENT_SPOIL_THE_CHARGE),
          (try_end),
          #SPOIL_THE_CHARGE achievement end
          #HARASSING_HORSEMAN achievement
          (try_begin),
            (ge, ":my_horse_agent", 0),
            (this_or_next|eq, ":my_item_class", multi_item_class_type_bow),
            (this_or_next|eq, ":my_item_class", multi_item_class_type_crossbow),
            (this_or_next|eq, ":my_item_class", multi_item_class_type_throwing),
            (eq, ":my_item_class", multi_item_class_type_throwing_axe),
            (get_achievement_stat, ":achievement_stat", ACHIEVEMENT_HARASSING_HORSEMAN, 0),
            (lt, ":achievement_stat", 100),
            (val_add, ":achievement_stat", 1),
            (set_achievement_stat, ACHIEVEMENT_HARASSING_HORSEMAN, 0, ":achievement_stat"),
            (ge, ":achievement_stat", 100),
            (unlock_achievement, ACHIEVEMENT_HARASSING_HORSEMAN),
          (try_end),
          #HARASSING_HORSEMAN achievement end
          #THROWING_STAR achievement
          (try_begin),
            (this_or_next|eq, ":my_item_class", multi_item_class_type_throwing),
            (eq, ":my_item_class", multi_item_class_type_throwing_axe),
            (get_achievement_stat, ":achievement_stat", ACHIEVEMENT_THROWING_STAR, 0),
            (lt, ":achievement_stat", 25),
            (val_add, ":achievement_stat", 1),
            (set_achievement_stat, ACHIEVEMENT_THROWING_STAR, 0, ":achievement_stat"),
            (ge, ":achievement_stat", 25),
            (unlock_achievement, ACHIEVEMENT_THROWING_STAR),
          (try_end),
          #THROWING_STAR achievement end
          #SHISH_KEBAB achievement
          (try_begin),
            (ge, ":my_horse_agent", 0),
            (eq, ":my_item_class", multi_item_class_type_lance),
            (get_achievement_stat, ":achievement_stat", ACHIEVEMENT_SHISH_KEBAB, 0),
            (lt, ":achievement_stat", 25),
            (val_add, ":achievement_stat", 1),
            (set_achievement_stat, ACHIEVEMENT_SHISH_KEBAB, 0, ":achievement_stat"),
            (ge, ":achievement_stat", 25),
            (unlock_achievement, ACHIEVEMENT_SHISH_KEBAB),
          (try_end),
          #SHISH_KEBAB achievement end
          #CHOPPY_CHOP_CHOP achievement
          (try_begin),
            (this_or_next|eq, ":my_item_class", multi_item_class_type_sword),
            (this_or_next|eq, ":my_item_class", multi_item_class_type_axe),
            (this_or_next|eq, ":my_item_class", multi_item_class_type_cleavers),
            (this_or_next|eq, ":my_item_class", multi_item_class_type_two_handed_sword),
            (this_or_next|eq, ":my_item_class", multi_item_class_type_two_handed_axe),
            (this_or_next|eq, ":my_wielded_item", "itm_long_light_spear2"), #sarranid item exception
            (eq, ":my_wielded_item", "itm_long_light_spear2"), #sarranid item exception
            (neq, ":my_wielded_item", "itm_long_light_spear2"), #sarranid item exception
            (get_achievement_stat, ":achievement_stat", ACHIEVEMENT_CHOPPY_CHOP_CHOP, 0),
            (lt, ":achievement_stat", 50),
            (val_add, ":achievement_stat", 1),
            (set_achievement_stat, ACHIEVEMENT_CHOPPY_CHOP_CHOP, 0, ":achievement_stat"),
            (ge, ":achievement_stat", 50),
            (unlock_achievement, ACHIEVEMENT_CHOPPY_CHOP_CHOP),
          (try_end),
          #CHOPPY_CHOP_CHOP achievement end
          #MACE_IN_YER_FACE achievement
          (try_begin),
            (this_or_next|eq, ":my_item_class", multi_item_class_type_blunt),
            (eq, ":my_wielded_item", "itm_long_light_spear2"), #sarranid item exception
            (neq, ":my_wielded_item", "itm_long_light_spear2"), #sarranid item exception
            (neq, ":my_wielded_item", "itm_long_light_spear2"), #sarranid item exception
            (get_achievement_stat, ":achievement_stat", ACHIEVEMENT_MACE_IN_YER_FACE, 0),
            (lt, ":achievement_stat", 25),
            (val_add, ":achievement_stat", 1),
            (set_achievement_stat, ACHIEVEMENT_MACE_IN_YER_FACE, 0, ":achievement_stat"),
            (ge, ":achievement_stat", 25),
            (unlock_achievement, ACHIEVEMENT_MACE_IN_YER_FACE),
          (try_end),
          #MACE_IN_YER_FACE achievement end
          #THE_HUSCARL achievement
          (try_begin),
            (eq, ":my_item_class", multi_item_class_type_throwing_axe),
            (get_achievement_stat, ":achievement_stat", ACHIEVEMENT_THE_HUSCARL, 0),
            (lt, ":achievement_stat", 50),
            (val_add, ":achievement_stat", 1),
            (set_achievement_stat, ACHIEVEMENT_THE_HUSCARL, 0, ":achievement_stat"),
            (ge, ":achievement_stat", 50),
            (unlock_achievement, ACHIEVEMENT_THE_HUSCARL),
          (try_end),
          #THE_HUSCARL achievement end
        (try_end),
      (try_end),
      
      (try_begin),
        (is_between, ":my_player_no", 0, multiplayer_max_possible_player_id),
        (player_get_agent_id, ":player_agent", ":my_player_no"),
        (eq, ":dead_agent_no", ":player_agent"),
        
        (assign, ":show_respawn_counter", 0),
        (try_begin),
          #TODO: add other game types with no respawns here
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_battle),
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_invasion),
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_raid),
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_sea),#naval battles
          (neq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle), #capitan chief
          (assign, ":show_respawn_counter", 1),
        (else_try),
          (eq, "$g_multiplayer_player_respawn_as_bot", 1),
          (player_get_team_no, ":my_player_team", ":my_player_no"),
          (assign, ":is_found", 0),
          (try_for_agents, ":cur_agent"),
            (eq, ":is_found", 0),
            (agent_is_alive, ":cur_agent"),
            (agent_is_human, ":cur_agent"),
            (agent_is_non_player, ":cur_agent"),
            (agent_get_team ,":cur_team", ":cur_agent"),
            (eq, ":cur_team", ":my_player_team"),
            (assign, ":is_found", 1),
          (try_end),
          (eq, ":is_found", 1),
          (assign, ":show_respawn_counter", 1),
        (try_end),
        
        (try_begin),
          #(player_get_slot, ":spawn_count", ":player_no", slot_player_spawn_count),
          (eq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
          (gt, "$g_multiplayer_number_of_respawn_count", 0),
          
          (ge, "$g_my_spawn_count", "$g_multiplayer_number_of_respawn_count"),
          
          (multiplayer_get_my_player, ":my_player_no"),
          (player_get_team_no, ":my_player_team", ":my_player_no"),
          
          (this_or_next|eq, ":my_player_team", 0),
          (ge, "$g_my_spawn_count", 999),
          
          (assign, "$g_show_no_more_respawns_remained", 1),
        (else_try),
          (assign, "$g_show_no_more_respawns_remained", 0),
        (try_end),
        
        (eq, ":show_respawn_counter", 1),
        
        (start_presentation, "prsnt_multiplayer_respawn_time_counter"),
      (try_end),
  ]),
  
  #script_multiplayer_get_item_value_for_troop
  # Input: arg1 = item_no, arg2 = troop_no
  # Output: reg0: item_value
  ("multiplayer_get_item_value_for_troop",
    [
      (store_script_param, ":item_no", 1),
      (store_script_param, ":troop_no", 2),
      (try_begin),
        (call_script, "script_cf_multiplayer_is_item_default_for_troop", ":item_no", ":troop_no"),
        (assign, ":item_value", 0),
      (else_try),
        (store_item_value, ":item_value", ":item_no"),
        (store_troop_faction, ":faction_no", ":troop_no"),
        (store_sub, ":faction_slot", ":faction_no", mp_factions_begin),
        (val_add, ":faction_slot", slot_item_multiplayer_faction_price_multipliers_begin),
        (item_get_slot, ":price_multiplier", ":item_no", ":faction_slot"),
        (val_mul, ":item_value", ":price_multiplier"),
        (val_div, ":item_value", 100),
      (try_end),
      (assign, reg0, ":item_value"),
  ]),
  
  #script_multiplayer_get_previous_item_for_item_and_troop
  # Input: arg1 = item_no, arg2 = troop_no
  # Output: reg0: previous_item_no (-1 if it is the root item, 0 if the item is invalid)
  ("multiplayer_get_previous_item_for_item_and_troop",
    [
      (store_script_param, ":item_no", 1),
      (store_script_param, ":troop_no", 2),
      (item_get_slot, ":item_class", ":item_no", slot_item_multiplayer_item_class),
      (call_script, "script_multiplayer_get_item_value_for_troop", ":item_no", ":troop_no"),
      (assign, ":item_value", reg0),
      (store_sub, ":troop_index", ":troop_no", multiplayer_troops_begin),
      (val_add, ":troop_index", slot_item_multiplayer_availability_linked_list_begin),
      (assign, ":max_item_no", -1),
      (assign, ":max_item_value", -1),
      (try_for_range, ":i_item", all_items_begin, all_items_end),
        (item_slot_eq, ":i_item", slot_item_multiplayer_item_class, ":item_class"),
        (item_slot_ge, ":i_item", ":troop_index", 1),
        (call_script, "script_multiplayer_get_item_value_for_troop", ":i_item", ":troop_no"),
        (assign, ":i_item_value", reg0),
        (try_begin),
          (eq, ":i_item_value", 0),
          (eq, ":max_item_value", 0),
          #choose between 2 default items
          (store_item_value, ":i_item_real_value", ":i_item"),
          (store_item_value, ":max_item_real_value", ":max_item_no"),
          (try_begin),
            (gt, ":i_item_real_value", ":max_item_real_value"),
            (assign, ":max_item_value", ":i_item_value"),
            (assign, ":max_item_no", ":i_item"),
          (try_end),
        (else_try),
          (gt, ":i_item_value", ":max_item_value"),
          (lt, ":i_item_value", ":item_value"),
          (assign, ":max_item_value", ":i_item_value"),
          (assign, ":max_item_no", ":i_item"),
        (try_end),
      (try_end),
      (try_begin),
        (eq, ":max_item_no", -1),
        (assign, ":item_upper_class", -1),
        (try_begin),
          (is_between, ":item_class", multi_item_class_type_melee_weapons_begin, multi_item_class_type_melee_weapons_end),
          (assign, ":item_upper_class", 0),
        (else_try),
          (is_between, ":item_class", multi_item_class_type_shields_begin, multi_item_class_type_shields_end),
          (assign, ":item_upper_class", 1),
        (else_try),
          (eq, ":item_class", multi_item_class_type_bow),
          (assign, ":item_upper_class", 2),
        (else_try),
          (eq, ":item_class", multi_item_class_type_crossbow),
          (assign, ":item_upper_class", 3),
        (else_try),
          (eq, ":item_class", multi_item_class_type_arrow),
          (assign, ":item_upper_class", 4),
        (else_try),
          (eq, ":item_class", multi_item_class_type_bolt),
          (assign, ":item_upper_class", 5),
        (else_try),
          (eq, ":item_class", multi_item_class_type_throwing),
          (assign, ":item_upper_class", 6),
        (else_try),
          (is_between, ":item_class", multi_item_class_type_heads_begin, multi_item_class_type_heads_end),
          (assign, ":item_upper_class", 7),
        (else_try),
          (is_between, ":item_class", multi_item_class_type_bodies_begin, multi_item_class_type_bodies_end),
          (assign, ":item_upper_class", 8),
        (else_try),
          (is_between, ":item_class", multi_item_class_type_feet_begin, multi_item_class_type_feet_end),
          (assign, ":item_upper_class", 9),
        (else_try),
          (is_between, ":item_class", multi_item_class_type_gloves_begin, multi_item_class_type_gloves_end),
          (assign, ":item_upper_class", 10),
        (else_try),
          (is_between, ":item_class", multi_item_class_type_horses_begin, multi_item_class_type_horses_end),
          (assign, ":item_upper_class", 11),
        (try_end),
        (neq, ":item_upper_class", 0),
        #search for the default item for non-weapon classes (only 1 slot is easy to fill)
        (assign, ":end_cond", all_items_end),
        (try_for_range, ":i_item", all_items_begin, ":end_cond"),
          (item_slot_ge, ":i_item", ":troop_index", 1),
          (item_get_slot, ":i_item_class", ":i_item", slot_item_multiplayer_item_class),
          (try_begin),
            (is_between, ":i_item_class", multi_item_class_type_melee_weapons_begin, multi_item_class_type_melee_weapons_end),
            (assign, ":i_item_upper_class", 0),
          (else_try),
            (is_between, ":i_item_class", multi_item_class_type_shields_begin, multi_item_class_type_shields_end),
            (assign, ":i_item_upper_class", 1),
          (else_try),
            (eq, ":i_item_class", multi_item_class_type_bow),
            (assign, ":i_item_upper_class", 2),
          (else_try),
            (eq, ":i_item_class", multi_item_class_type_crossbow),
            (assign, ":i_item_upper_class", 3),
          (else_try),
            (eq, ":i_item_class", multi_item_class_type_arrow),
            (assign, ":i_item_upper_class", 4),
          (else_try),
            (eq, ":i_item_class", multi_item_class_type_bolt),
            (assign, ":i_item_upper_class", 5),
          (else_try),
            (eq, ":i_item_class", multi_item_class_type_throwing),
            (assign, ":i_item_upper_class", 6),
          (else_try),
            (is_between, ":i_item_class", multi_item_class_type_heads_begin, multi_item_class_type_heads_end),
            (assign, ":i_item_upper_class", 7),
          (else_try),
            (is_between, ":i_item_class", multi_item_class_type_bodies_begin, multi_item_class_type_bodies_end),
            (assign, ":i_item_upper_class", 8),
          (else_try),
            (is_between, ":i_item_class", multi_item_class_type_feet_begin, multi_item_class_type_feet_end),
            (assign, ":i_item_upper_class", 9),
          (else_try),
            (is_between, ":i_item_class", multi_item_class_type_gloves_begin, multi_item_class_type_gloves_end),
            (assign, ":i_item_upper_class", 10),
          (else_try),
            (is_between, ":i_item_class", multi_item_class_type_horses_begin, multi_item_class_type_horses_end),
            (assign, ":i_item_upper_class", 11),
          (try_end),
          (eq, ":i_item_upper_class", ":item_upper_class"),
          (call_script, "script_cf_multiplayer_is_item_default_for_troop", ":i_item", ":troop_no"),
          (assign, ":max_item_no", ":i_item"),
          (assign, ":end_cond", 0), #break
        (try_end),
      (try_end),
      (assign, reg0, ":max_item_no"),
  ]),
  
  #script_cf_multiplayer_is_item_default_for_troop
  # Input: arg1 = item_no, arg2 = troop_no
  # Output: reg0: total_cost
  ("cf_multiplayer_is_item_default_for_troop",
    [
      (store_script_param, ":item_no", 1),
      (store_script_param, ":troop_no", 2),
      (assign, ":default_item", 0),
      (try_begin),
        (neg|is_between, ":item_no", horses_begin, horses_end),
        ##       (neq, ":item_no", "itm_charger"),
        ##       (neq, ":item_no", "itm_warhorse_steppe"),
        
        (troop_get_inventory_capacity, ":end_cond", ":troop_no"), #troop no can come -1 here error occured at friday
        (try_for_range, ":i_slot", 0, ":end_cond"),
          (troop_get_inventory_slot, ":default_item_id", ":troop_no", ":i_slot"),
          (eq, ":item_no", ":default_item_id"),
          (assign, ":default_item", 1),
          (assign, ":end_cond", 0), #break
        (try_end),
      (try_end),
      (eq, ":default_item", 1),
  ]),
  
  #script_multiplayer_calculate_cur_selected_items_cost
  # Input: arg1 = player_no
  # Output: reg0: total_cost
  ("multiplayer_calculate_cur_selected_items_cost",
    [
      (store_script_param, ":player_no", 1),
      (store_script_param, ":calculation_type", 2), #0 for normal calculation
      (assign, ":total_cost", 0),
      (player_get_troop_id, ":troop_no", ":player_no"),
      
      (try_begin),
        (eq, ":calculation_type", 0),
        (assign, ":begin_cond", slot_player_cur_selected_item_indices_begin),
        (assign, ":end_cond", slot_player_cur_selected_item_indices_end),
      (else_try),
        (assign, ":begin_cond", slot_player_selected_item_indices_begin),
        (assign, ":end_cond", slot_player_selected_item_indices_end),
      (try_end),
      
      (try_for_range, ":i_item", ":begin_cond", ":end_cond"),
        (player_get_slot, ":item_id", ":player_no", ":i_item"),
        (ge, ":item_id", 0), #might be -1 for horses etc.
        (call_script, "script_multiplayer_get_item_value_for_troop", ":item_id", ":troop_no"),
        (val_add, ":total_cost", reg0),
      (try_end),
      (assign, reg0, ":total_cost"),
  ]),
  
  #script_multiplayer_set_item_available_for_troop
  # Input: arg1 = item_no, arg2 = troop_no
  # Output: none
  ("multiplayer_set_item_available_for_troop",
    [
      (store_script_param, ":item_no", 1),
      (try_begin),
        (gt,":item_no",0),
        (store_script_param, ":troop_no", 2),
        (store_sub, ":item_troop_slot", ":troop_no", multiplayer_troops_begin),
        (val_add, ":item_troop_slot", slot_item_multiplayer_availability_linked_list_begin),
        (item_set_slot, ":item_no", ":item_troop_slot", 1),
        ###debug
        (try_begin),
          (item_slot_eq,":item_no",slot_item_multiplayer_item_class,0),
          (assign,reg1,":item_no"),
          (str_store_item_name,s1,":item_no"),
          (assign,reg0,":troop_no"),
          (str_store_troop_name,s0,":troop_no"),
          (display_message,"str_item_class_debug"),
        (try_end),
      (try_end),
  ]),
  
  #script_multiplayer_send_item_selections
  # Input: none
  # Output: none
  ("multiplayer_send_item_selections",
    [
      (multiplayer_get_my_player, ":my_player_no"),
      (try_for_range, ":i_item", slot_player_selected_item_indices_begin, slot_player_selected_item_indices_end),
        (player_get_slot, ":item_id", ":my_player_no", ":i_item"),
        (multiplayer_send_2_int_to_server, multiplayer_event_set_item_selection, ":i_item", ":item_id"),
      (try_end),
  ]),
  
  #script_multiplayer_set_default_item_selections_for_troop
  # Input: arg1 = troop_no
  # Output: none
  ("multiplayer_set_default_item_selections_for_troop",
    [
      (store_script_param, ":troop_no", 1),
      (multiplayer_get_my_player, ":my_player_no"),
      (call_script, "script_multiplayer_clear_player_selected_items", ":my_player_no"),
      (assign, ":cur_weapon_slot", 0),
      (try_begin),
        (ge,"$g_multiplayer_deadly_bow",1),
        (player_set_slot, ":my_player_no", slot_player_selected_item_indices_begin+1, "itm_deadly_bow"),
        (player_set_slot, ":my_player_no", slot_player_selected_item_indices_begin+2, "itm_deadly_arrows"),
      (else_try),
        (troop_get_inventory_capacity, ":inv_cap", ":troop_no"),
        (try_for_range, ":i_slot", 0, ":inv_cap"),
          (troop_get_inventory_slot, ":item_id", ":troop_no", ":i_slot"),
          (ge, ":item_id", 0),
          (item_get_slot, ":item_class", ":item_id", slot_item_multiplayer_item_class),
          (try_begin),
            (is_between, ":item_class", multi_item_class_type_weapons_begin, multi_item_class_type_weapons_end),
            (this_or_next|eq, "$g_multiplayer_disallow_ranged_weapons", 0),
            (neg|is_between, ":item_class", multi_item_class_type_ranged_weapons_begin, multi_item_class_type_ranged_weapons_end),
            (store_add, ":selected_item_slot", slot_player_selected_item_indices_begin, ":cur_weapon_slot"),
            (player_set_slot, ":my_player_no", ":selected_item_slot", ":item_id"),
            (val_add, ":cur_weapon_slot", 1),
          (else_try),
            (is_between, ":item_class", multi_item_class_type_heads_begin, multi_item_class_type_heads_end),
            (store_add, ":selected_item_slot", slot_player_selected_item_indices_begin, 4),
            (player_set_slot, ":my_player_no", ":selected_item_slot", ":item_id"),
          (else_try),
            (is_between, ":item_class", multi_item_class_type_bodies_begin, multi_item_class_type_bodies_end),
            (call_script,"script_cf_mp_gender_check",":item_id"),
            (store_add, ":selected_item_slot", slot_player_selected_item_indices_begin, 5),
            (player_set_slot, ":my_player_no", ":selected_item_slot", ":item_id"),
          (else_try),
            (is_between, ":item_class", multi_item_class_type_feet_begin, multi_item_class_type_feet_end),
            (store_add, ":selected_item_slot", slot_player_selected_item_indices_begin, 6),
            (player_set_slot, ":my_player_no", ":selected_item_slot", ":item_id"),
          (else_try),
            (is_between, ":item_class", multi_item_class_type_gloves_begin, multi_item_class_type_gloves_end),
            (store_add, ":selected_item_slot", slot_player_selected_item_indices_begin, 7),
            (player_set_slot, ":my_player_no", ":selected_item_slot", ":item_id"),
          (else_try),
            (is_between, ":item_class", multi_item_class_type_horses_begin, multi_item_class_type_horses_end),
            (eq, "$g_horses_are_avaliable", 1),
            (store_add, ":selected_item_slot", slot_player_selected_item_indices_begin, 8),
            (player_set_slot, ":my_player_no", ":selected_item_slot", ":item_id"),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  #script_multiplayer_display_available_items_for_troop_and_item_classes
  # Input: arg1 = troop_no, arg2 = item_classes_begin, arg3 = item_classes_end, arg4 = pos_x_begin, arg5 = pos_y_begin
  # Output: none
  ("multiplayer_display_available_items_for_troop_and_item_classes",
    [
      (store_script_param, ":troop_no", 1),
      (store_script_param, ":item_classes_begin", 2),
      (store_script_param, ":item_classes_end", 3),
      (store_script_param, ":pos_x_begin", 4),
      (store_script_param, ":pos_y_begin", 5),
      
      (assign, ":x_adder", 100),
      (try_begin),
        (gt, ":pos_x_begin", 500),
        (assign, ":x_adder", -100),
      (try_end),
      
      (store_sub, ":item_troop_slot", ":troop_no", multiplayer_troops_begin),
      (val_add, ":item_troop_slot", slot_item_multiplayer_availability_linked_list_begin),
      
      (try_for_range, ":cur_slot", multi_data_item_button_indices_begin, multi_data_item_button_indices_end),
        (troop_set_slot, "trp_multiplayer_data", ":cur_slot", -1),
      (try_end),
      
      (assign, ":num_available_items", 0),
      
      (try_for_range, ":item_no", all_items_begin, all_items_end),
        (item_get_slot, ":item_class", ":item_no", slot_item_multiplayer_item_class),
        (try_begin),
          (eq,":item_classes_begin",multi_item_class_type_weapons_begin),
          (ge,"$g_multiplayer_deadly_bow",1),
          (try_begin),
            # (this_or_next|eq,":item_no","itm_deadly_bow"),
            # (eq,":item_no","itm_deadly_arrows"),
            # (assign,":force_pass",2),
            # (else_try),
            (assign,":force_pass",-1),
          (try_end),
        (else_try),
          (assign,":force_pass",0),
        (try_end),
        
        (neq,":force_pass",-1),
        (this_or_next|ge,":force_pass",1),
        (is_between, ":item_class", ":item_classes_begin", ":item_classes_end"),
        (this_or_next|eq, "$g_multiplayer_disallow_ranged_weapons", 0),
        (neg|is_between, ":item_class", multi_item_class_type_ranged_weapons_begin, multi_item_class_type_ranged_weapons_end),
        
        (this_or_next|ge,":force_pass",1),
        (item_slot_ge, ":item_no", ":item_troop_slot", 1),
        (call_script,"script_cf_mp_gender_check",":item_no"),
        (store_add, ":cur_slot_index", ":num_available_items", multi_data_item_button_indices_begin),
        #using the result array for item_ids
        (troop_set_slot, "trp_multiplayer_data", ":cur_slot_index", ":item_no"),
        (val_add, ":num_available_items", 1),
      (try_end),
      
      #sorting
      (store_add, ":item_slots_end", ":num_available_items", multi_data_item_button_indices_begin),
      (store_sub, ":item_slots_end_minus_one", ":item_slots_end", 1),
      (try_for_range, ":cur_slot", multi_data_item_button_indices_begin, ":item_slots_end_minus_one"),
        (store_add, ":cur_slot_2_begin", ":cur_slot", 1),
        (try_for_range, ":cur_slot_2", ":cur_slot_2_begin", ":item_slots_end"),
          (troop_get_slot, ":item_1", "trp_multiplayer_data", ":cur_slot"),
          (troop_get_slot, ":item_2", "trp_multiplayer_data", ":cur_slot_2"),
          (call_script, "script_multiplayer_get_item_value_for_troop", ":item_1", ":troop_no"),
          (assign, ":item_1_point", reg0),
          (call_script, "script_multiplayer_get_item_value_for_troop", ":item_2", ":troop_no"),
          (assign, ":item_2_point", reg0),
          (item_get_slot, ":item_1_class", ":item_1", slot_item_multiplayer_item_class),
          (item_get_slot, ":item_2_class", ":item_2", slot_item_multiplayer_item_class),
          (val_mul, ":item_1_class", 1000000), #assuming maximum item price is 1000000
          (val_mul, ":item_2_class", 1000000), #assuming maximum item price is 1000000
          (val_add, ":item_1_point", ":item_1_class"),
          (val_add, ":item_2_point", ":item_2_class"),
          (lt, ":item_2_point", ":item_1_point"),
          (troop_set_slot, "trp_multiplayer_data", ":cur_slot", ":item_2"),
          (troop_set_slot, "trp_multiplayer_data", ":cur_slot_2", ":item_1"),
        (try_end),
      (try_end),
      
      (troop_get_slot, ":last_item_no", "trp_multiplayer_data", multi_data_item_button_indices_begin),
      (assign, ":num_item_classes", 0),
      (assign,":slot_in_row",0),
      (try_begin),
        (ge, ":last_item_no", 0),
        (item_get_slot, ":last_item_class", ":last_item_no", slot_item_multiplayer_item_class),
        
        (try_for_range, ":cur_slot", multi_data_item_button_indices_begin, ":item_slots_end"),
          (troop_get_slot, ":item_no", "trp_multiplayer_data", ":cur_slot"),
          (item_get_slot, ":item_class", ":item_no", slot_item_multiplayer_item_class),
          (try_begin),
            (neq, ":item_class", ":last_item_class"),
            (assign,":slot_in_row",0),
            (val_add, ":num_item_classes", 1),
            (assign, ":last_item_class", ":item_class"),
          (else_try),
            (val_add,":slot_in_row",1),
            (ge,":slot_in_row",7),
            (assign,":slot_in_row",0),
            (val_add, ":num_item_classes", 1),
          (try_end),
        (try_end),
        
        (try_begin),#fix for armors
          (eq,":item_classes_begin",multi_item_class_type_bodies_begin),
          (gt,":num_item_classes",3),
          (val_sub,":num_item_classes",1),
          (val_add,":pos_y_begin",100),
        (try_end),
        
        (try_begin),
          (store_mul, ":required_y", ":num_item_classes", 100),
          (gt, ":required_y", ":pos_y_begin"),
          (store_sub, ":dif", ":required_y", ":pos_y_begin"),
          (val_div, ":dif", 100),
          (val_add, ":dif", 1),
          (val_mul, ":dif", 100),
          (val_add, ":pos_y_begin", ":dif"),
        (try_end),
        
        (item_get_slot, ":last_item_class", ":last_item_no", slot_item_multiplayer_item_class),
      (try_end),
      (assign, ":cur_x", ":pos_x_begin"),
      (assign, ":cur_y", ":pos_y_begin"),
      (assign,":slot_in_row",1),
      (try_for_range, ":cur_slot", multi_data_item_button_indices_begin, ":item_slots_end"),
        (troop_get_slot, ":item_no", "trp_multiplayer_data", ":cur_slot"),
        (item_get_slot, ":item_class", ":item_no", slot_item_multiplayer_item_class),
        (try_begin),
          (is_between, ":item_class", multi_item_class_type_light_armor, multi_item_class_type_light_helm),
          (assign,":max_slots",7),
        (else_try),
          (assign,":max_slots",8),
        (try_end),
        (try_begin),
          (this_or_next|ge,":slot_in_row",":max_slots"),
          (neq, ":item_class", ":last_item_class"),
          (assign,":slot_in_row",1),
          (val_sub, ":cur_y", 100),
          (assign, ":cur_x", ":pos_x_begin"),
          (assign, ":last_item_class", ":item_class"),
        (try_end),
        (create_image_button_overlay, ":cur_obj", "mesh_mp_inventory_choose", "mesh_mp_inventory_choose"),
        (position_set_x, pos1, 800),
        (position_set_y, pos1, 800),
        (overlay_set_size, ":cur_obj", pos1),
        (position_set_x, pos1, ":cur_x"),
        (position_set_y, pos1, ":cur_y"),
        (overlay_set_position, ":cur_obj", pos1),
        (create_mesh_overlay_with_item_id, reg0, ":item_no"),
        (store_add, ":item_x", ":cur_x", 50),
        (store_add, ":item_y", ":cur_y", 50),
        (position_set_x, pos1, ":item_x"),
        (position_set_y, pos1, ":item_y"),
        (overlay_set_position, reg0, pos1),
        (val_add, ":cur_x", ":x_adder"),
        (val_add,":slot_in_row",1),
      (try_end),
  ]),
  
  # script_multiplayer_fill_map_game_types
  # Input: game_type
  # Output: num_maps
  ("multiplayer_fill_map_game_types",
    [
      (store_script_param, ":game_type", 1),
      (try_for_range, ":i_multi", multi_data_maps_for_game_type_begin, multi_data_maps_for_game_type_end),
        (troop_set_slot, "trp_multiplayer_data", ":i_multi", -1),
      (try_end),
      (assign, ":num_maps", 0),
      (try_begin),
        (eq, ":game_type", multiplayer_game_type_invasion),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin, "scn_multi_scene_inv_1"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 1, "scn_multi_scene_inv_2"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 2, "scn_multi_scene_inv_3"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 3, "scn_multi_scene_inv_4"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 4, "scn_multi_scene_inv_5"),
        (assign, ":num_maps", 5),
      (else_try),
        (eq, ":game_type", multiplayer_game_type_sea),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin, "scn_multi_sea_1"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 1, "scn_multi_sea_2"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 2, "scn_multi_coast_1"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 3, "scn_multi_coast_2"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 4, "scn_multi_scene_coast"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 5, "scn_multi_scene_fjord"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 6, "scn_multi_scene_islands"),
        (assign, ":num_maps", 7),
      (else_try),
        (eq, ":game_type", multiplayer_game_type_raid),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin, "scn_multi_coast_1"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 1, "scn_multi_coast_2"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 2, "scn_multi_scene_8"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 3, "scn_multi_scene_10"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 4, "scn_multi_scene_16"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 5, "scn_multi_scene_13"),
        (assign, ":num_maps", 6),
      (else_try),
        (eq, ":game_type", multiplayer_game_type_siege),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin, "scn_multi_scene_3"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 1, "scn_multi_scene_8"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 2, "scn_multi_scene_10"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 3, "scn_multi_scene_16"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 4, "scn_multi_scene_13"),
        (assign, ":num_maps", 5),
      (else_try),
        (eq, ":game_type", multiplayer_game_type_duel),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin, "scn_multi_scene_15"),
        (assign, ":num_maps", 1),
      (else_try),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin, "scn_multi_scene_1"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 1, "scn_multi_scene_2"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 2, "scn_multi_scene_4"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 3, "scn_multi_scene_7"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 4, "scn_multi_scene_9"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 5, "scn_multi_scene_11"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 6, "scn_multi_scene_12"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 7, "scn_multi_scene_17"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 8, "scn_multi_scene_18"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 9, "scn_multi_scene_5"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 10, "scn_multi_scene_19"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 11, "scn_multi_scene_6"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 12, "scn_multi_scene_14"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 13, "scn_random_multi_plain"),
        (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 14, "scn_random_multi_forest"),
        # (troop_set_slot, "trp_multiplayer_data", multi_data_maps_for_game_type_begin + 14, "scn_random_multi_snow"),  trees look crappy
        (assign, ":num_maps", 15),
      (try_end),
      (assign, reg0, ":num_maps"),
  ]),
  
  # script_multiplayer_count_players_bots
  # Input: none
  # Output: none
  ("multiplayer_count_players_bots",
    [
      (get_max_players, ":num_players"),
      (try_for_range, ":cur_player", 0, ":num_players"),
        (player_is_active, ":cur_player"),
        (player_set_slot, ":cur_player", slot_player_last_bot_count, 0),
      (try_end),
      
      (try_for_agents, ":cur_agent"),
        (agent_is_human, ":cur_agent"),
        (agent_is_alive, ":cur_agent"),
        (agent_get_player_id, ":agent_player", ":cur_agent"),
        (lt, ":agent_player", 0), #not a player
        (agent_get_group, ":agent_group", ":cur_agent"),
        (player_is_active, ":agent_group"),
        (player_get_slot, ":bot_count", ":agent_group", slot_player_last_bot_count),
        (val_add, ":bot_count", 1),
        (player_set_slot, ":agent_group", slot_player_last_bot_count, ":bot_count"),
      (try_end),
  ]),
  
  # script_multiplayer_find_player_leader_for_bot
  # Input: arg1 = team_no
  # Output: reg0 = player_no
  ("multiplayer_find_player_leader_for_bot",
    [
      (store_script_param, ":team_no", 1),
      (store_script_param, ":look_only_actives", 2),
      
      (team_get_faction, ":team_faction", ":team_no"),
      (assign, ":num_ai_troops", 0),
      (try_for_range, ":cur_ai_troop", multiplayer_ai_troops_begin, multiplayer_ai_troops_end),
        (store_troop_faction, ":ai_troop_faction", ":cur_ai_troop"),
        (eq, ":ai_troop_faction", ":team_faction"),
        (val_add, ":num_ai_troops", 1),
      (try_end),
      
      (call_script, "script_multiplayer_count_players_bots"),
      
      (assign, ":team_player_count", 0),
      
      (get_max_players, ":num_players"),
      (try_for_range, ":cur_player", 0, ":num_players"),
        (assign, ":continue", 0),
        (player_is_active, ":cur_player"),
        (try_begin),
          (eq, ":look_only_actives", 0),
          (assign, ":continue", 1),
        (else_try),
          (neq, ":look_only_actives", 0),
          (player_get_agent_id, ":cur_agent", ":cur_player"),
          (ge, ":cur_agent", 0),
          (agent_is_alive, ":cur_agent"),
          (assign, ":continue", 1),
        (try_end),
        
        (eq, ":continue", 1),
        
        (player_get_team_no, ":player_team", ":cur_player"),
        (eq, ":team_no", ":player_team"),
        (val_add, ":team_player_count", 1),
      (try_end),
      (assign, ":result_leader", -1),
      
      (call_script,"script_mp_get_num_of_bots"),
      (assign,":num_bots_1",reg1),
      (assign,":num_bots_2",reg2),
      (try_begin),
        (gt, ":team_player_count", 0),
        (assign, ":total_bot_count", ":num_bots_1"),
        (try_begin),
          (eq, ":team_no", 1),
          (assign, ":total_bot_count", ":num_bots_2"),
        (try_end),
        (store_div, ":num_bots_for_each_player", ":total_bot_count", ":team_player_count"),
        (store_mul, ":check_remainder", ":num_bots_for_each_player", ":team_player_count"),
        (try_begin),
          (lt, ":check_remainder", ":total_bot_count"),
          (val_add, ":num_bots_for_each_player", 1),
        (try_end),
        
        (assign, ":total_bot_req", 0),
        (try_for_range, ":cur_player", 0, ":num_players"),
          (player_is_active, ":cur_player"),
          
          (player_get_agent_id, ":cur_agent", ":cur_player"),
          (ge, ":cur_agent", 0),
          (agent_is_alive, ":cur_agent"),
          
          (player_get_team_no, ":player_team", ":cur_player"),
          (eq, ":team_no", ":player_team"),
          (assign, ":ai_wanted", 0),
          (store_add, ":end_cond", slot_player_bot_type_1_wanted, ":num_ai_troops"),
          (try_for_range, ":bot_type_wanted_slot", slot_player_bot_type_1_wanted, ":end_cond"),
            (player_slot_ge, ":cur_player", ":bot_type_wanted_slot", 1),
            (assign, ":ai_wanted", 1),
            (assign, ":end_cond", 0), #break
          (try_end),
          (eq, ":ai_wanted", 1),
          (player_get_slot, ":player_bot_count", ":cur_player", slot_player_last_bot_count),
          (lt, ":player_bot_count", ":num_bots_for_each_player"),
          (val_add, ":total_bot_req", ":num_bots_for_each_player"),
          (val_sub, ":total_bot_req", ":player_bot_count"),
        (try_end),
        (gt, ":total_bot_req", 0),
        
        (store_random_in_range, ":random_bot", 0, ":total_bot_req"),
        (try_for_range, ":cur_player", 0, ":num_players"),
          (player_is_active, ":cur_player"),
          
          (player_get_agent_id, ":cur_agent", ":cur_player"),
          (ge, ":cur_agent", 0),
          (agent_is_alive, ":cur_agent"),
          
          (player_get_team_no, ":player_team", ":cur_player"),
          (eq, ":team_no", ":player_team"),
          (assign, ":ai_wanted", 0),
          (store_add, ":end_cond", slot_player_bot_type_1_wanted, ":num_ai_troops"),
          (try_for_range, ":bot_type_wanted_slot", slot_player_bot_type_1_wanted, ":end_cond"),
            (player_slot_ge, ":cur_player", ":bot_type_wanted_slot", 1),
            (assign, ":ai_wanted", 1),
            (assign, ":end_cond", 0), #break
          (try_end),
          (eq, ":ai_wanted", 1),
          (player_get_slot, ":player_bot_count", ":cur_player", slot_player_last_bot_count),
          (lt, ":player_bot_count", ":num_bots_for_each_player"),
          (val_sub, ":random_bot", ":num_bots_for_each_player"),
          (val_add, ":random_bot", ":player_bot_count"),
          (lt, ":random_bot", 0),
          (assign, ":result_leader", ":cur_player"),
          (assign, ":num_players", 0), #break
        (try_end),
      (try_end),
      (assign, reg0, ":result_leader"),
  ]),
  
  # script_multiplayer_find_bot_troop_and_group_for_spawn
  # Input: arg1 = team_no
  # Output: reg0 = troop_id, reg1 = group_id
  ("multiplayer_find_bot_troop_and_group_for_spawn",
    [
      (store_script_param, ":team_no", 1),
      (store_script_param, ":look_only_actives", 2),
      
      (assign, ":selected_troop", -1),
      (call_script, "script_multiplayer_find_player_leader_for_bot", ":team_no", ":look_only_actives"),
      (assign, ":leader_player", reg0),
      
      (assign, ":available_troops_in_faction", 0),
      (assign, ":available_troops_to_spawn", 0),
      (team_get_faction, ":team_faction_no", ":team_no"),
      
      (call_script,"script_mp_get_num_of_bots"),
      (assign,":num_bots_1",reg1),
      (assign,":num_bots_2",reg2),
      (try_begin),
        (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_raid),
        (eq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle),
        (assign,":priests_spawned",0),
        (assign,":flagbearers_spawned",0),
        (assign,":max",0),
      (else_try),
        (try_begin),
          (eq,":team_no",0),
          (assign,":flagbearers_spawned","$g_multiplayer_team_0_flagbearers_spawned"),
          (assign,":priests_spawned","$g_multiplayer_team_0_priests_spawned"),
          (store_div,":max",":num_bots_1",8),
          (val_max,":max",1),
        (else_try),
          (assign,":flagbearers_spawned","$g_multiplayer_team_1_flagbearers_spawned"),
          (assign,":priests_spawned","$g_multiplayer_team_1_priests_spawned"),
          (store_div,":max",":num_bots_2",8),
          (val_max,":max",1),
        (try_end),
        
        (assign,":num_players",0),
        (try_for_players,":player"),
          (player_is_active,":player"),
          (player_get_team_no,":player_team",":player"),
          (eq,":team_no",":player_team"),
          (val_add,":num_players",1),
        (try_end),
        (val_div,":num_players",6),
        (val_add,":max",":num_players"),
      (try_end),
      
      (try_for_range, ":troop_no", multiplayer_ai_troops_begin, multiplayer_ai_troops_end),
        (store_troop_faction, ":troop_faction", ":troop_no"),
        (eq, ":troop_faction", ":team_faction_no"),
        
        (assign,":stop",0),
        (try_begin),
          (call_script,"script_cf_is_priest",":troop_no"),
          (assign,":stop",1),
          (try_begin),
            (lt,":priests_spawned",":max"),
            (assign,":stop",-1),
          (try_end),
        (else_try),
          (call_script,"script_cf_is_standard_bearer",":troop_no"),
          (assign,":stop",1),
          (try_begin),
            (lt,":flagbearers_spawned",":max"),
            (assign,":stop",-1),
          (try_end),
        (else_try),
          (eq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle),
          (assign,":stop",1),
          (try_begin),
            (call_script,"script_cf_lbt_skip_weak_infantry_troops",":troop_no"),
            (assign,":stop",0),
          (try_end),
        (try_end),
        (le,":stop",0),
        
        (store_add, ":wanted_slot", slot_player_bot_type_1_wanted, ":available_troops_in_faction"),
        (val_add, ":available_troops_in_faction", 1),
        (try_begin),
          (this_or_next|lt, ":leader_player", 0),
          (this_or_next|eq,":stop",-1),
          (player_slot_ge, ":leader_player", ":wanted_slot", 1),
          (val_add, ":available_troops_to_spawn", 1),
        (try_end),
      (try_end),
      
      (assign, ":available_troops_in_faction", 0),
      
      (store_random_in_range, ":random_troop_index", 0, ":available_troops_to_spawn"),
      (assign, ":end_cond", multiplayer_ai_troops_end),
      (try_for_range, ":troop_no", multiplayer_ai_troops_begin, ":end_cond"),
        (store_troop_faction, ":troop_faction", ":troop_no"),
        (eq, ":troop_faction", ":team_faction_no"),
        
        (assign,":stop",0),
        (try_begin),
          (call_script,"script_cf_is_priest",":troop_no"),
          (assign,":stop",1),
          (try_begin),
            (lt,":priests_spawned",":max"),
            (assign,":stop",-1),
          (try_end),
        (else_try),
          (call_script,"script_cf_is_standard_bearer",":troop_no"),
          (assign,":stop",1),
          (try_begin),
            (lt,":flagbearers_spawned",":max"),
            (assign,":stop",-1),
          (try_end),
        (else_try),
          (eq, "$g_multiplayer_game_type", multiplayer_game_type_lords_battle),
          (assign,":stop",1),
          (try_begin),
            (call_script,"script_cf_lbt_skip_weak_infantry_troops",":troop_no"),
            (assign,":stop",0),
          (try_end),
        (try_end),
        (le,":stop",0),
        
        (store_add, ":wanted_slot", slot_player_bot_type_1_wanted, ":available_troops_in_faction"),
        (val_add, ":available_troops_in_faction", 1),
        (this_or_next|lt, ":leader_player", 0),
        (this_or_next|eq,":stop",-1),
        (player_slot_ge, ":leader_player", ":wanted_slot", 1),
        (val_sub, ":random_troop_index", 1),
        (lt, ":random_troop_index", 0),
        (assign, ":end_cond", 0),
        (assign, ":selected_troop", ":troop_no"),
      (try_end),
      
      
      (try_begin),
        (call_script,"script_cf_is_priest",":selected_troop"),
        (try_begin),
          (eq,":team_no",0),
          (val_add,"$g_multiplayer_team_0_priests_spawned",1),
        (else_try),
          (val_add,"$g_multiplayer_team_1_priests_spawned",1),
        (try_end),
      (else_try),
        (call_script,"script_cf_is_standard_bearer",":selected_troop"),
        (try_begin),
          (eq,":team_no",0),
          (val_add,"$g_multiplayer_team_0_flagbearers_spawned",1),
        (else_try),
          (val_add,"$g_multiplayer_team_1_flagbearers_spawned",1),
        (try_end),
      (try_end),
      
      (assign, reg0, ":selected_troop"),
      (assign, reg1, ":leader_player"),
  ]),
  
  # script_multiplayer_change_leader_of_bot
  # Input: arg1 = agent_no
  # Output: none
  ("multiplayer_change_leader_of_bot",
    [
      (store_script_param, ":agent_no", 1),
      (agent_get_team, ":team_no", ":agent_no"),
      (call_script, "script_multiplayer_find_player_leader_for_bot", ":team_no", 1),
      (assign, ":leader_player", reg0),
      (agent_set_group, ":agent_no", ":leader_player"),
  ]),
  
  ("multiplayer_find_spawn_point",
    [
      (store_script_param, ":team_no", 1),
      (store_script_param, ":examine_all_spawn_points", 2), #0-dm, 1-tdm, 2-cf, 3-hq, 4-sg
      (store_script_param, ":is_horseman", 3), #0:no, 1:yes, -1:do not care
      
      (set_fixed_point_multiplier, 100),
      
      (assign, ":flags", 0),
      
      (try_begin),
        (eq, ":examine_all_spawn_points", 1),
        (val_or, ":flags", spf_examine_all_spawn_points),
      (try_end),
      
      (try_begin),
        (eq, ":is_horseman", 1),
        (val_or, ":flags", spf_is_horseman),
      (try_end),
      
      (try_begin),
        (eq, "$g_multiplayer_game_type", multiplayer_game_type_deathmatch),
        (eq, "$g_multiplayer_game_type", multiplayer_game_type_duel),
        (val_or, ":flags", spf_all_teams_are_enemy),
        (val_or, ":flags", spf_try_to_spawn_close_to_at_least_one_enemy),
      (else_try),
        (eq, "$g_multiplayer_game_type", multiplayer_game_type_team_deathmatch),
        (val_or, ":flags", spf_try_to_spawn_close_to_at_least_one_enemy),
      (else_try),
        (eq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
        (val_or, ":flags", spf_team_1_spawn_far_from_entry_66), #team 1 agents will not spawn 70 meters around of entry 0
        (val_or, ":flags", spf_team_0_walkers_spawn_at_high_points),
        (val_or, ":flags", spf_team_0_spawn_near_entry_66),
        (val_or, ":flags", spf_care_agent_to_agent_distances_less),
      (try_end),
      
      (multiplayer_find_spawn_point, reg0, ":team_no", ":flags"),
  ]),
  
  # script_multiplayer_find_spawn_point_2
  # Input: arg1 = team_no, arg2 = examine_all_spawn_points, arg3 = is_horseman
  # Output: reg0 = entry_point_no
  # ("multiplayer_find_spawn_point_2",
    # [
      # (store_script_param, ":team_no", 1),
      # (store_script_param, ":examine_all_spawn_points", 2), #0-dm, 1-tdm, 2-cf, 3-hq, 4-sg
      # (store_script_param, ":is_horseman", 3), #0:no, 1:yes, -1:do not care
      
      # (assign, ":best_entry_point_score", -10000000),
      # (assign, ":best_entry_point", 0),
      
      # (assign, ":num_operations", 0),
      
      # (assign, ":num_human_agents_div_3_plus_one", 0),
      # (assign, ":num_human_agents_plus_one", ":num_human_agents_div_3_plus_one"),
      
      # (try_begin),
        # (le, ":num_human_agents_plus_one", 4),
        # (assign, ":random_number_upper_limit", 2), #this is not typo-mistake this should be 2 too, not 1.
      # (else_try),
        # (le, ":num_human_agents_plus_one", 8),
        # (assign, ":random_number_upper_limit", 2),
      # (else_try),
        # (le, ":num_human_agents_plus_one", 16),
        # (assign, ":random_number_upper_limit", 3),
      # (else_try),
        # (le, ":num_human_agents_plus_one", 24),
        # (assign, ":random_number_upper_limit", 4),
      # (else_try),
        # (le, ":num_human_agents_plus_one", 32),
        # (assign, ":random_number_upper_limit", 5),
      # (else_try),
        # (le, ":num_human_agents_plus_one", 40),
        # (assign, ":random_number_upper_limit", 6),
      # (else_try),
        # (assign, ":random_number_upper_limit", 7),
      # (try_end),
      
      # (val_div, ":num_human_agents_div_3_plus_one", 3),
      # (val_add, ":num_human_agents_div_3_plus_one", 1),
      # (store_mul, ":negative_num_human_agents_div_3_plus_one", ":num_human_agents_div_3_plus_one", -1),
      
      # (try_begin),
        # (eq, ":examine_all_spawn_points", 1),
        # (assign, ":random_number_upper_limit", 1),
      # (try_end),
      
      # (assign, ":first_agent", 0),
      # (try_begin), #first spawned agents will be spawned at their base points in tdm, cf and hq mods.
        # (eq, "$g_multiplayer_game_type", multiplayer_game_type_team_deathmatch),
        # (try_begin),
          # (eq, ":team_no", 0),
          # (eq, "$g_multiplayer_team_1_first_spawn", 1),
          # (assign, ":first_agent", 1),
          # (assign, "$g_multiplayer_team_1_first_spawn", 0),
        # (else_try),
          # (eq, ":team_no", 1),
          # (eq, "$g_multiplayer_team_2_first_spawn", 1),
          # (assign, ":first_agent", 1),
          # (assign, "$g_multiplayer_team_2_first_spawn", 0),
        # (try_end),
      # (try_end),
      
      # (try_begin),
        # (eq, ":first_agent", 1),
        # (store_mul, ":best_entry_point", ":team_no", multi_num_valid_entry_points_div_2),
      # (else_try),
        # (try_for_range, ":i_entry_point", 0, multi_num_valid_entry_points),
          # (assign, ":minimum_enemy_distance", 3000),
          # (assign, ":second_minimum_enemy_distance", 3000),
          
          # (assign, ":entry_point_score", 0),
          # (store_random_in_range, ":random_value", 0, ":random_number_upper_limit"), #in average it is 5
          # (eq, ":random_value", 0),
          # (entry_point_get_position, pos0, ":i_entry_point"), #pos0 holds current entry point position
          # (try_for_agents, ":i_agent"),
            # (agent_is_alive, ":i_agent"),
            # (agent_is_human, ":i_agent"),
            # (agent_get_team, ":agent_team", ":i_agent"),
            # (try_begin),
              # (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_team_deathmatch),
              # (eq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
              # (try_begin),
                # (teams_are_enemies, ":team_no", ":agent_team"),
                # (assign, ":multiplier", -2),
              # (else_try),
                # (assign, ":multiplier", 1),
              # (try_end),
            # (else_try),
              # (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_thor),
              # (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_deathmatch),
              # (eq, "$g_multiplayer_game_type", multiplayer_game_type_duel),
              # (assign, ":multiplier", -1),
            # (try_end),
            # (agent_get_position, pos1, ":i_agent"),
            # (get_distance_between_positions_in_meters, ":distance", pos0, pos1),
            # (val_add, ":num_operations", 1),
            # (try_begin),
              # (try_begin), #find closest enemy soldiers
                # (lt, ":multiplier", 0),
                # (try_begin),
                  # (lt, ":distance", ":minimum_enemy_distance"),
                  # (assign, ":second_minimum_enemy_distance", ":minimum_enemy_distance"),
                  # (assign, ":minimum_enemy_distance", ":distance"),
                # (else_try),
                  # (lt, ":distance", ":second_minimum_enemy_distance"),
                  # (assign, ":second_minimum_enemy_distance", ":distance"),
                # (try_end),
              # (try_end),
              
              # (lt, ":distance", 100),
              # (try_begin), #do not spawn over or too near to another agent (limit is 2 meters, squared 4 meters)
                # (lt, ":distance", 3),
                # (try_begin),
                  # (this_or_next|eq, ":examine_all_spawn_points", 0),
                  # (this_or_next|lt, ":multiplier", 0), #new added 20.08.08
                  # (neq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
                  # (try_begin),
                    # (lt, ":distance", 1),
                    # (assign, ":dist_point", -1000000), #never place
                  # (else_try),
                    # (lt, ":distance", 2),
                    # (try_begin),
                      # (lt, ":multiplier", 0),
                      # (assign, ":dist_point", -20000),
                    # (else_try),
                      # (assign, ":dist_point", -2000), #can place, friend and distance is between 1-2 meters
                    # (try_end),
                  # (else_try),
                    # (try_begin),
                      # (lt, ":multiplier", 0),
                      # (assign, ":dist_point", -10000),
                    # (else_try),
                      # (assign, ":dist_point", -1000), #can place, friend and distance is between 2-3 meters
                    # (try_end),
                  # (try_end),
                # (else_try),
                  # #if examinining all spawn points and mod is siege only. This happens in new round start placings.
                  # (try_begin),
                    # (lt, ":distance", 1),
                    # (assign, ":dist_point", -20000), #very hard to place distance is < 1 meter
                  # (else_try),
                    # (lt, ":distance", 2),
                    # (assign, ":dist_point", -2000),
                  # (else_try),
                    # (assign, ":dist_point", -1000), #can place, distance is between 2-3 meters
                  # (try_end),
                # (try_end),
                
                # (val_mul, ":dist_point", ":num_human_agents_div_3_plus_one"),
              # (else_try),
                # (assign, ":dist_point", 0),
                # (this_or_next|neq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
                # (this_or_next|lt, ":multiplier", 0),
                # (eq, ":team_no", 1), #only attackers are effected by positive enemy & friend distance at siege mod, defenders only get negative score effect a bit
                
                # (try_begin), #in siege give no positive or negative score to > 40m distance. (6400 = 10000 - 3600(60 * 60))
                  # (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
                  
                  # (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_thor), #new added after moving below part to above
                  # (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_deathmatch), #new added after moving below part to above
                  # (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_duel), #new added after moving below part to above
                  # (eq, "$g_multiplayer_game_type", multiplayer_game_type_team_deathmatch), #new added after moving below part to above
                  
                  # (store_sub, ":dist_point", multiplayer_spawn_min_enemy_dist_limit, ":distance"), #up to 40 meters give (positive(if friend) or negative(if enemy)) points
                  # (val_max, ":dist_point", 0),
                  # (val_mul, ":dist_point", ":dist_point"),
                # (else_try),
                  # (store_mul, ":one_and_half_limit", multiplayer_spawn_min_enemy_dist_limit, 3),
                  # (val_div, ":one_and_half_limit", 2),
                  # (store_sub, ":dist_point", ":one_and_half_limit", ":distance"), #up to 60 meters give (positive(if friend) or negative(if enemy)) points
                  # (val_mul, ":dist_point", ":dist_point"),
                # (try_end),
                
                # (val_mul, ":dist_point", ":multiplier"),
              # (try_end),
              # (val_add, ":entry_point_score", ":dist_point"),
            # (try_end),
          # (try_end),
          
          # (try_begin),
            # (eq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
            # (store_mul, ":max_enabled_agent_distance_score", 1000, ":num_human_agents_div_3_plus_one"),
            # (ge, ":entry_point_score", ":max_enabled_agent_distance_score"),
            # (assign, ":entry_point_score", ":max_enabled_agent_distance_score"),
          # (try_end),
          
          # (try_begin),
            # (neq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
            
            # #(assign, ":minimum_enemy_dist_score", 0), #close also these with displays
            # #(assign, ":second_minimum_enemy_dist_score", 0), #close also these with displays
            # #(assign, reg2, ":minimum_enemy_distance"), #close also these with displays
            # #(assign, reg3, ":second_minimum_enemy_distance"), #close also these with displays
            
            # (try_begin), #if minimum enemy dist score is greater than 40(multiplayer_spawn_above_opt_enemy_dist_point) meters then give negative score
              # (lt, ":minimum_enemy_distance", 3000),
              # (try_begin),
                # (gt, ":minimum_enemy_distance", multiplayer_spawn_above_opt_enemy_dist_point),
                # (val_sub, ":minimum_enemy_distance", multiplayer_spawn_above_opt_enemy_dist_point),
                # (store_mul, ":minimum_enemy_dist_score", ":minimum_enemy_distance", -50),
                # (val_mul, ":minimum_enemy_dist_score", ":num_human_agents_div_3_plus_one"),
                # (val_add, ":entry_point_score", ":minimum_enemy_dist_score"),
              # (try_end),
            # (try_end),
            
            # (try_begin), #if second minimum enemy dist score is greater than 40(multiplayer_spawn_above_opt_enemy_dist_point) meters then give negative score
              # (lt, ":second_minimum_enemy_distance", 3000), #3000 x 3000
              # (try_begin),
                # (gt, ":second_minimum_enemy_distance", multiplayer_spawn_above_opt_enemy_dist_point),
                # (val_sub, ":second_minimum_enemy_distance", multiplayer_spawn_above_opt_enemy_dist_point),
                # (store_mul, ":second_minimum_enemy_dist_score", ":second_minimum_enemy_distance", -50),
                # (val_mul, ":second_minimum_enemy_dist_score", ":num_human_agents_div_3_plus_one"),
                # (val_add, ":entry_point_score", ":second_minimum_enemy_dist_score"),
              # (try_end),
            # (try_end),
            
            # #(assign, reg0, ":minimum_enemy_dist_score"), #close also above assignment lines with these displays
            # #(assign, reg1, ":second_minimum_enemy_dist_score"), #close also above assignment lines with these displays
            # #(display_message, "@{!}minimum enemy distance : {reg2}, score : {reg0}"), #close also above assignment lines with these displays
            # #(display_message, "@{!}second minimum enemy distance : {reg3}, score : {reg1}"), #close also above assignment lines with these displays
          # (try_end),
          
          # (try_begin), #giving positive points for "distance of entry point position to ground" while searching for entry point for defender team
            # (neq, ":is_horseman", -1), #if being horseman or rider is not (not important)
            
            # #additional score to entry points which has distance to ground value of > 0 meters
            # (position_get_distance_to_terrain, ":height_to_terrain", pos0),
            # (val_max, ":height_to_terrain", 0),
            # (val_min, ":height_to_terrain", 300),
            # (ge, ":height_to_terrain", 40),
            
            # (store_mul, ":height_to_terrain_score", ":height_to_terrain", ":num_human_agents_div_3_plus_one"), #it was 8
            
            # (try_begin),
              # (eq, "$g_multiplayer_game_type", multiplayer_game_type_team_deathmatch),
              # (val_mul, ":height_to_terrain_score", 16),
            # (else_try),
              # (val_mul, ":height_to_terrain_score", 4),
            # (try_end),
            
            # (try_begin),
              # (eq, ":is_horseman", 0),
              # (try_begin),
                # (eq, "$g_multiplayer_game_type", multiplayer_game_type_siege), #but only in siege mod, defender infantries will get positive points for spawning in high places.
                # (eq, ":team_no", 0),
                # (val_add, ":entry_point_score", ":height_to_terrain_score"),
              # (try_end),
            # (else_try),
              # (val_mul, ":height_to_terrain_score", 5),
              # (val_sub, ":entry_point_score", ":height_to_terrain_score"),
            # (try_end),
          # (try_end),
          
          # (try_begin), #additional random entry point score at deathmatch, teamdethmatch, capture the flag and siege
            # (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
            # (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_thor),
            # (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_deathmatch),
            # (this_or_next|eq, "$g_multiplayer_game_type", multiplayer_game_type_duel),
            # (eq, "$g_multiplayer_game_type", multiplayer_game_type_team_deathmatch),
            # (try_begin),
              # (neq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
              # (store_random_in_range, ":random_value", 0, 400),
            # (else_try),
              # (eq, ":team_no", 1),
              # (store_random_in_range, ":random_value", 0, 600), #siege-attacker
            # (else_try),
              # (store_random_in_range, ":random_value", 0, 200), #siege-defender
            # (try_end),
            # (val_mul, ":random_value", ":num_human_agents_div_3_plus_one"),
            # (val_add, ":entry_point_score", ":random_value"),
          # (try_end),
          
          # (try_begin),
            # (eq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
            
            # (try_begin), #siege
              # (eq, ":team_no", 0),
              # (entry_point_get_position, pos1, multi_siege_flag_point), #our base is at pos1 (it was multi_initial_spawn_point_team_1 changed at v622)
              # (entry_point_get_position, pos2, multi_initial_spawn_point_team_2), #enemy base is at pos2
            # (else_try),
              # (entry_point_get_position, pos1, multi_initial_spawn_point_team_2), #our base is at pos2
              # (entry_point_get_position, pos2, multi_siege_flag_point), #enemy base is at pos1 (it was multi_initial_spawn_point_team_1 changed at v622)
            # (try_end),
            
            # (try_begin),
              # (eq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
              # (position_get_z, ":pos0_z", pos0),
              # (position_set_z, pos1, ":pos0_z"), #make z of our base same with entry point position z
              # (position_set_z, pos2, ":pos0_z"), #make z of enemy base same with entry point position z
            # (try_end),
            
            # (get_sq_distance_between_positions_in_meters, ":sq_dist_to_our_base", pos0, pos1),
            # (get_sq_distance_between_positions_in_meters, ":sq_dist_to_enemy_base", pos0, pos2),
            # (get_distance_between_positions_in_meters, ":dist_to_enemy_base", pos0, pos2),
            
            # #give positive points if this entry point is near to our base.
            # (assign, ":dist_to_our_base_point", 0),
            # (try_begin), #siege (points for being near to base)
              # (lt, ":sq_dist_to_our_base", 10000), #in siege give entry points score until 100m distance is reached
              # (try_begin),
                # (eq, ":team_no", 0),
                # (try_begin),
                  # (lt, ":sq_dist_to_our_base", 2500), #if distance is < 50m in siege give all highest point possible
                  # (assign, ":sq_dist_to_our_base", 0),
                # (else_try),
                  # (val_sub, ":sq_dist_to_our_base", 2500),
                  # (val_mul, ":sq_dist_to_our_base", 2),
                # (try_end),
              # (try_end),
              
              # (store_sub, ":dist_to_our_base_point", 10000, ":sq_dist_to_our_base"),
              
              # #can be (10000 - (10000 - 2500) * 2) = -5000 (for only defenders) so we are adding this loss.
              # (val_add, ":dist_to_our_base_point", 5000), #so score getting from being near to base changes between 0 to 15000
              
              # (try_begin),
                # (eq, ":team_no", 0),
              # (else_try), #in siege mod for attackers being near to base entry point has 45 times less importance
                # (val_div, ":dist_to_our_base_point", 45),
              # (try_end),
              # (val_mul, ":dist_to_our_base_point", ":num_human_agents_div_3_plus_one"),
            # (try_end),
            
            # (val_add, ":entry_point_score", ":dist_to_our_base_point"),
            
            
            # #give negative points if this entry point is near to enemy base.
            # (assign, ":dist_to_enemy_base_point", 0),
            # (try_begin), #capture the flag
              # (this_or_next|neq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
              # (eq, ":team_no", 1),
              
              # (assign, ":dist_to_enemy_base_point", 0),
              
              # (try_begin),
                # (neq, "$g_multiplayer_game_type", multiplayer_game_type_siege),
                
                # (try_begin),
                  # (lt, ":sq_dist_to_enemy_base", 10000),
                  # (store_sub, ":dist_to_enemy_base_point", 10000, ":sq_dist_to_enemy_base"),
                  # (val_div, ":dist_to_enemy_base_point", 4),
                  # (val_mul, ":dist_to_enemy_base_point", ":negative_num_human_agents_div_3_plus_one"),
                # (try_end),
              # (else_try),
                # (val_max, ":dist_to_enemy_base", 60), #<60 meters has all most negative score
                
                # (try_begin),
                  # (eq, ":is_horseman", 1),
                  # (assign, ":optimal_distance", 120),
                # (else_try),
                  # (assign, ":optimal_distance", 80),
                # (try_end),
                
                # (try_begin),
                  # (le, ":dist_to_enemy_base", ":optimal_distance"),
                  # (store_sub, ":dist_to_enemy_base_point", ":optimal_distance", ":dist_to_enemy_base"),
                  # (val_mul, ":dist_to_enemy_base_point", 180), #-3600 max
                # (else_try),
                  # (store_sub, ":dist_to_enemy_base_point", ":dist_to_enemy_base", ":optimal_distance"),
                  # (val_mul, ":dist_to_enemy_base_point", 30), #-unlimited max but lower slope
                # (try_end),
                
                # (val_sub, ":dist_to_enemy_base_point", 600),
                # (val_max, ":dist_to_enemy_base_point", 0),
                
                # (val_mul, ":dist_to_enemy_base_point", ":negative_num_human_agents_div_3_plus_one"),
              # (try_end),
            # (try_end),
            
            # (val_add, ":entry_point_score", ":dist_to_enemy_base_point"),
          # (try_end),
          
          # #(assign, reg1, ":i_entry_point"),
          # #(assign, reg2, ":entry_point_score"),
          # #(display_message, "@{!}entry_no : {reg1} , entry_score : {reg2}"),
          
          # (gt, ":entry_point_score", ":best_entry_point_score"),
          # (assign, ":best_entry_point_score", ":entry_point_score"),
          # (assign, ":best_entry_point", ":i_entry_point"),
        # (try_end),
        
        # #(assign, reg0, ":best_entry_point"),
        # #(assign, reg1, ":best_entry_point_score"),
        # #(assign, reg2, ":num_operations"),
        # #(assign, reg7, ":is_horseman"),
        # #(display_message, "@{!},is horse:{reg7}, best entry:{reg0}, best entry score:{reg1}, num_operations:{reg2}"),
      # (try_end),
      # (assign, reg0, ":best_entry_point"),
  # ]),
  
  #script_multiplayer_buy_agent_equipment
  # Input: arg1 = player_no
  # Output: none
  ("multiplayer_buy_agent_equipment",
    [
      (store_script_param, ":player_no", 1),
      (player_get_troop_id, ":player_troop", ":player_no"),
      (player_get_gold, ":player_gold", ":player_no"),
      (player_get_slot, ":added_gold", ":player_no", slot_player_last_rounds_used_item_earnings),
      (player_set_slot, ":player_no", slot_player_last_rounds_used_item_earnings, 0),
      (val_add, ":player_gold", ":added_gold"),
      (assign, ":armor_bought", 0),
      
      #moving original values to temp slots
      (try_for_range, ":i_item", slot_player_selected_item_indices_begin, slot_player_selected_item_indices_end),
        (player_get_slot, ":selected_item_index", ":player_no", ":i_item"),
        (store_sub, ":i_cur_selected_item", ":i_item", slot_player_selected_item_indices_begin),
        (try_begin),
          (player_item_slot_is_picked_up, ":player_no", ":i_cur_selected_item"),
          (assign, ":selected_item_index", -1),
        (try_end),
        (val_add, ":i_cur_selected_item", slot_player_cur_selected_item_indices_begin),
        (player_set_slot, ":player_no", ":i_cur_selected_item", ":selected_item_index"),
      (try_end),
      (assign, ":end_cond", 1000),
      (try_for_range, ":unused", 0, ":end_cond"),
        (call_script, "script_multiplayer_calculate_cur_selected_items_cost", ":player_no", 0),
        (assign, ":total_cost", reg0),
        (try_begin),
          (gt, ":total_cost", ":player_gold"),
          #downgrade one of the selected items
          #first normalize the prices
          #then prioritize some of the weapon classes for specific troop classes
          (call_script, "script_multiplayer_get_troop_class", ":player_troop"),
          (assign, ":player_troop_class", reg0),
          
          (assign, ":max_cost_value", 0),
          (assign, ":max_cost_value_index", -1),
          (try_for_range, ":i_item", slot_player_cur_selected_item_indices_begin, slot_player_cur_selected_item_indices_end),
            (player_get_slot, ":item_id", ":player_no", ":i_item"),
            (ge, ":item_id", 0), #might be -1 for horses etc.
            (call_script, "script_multiplayer_get_item_value_for_troop", ":item_id", ":player_troop"),
            (assign, ":item_value", reg0),
            (store_sub, ":item_type", ":i_item", slot_player_cur_selected_item_indices_begin),
            (try_begin), #items
              (this_or_next|eq, ":item_type", 0),
              (this_or_next|eq, ":item_type", 1),
              (this_or_next|eq, ":item_type", 2),
              (eq, ":item_type", 3),
              (val_mul, ":item_value", 5),
            (else_try), #head
              (eq, ":item_type", 4),
              (val_mul, ":item_value", 4),
            (else_try), #body
              (eq, ":item_type", 5),
              (val_mul, ":item_value", 2),
            (else_try), #foot
              (eq, ":item_type", 6),
              (val_mul, ":item_value", 8),
            (else_try), #gloves
              (eq, ":item_type", 7),
              (val_mul, ":item_value", 8),
            (else_try), #horse
              #base value (most expensive)
            (try_end),
            (item_get_slot, ":item_class", ":item_id", slot_item_multiplayer_item_class),
            (try_begin),
              (eq, ":player_troop_class", multi_troop_class_infantry),
              (this_or_next|eq, ":item_class", multi_item_class_type_sword),
              (this_or_next|eq, ":item_class", multi_item_class_type_axe),
              (this_or_next|eq, ":item_class", multi_item_class_type_blunt),
              (this_or_next|eq, ":item_class", multi_item_class_type_war_picks),
              (this_or_next|eq, ":item_class", multi_item_class_type_two_handed_sword),
              (this_or_next|eq, ":item_class", multi_item_class_type_small_shield),
              (eq, ":item_class", multi_item_class_type_two_handed_axe),
              (val_div, ":item_value", 2),
            (else_try),
              (eq, ":player_troop_class", multi_troop_class_spearman),
              (this_or_next|eq, ":item_class", multi_item_class_type_spear),
              (eq, ":item_class", multi_item_class_type_large_shield),
              (val_div, ":item_value", 2),
            (else_try),
              (eq, ":player_troop_class", multi_troop_class_cavalry),
              (this_or_next|eq, ":item_class", multi_item_class_type_lance),
              (this_or_next|eq, ":item_class", multi_item_class_type_sword),
              (eq, ":item_class", multi_item_class_type_horse),
              (val_div, ":item_value", 2),
            (else_try),
              (eq, ":player_troop_class", multi_troop_class_archer),
              (this_or_next|eq, ":item_class", multi_item_class_type_bow),
              (eq, ":item_class", multi_item_class_type_arrow),
              (val_div, ":item_value", 2),
            (else_try),
              (eq, ":player_troop_class", multi_troop_class_crossbowman),
              (this_or_next|eq, ":item_class", multi_item_class_type_crossbow),
              (eq, ":item_class", multi_item_class_type_bolt),
              (val_div, ":item_value", 2),
            (else_try),
              (eq, ":player_troop_class", multi_troop_class_mounted_archer),
              (this_or_next|eq, ":item_class", multi_item_class_type_bow),
              (this_or_next|eq, ":item_class", multi_item_class_type_arrow),
              (eq, ":item_class", multi_item_class_type_horse),
              (val_div, ":item_value", 2),
            (else_try),
              (eq, ":player_troop_class", multi_troop_class_mounted_crossbowman),
              (this_or_next|eq, ":item_class", multi_item_class_type_crossbow),
              (this_or_next|eq, ":item_class", multi_item_class_type_bolt),
              (eq, ":item_class", multi_item_class_type_horse),
              (val_div, ":item_value", 2),
            (try_end),
            
            (try_begin),
              (gt, ":item_value", ":max_cost_value"),
              (assign, ":max_cost_value", ":item_value"),
              (assign, ":max_cost_value_index", ":i_item"),
            (try_end),
          (try_end),
          
          #max_cost_value and max_cost_value_index will definitely be valid
          #unless no items are left (therefore some items must cost 0 gold)
          (player_get_slot, ":item_id", ":player_no", ":max_cost_value_index"),
          (call_script, "script_multiplayer_get_previous_item_for_item_and_troop", ":item_id", ":player_troop"),
          (assign, ":item_id", reg0),
          (player_set_slot, ":player_no", ":max_cost_value_index", ":item_id"),
        (else_try),
          (assign, ":end_cond", 0),
          (val_sub, ":player_gold", ":total_cost"),
          (player_set_gold, ":player_no", ":player_gold", multi_max_gold_that_can_be_stored),
          (try_for_range, ":i_item", slot_player_cur_selected_item_indices_begin, slot_player_cur_selected_item_indices_end),
            (player_get_slot, ":item_id", ":player_no", ":i_item"),
            #checking if different class default item replace is needed for weapons
            (try_begin),
              (ge, ":item_id", 0),
              #then do nothing
            (else_try),
              (store_sub, ":base_index_slot", ":i_item", slot_player_cur_selected_item_indices_begin),
              (store_add, ":selected_item_index_slot", ":base_index_slot", slot_player_selected_item_indices_begin),
              (player_get_slot, ":selected_item_index", ":player_no", ":selected_item_index_slot"),
              (this_or_next|eq, ":selected_item_index", -1),
              (player_item_slot_is_picked_up, ":player_no", ":base_index_slot"),
              #then do nothing
            (else_try),
              #an item class without a default value is -1, then find a default weapon
              (item_get_slot, ":item_class", ":selected_item_index", slot_item_multiplayer_item_class),
              (is_between, ":item_class", multi_item_class_type_weapons_begin, multi_item_class_type_weapons_end),
              (assign, ":dc_replaced_item", -1),
              (try_for_range, ":i_dc_item_class", multi_item_class_type_melee_weapons_begin, multi_item_class_type_melee_weapons_end),
                (lt, ":dc_replaced_item", 0),
                (assign, ":dc_item_class_used", 0),
                (try_for_range, ":i_dc_item", slot_player_cur_selected_item_indices_begin, slot_player_cur_selected_item_indices_end),
                  (player_get_slot, ":dc_cur_item", ":player_no", ":i_dc_item"),
                  (ge, ":dc_cur_item", 0),
                  (item_get_slot, ":dc_item_class", ":dc_cur_item", slot_item_multiplayer_item_class),
                  (eq, ":dc_item_class", ":i_dc_item_class"),
                  (assign, ":dc_item_class_used", 1),
                (try_end),
                (eq, ":dc_item_class_used", 0),
                (assign, ":dc_end_cond", all_items_end),
                (try_for_range, ":i_dc_new_item", all_items_begin, ":dc_end_cond"),
                  (item_slot_eq, ":i_dc_new_item", slot_item_multiplayer_item_class, ":i_dc_item_class"),
                  (call_script, "script_cf_multiplayer_is_item_default_for_troop", ":i_dc_new_item", ":player_troop"),
                  (assign, ":dc_end_cond", 0), #break
                  (assign, ":dc_replaced_item", ":i_dc_new_item"),
                (try_end),
              (try_end),
              (ge, ":dc_replaced_item", 0),
              (player_set_slot, ":player_no", ":i_item", ":dc_replaced_item"),
              (assign, ":item_id", ":dc_replaced_item"),
            (try_end),
            
            #finally, add the item to agent
            (try_begin),
              (ge, ":item_id", 0), #might be -1 for horses etc.
              (store_sub, ":item_slot", ":i_item", slot_player_cur_selected_item_indices_begin),
              (player_add_spawn_item, ":player_no", ":item_slot", ":item_id"),
              (try_begin),
                (eq, ":item_slot", ek_body), #ek_body is the slot for armor
                (assign, ":armor_bought", 1),
              (try_end),
            (try_end),
          (try_end),
          
          (player_set_slot, ":player_no", slot_player_total_equipment_value, ":total_cost"),
        (try_end),
      (try_end),
      (try_begin),
        (eq, ":armor_bought", 0),
        (eq, "$g_multiplayer_force_default_armor", 1),
        (assign, ":end_cond", all_items_end),
        (try_for_range, ":i_new_item", all_items_begin, ":end_cond"),
          (this_or_next|item_slot_eq, ":i_new_item", slot_item_multiplayer_item_class, multi_item_class_type_light_armor),
          (this_or_next|item_slot_eq, ":i_new_item", slot_item_multiplayer_item_class, multi_item_class_type_medium_armor),
          (item_slot_eq, ":i_new_item", slot_item_multiplayer_item_class, multi_item_class_type_heavy_armor),
          (call_script, "script_cf_multiplayer_is_item_default_for_troop", ":i_new_item", ":player_troop"),
          (assign, ":end_cond", 0), #break
          (player_add_spawn_item, ":player_no", ek_body, ":i_new_item"), #ek_body is the slot for armor
        (try_end),
      (try_end),
  ]),
  
  # script_party_get_ideal_size @used for NPC parties.
  # Input: arg1 = party_no
  # Output: reg0: ideal size
  ("party_get_ideal_size",
    [
      (store_script_param_1, ":party_no"),
      
      #default limit is 30 for any party
      (assign, ":limit", 30),
      
      (try_begin),
        (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
        (party_stack_get_troop_id, ":party_leader", ":party_no", 0),
        (store_faction_of_party, ":faction_id", ":party_no"),
        
        #default limit is 20 for kingdom lords
        (try_begin),
          (neq, ":faction_id", "fac_adventurers"),
          (assign, ":limit", 20),
        (try_end),
        
        #each (leadership level) gives 5 to limit
        (store_skill_level, ":skill", "skl_leadership", ":party_leader"),
        (store_attribute_level, ":charisma", ":party_leader", ca_charisma),
        (val_mul, ":skill", 5),
        (val_add, ":limit", ":skill"),
        
        #each (charisma level) gives 1 to limit
        (val_add, ":limit", ":charisma"),
        
        #each (9 renown) gives 1 to limit #same that player chief
        (troop_get_slot, ":troop_renown", ":party_leader", slot_troop_renown),
        (store_div, ":renown_bonus", ":troop_renown", 9), #VC use 9. Renown is important but no the key
        (val_add, ":limit", ":renown_bonus"), #common renown in lords and kings enter 1000 and 1500 = 166 men
        
        #if this party is faction leader it takes additional 100 limit #possesions give bonus
        (try_begin),
          (neq,":faction_id", "fac_adventurers"),
          (faction_slot_eq, ":faction_id", slot_faction_leader, ":party_leader"),
          (faction_get_slot,":num_castles",":faction_id",slot_faction_num_castles),
          (faction_get_slot,":num_towns",":faction_id",slot_faction_num_towns),
          (val_mul,":num_castles",10),
          (val_mul,":num_towns",20),
          (val_add,":num_towns",":num_castles"),
          (val_add, ":limit", ":num_towns"),
          # (val_add, ":limit", 100),
        (try_end),
        
        #if this party is faction marshal it takes additional 20 limit
        (try_begin),
          (neq,":faction_id", "fac_adventurers"),
          (faction_slot_eq, ":faction_id", slot_faction_marshal, ":party_leader"),
          (val_add, ":limit", 20),
        (try_end),
        
        #party takes additional 20 limit per each castle it's party leader owns
        #Brytenwalda based system was renown, but VC prefer propierties -Fryd concept-
        (try_for_range, ":cur_center", walled_centers_begin, walled_centers_end),
          #  (try_for_range, ":cur_center", castles_begin, castles_end),
          (party_slot_eq, ":cur_center", slot_town_lord, ":party_leader"),
          (try_begin), #towns
            (party_slot_eq, ":cur_center", slot_party_type, spt_town),
            (val_add, ":limit", 80),
          (else_try), #castles forts
            (val_add, ":limit", 40),
          (try_end),
        (try_end),
      (try_end),
      
      #if player has level of 0 then ideal limit will be exactly same, if player has level of 80 then ideal limit will be multiplied by 2 ((80 + 80) / 80)
      #below code will increase limits a little as the game progresses and player gains level
      (store_character_level, ":level", "trp_player"),
      (val_min, ":level", 80),
      (store_add, ":level_factor", 80, ":level"),
      (val_mul, ":limit", ":level_factor"),
      (val_div, ":limit", 80),
      (assign, reg0, ":limit"),
  ]),
  
  #script_game_get_party_prisoner_limit:
  # This script is called from the game engine when the prisoner limit is needed for a party.
  # INPUT: arg1 = party_no
  # OUTPUT: reg0 = prisoner_limit
  # This skill depend on troops size now
  # Reworked by JuJu70
  ("game_get_party_prisoner_limit",
    [
      #      (store_script_param_1, ":party_no"),
      (assign, ":troop_no", "trp_player"),
      (store_party_size_wo_prisoners, ":player_party_size", "p_main_party"),
      (store_div, ":adj_size", ":player_party_size", 4),
      (assign, ":limit", 0),
      (store_skill_level, ":skill", "skl_prisoner_management", ":troop_no"),
      (val_sub, ":skill", 1),
      (store_mul, ":limit", ":skill", 9), #chief 9 PRISONERS PER SKILL LEVEL
      (val_add, ":limit", 5), #chief you can handle 5 prisoners even w/o ranks in management
      (val_add, ":limit", ":adj_size"),
      (val_max, ":limit", 5),
      (assign, reg0, ":limit"),
      (set_trigger_result, reg0),
      #      (assign, ":limit", 0),
      #      (store_skill_level, ":skill", "skl_prisoner_management", ":troop_no"),
      #      (store_mul, ":limit", ":skill", 15), #chief 15 PRISONERS PER SKILL LEVEL
      #      (val_add, ":limit", 5), #chief you can handle 5 prisoners even w/o ranks in management
      #      (assign, reg0, ":limit"),
      #      (set_trigger_result, reg0),
  ]),
  
  #script_game_get_item_extra_text:
  # This script is called from the game engine when an item's properties are displayed.
  # INPUT: arg1 = item_no, arg2 = extra_text_id (this can be between 0-7 (7 included)), arg3 = item_modifier
  # OUTPUT: result_string = item extra text, trigger_result = text color (0 for default)
  ("game_get_item_extra_text",
    [
      (store_script_param, ":item_no", 1),
      (store_script_param, ":extra_text_id", 2),
      (store_script_param, ":item_modifier", 3),
      #(item_get_type, ":type", ":item_no"),
      (try_begin),
        (is_between, ":item_no", food_begin, food_end),
        (try_begin),
          (eq, ":extra_text_id", 0),
          (assign, ":continue", 1),
          (try_begin),
            (this_or_next|eq, ":item_no", "itm_cattle_meat"),
            (this_or_next|eq, ":item_no", "itm_pork"),
            (eq, ":item_no", "itm_chicken"),
            (eq, ":item_modifier", imod_rotten),
            (assign, ":continue", 0),
          (try_end),
          (eq, ":continue", 1),
          (item_get_slot, ":food_bonus", ":item_no", slot_item_food_bonus),
          (assign, reg1, ":food_bonus"),
          (set_result_string, "@Grants +{reg1} to party morale"),
          (set_trigger_result, 0x4444FF),
        (try_end),
      (else_try),
        #anadido chief para estandartes
        (is_between, ":item_no", "itm_standard", "itm_horn_multi"),
        (try_begin),
          (eq, ":extra_text_id", 0),
          (set_result_string, "@Increases morale during battle"),
          (set_trigger_result, 0x4444FF),
        (try_end),
      (else_try),
        #anadido chief para estandartes
        #anadido chief para armaduras y yelmos
        (eq, "$g_armor_penalties", 1),
        (this_or_next|is_between, ":item_no", armadura_pesada_begin, armadura_pesada_end),
        (is_between, ":item_no", armadura_pesada2_begin, armadura_pesada2_end),
        (try_begin),
          (eq, ":extra_text_id", 0),
          (set_result_string, "@Heavy Armour: you suffer high penalties for certain skills."),
          (set_trigger_result, 0xE12126),
        (try_end),
      (else_try),
        (eq, "$g_armor_penalties", 1),
        (is_between, ":item_no", armadura_media_begin, armadura_media_end),
        (try_begin),
          (eq, ":extra_text_id", 0),
          (set_result_string, "@Medium Armour: you suffer penalties for certain skills."),
          (set_trigger_result, 0xE12126),
        (try_end),
      (else_try),
        (eq, "$g_armor_penalties", 1),
        (this_or_next|is_between, ":item_no", escudos_pesados_begin, escudos_pesados_end),
        (is_between, ":item_no", yelmos_pesados_begin, yelmos_pesados_end),
        (try_begin),
          (eq, ":extra_text_id", 0),
          (set_result_string, "@Heavy Equipment: you suffer penalties for certain skills."),
          (set_trigger_result, 0xE12126),
        (try_end),
      (else_try),
        #anadido chief para desnudos
        (eq, "$g_armor_penalties", 1),
        (is_between, ":item_no", desnudos_begin, desnudos_end),
        (try_begin),
          (eq, ":extra_text_id", 0),
          (set_result_string, "@Ritual Equipment: Advantages in certain skills"),
          (set_trigger_result, 0xFFEEDD),
        (try_end),
      (else_try),
        #anadido chief para armaduras y yelmos
        ###espada descripcion
        ##				(eq, ":item_no", "itm_sword_viking_1"),
        ##        (try_begin),
        ##          (eq, ":extra_text_id", 0),
        ##          (set_result_string, "@The sword of Rhydderch Hael, one of the thirteen treasures of Britain."),
        ##          (set_trigger_result, 0xFFEEDD),
        ##        (try_end),
        ##      (else_try),
        ##				(eq, ":item_no", "itm_espada_beowulf"),
        ##        (try_begin),
        ##          (eq, ":extra_text_id", 0),
        ##          (set_result_string, "@This is the sword which Unferth gives to Beowulf before the fight with Grendel's mother."),
        ##          (set_trigger_result, 0xFFEEDD),
        ##        (try_end),
        ##      (else_try),
        ###espada chief
        #otros
        (eq, "$g_armor_penalties", 1),
        (eq, ":item_no", "itm_crown1"),
        (try_begin),
          (eq, ":extra_text_id", 0),
          (set_result_string, "@Improves leadership and persuasion."),
          (set_trigger_result, 0x006400),
        (try_end),
      (else_try),
        (is_between, ":item_no", readable_books_begin, readable_books_end),
        (try_begin),
          (eq, ":extra_text_id", 0),
          (item_get_slot, reg1, ":item_no", slot_item_intelligence_requirement),
          (set_result_string, "@Requires {reg1} intelligence to read"),
          (set_trigger_result, 0xE12126),
        (else_try),
          (eq, ":extra_text_id", 1),
          (item_get_slot, ":progress", ":item_no", slot_item_book_reading_progress),
          (val_div, ":progress", 10),
          (assign, reg1, ":progress"),
          (set_result_string, "@Reading Progress: {reg1}%"),
          (set_trigger_result, 0x006400),
        (try_end),
      (else_try),
        (is_between, ":item_no", reference_books_begin, reference_books_end),
        (try_begin),
          (eq, ":extra_text_id", 0),
          (try_begin),
            (eq, ":item_no", "itm_book_wound_treatment_reference"),
            (str_store_string, s1, "@wound treatment"),
          (else_try),
            (eq, ":item_no", "itm_book_training_reference"),
            (str_store_string, s1, "@trainer"),
          (else_try),
            (eq, ":item_no", "itm_book_surgery_reference"),
            (str_store_string, s1, "@surgery"),
          (else_try),
            (eq, ":item_no", "itm_relic"), #chief book
            (str_store_string, s1, "@persuasion"),
          (try_end),
          (set_result_string, "@Gives +1 to {s1} while in inventory"),
          (set_trigger_result, 0x006400),
        (try_end),
        #VC-2393 begin
      (else_try),
        (eq, "$g_armor_penalties", 1),
        (this_or_next|eq, ":item_no", "itm_bonus_tunic_1"),
        (this_or_next|eq, ":item_no", "itm_bonus_tunic_2"),
        (this_or_next|eq, ":item_no", "itm_bonus_tunic_3"),
        (this_or_next|eq, ":item_no", "itm_bonus_dress_1"),
        (this_or_next|eq, ":item_no", "itm_bonus_dress_2"),
        (			  eq, ":item_no", "itm_bonus_dress_3"),
        (eq, ":extra_text_id", 0),
        (set_result_string, "@Special Attire: Bonus in certain skills"),
        (set_trigger_result, 0xEEEE00),
        #VC-2393 end
      (else_try),
        (game_in_multiplayer_mode),
        (is_between,":item_no","itm_pictish_painted1","itm_picts_hoodtunic_11"),
        (neq,":item_no","itm_picts_hoodtunic_04"),
        (neq,":item_no","itm_picts_hoodtunic_06"),
        (eq, ":extra_text_id", 0),
        (set_result_string, "@Bonus to movement speed"),
        (set_trigger_result, 0xaaffaa),
      (try_end),
  ]),
  
  #script_game_on_disembark: (outdated)
  # This script is called from the game engine when the player reaches the shore with a ship.
  # INPUT: pos0 = disembark position
  # OUTPUT: none
  ("game_on_disembark",
    [
      #(jump_to_menu, "mnu_disembark"),	# PHAIAK: I made new trigger for this
      
  ]),
  
  #script_game_context_menu_get_buttons:
  # This script is called from the game engine when the player clicks the right mouse button over a party on the map.
  # INPUT: arg1 = party_no
  # OUTPUT: none, fills the menu buttons
  ("game_context_menu_get_buttons",
    [
      (store_script_param, ":party_no", 1),
      (try_begin),
        (neq, ":party_no", "p_main_party"),
        (context_menu_add_item, "@Move here", cmenu_move),
      (try_end),
      
      (try_begin),
        (is_between, ":party_no", centers_begin, centers_end),
        (context_menu_add_item, "@View notes", 1),
      (else_try),
        (party_get_num_companion_stacks, ":num_stacks", ":party_no"),
        (gt, ":num_stacks", 0),
        (party_stack_get_troop_id, ":troop_no", ":party_no", 0),
        (is_between, ":troop_no", active_npcs_begin, active_npcs_end),
        (context_menu_add_item, "@View notes", 2),
      (try_end),
      
      (try_begin),
        (neq, ":party_no", "p_main_party"),
        (store_faction_of_party, ":party_faction", ":party_no"),
        
        (this_or_next|eq, ":party_faction", "$players_kingdom"),
        (this_or_next|eq, ":party_faction", "fac_player_supporters_faction"),
        (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_caravan),
        
        (neg|is_between, ":party_no", centers_begin, centers_end),
        
        (context_menu_add_item, "@Accompany", cmenu_follow),
      (try_end),
      
      #phaiak begin
      (try_begin),
        (eq, ":party_no", "p_main_party"),
        (party_slot_eq, "p_main_party", slot_party_on_water, 1),
        (map_get_land_position_around_position, pos2, pos1, 4),
        (party_set_position, "p_temp_party", pos2),
        (party_get_current_terrain, ":terrain_type", "p_temp_party"),
        (neq, ":terrain_type", 0),
        (neq, ":terrain_type", 1), #cliffs
        (neq, ":terrain_type", 7),
        (neq, ":terrain_type", 8),
        (context_menu_add_item, "@Find landing point", 3),
      (try_end),
      (try_begin),
        (ge, "$vc_debug_mode", 1),
        (context_menu_add_item, "@{!}party slots", 4),
      (try_end),
  ]),
  
  #script_game_event_context_menu_button_clicked:
  # This script is called from the game engine when the player clicks on a button at the right mouse menu.
  # INPUT: arg1 = party_no, arg2 = button_value
  # OUTPUT: none
  ("game_event_context_menu_button_clicked",
    [(store_script_param, ":party_no", 1),
      (store_script_param, ":button_value", 2),
      (try_begin),
        (eq, ":button_value", 1),
        (change_screen_notes, 3, ":party_no"),
      (else_try),
        (eq, ":button_value", 2),
        (party_stack_get_troop_id, ":troop_no", ":party_no", 0),
        (change_screen_notes, 1, ":troop_no"),
      (else_try),
        #phaiak begin
        (eq, ":button_value", 3),
        (party_get_position, pos1, "p_main_party"),
        (call_script, "script_get_next_land_position", 1),
        (copy_position, pos1, pos2),
        (call_script, "script_get_next_water_position", 1),
		#VC-3653
        (set_fixed_point_multiplier, 1),
        (position_get_x, ":x", pos1),
		# (assign, reg1, ":x"),
		# (assign, reg2, map_min_x),
		# (assign, reg3, map_max_x),
		# (display_message, "@{!}x={reg1} map_min_x={reg2} map_max_x={reg3}"),
        (gt, ":x", map_min_x),
        (lt, ":x", map_max_x),
        (position_get_y, ":y", pos1),
		# (assign, reg1, ":y"),
		# (assign, reg2, map_min_y),
		# (assign, reg3, map_max_y),
		# (display_message, "@{!}y={reg1} map_min_y={reg2} map_max_y={reg3}"),
        (gt, ":y", map_min_y),
        (lt, ":y", map_max_y),
        (set_fixed_point_multiplier, 100),
        #
        (party_set_position, "p_landing_point", pos2),
      (else_try),
        (eq, ":button_value", 4),
        (ge, "$vc_debug_mode", 1),
        (assign, "$temp1", ":party_no"),
        (start_presentation, "prsnt_party_slot_check"),
      (try_end),
  ]),
  
  #script_game_get_skill_modifier_for_troop
  # This script is called from the game engine when a skill's modifiers are needed
  # INPUT: arg1 = troop_no, arg2 = skill_no
  # OUTPUT: trigger_result = modifier_value
  ("game_get_skill_modifier_for_troop",
    [(store_script_param, ":troop_no", 1),
      (store_script_param, ":skill_no", 2),
      
      (assign, ":modifier_value", 0),
      (troop_get_inventory_slot,":cur_armor",":troop_no",ek_body),
      (troop_get_inventory_slot,":cur_helmet",":troop_no",ek_head),
      
      (assign, ":heavy_shield", 0),
      (try_for_range, ":weapon_slot", ek_item_0, ek_head),
        (troop_get_inventory_slot, reg0, ":troop_no", ":weapon_slot"),
        (is_between, reg0, escudos_pesados_begin, escudos_pesados_end),
        (assign, ":heavy_shield", 1),
      (try_end),
        
      (try_begin),
        (eq, ":troop_no", "trp_player"),
        
        #VC-2404 (Use second outfit in second outfit situations)
        (try_begin),
          (is_between, "$g_encountered_party",  centers_begin, centers_end),
          (eq, "$encountered_party_hostile", 0),
          (party_get_battle_opponent,":opponent","$g_encountered_party"),
          (le, ":opponent", 0), #party is not itself involved in a battle
          (call_script, "script_cf_player_use_second_outfit"),
          (troop_get_inventory_slot, ":cur_armor", "trp_pseudo_troop_end", ek_body),
          (troop_get_inventory_slot, ":cur_helmet", "trp_pseudo_troop_end", ek_head),
          
          (assign, ":heavy_shield", 0),
          (try_for_range, ":weapon_slot", ek_item_0, ek_head),
            (troop_get_inventory_slot, reg0, "trp_pseudo_troop_end", ":weapon_slot"),
            (is_between, reg0, escudos_pesados_begin, escudos_pesados_end),
            (assign, ":heavy_shield", 1),
          (try_end),
        (end_try),
        
        (try_begin),
          (eq, ":skill_no", "skl_wound_treatment"),
          (player_has_item, "itm_book_wound_treatment_reference"),
          (val_add, ":modifier_value", 1),
        (else_try),
          (eq, ":skill_no", "skl_trainer"),
          (player_has_item, "itm_book_training_reference"),
          (val_add, ":modifier_value", 1),
          #Chief add more vision with scouts send scouts ok
        (else_try), #
          (eq, ":skill_no", "skl_spotting"),
          (this_or_next|eq, "$g_player_icon_state", pis_ship),
          (eq, "$send_scouts", 1), #scouts ok
          (val_add, ":modifier_value", 4), # 4  This do spotting/scouts important. Max skill 14, 10 norma + 4 scouts
        (else_try),
          (eq, ":skill_no", "skl_spotting"),	#only add dog bonus if there are no scouts
          (ge, "$dog_companion", 1),
          #(party_slot_eq, "p_main_party", slot_party_on_water, 0),
          (val_add, ":modifier_value", 1),
        (else_try),
          (eq, ":skill_no", "skl_tracking"),
          (ge, "$dog_companion", 1),
          #(party_slot_eq, "p_main_party", slot_party_on_water, 0),
          (val_add, ":modifier_value", 1),
        (else_try),
          (eq, ":skill_no", "skl_surgery"),
          (player_has_item, "itm_book_surgery_reference"),
          (val_add, ":modifier_value", 1),
        (else_try), #cathach book chief
          (eq, ":skill_no", "skl_persuasion"),
          (player_has_item, "itm_relic"),
          (val_add, ":modifier_value", 1),
        (try_end),
      (try_end),
      
      #VC-2393 begin
      (troop_get_type, ":is_female", ":troop_no"),
      (val_mod, ":is_female", 2),
      
      (try_begin),
        (neq, ":is_female", 1),
        (eq, "$g_armor_penalties", 1),
        (try_begin),
          (eq, ":skill_no", "skl_persuasion"),
          (eq, ":cur_armor", "itm_bonus_tunic_1"),
          (val_add, ":modifier_value", 1),
        (else_try),
          (eq, ":skill_no", "skl_leadership"),
          (eq, ":cur_armor", "itm_bonus_tunic_2"),
          (val_add, ":modifier_value", 1),
        (else_try),
          (eq, ":skill_no", "skl_trade"),
          (eq, ":cur_armor", "itm_bonus_tunic_3"),
          (val_add, ":modifier_value", 1),
        (try_end),
        
        #female bonus clothes
      (else_try),
        (eq, "$g_armor_penalties", 1),
        (try_begin),
          (eq, ":skill_no", "skl_persuasion"),
          (eq, ":cur_armor", "itm_bonus_dress_1"),
          (val_add, ":modifier_value", 1),
        (else_try),
          (eq, ":skill_no", "skl_leadership"),
          (eq, ":cur_armor", "itm_bonus_dress_2"),
          (val_add, ":modifier_value", 1),
        (else_try),
          (eq, ":skill_no", "skl_trade"),
          (eq, ":cur_armor", "itm_bonus_dress_3"),
          (val_add, ":modifier_value", 1),
        (try_end),
      (try_end),
      
      (try_begin),
        (eq, ":cur_helmet", "itm_crown1"),
        (eq, "$g_armor_penalties", 1),
        (this_or_next|eq, ":skill_no", "skl_leadership"),
        (eq, ":skill_no", "skl_persuasion"),
        (val_add, ":modifier_value", 1),#And suma 1
      (try_end),
      
      #anadido chief mas objetos modificadores
      # Reworked by JuJu70
      (try_begin),
        (eq, "$g_armor_penalties", 1),
        (this_or_next|eq, ":skill_no", "skl_power_strike"),
        (this_or_next|eq, ":skill_no", "skl_power_throw"),
        (this_or_next|eq, ":skill_no", "skl_power_draw"),
        (this_or_next|eq, ":skill_no", "skl_horse_archery"),
        (eq, ":skill_no", "skl_athletics"),
        
        (try_begin),
          (this_or_next|is_between, ":cur_armor", armadura_pesada_begin, armadura_pesada_end),
          (is_between, ":cur_armor", armadura_pesada2_begin, armadura_pesada2_end),
          (try_begin),
            (this_or_next|eq, ":skill_no", "skl_power_strike"),
            (this_or_next|eq, ":skill_no", "skl_power_throw"),
            (this_or_next|eq, ":skill_no", "skl_power_draw"),
            (eq, ":skill_no", "skl_horse_archery"),
            (val_sub, ":modifier_value", 1),#And resta 1
          (else_try),
            (eq, ":skill_no", "skl_athletics"),
            (val_sub, ":modifier_value", 4),#And resta 4
          (try_end),
          
        (else_try),
          (is_between, ":cur_armor", armadura_media_begin, armadura_media_end),
          (try_begin),
            (eq, ":skill_no", "skl_horse_archery"),
            (val_sub, ":modifier_value", 1),#And resta 1
          (else_try),
            (eq, ":skill_no", "skl_athletics"),
            (val_sub, ":modifier_value", 2),#And resta 2 No necesary weight as it is 4 to 6 only.
          (try_end),
          
        (else_try),
          (is_between, ":cur_armor", desnudos_begin, desnudos_end),
          (this_or_next|eq, ":skill_no", "skl_power_strike"),
          (this_or_next|eq, ":skill_no", "skl_power_throw"),
          (this_or_next|eq, ":skill_no", "skl_power_draw"),
          (eq, ":skill_no", "skl_athletics"),
          (val_add, ":modifier_value", 2),#suma 2
        (try_end),
        
        (try_begin),
          (is_between, ":cur_helmet", yelmos_pesados_begin, yelmos_pesados_end),
          (this_or_next|eq, ":skill_no", "skl_power_draw"),
          (eq, ":skill_no", "skl_horse_archery"),
          (val_sub, ":modifier_value", 1),#And resta 1
        (try_end),
        
        (try_begin),
          (neq, ":heavy_shield", 0),
          
          (this_or_next|eq, ":skill_no", "skl_power_draw"),
          (this_or_next|eq, ":skill_no", "skl_athletics"),
          (this_or_next|eq, ":skill_no", "skl_horse_archery"),
          (eq, ":skill_no", "skl_power_throw"),
          (val_sub, ":modifier_value", 1),#And resta 1
        (try_end),
      (try_end),
      
      #chief acaba
      #habilidades aumentadas por tropa chief
      (try_begin),
        (troop_is_hero, ":troop_no"),
        (neg|troop_slot_eq, ":troop_no", slot_troop_occupation, slto_player_companion),
        (assign, ":party", -1),
        (try_begin),
          (main_party_has_troop, ":troop_no"),
          (assign, ":party", "p_main_party"),
        (else_try),
          (troop_get_slot, ":leaded_party", ":troop_no", slot_troop_leaded_party),
          (ge, ":leaded_party", 1), #1 = troop leader yes
          (assign, ":party", ":leaded_party"),
        (try_end),
        
        (ge, ":party", 0),
        (party_is_active, ":party"),
        
        (try_begin),
          (eq, ":skill_no", "skl_tactics"),
          (assign, ":total_valor", 0),
          (party_get_num_companion_stacks, ":num_of_stacks", ":party"),
          
          (try_for_range, ":i", 0, ":num_of_stacks"),
            (party_stack_get_troop_id, ":stack_troop", ":party", ":i"),
            (this_or_next|eq, ":stack_troop", "trp_scotch_musician"),
            (eq, ":stack_troop", "trp_todos_cuerno"),
            
            (party_stack_get_size, ":stack_size", ":party", ":i"),
            (party_stack_get_num_wounded, ":stack_wounded", ":party", ":i"),
            (val_sub, ":stack_size", ":stack_wounded"),
            (val_add, ":total_valor", ":stack_size"),
          (try_end),
          
          (try_begin),
            (gt, ":total_valor", 29),
            (val_add, ":modifier_value", 3),
          (else_try),
            (gt, ":total_valor", 9),
            (val_add, ":modifier_value", 2),
          (else_try),
            (gt, ":total_valor", 0),
            (val_add, ":modifier_value", 1),
          (try_end),
          
        (else_try),
          (eq, ":skill_no", "skl_leadership"),
          (assign, ":total_valor", 0),
          (party_get_num_companion_stacks, ":num_of_stacks", ":party"),
          
          (try_for_range, ":i", 0, ":num_of_stacks"),
            (party_stack_get_troop_id, ":stack_troop", ":party", ":i"),
            (this_or_next|eq, ":stack_troop", "trp_norse_standard_bearer"),
            (this_or_next|eq, ":stack_troop", "trp_briton_standard_bearer"),
            (this_or_next|eq, ":stack_troop", "trp_saxon_standard_bearer"),
            (this_or_next|eq, ":stack_troop", "trp_scotch_standard_bearer"),
            (this_or_next|eq, ":stack_troop", "trp_angle_standard_bearer"),
            (eq, ":stack_troop", "trp_irish_standard_bearer"),
            
            (party_stack_get_size, ":stack_size", ":party", ":i"),
            (party_stack_get_num_wounded, ":stack_wounded", ":party", ":i"),
            (val_sub, ":stack_size", ":stack_wounded"),
            (val_add, ":total_valor", ":stack_size"),
          (try_end),
          
          (try_begin),
            (gt, ":total_valor", 29),
            (val_add, ":modifier_value", 3),
          (else_try),
            (gt, ":total_valor", 19),
            (val_add, ":modifier_value", 2),
          (else_try),
            (gt, ":total_valor", 9),
            (val_add, ":modifier_value", 1),
          (try_end),
          
        (else_try),
          (eq, ":skill_no", "skl_surgery"),
          (eq, ":party", "p_main_party"),
          (eq,"$followers_on",1),
          
          (assign, ":total_valor", 0),
          (party_get_num_companion_stacks, ":num_of_stacks", "p_followers_camp"),
          
          (try_for_range, ":i", 0, ":num_of_stacks"),
            (party_stack_get_troop_id, ":stack_troop", "p_followers_camp", ":i"),
            (this_or_next|eq, ":stack_troop", "trp_norse_priest"),
            (this_or_next|eq, ":stack_troop", "trp_briton_priest"),
            (this_or_next|eq, ":stack_troop", "trp_saxon_priest"),
            (this_or_next|eq, ":stack_troop", "trp_scotch_priest"),
            (this_or_next|eq, ":stack_troop", "trp_angle_priest"),
            (eq, ":stack_troop", "trp_irish_priest"),
            
            (party_stack_get_size, ":stack_size", "p_followers_camp", ":i"),
            (party_stack_get_num_wounded, ":stack_wounded", "p_followers_camp", ":i"),
            (val_sub, ":stack_size", ":stack_wounded"),
            (val_add, ":total_valor", ":stack_size"),
          (try_end),
          (try_begin),
            (gt, ":total_valor", 29),
            (val_add, ":modifier_value", 3),
          (else_try),
            (gt, ":total_valor", 9),
            (val_add, ":modifier_value", 2),
          (else_try),
            (gt, ":total_valor", 0),
            (val_add, ":modifier_value", 1),
          (try_end),
          
        (else_try),
          (eq, ":skill_no", "skl_wound_treatment"),
          (eq, ":party", "p_main_party"),
          (eq,"$followers_on",1),
          
          (assign, ":total_valor", 0),
          (party_get_num_companion_stacks, ":num_of_stacks", "p_followers_camp"),
          
          (try_for_range, ":i", 0, ":num_of_stacks"),
            (party_stack_get_troop_id, ":stack_troop", "p_followers_camp", ":i"),
            (is_between, ":stack_troop", "trp_follower_woman", "trp_refugeeromanruins"),
            
            (party_stack_get_size, ":stack_size", "p_followers_camp", ":i"),
            (party_stack_get_num_wounded, ":stack_wounded", "p_followers_camp", ":i"),
            (val_sub, ":stack_size", ":stack_wounded"),
            (val_add, ":total_valor", ":stack_size"),
          (try_end),
          
          (try_begin),
            (gt, ":total_valor", 69),
            (val_add, ":modifier_value", 3),
          (else_try),
            (gt, ":total_valor", 39),
            (val_add, ":modifier_value", 2),
          (else_try),
            (gt, ":total_valor", 19),
            (val_add, ":modifier_value", 1),
          (try_end),
        (try_end),
      (try_end),
      
      (set_trigger_result, ":modifier_value"),
  ]),
  
  #script_count_troops_inparty
  ("count_troops_inparty",
    [
      (store_script_param_1, ":party_no"),
      (store_script_param_2, ":troop_no"),
      
      (assign, ":stack_size", 0),
      (party_get_num_companion_stacks, ":num_of_stacks", ":party_no"),
      (try_for_range, ":i", 0, ":num_of_stacks"),
        (party_stack_get_troop_id, ":stack_troop", ":party_no", ":i"),
        (eq, ":stack_troop", ":troop_no"),
        (party_stack_get_size, ":stack_size", ":party_no", ":i"),
        (party_stack_get_num_wounded, ":stack_wounded", ":party_no", ":i"),
        (val_sub, ":stack_size", ":stack_wounded"),
        (assign, ":num_of_stacks", 0), # loop
      (try_end),
      (assign, reg0, ":stack_size"),
  ]),
  
  # Note to modders: Uncomment these if you'd like to use the following.
  ##  #script_game_check_party_sees_party
  ##  # This script is called from the game engine when a party is inside the range of another party
  ##  # INPUT: arg1 = party_no_seer, arg2 = party_no_seen
  ##  # OUTPUT: trigger_result = true or false (1 = true, 0 = false)
  ##  ("game_check_party_sees_party",
  ##   [
  ##     (store_script_param, ":party_no_seer", 1), #party
  ##     (store_script_param, ":party_no_seen", 2), #target
  ##
  ##		(assign, ":seen_result", 1),
  ###mejor ai chief
  ##                  (try_begin),
  ##			(party_slot_eq, ":party_no_seer", slot_party_type, spt_kingdom_hero_party),
  ##			(this_or_next|party_slot_eq, ":party_no_seer", slot_party_ai_state, spai_besieging_center),
  ##			#(this_or_next|party_slot_eq, ":party_no_seer", slot_party_ai_state, spai_raiding_around_center),
  ##			#(this_or_next|party_slot_eq, ":party_no_seer", slot_party_ai_state, spai_engaging_army),
  ##			(party_slot_eq, ":party_no_seer", slot_party_ai_state, spai_accompanying_army),
  ##			#(party_slot_eq, ":party_no_seer", slot_party_ai_state, spai_screening_army),
  ##                        ### this should advoid lords run away follow other party
  ##                         (assign, ":seen_result", 0),
  ##		(try_end),
  ##      		(set_trigger_result, ":seen_result"),
  ##
  ##    # (set_trigger_result, 1),
  ##    ]),
  
  
  #script_game_get_party_speed_multiplier (Note: I (Phaiak) have uncomment this...
  # This script is called from the game engine when a skill's modifiers are needed
  # INPUT: arg1 = party_no
  # OUTPUT: trigger_result = multiplier (scaled by 100, meaning that giving 100 as the trigger result does not change the party speed)
  ("game_get_party_speed_multiplier",
    [
      
      (store_script_param, ":party_no", 1),
      (party_get_current_terrain, ":cur_terrain", ":party_no"),
      (try_begin),
        # PLAYER
        (eq, ":party_no", "p_main_party"),
        
        (try_begin),
          #on water
          (this_or_next|eq,":cur_terrain",rt_bridge),
          (this_or_next|eq,":cur_terrain",rt_water),
          (eq,":cur_terrain",rt_river),
          (try_begin),
            #no ship
            (party_slot_eq, ":party_no", slot_party_on_water, 0),
            #(display_message, "@Player on water without ship"),
            (set_trigger_result, 1),
            (party_get_position, pos1, ":party_no"),
            (rest_for_hours, 1, 1, 0),
            (rest_for_hours, 0, 0, 0),
            (set_fixed_point_multiplier, 100),
            (try_begin),
              (assign, ":min_dist", 250),
              (try_for_range, ":cur_ferry_station", "p_ferry_1a", "p_jetty_1"),	#use ferry
                (party_get_position, pos3, ":cur_ferry_station"),
                (get_distance_between_positions, ":dist", pos1, pos3),
                (le, ":dist", ":min_dist"),
                (assign, ":min_dist", ":dist"),
                (party_get_position, pos2, ":cur_ferry_station"),
                (assign, "$auto_enter_town", ":cur_ferry_station"),
              (end_try),
              (le, "$auto_enter_town", 0),
              (try_for_range, ":cur_town", towns_begin, towns_end),	#use port
                (party_slot_eq,":cur_town",slot_town_port, 1),
                (party_get_position, pos3, ":cur_town"),
                (get_distance_between_positions, ":dist", pos1, pos3),
                (le, ":dist", 350),
                (party_get_position, pos2, ":cur_town"),
                (assign, "$auto_enter_town", ":cur_town"),
              (end_try),
              (le, "$auto_enter_town", 0),
              (call_script, "script_get_next_land_position", 1),
            (end_try),
            (party_set_position, "p_main_party", pos2),
          (else_try),
            #with ship
            (call_script, "script_calculate_party_speed_water", ":party_no"),
            (set_trigger_result, reg1), # speed
            # (party_set_slot, ":party_no", slot_party_on_water, 1),
            # (call_script, "script_update_party_icon", ":party_no"),
          (end_try),
        (else_try),
          #on land
          (try_begin),
            #no ship
            (party_slot_eq, ":party_no", slot_party_on_water, 0),
            (try_begin),
              (party_get_skill_level, ":speed_multiplier", ":party_no", skl_pathfinding), #skills is important now
              (ge,":speed_multiplier",1),
              (val_mul,":speed_multiplier",2),
              (val_add,":speed_multiplier",100),
            (else_try),
              (assign,":speed_multiplier",100),
            (try_end),
            (try_begin),
              (eq,"$followers_on",1), # followers on = less speed
              (val_sub,":speed_multiplier", 30),
            (try_end),
            (val_max, ":speed_multiplier", 0),
            (set_trigger_result, ":speed_multiplier"),
            
          (else_try),
            #with ship
            (party_slot_eq, ":party_no", slot_party_on_water, 1),
            # (party_set_slot, ":party_no", slot_party_on_water, -1),
            (set_trigger_result, 1),
            (party_get_position, pos1, ":party_no"),
            (call_script, "script_get_next_water_position", 1),
            (party_set_position, "p_main_party", pos2),
            (rest_for_hours, 0, 0, 0),
          (end_try),
        (end_try),
        
        # ADDITION FOR MAP BORDERS (VC-649)
        (set_fixed_point_multiplier, 1),
        (party_get_position, pos1, ":party_no"),
        (position_get_x, ":x", pos1),
        (position_get_y, ":y", pos1),
        (store_add, ":map_max_x_plus", map_max_x, 25),	#fixing VC-1400
        (store_add, ":map_max_y_plus", map_max_y, 15),
        (store_add, ":map_min_y_plus", map_min_y, -6),
        
        (try_begin),
          (gt, ":x", ":map_max_x_plus"),
          (position_set_x, pos1, ":map_max_x_plus"),
          (party_set_position, "p_main_party", pos1),
          (rest_for_hours, 0, 0, 0),
        (else_try),
          (gt, ":y", ":map_max_y_plus"),
          (position_set_y, pos1, ":map_max_y_plus"),
          (party_set_position, "p_main_party", pos1),
          (rest_for_hours, 0, 0, 0),
        (else_try),
          (lt, ":y", ":map_min_y_plus"),
          (position_set_y, pos1, ":map_min_y_plus"),
          (party_set_position, "p_main_party", pos1),
          (rest_for_hours, 0, 0, 0),
        (end_try),
        
      (else_try),
        # QUEST
        (eq, ":party_no", "p_the_fleet"),
        (set_trigger_result, 10),
        
      (else_try),
        # TRAVEL SYSTEM (Ferry)
        (eq, ":party_no", "p_transporter"),
        (gt, "$travel_town", towns_end),	#this means it is a ferry
        (set_trigger_result, 15),
        
      (else_try),
        # AI
        (try_begin),
          #on water
          (this_or_next|eq,":cur_terrain",rt_bridge),
          (this_or_next|eq,":cur_terrain",rt_water),		# maybe remove line later...
          (eq,":cur_terrain",rt_river),
          (call_script, "script_calculate_party_speed_water", ":party_no"),
          (set_trigger_result, reg1), # speed
          (try_begin),
            (party_slot_eq, ":party_no", slot_party_on_water, 0),	#PARTY IS SWITCHING FROM LAND TO WATER
            (try_begin), #no swimming boar herds (VC-2015)
              (party_get_template_id,":party_template",":party_no"),
              (eq,":party_template","pt_boar_herd"),
              (try_begin),
                (ge, "$vc_debug_mode", 1),
                (display_message, "@{!}WARNING: A boar herd wants to land"),
              (end_try),
              (party_get_position, pos1, ":party_no"),
              (call_script, "script_get_next_land_position", 1),
              (party_set_position, ":party_no", pos2),
            (else_try),
              (party_set_slot, ":party_no", slot_party_on_water, 1),
              (party_set_flags, ":party_no", pf_is_ship, 1),
              (call_script, "script_update_party_icon", ":party_no"),
            (end_try),
            (try_begin),	#free player if he is captive
              (eq, "$g_player_is_captive", 1),
              (eq, "$travel_town", 0),	#VC-2283
              (eq, ":party_no", "$capturer_party"),
              (jump_to_menu,"mnu_captivity_end_wilderness_escape"),
            (end_try),
          (end_try),
        (else_try),
          #in land
          (try_begin), #lords
            (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
            (party_get_skill_level, ":speed_multiplier", ":party_no", skl_pathfinding), #skills is important now
            (ge,":speed_multiplier",1),
            (val_mul,":speed_multiplier",2),
            (val_add,":speed_multiplier",100),
          (else_try),
            (assign,":speed_multiplier",100),
          (try_end),
          # (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
          # (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
          # (store_faction_of_party, ":lord_faction", ":party_no"),
          # (store_faction_of_party, ":center_faction", ":center_no"),
          # (store_relation, ":cur_relation", ":lord_faction", ":center_faction"),
          # (lt, ":cur_relation", 0),
          # (store_distance_to_party_from_party, ":dist", ":party_no", ":center_no"),
          # (lt, ":dist", 15), #15 km
          # (val_sub,":speed_multiplier", 10),
          # (try_end),
          
          (party_get_template_id,":party_template",":party_no"),
          (try_begin),
            (eq,":party_template","pt_kingdom_caravan_party"),
            (val_sub,":speed_multiplier", 40),
          (else_try),
            (eq,":party_template","pt_kennemer_revenge"), #storyline, faster than player
            (val_add,":speed_multiplier", 40),
          (else_try),
            (eq,":party_template","pt_wessex_patrol"), #storyline, faster than player
            (val_add,":speed_multiplier", 40),
          (else_try),
            (eq,":party_template","pt_messenger_party"),
            (val_add,":speed_multiplier", 50),
          (else_try),
            (eq,":party_template","pt_sacerdotes_party"),
            (val_sub,":speed_multiplier", 20),
          (else_try),
            (eq,":party_template","pt_paganos_party"),
            (val_sub,":speed_multiplier", 20),
          (else_try), #anadido chief mas abajo para limitar velocidad de dena
            (eq,":party_template","pt_sea_raiders2"),
            (val_sub,":speed_multiplier", 10),
          (else_try), #anadido chief mas abajo para limitar velocidad de franks.
            (eq,":party_template","pt_sea_raiders"),
            (val_sub,":speed_multiplier", 10),
          (try_end),
          (val_max, ":speed_multiplier", 0),
          (set_trigger_result, ":speed_multiplier"),
          # (set_trigger_result, 100),
          (try_begin),
            (party_slot_eq, ":party_no", slot_party_on_water, 1),	#PARTY IS SWITCHING FROM WATER TO LAND
            (try_begin),
              # VC-2237 avoid sea kings from landing
              (this_or_next|eq,":party_template","pt_sea_king_norweg"),
              (eq,":party_template","pt_sea_king_danish"),
              (try_begin),
                (ge, "$vc_debug_mode", 1),
                (display_message, "@{!}WARNING: A sea king wants to land"),
              (end_try),
              (party_get_position, pos1, ":party_no"),
              (call_script, "script_get_next_water_position", 1),
              (party_set_position, ":party_no", pos2),
            (else_try),
              (party_set_slot, ":party_no", slot_party_on_water, 0),
              (party_set_flags, ":party_no", pf_is_ship, 0),
              (call_script, "script_update_party_icon", ":party_no"),
            (end_try),
          (end_try),
        (end_try),
      (end_try),
  ]),
  
  #script_game_troop_upgrades_button_clicked
  # This script is called from the game engine when the player clicks on said button from the party screen
  # INPUT: arg1 = troop_id
  ("game_troop_upgrades_button_clicked", [
      (store_script_param, reg0, 1),
      (start_presentation, "prsnt_game_troop_tree"),
  ]),
  
  # script_npc_get_troop_wage
  # This script is called from module system to calculate troop wages for npc parties.
  # Input:
  # param1: troop_id
  # Output: reg0: weekly wage
  
  ("npc_get_troop_wage",
    [
      (store_script_param_1, ":troop_id"),
      (store_script_param_2, ":party_no"),
      (try_begin),
        (this_or_next|party_slot_eq, ":party_no", slot_party_type, spt_town),
        (party_slot_eq, ":party_no", slot_party_type, spt_castle),
        (party_get_slot, ":town_lord", ":party_no", slot_town_lord),
        (try_begin),
          (gt, ":town_lord", 0),
          (store_skill_level, ":leadership_level", "skl_leadership", ":town_lord"),
        (else_try),
          (assign, ":leadership_level", 5),
        (try_end),
      (else_try),
        (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
        (party_stack_get_troop_id, ":party_leader", ":party_no", 0),
        (troop_is_hero, ":party_leader"),
        (store_skill_level, ":leadership_level", "skl_leadership", ":party_leader"), #good leadership mean pay a lot less
      (try_end),
      (assign,":wage", 0),
      (try_begin),
        (troop_is_hero, ":troop_id"),
      (else_try),
        (store_character_level, ":troop_level", ":troop_id"),
        (assign, ":wage", ":troop_level"), #similar player
        (try_begin),
          (le, ":troop_level", 23), #same here for 23 and lower troop level
          (val_add, ":wage", 4), #chief cambia
          (val_sub, ":wage", ":leadership_level"), #chief cambia
        (else_try),
          (le, ":troop_level", 26), #26
          (val_add, ":wage", 10), #chief cambia
          (val_sub, ":wage", ":leadership_level"), #chief cambia
          (val_mul, ":wage", 6), #chief cambiado
          (val_div, ":wage", 5), #chief cambiado
        (else_try),
          #       (gt, ":troop_level", 23),
          (val_add, ":wage", 20), #chief cambia
          (val_sub, ":wage", ":leadership_level"), #chief cambia
          (val_mul, ":wage", 4), #chief cambiado
          (val_div, ":wage", 3), #chief cambiado
        (try_end),
        (val_max, ":wage", 1),
        
        (try_begin), #mounted troops cost 65% more than the normal cost
          (troop_is_mounted, ":troop_id"),
          (val_mul, ":wage", 5), #chief cambiado
          (val_div, ":wage", 4), #chief cambiado
        (try_end),
        (try_begin), #mercenaries cost %50 more than the normal cost
          (is_between, ":troop_id", mercenary_troops_begin, mercenary_troops_end),
          (val_mul, ":wage", 5),
          (val_div, ":wage", 4), #chief cambiado
        (try_end),
        (try_begin), #aumenta bandidos
          (is_between, ":troop_id", bandits_begin, bandits_end),
          (val_mul, ":wage", 5),
          (val_div, ":wage", 4), #chief cambiado
        (try_end),
        
      (try_end),
      (assign, reg0, ":wage"),
  ]),
  
  #script_setup_talk_info
  # INPUT: $g_talk_troop, $g_talk_troop_relation
  ("setup_talk_info",
    [
      (talk_info_set_relation_bar, "$g_talk_troop_relation"),
      (str_store_troop_name, s61, "$g_talk_troop"),
      (str_store_string, s61, "@{!} {s61}"),
      (assign, reg1, "$g_talk_troop_relation"),
      (str_store_string, s62, "str_relation_reg1"),
      (talk_info_set_line, 0, s61),
      (talk_info_set_line, 1, s62),
      (call_script, "script_describe_relation_to_s63", "$g_talk_troop_relation"),
      (talk_info_set_line, 3, s63),
  ]),
  
  #NPC companion changes begin
  #script_setup_talk_info_companions
  ("setup_talk_info_companions",
    [
      (call_script, "script_npc_morale", "$g_talk_troop"),
      (assign, ":troop_morale", reg0),
      
      (talk_info_set_relation_bar, ":troop_morale"),
      
      (str_store_troop_name, s61, "$g_talk_troop"),
      (str_store_string, s61, "@{!} {s61}"),
      (assign, reg1, ":troop_morale"),
      (str_store_string, s62, "str_morale_reg1"),
      (talk_info_set_line, 0, s61),
      (talk_info_set_line, 1, s62),
      (talk_info_set_line, 3, s63),
  ]),
  #NPC companion changes end
  
  #script_update_party_creation_random_limits
  # INPUT: none
  # OUTPUT: $spawn_party_max_size
  ("update_party_creation_random_limits",
    [
      # (store_character_level, ":player_level", "trp_player"),
      # (store_mul, ":upper_limit", ":player_level", 3),
      # (val_add, ":upper_limit", 25),
      # (val_min, ":upper_limit", 100),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (display_message, "@{!}DEBUG : Doing script_update_party_creation_random_limits"),
      (try_end),
      
      (assign, "$spawn_party_max_size", 0),
      
      #adjust to strength of player party
      (party_get_num_companion_stacks, ":num_stacks","p_main_party"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop", "p_main_party", ":i_stack"),
        (store_character_level, ":stack_strength", ":stack_troop"),
        (party_stack_get_size, ":stack_size","p_main_party",":i_stack"),
        (val_mul, ":stack_strength", ":stack_size"),
        (val_add, "$spawn_party_max_size", ":stack_strength"),
      (try_end),
      (val_div, "$spawn_party_max_size", AI_Poor_Troop_Level),  #div by mode level of typical troop to normalize for troop quality
      
      (options_get_campaign_ai, ":ai"),
      (try_begin),
        (eq, ":ai", 0),
        (val_mul, "$spawn_party_max_size", 22),  #one SD up, 85% of parties larger, mean 30% larger
      (else_try),
        (eq, ":ai", 1),
        (val_mul, "$spawn_party_max_size", 17),  #player party size on average
      (else_try),
        (val_mul, "$spawn_party_max_size", 13),  #one SD down, 90% of parties smaller, mean 15% smaller
      (try_end),
      (val_div, "$spawn_party_max_size", 15),	#for some reason, parties can spawn about 50% larger than limit
      (val_max, "$spawn_party_max_size", 1),
      
      (store_div, ":min", "$spawn_party_max_size", 5),
      (set_party_creation_random_limits, ":min", "$spawn_party_max_size"), #minimum is applied to every COMPONENT
  ]),
  
  #script_set_trade_route_between_centers
  # INPUT:
  # param1: center_no_1
  # param1: center_no_2
  ("set_trade_route_between_centers",
    [(store_script_param, ":center_no_1", 1),
      (store_script_param, ":center_no_2", 2),
      (assign, ":center_1_added", 0),
      (assign, ":center_2_added", 0),
      (try_for_range, ":cur_slot", slot_town_trade_routes_begin, slot_town_trade_routes_end),
        (try_begin),
          (eq, ":center_1_added", 0),
          (party_slot_eq, ":center_no_1", ":cur_slot", 0),
          (party_set_slot, ":center_no_1", ":cur_slot", ":center_no_2"),
          (assign, ":center_1_added", 1),
        (try_end),
        (try_begin),
          (eq, ":center_2_added", 0),
          (party_slot_eq, ":center_no_2", ":cur_slot", 0),
          (party_set_slot, ":center_no_2", ":cur_slot", ":center_no_1"),
          (assign, ":center_2_added", 1),
        (try_end),
      (try_end),
      (try_begin),
        (eq, ":center_1_added", 0),
        (str_store_party_name, s1, ":center_no_1"),
        (display_message, "@{!}DEBUG -- ERROR: More than 15 trade routes are given for {s1}."),
      (try_end),
      (try_begin),
        (eq, ":center_2_added", 0),
        (str_store_party_name, s1, ":center_no_2"),
        (display_message, "@{!}DEBUG -- ERROR: More than 15 trade routes are given for {s1}."),
      (try_end),
  ]),
  
  # script_center_change_trade_good_production
  # INPUT:
  # param1: center_no
  # param2: item_id
  # param3: production_rate (should be between -100 (for net consumption) and 100 (for net production)
  # param4: randomness (between 0-100)
  # ("center_change_trade_good_production",
  # [
  # (display_message, "@CHANGING"),
  # (store_script_param, ":center_no", 1),
  # (store_script_param, ":item_no", 2),
  # (store_script_param, ":production_rate", 3),
  # (store_script_param, ":randomness", 4),
  # (store_random_in_range, ":random_num", 0, ":randomness"),
  # (store_random_in_range, ":random_sign", 0, 2),
  # (try_begin),
  # (eq, ":random_sign", 0),
  # (val_add, ":production_rate", ":random_num"),
  # (else_try),
  # (val_sub, ":production_rate", ":random_num"),
  # (try_end),
  # (val_sub, ":item_no", trade_goods_begin),
  # (val_add, ":item_no", slot_town_trade_good_productions_begin),
  
  # (party_get_slot, ":old_production_rate", ":center_no", ":item_no"),
  # (val_add, ":production_rate", ":old_production_rate"),
  # (party_set_slot, ":center_no", ":item_no", ":production_rate"),
  # ]),
  
  # # script_average_trade_good_prices
  # ("average_trade_good_prices", #Called from start
    # [
      # #This should be done by route rather than distance
      # (store_sub, ":item_to_slot", slot_town_trade_good_prices_begin, trade_goods_begin),
      
      # (try_for_range, ":center_no", towns_begin, towns_end),
        # (this_or_next|is_between, ":center_no", towns_begin, towns_end),
        # (is_between, ":center_no", villages_begin, villages_end),
        
        # (try_for_range, ":other_center", centers_begin, centers_end),
          # (this_or_next|is_between, ":center_no", towns_begin, towns_end),
          # (is_between, ":center_no", villages_begin, villages_end),
          
          # (neq, ":other_center", ":center_no"),
          # (store_distance_to_party_from_party, ":cur_distance", ":center_no", ":other_center"),
          # (lt, ":cur_distance", 50), #Reduced from 110
          # (store_sub, ":dist_factor", 50, ":cur_distance"),
          
          # (try_for_range, ":cur_good", trade_goods_begin, trade_goods_end),
            # (store_add, ":cur_good_slot", ":cur_good", ":item_to_slot"),
            # (party_get_slot, ":center_price", ":center_no", ":cur_good_slot"),
            # (party_get_slot, ":other_center_price", ":other_center", ":cur_good_slot"),
            # (store_sub, ":price_dif", ":center_price", ":other_center_price"),
            
            # (assign, ":price_dif_change", ":price_dif"),
            
            # (val_mul ,":price_dif_change", ":dist_factor"),
            # (val_div ,":price_dif_change", 1000), #Maximum of 1/20 per center
            # (val_add, ":other_center_price", ":price_dif_change"),
            # (party_set_slot, ":other_center", ":cur_good_slot", ":other_center_price"),
            
            # (val_sub, ":center_price", ":price_dif_change"),
            # (party_set_slot, ":center_no", ":cur_good_slot", ":center_price"),
          # (try_end),
        # (try_end),
      # (try_end),
  # ]),
  
  # # script_average_trade_good_prices_2
  # ("average_trade_good_prices_2", #Called from start
    # [
      
      # #This should be done by route rather than distance
      # (store_sub, ":item_to_slot", slot_town_trade_good_prices_begin, trade_goods_begin),
      
      # (try_for_range, ":center_no", towns_begin, towns_end),
        # (try_for_range, ":other_center", centers_begin, centers_end),
          # (this_or_next|is_between, ":other_center", towns_begin, towns_end),
          # (is_between, ":other_center", villages_begin, villages_end),
          
          # (this_or_next|party_slot_eq, ":other_center", slot_village_market_town, ":center_no"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_1, ":other_center"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_2, ":other_center"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_3, ":other_center"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_4, ":other_center"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_5, ":other_center"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_6, ":other_center"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_7, ":other_center"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_8, ":other_center"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_9, ":other_center"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_10, ":other_center"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_11, ":other_center"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_12, ":other_center"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_13, ":other_center"),
          # (this_or_next|party_slot_eq, ":center_no", slot_town_trade_route_14, ":other_center"),
          # (party_slot_eq, ":center_no", slot_town_trade_route_15, ":other_center"),
          
          # #          (neq, ":other_center", ":center_no"),
          # #          (store_distance_to_party_from_party, ":cur_distance", ":center_no", ":other_center"),
          # #          (lt, ":cur_distance", 50), #Reduced from 110
          # #          (store_sub, ":dist_factor", 50, ":cur_distance"),
          
          # (try_for_range, ":cur_good", trade_goods_begin, trade_goods_end),
            # (store_add, ":cur_good_slot", ":cur_good", ":item_to_slot"),
            # (party_get_slot, ":center_price", ":center_no", ":cur_good_slot"),
            # (party_get_slot, ":other_center_price", ":other_center", ":cur_good_slot"),
            # (store_sub, ":price_dif", ":center_price", ":other_center_price"),
            
            # (store_div, ":price_dif_change", ":price_dif", 5), #this is done twice, reduced from 4
            # #            (assign, ":price_dif_change", ":price_dif"),
            
            # #            (val_mul ,":price_dif_change", ":dist_factor"),
            # #            (val_div ,":price_dif_change", 500), #Maximum of 1/10 per center
            # (val_add, ":other_center_price", ":price_dif_change"),
            # (party_set_slot, ":other_center", ":cur_good_slot", ":other_center_price"),
            
            # (val_sub, ":center_price", ":price_dif_change"),
            # (party_set_slot, ":center_no", ":cur_good_slot", ":center_price"),
            
          # (try_end),
        # (try_end),
      # (try_end),
  # ]),
  
  #script_update_trade_good_prices
  # INPUT: none
  ("update_trade_good_prices",
    [
      (try_for_range, ":center_no", centers_begin, centers_end),
        (this_or_next|is_between, ":center_no", towns_begin, towns_end),
        (is_between, ":center_no", villages_begin, villages_end),
        (call_script, "script_update_trade_good_price_for_party", ":center_no"),
      (try_end),
      
      (try_for_range, ":cur_good", trade_goods_begin, trade_goods_end),
        (assign, ":total_price", 0),
        (assign, ":total_constants", 0),
        
        (try_for_range, ":center_no", centers_begin, centers_end),
          (this_or_next|is_between, ":center_no", towns_begin, towns_end),
          (is_between, ":center_no", villages_begin, villages_end),
          
          (store_sub, ":cur_good_price_slot", ":cur_good", trade_goods_begin),
          (val_add, ":cur_good_price_slot", slot_town_trade_good_prices_begin),
          (party_get_slot, ":cur_price", ":center_no", ":cur_good_price_slot"),
          
          (try_begin),
            (is_between, ":center_no", towns_begin, towns_end),
            (assign, ":constant", 5),
          (else_try),
            (assign, ":constant", 1),
          (try_end),
          
          (val_mul, ":cur_price", ":constant"),
          
          (val_add, ":total_price", ":cur_price"),
          (val_add, ":total_constants", ":constant"),
        (try_end),
        
        (try_for_range, ":center_no", centers_begin, centers_end),
          (this_or_next|is_between, ":center_no", towns_begin, towns_end),
          (is_between, ":center_no", villages_begin, villages_end),
          
          (store_sub, ":cur_good_price_slot", ":cur_good", trade_goods_begin),
          (val_add, ":cur_good_price_slot", slot_town_trade_good_prices_begin),
          (party_get_slot, ":cur_price", ":center_no", ":cur_good_price_slot"),
          
          (val_mul, ":cur_price", 1000),
          (val_mul, ":cur_price", ":total_constants"),
          (val_div, ":cur_price", ":total_price"),
          
          (val_clamp, ":cur_price", minimum_price_factor, maximum_price_factor),
          (party_set_slot, ":center_no", ":cur_good_price_slot", ":cur_price"),
        (try_end),
      (try_end),
  ]),
  
  # script_update_trade_good_price_for_party
  # INPUT: arg1 = party_no
  # Called once every 72 hours
  ("update_trade_good_price_for_party",
    [
      (store_script_param, ":center_no", 1),
      (try_for_range, ":cur_good", trade_goods_begin, trade_goods_end),
        (store_sub, ":cur_good_price_slot", ":cur_good", trade_goods_begin),
        (val_add, ":cur_good_price_slot", slot_town_trade_good_prices_begin),
        (party_get_slot, ":cur_price", ":center_no", ":cur_good_price_slot"),
        
        (call_script, "script_center_get_production", ":center_no", ":cur_good"),
        (assign, ":production", reg0),
        
        (call_script, "script_center_get_consumption", ":center_no", ":cur_good"),
        (assign, ":consumption", reg0),
        
        #OZANDEBUG
        #(assign, reg1, ":production"),
        #(assign, reg2, ":consumption"),
        #(str_store_party_name, s1, ":center_no"),
        #(str_store_item_name, s2, ":cur_good"),
        
        (val_sub, ":production", ":consumption"),
        
        #Change average production x 2(1+random(2)) (was average 4, random(8)) for excess demand
        (try_begin),
          #supply is greater than demand
          (gt, ":production", 0),
          (store_mul, ":change_factor", ":production", 6), #price will be decreased by his factor MOTO chief increase 20% (from 1)
          (val_div, ":change_factor", 5),    #MOTO chief increase 20%
          (store_random_in_range, ":random_change", 0, ":change_factor"),
          (val_add, ":random_change", ":change_factor"),
          (val_add, ":random_change", ":change_factor"),
          
          #simulation starts
          (store_sub, ":final_price", ":cur_price", ":random_change"),
          (val_clamp, ":final_price", minimum_price_factor, maximum_price_factor),
          (try_begin), #Excess of supply decelerates over time, as low price reduces output
            #if expected final price is 100 then it will multiply random_change by 0.308x ((100+300)/(1300) = 400/1300).
            (lt, ":final_price", 1000),
            (store_add, ":final_price_plus_300", ":final_price", 300),
            (val_mul, ":random_change", ":final_price_plus_300"),
            (val_div, ":random_change", 1300),
          (try_end),
          (val_sub, ":cur_price", ":random_change"),
        (else_try),
          (lt, ":production", 0),
          (store_sub, ":change_factor", 0, ":production"), #price will be increased by his factor
          (val_mul, ":change_factor", 6),    #MOTO chief increase 20%      (from 1)
          (val_div, ":change_factor", 5),    #MOTO chief increase 20%
          (store_random_in_range, ":random_change", 0, ":change_factor"),
          (val_add, ":random_change", ":change_factor"),
          (val_add, ":random_change", ":change_factor"),
          (val_add, ":cur_price", ":random_change"),
        (try_end),
        
        #Move price towards average by 3%...
        #Equilibrium is 33 cycles, or 100 days
        #Change per cycle is Production x 4
        #Thus, max differential = -5 x 4 x 33 = -660 for -5
        (try_begin),
          (is_between, ":center_no", villages_begin, villages_end),
          (store_sub, ":price_difference", ":cur_price", average_price_factor),
          (val_mul, ":price_difference", 96),
          (val_div, ":price_difference", 100),
          (store_add, ":new_price", average_price_factor, ":price_difference"),
        (else_try),
          (store_sub, ":price_difference", ":cur_price", average_price_factor),
          (val_mul, ":price_difference", 96),
          (val_div, ":price_difference", 100),
          (store_add, ":new_price", average_price_factor, ":price_difference"),
        (try_end),
        
        #Price of manufactured goods drift towards primary raw material
        (try_begin),
          (item_get_slot, ":raw_material", ":cur_good", slot_item_primary_raw_material),
          (neq, ":raw_material", 0),
          (store_sub, ":raw_material_price_slot", ":raw_material", trade_goods_begin),
          (val_add, ":raw_material_price_slot", slot_town_trade_good_prices_begin),
          
          (party_get_slot, ":total_raw_material_price", ":center_no", ":raw_material_price_slot"),
          (val_mul, ":total_raw_material_price", 3),
          (assign, ":number_of_centers", 3),
          
          (try_for_range, ":village_no", villages_begin, villages_end),
            (party_slot_eq, ":village_no", slot_village_bound_center, ":center_no"),
            (party_get_slot, ":raw_material_price", ":village_no", ":raw_material_price_slot"),
            (val_add, ":total_raw_material_price", ":raw_material_price"),
            (val_add, ":number_of_centers", 1),
          (try_end),
          
          (store_div, ":average_raw_material_price", ":total_raw_material_price", ":number_of_centers"),
          
          (gt, ":average_raw_material_price", ":new_price"),
          (store_sub, ":raw_material_boost", ":average_raw_material_price", ":new_price"),
          (val_div, ":raw_material_boost", 10),
          (val_add, ":new_price", ":raw_material_boost"),
        (try_end),
        
        (val_clamp, ":new_price", minimum_price_factor, maximum_price_factor),
        (party_set_slot, ":center_no", ":cur_good_price_slot", ":new_price"),
        
        #(assign, reg3, ":new_price"),
        #(str_store_item_name, s2, ":cur_good"),
        #(display_log_message, "@{!}DEBUG : {s1}-{s2}, prod:{reg1}, cons:{reg2}, price:{reg3}"),
      (try_end),
  ]),
  
  # script_center_get_production
  ("center_get_production",
    [
      #Actually, this could be reset somewhat to yield supply and demand as raw numbers
      #Demand could be set values for rural and urban
      #Supply could be based on capital goods -- head of cattle, head of sheep, fish ponds, fishing fleets, acres of grain fields, olive orchards, olive presses, wine presses, mills, smithies, salt pans, potters' kilns, etc
      #Prosperity would increase both demand and supply
      (store_script_param_1, ":center_no"),
      (store_script_param_2, ":cur_good"),
      
      (assign, ":base_production", 0),
      
      #Grain products
      (try_begin),
        (eq, ":cur_good", "itm_bread"), #Demand = 3000 across Calradia
        (party_get_slot, ":base_production", ":center_no", slot_center_mills),
        (val_mul, ":base_production", 20), #one mills per village, five mills per town = 160 mills
      (else_try),
        (eq, ":cur_good", "itm_grain"), #Demand =  3200+, 1600 to mills, 1500 on its own, extra to breweries
        (party_get_slot, ":base_production", ":center_no", slot_center_acres_grain),
        (val_div, ":base_production", 125), #10000 acres is the average across Calradia, extra in Swadia, less in snows and steppes, a bit from towns
      (else_try),
        (eq, ":cur_good", "itm_ale"), #
        (party_get_slot, ":base_production", ":center_no", slot_center_breweries),
        (val_mul, ":base_production", 50),
        
      (else_try),
        (eq, ":cur_good", "itm_smoked_fish"), #Demand = 20
        (party_get_slot, ":base_production", ":center_no", slot_center_fishing_fleet),
        (val_mul, ":base_production", 4), #was originally 5
      (else_try),
        (eq, ":cur_good", "itm_salt"),
        (party_get_slot, ":base_production", ":center_no", slot_center_salt_pans),
        (val_mul, ":base_production", 25),
        
        #Cattle products
      (else_try),
        (eq, ":cur_good", "itm_cattle_meat"), #Demand = 5
        (party_get_slot, ":base_production", ":center_no", slot_center_head_cattle),
        (val_div, ":base_production", 4), #was 9
      (else_try),
        (eq, ":cur_good", "itm_dried_meat"), #Demand = 15
        (party_get_slot, ":base_production", ":center_no", slot_center_head_cattle),
        (val_div, ":base_production", 2), #was 3
      (else_try),
        (eq, ":cur_good", "itm_cheese"), 	 #Demand = 10
        (party_get_slot, ":base_production", ":center_no", slot_center_head_cattle),
        (party_get_slot, ":sheep_addition", ":center_no", slot_center_head_sheep),
        (val_div, ":sheep_addition", 2),
        (val_add, ":base_production", ":sheep_addition"),
        (party_get_slot, ":gardens", ":center_no", slot_center_household_gardens),
        (val_mul, ":base_production", ":gardens"),
        (val_div, ":base_production", 10),
      (else_try),
        (eq, ":cur_good", "itm_butter"), 	 #Demand = 2
        (party_get_slot, ":base_production", ":center_no", slot_center_head_cattle),
        (party_get_slot, ":gardens", ":center_no", slot_center_household_gardens),
        (val_mul, ":base_production", ":gardens"),
        (val_div, ":base_production", 15),
        
      (else_try),
        (eq, ":cur_good", "itm_vc_raw_leather"), 	 #Demand = ??
        (party_get_slot, ":base_production", ":center_no", slot_center_head_cattle),
        (val_div, ":base_production", 18),
        (party_get_slot, ":sheep_addition", ":center_no", slot_center_head_sheep),
        (val_div, ":sheep_addition", 36),
        (val_add, ":base_production", ":sheep_addition"),
        
      (else_try),
        (eq, ":cur_good", "itm_timber"), 	 #Demand = ?? (VC total demand = 600)
        (party_get_slot, ":base_production", ":center_no", slot_center_forest),
        (val_mul, ":base_production", 1),	# Changed on 02.03.15 from 20 to 1
        
      (else_try),
        (eq, ":cur_good", "itm_vc_honey"), 	 #Demand = 5
        (party_get_slot, ":base_production", ":center_no", slot_center_apiaries),
        (val_mul, ":base_production", 10),
      (else_try),
        (eq, ":cur_good", "itm_cabbages"), 	 #Demand = 7
        (party_get_slot, ":base_production", ":center_no", slot_center_household_gardens),
        (val_mul, ":base_production", 10),
      (else_try),
        (eq, ":cur_good", "itm_apples"), 	 #Demand = 7
        (party_get_slot, ":base_production", ":center_no", slot_center_household_gardens),
        (val_mul, ":base_production", 10),
        
        #Sheep products
      (else_try),
        (eq, ":cur_good", "itm_sausages"), 	 #Demand = 5
        (party_get_slot, ":base_production", ":center_no", slot_center_head_sheep), #average of 90 sheep
        (val_div, ":base_production", 15),
      (else_try),
        (eq, ":cur_good", "itm_wool"), 	 #(Demand = 0, but 15 averaged out perhaps)
        (party_get_slot, ":base_production", ":center_no", slot_center_head_sheep), #average of 90 sheep
        (val_div, ":base_production", 5),
      (else_try),
        (eq, ":cur_good", "itm_vc_wool_cloth"), 	 #(VC total Demand = 3950)
        (party_get_slot, ":base_production", ":center_no", slot_center_wool_looms),
        (val_mul, ":base_production", 5), #300 across Calradia (VC = 500)
        
      (else_try),
        (this_or_next|eq, ":cur_good", "itm_pork"),
        (eq, ":cur_good", "itm_chicken"),
        (try_begin),
          (is_between, ":center_no", villages_begin, villages_end),
          (assign, ":base_production", 30),
        (else_try),
          (assign, ":base_production", 0),
        (try_end),
        
      (else_try),
        (eq, ":cur_good", "itm_iron"), 	 #Demand = 5, one supplies three smithies
        (party_get_slot, ":base_production", ":center_no", slot_center_iron_deposits),
        (val_mul, ":base_production", 10),
      (else_try),
        (eq, ":cur_good", "itm_tools"), 	 #Demand = 560 across Calradia
        (party_get_slot, ":base_production", ":center_no", slot_center_smithies),
        (val_mul, ":base_production", 6),
        
        #Other artisanal goods
      (else_try),
        (eq, ":cur_good", "itm_jewelry"), #560 is total demand	 #eco note
        (party_get_slot, ":base_production", ":center_no", slot_center_pottery_kilns),
        (val_mul, ":base_production", 9),	#was 5
        
      (else_try),
        (eq, ":cur_good", "itm_barley"),
        (party_get_slot, ":base_production", ":center_no", slot_center_acres_vineyard),
        (val_div, ":base_production", 50),# was 100
      (else_try),
        (eq, ":cur_good", "itm_mead"),
        (party_get_slot, ":base_production", ":center_no", slot_center_wine_presses),
        (val_mul, ":base_production", 50),
      (else_try),
        (eq, ":cur_good", "itm_venison"),
        (party_get_slot, ":base_production", ":center_no", slot_center_acres_hunting),
        (val_div, ":base_production", 150),
      (else_try),
        (eq, ":cur_good", "itm_tar"),
        (party_get_slot, ":base_production", ":center_no", slot_center_tar_ovens),
        (val_mul, ":base_production", 12),
        
        #Flax and linen
      (else_try),
        (eq, ":cur_good", "itm_linen"),
        (party_get_slot, ":base_production", ":center_no", slot_center_linen_looms),
        (val_mul, ":base_production", 5),
      (else_try),
        (eq, ":cur_good", "itm_raw_flax"),
        (party_get_slot, ":base_production", ":center_no", slot_center_acres_flax),
        (val_div, ":base_production", 80),
      (else_try),
        (eq, ":cur_good", "itm_silver"),
        (party_get_slot, ":base_production", ":center_no", slot_center_silver_deposits),
        (val_mul, ":base_production", 1),
      (else_try),
        (eq, ":cur_good", "itm_amber"),
        (party_get_slot, ":base_production", ":center_no", slot_center_amber_deposits),
        (val_mul, ":base_production", 1),
      (else_try),
        (eq, ":cur_good", "itm_ivory"), 	 #phaiak eco_note
        (party_get_slot, ":base_production", ":center_no", slot_center_walrus_fleet),
        (val_mul, ":base_production", 1),	#was 5 before content update
      (else_try),
        (eq, ":cur_good", "itm_soapstone"),
        (party_get_slot, ":base_production", ":center_no", slot_center_soapstone_depositis),
        (val_mul, ":base_production", 10),
      (else_try),
        (eq, ":cur_good", "itm_vc_furs"), 	 #Demand = 90 across Calradia
        (party_get_slot, ":base_production", ":center_no", slot_center_fur_traps),
        (val_mul, ":base_production", 3),
      (else_try),
        (eq, ":cur_good", "itm_wine"),	#eco note
        (try_begin),
          (eq, ":center_no", "p_town_11"), #Dorestad			#Wine only in Dorestad
          (assign, ":base_production", 250),
          # (else_try),
          # (eq, ":center_no", "p_town_17"), #Ichamur
          # (assign, ":base_production", 50),
          # (else_try),
          # (eq, ":center_no", "p_town_19"), #Shariz
          # (assign, ":base_production", 50),
          # (else_try),
          # (eq, ":center_no", "p_town_22"), #Bariyye
          # (assign, ":base_production", 50),
          ##			(else_try), #chief pone off de momento
          ##				(this_or_next|eq, ":center_no", "p_village_11"), #Dusturil (village of Tulga)
          ##				(eq, ":center_no", "p_village_25"), #Dashbigha (village of Tulga)
          ##				(assign, ":base_production", 50),
          ##			(else_try),
          ##				(this_or_next|eq, ":center_no", "p_village_37"), #Ada Kulun (village of Ichlamur)
          ##				(this_or_next|eq, ":center_no", "p_village_42"), #Dirigh Aban (village of Ichlamur)
          ##				(this_or_next|eq, ":center_no", "p_village_99"), #Fishara (village of Bariyye)
          ##				(eq, ":center_no", "p_village_100"), #Iqbayl (village of Bariyye)
          ##				(assign, ":base_production", 25),
        (try_end),
      (try_end),
      
      #Modify production by other goods
      (assign, ":modified_production", ":base_production"),
      (try_begin),
        (eq, ":cur_good", "itm_bread"),
        (call_script, "script_good_price_affects_good_production", ":center_no", "itm_grain", ":base_production", 1),
        (assign, ":modified_production", reg0),
      (else_try),
        (eq, ":cur_good", "itm_ale"),
        (call_script, "script_good_price_affects_good_production", ":center_no", "itm_grain", ":base_production", 2),
        (assign, ":initially_modified_production", reg0),
        (call_script, "script_good_price_affects_good_production", ":center_no", "itm_barley", ":initially_modified_production", 1),
        (assign, ":modified_production", reg0),
      (else_try),
        (eq, ":cur_good", "itm_dried_meat"),
        (call_script, "script_good_price_affects_good_production", ":center_no", "itm_salt", ":base_production", 2),
        (assign, ":modified_production", reg0),
      (else_try),
        (eq, ":cur_good", "itm_smoked_fish"),
        (call_script, "script_good_price_affects_good_production", ":center_no", "itm_salt", ":base_production", 2),
        (assign, ":modified_production", reg0),
      (else_try),
        (eq, ":cur_good", "itm_tools"),
        (call_script, "script_good_price_affects_good_production", ":center_no", "itm_iron", ":base_production", 1),
        (assign, ":modified_production", reg0),
      (else_try),
        (eq, ":cur_good", "itm_vc_wool_cloth"),
        (call_script, "script_good_price_affects_good_production", ":center_no", "itm_wool", ":base_production", 1),
        (assign, ":modified_production", reg0),
        # (else_try),
        # (eq, ":cur_good", "itm_wine"),
        # (call_script, "script_good_price_affects_good_production", ":center_no", "itm_barley", ":base_production", 1),
        # (assign, ":modified_production", reg0),
        # (else_try),
        # (eq, ":cur_good", "itm_tar"),	#eco note
        #(call_script, "script_good_price_affects_good_production", ":center_no", "itm_venison", ":base_production", 1),
        # (assign, ":modified_production", reg0),
        # (else_try),
        # (eq, ":cur_good", "itm_silver"),
        # (call_script, "script_good_price_affects_good_production", ":center_no", "itm_amber", ":base_production", 1),
        # (assign, ":initially_modified_production", reg0),
        # (call_script, "script_good_price_affects_good_production", ":center_no", "itm_ivory", ":initially_modified_production", 2),
        # (assign, ":modified_production", reg0),
        # (else_try),
        # (eq, ":cur_good", "itm_timber"),	#eco note
        #(call_script, "script_good_price_affects_good_production", ":center_no", "itm_vc_raw_leather", ":base_production", 1),
        # (assign, ":modified_production", reg0),
      (else_try),
        (eq, ":cur_good", "itm_linen"),
        (call_script, "script_good_price_affects_good_production", ":center_no", "itm_raw_flax", ":base_production", 1),
        (assign, ":modified_production", reg0),
      (try_end),
      
      (assign, ":base_production_modded_by_raw_materials", ":modified_production"), #this is just logged for the report screen
      
      #Increase both positive and negative production by the center's prosperity
      #Richer towns have more people and consume more, but also produce more
      (try_begin),
        (party_get_slot, ":prosperity_plus_75", ":center_no", slot_town_prosperity),
        (val_add, ":prosperity_plus_75", 75),
        (val_mul, ":modified_production", ":prosperity_plus_75"),
        (val_div, ":modified_production", 125),
      (try_end),
      
      (try_begin),
        (this_or_next|party_slot_eq, ":center_no", slot_village_state, svs_being_raided),
        (party_slot_eq, ":center_no", slot_village_state, svs_looted),
        (assign, ":modified_production", 0),
      (try_end),
      
      (assign, reg0, ":modified_production"), #modded by prosperity
      (assign, reg1, ":base_production_modded_by_raw_materials"),
      (assign, reg2, ":base_production"),
  ]),
  
  # script_center_get_consumption
  ("center_get_consumption",
    [
      (store_script_param_1, ":center_no"),
      (store_script_param_2, ":cur_good"),
      
      (assign, ":consumer_consumption", 0),
      (try_begin),
        ### VC has no desert
        # (this_or_next|is_between, ":center_no", "p_town_19", "p_castle_1"),
        # (ge, ":center_no", "p_village_1"),
        # (item_slot_ge, ":cur_good", slot_item_desert_demand, 0), #Otherwise use rural or urban
        # (item_get_slot, ":consumer_consumption", ":cur_good", slot_item_desert_demand),
        # (else_try),
        (is_between, ":center_no", villages_begin, villages_end),
        (item_get_slot, ":consumer_consumption", ":cur_good", slot_item_rural_demand),
      (else_try),
        (is_between, ":center_no", towns_begin, towns_end),
        (item_get_slot, ":consumer_consumption", ":cur_good", slot_item_urban_demand),
      (try_end),
      
      (assign, ":raw_material_consumption", 0),
      (try_begin),
        (eq, ":cur_good", "itm_grain"),
        (party_get_slot, ":grain_for_bread", ":center_no", slot_center_mills),
        (val_mul, ":grain_for_bread", 20),
        
        (party_get_slot, ":grain_for_ale", ":center_no", slot_center_breweries),
        (val_mul, ":grain_for_ale", 5),
        
        (store_add, ":raw_material_consumption", ":grain_for_bread", ":grain_for_ale"),
        
      (else_try),
        (eq, ":cur_good", "itm_iron"),
        (party_get_slot, ":raw_material_consumption", ":center_no", slot_center_smithies),
        (val_mul, ":raw_material_consumption", 3),
        
      (else_try),
        (eq, ":cur_good", "itm_wool"),
        (party_get_slot, ":raw_material_consumption", ":center_no", slot_center_wool_looms),
        (val_mul, ":raw_material_consumption", 4),#was 5
        
      (else_try),
        (eq, ":cur_good", "itm_raw_flax"),
        (party_get_slot, ":raw_material_consumption", ":center_no", slot_center_linen_looms),
        (val_mul, ":raw_material_consumption", 4),#was 5
        
        # (else_try),
        # (eq, ":cur_good", "itm_vc_raw_leather"), #eco note
        # (party_get_slot, ":raw_material_consumption", ":center_no", slot_center_forest),
        # (val_mul, ":raw_material_consumption", 20),
        
        # (else_try),
        # (eq, ":cur_good", "itm_barley"),
        # (party_get_slot, ":raw_material_consumption", ":center_no", slot_center_wine_presses),
        # (val_mul, ":raw_material_consumption", 30),
        
        # This is a copy of wine edited for Mead:
      (else_try),
        (eq, ":cur_good", "itm_vc_honey"),
        (party_get_slot, ":raw_material_consumption", ":center_no", slot_center_wine_presses),
        (val_mul, ":raw_material_consumption", 2),
        
        # (else_try),
        # (eq, ":cur_good", "itm_venison"),	#eco note
        # (party_get_slot, ":raw_material_consumption", ":center_no", slot_center_tar_ovens),
        # (val_mul, ":raw_material_consumption", 12),
        
        # (else_try),
        # (eq, ":cur_good", "itm_ivory"),	#phaiak eco note
        # (party_get_slot, ":raw_material_consumption", ":center_no", slot_center_silver_deposits),
        # (val_mul, ":raw_material_consumption", 1),
        
        # (else_try),
        # (eq, ":cur_good", "itm_amber"),
        # (party_get_slot, ":raw_material_consumption", ":center_no", slot_center_silver_deposits),
        # (val_mul, ":raw_material_consumption", 5),
        
      (else_try),
        (eq, ":cur_good", "itm_salt"),
        (party_get_slot, ":salt_for_beef", ":center_no", slot_center_head_cattle),
        (val_div, ":salt_for_beef", 10),
        
        (party_get_slot, ":salt_for_fish", ":center_no", slot_center_fishing_fleet),
        (val_div, ":salt_for_fish", 5),
        
        (store_add, ":raw_material_consumption", ":salt_for_beef", ":salt_for_fish"),
      (try_end),
      
      (try_begin), #Reduce consumption of raw materials if their cost is high
        (gt, ":raw_material_consumption", 0),
        (store_sub, ":item_to_price_slot", slot_town_trade_good_prices_begin, trade_goods_begin),
        (store_add, ":cur_good_price_slot", ":cur_good", ":item_to_price_slot"),
        (party_get_slot, ":cur_center_price", ":center_no", ":cur_good_price_slot"),
        (gt, ":cur_center_price", 1000),
        (val_mul, ":raw_material_consumption", 1000),
        (val_div, ":raw_material_consumption", ":cur_center_price"),
      (try_end),
      
      (store_add, ":modified_consumption", ":consumer_consumption", ":raw_material_consumption"),
      (try_begin),
        (party_get_slot, ":prosperity_plus_75", ":center_no", slot_town_prosperity),
        (val_add, ":prosperity_plus_75", 75),
        (val_mul, ":modified_consumption", ":prosperity_plus_75"),
        (val_div, ":modified_consumption", 125),
      (try_end),
      
      
      (assign, reg0, ":modified_consumption"), #modded by prosperity
      (assign, reg1, ":raw_material_consumption"),
      (assign, reg2, ":consumer_consumption"),
  ]),
  
  #script_get_enterprise_name
  # INPUT: arg1 = item_no
  # Output: reg0: production string
  ("get_enterprise_name",
    [
      (store_script_param_1, ":item_produced"),
      (assign, ":enterprise_name", "str_bread_site"),
      (try_begin),
        (eq, ":item_produced", "itm_bread"),
        (assign, ":enterprise_name", "str_bread_site"),
      (else_try),
        (eq, ":item_produced", "itm_ale"),
        (assign, ":enterprise_name", "str_ale_site"),
      (else_try),
        (eq, ":item_produced", "itm_tar"),
        (assign, ":enterprise_name", "str_tar_kiln"),
      (else_try),
        (eq, ":item_produced", "itm_wine"),
        (assign, ":enterprise_name", "str_wine_cellar"),
      (else_try),
        (eq, ":item_produced", "itm_timber"),
        (assign, ":enterprise_name", "str_leather_site"),
      (else_try),
        (eq, ":item_produced", "itm_vc_wool_cloth"),
        (assign, ":enterprise_name", "str_wool_cloth_site"),
      (else_try),
        (eq, ":item_produced", "itm_linen"),
        (assign, ":enterprise_name", "str_linen_site"),
      (else_try),
        (eq, ":item_produced", "itm_silver"),
        (assign, ":enterprise_name", "str_velvet_site"),
      (else_try),
        (eq, ":item_produced", "itm_tools"),
        (assign, ":enterprise_name", "str_tool_site"),
      (try_end),
      (assign, reg0, ":enterprise_name"),
  ]),
  
  #script_do_merchant_town_trade
  # INPUT: arg1 = party_no (of the merchant), arg2 = center_no
  ("do_merchant_town_trade",
    [
      (store_script_param_1, ":party_no"),
      (store_script_param_2, ":center_no"),
      
      (party_get_slot, ":origin", ":party_no", slot_party_last_traded_center),
      
      (try_begin),
        (eq, "$cheat_mode", 2),
        (str_store_party_name, s4, ":center_no"),
        (str_store_party_name, s5, ":origin"),
        (display_message, "@{!}DEBUG -- Caravan trades in {s4}, originally from {s5}"),
      (try_end),
      
      (call_script, "script_add_log_entry", logent_party_traded, ":party_no", ":origin", ":center_no", -1),
      
      (call_script, "script_do_party_center_trade", ":party_no", ":center_no", 4), #it was first 10 then increased 20 then increased 30, now I decrease it to back 6. Because otherwise prices do not differiate much. Trade become useless in game.
      
      (assign, ":total_change", reg0),
      #Adding the earnings to the wealth (maximum changed price is the earning)
      (val_div, ":total_change", 2),
      (str_store_party_name, s1, ":party_no"),
      (str_store_party_name, s2, ":center_no"),
      (assign, reg1, ":total_change"),
      
      #Adding tariffs to the town
      (party_get_slot, ":accumulated_tariffs", ":center_no", slot_center_accumulated_tariffs),
      (party_get_slot, ":prosperity", ":center_no", slot_town_prosperity),
      
      (assign, ":tariffs_generated", ":total_change"),
      (val_mul, ":tariffs_generated", ":prosperity"),
      (val_div, ":tariffs_generated", 100),
      (val_div, ":tariffs_generated", 10), #10 for caravans, 20 for villages
      (val_add, ":accumulated_tariffs", ":tariffs_generated"),
      
      (try_begin),
        (ge, "$cheat_mode", 1),
        (assign, reg4, ":tariffs_generated"),
        (str_store_party_name, s4, ":center_no"),
        (assign, reg5, ":accumulated_tariffs"),
        (display_message, "@{!}New tariffs at {s4} = {reg4}, total = {reg5}"),
      (try_end),
      
      (party_set_slot, ":center_no", slot_center_accumulated_tariffs, ":accumulated_tariffs"),
      
      #Adding 1 to center prosperity with 18% for each caravan in that center
      (try_begin),
        (store_random_in_range, ":rand", 0, 80),
        (call_script, "script_center_get_goods_availability", ":center_no"),
        (assign, ":hardship_index", reg0),
        (gt, ":rand", ":hardship_index"),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (gt, ":rand", 82),
          (call_script, "script_change_center_prosperity", ":center_no", 1),
          (val_add, "$newglob_total_prosperity_from_caravan_trade", 1),
        (try_end),
      (try_end),
  ]),
  
  #script_party_calculate_regular_strength:
  # INPUT:
  # param1: Party-id
  ("party_calculate_regular_strength",
    [
      (store_script_param_1, ":party"), #Party_id
      
      (assign, reg0,0),
      (party_get_num_companion_stacks, ":num_stacks",":party"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop", ":party", ":i_stack"),
        (neg|troop_is_hero, ":stack_troop"),
        (store_character_level, ":stack_strength", ":stack_troop"),
        #mounted
        (try_begin),
          (troop_is_mounted, ":stack_troop"),
          (val_add, ":stack_strength", 2),
        (try_end),
        #elite troops
        (store_div,":strength_bonus",":stack_strength",4),
        (val_add, ":stack_strength",":strength_bonus"),
        #
        (val_add, ":stack_strength", 12),
        (val_mul, ":stack_strength", ":stack_strength"),
        (val_div, ":stack_strength", 100),
        (party_stack_get_size, ":stack_size",":party",":i_stack"),
        (party_stack_get_num_wounded, ":num_wounded",":party",":i_stack"),
        (val_sub, ":stack_size", ":num_wounded"),
        (val_mul, ":stack_strength", ":stack_size"),
        (val_add,reg0, ":stack_strength"),
      (try_end),
  ]),
  
  #script_party_calculate_strength:
  # INPUT: arg1 = party_id, arg2 = exclude leader
  # OUTPUT: reg0 = strength
  
  ("party_calculate_strength",
    [
      (store_script_param_1, ":party"), #Party_id
      (store_script_param_2, ":exclude_leader"), #Party_id
      
      (assign, reg0,0),
      (party_get_num_companion_stacks, ":num_stacks", ":party"),
      (assign, ":first_stack", 0),
      (try_begin),
        (neq, ":exclude_leader", 0),
        (assign, ":first_stack", 1),
      (try_end),
      (try_for_range, ":i_stack", ":first_stack", ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop",":party", ":i_stack"),
        (store_character_level, ":stack_strength", ":stack_troop"),
        #hero +4 (they are better equipped)
        (try_begin),
          (troop_is_hero, ":stack_troop"),
          (val_add, ":stack_strength", 6),
        (try_end),
        #Mounted +2 (they have horses)
        (try_begin),
          (troop_is_mounted, ":stack_troop"),
          (val_add, ":stack_strength", 2),
        (try_end),
        ###
        (party_get_current_terrain, ":terrain_type", ":party"),
        (try_begin),
          (eq, ":terrain_type", rt_snow),
          (neg|troop_is_guarantee_horse, ":stack_troop"),#horses problem in snow
          #      (troop_is_guarantee_horse, ":stack_troop"),
          (val_add, ":stack_strength", 2),
        (else_try),
          (this_or_next|eq, ":terrain_type", rt_steppe_forest),
          (this_or_next|eq, ":terrain_type", rt_forest),
          (eq, ":terrain_type", rt_snow_forest),
          (neg|troop_is_guarantee_horse, ":stack_troop"), #horses has problems with trees
          (neg|troop_is_guarantee_ranged, ":stack_troop"), #archers has problems with trees
          (val_add, ":stack_strength", 2),
        (else_try),
          (val_add, ":stack_strength", 1), #global in normal terrain
        (try_end),
        ###
        (val_add, ":stack_strength", 4), #new was 12 (patch 1.125)
        (val_mul, ":stack_strength", ":stack_strength"),
        (val_mul, ":stack_strength", 2), #new (patch 1.125)
        (val_div, ":stack_strength", 100),
        (val_max, ":stack_strength", 1), #new (patch 1.125)
        
        (try_begin),
          (neg|troop_is_hero, ":stack_troop"),
          (party_stack_get_size, ":stack_size",":party",":i_stack"),
          (party_stack_get_num_wounded, ":num_wounded",":party",":i_stack"),
          (val_sub, ":stack_size", ":num_wounded"),
          (val_mul, ":stack_strength", ":stack_size"),
        (else_try),
          (troop_is_wounded, ":stack_troop"), #hero  wounded
          (assign, ":stack_strength", 0),
        (try_end),
        (val_add, reg0, ":stack_strength"),
      (try_end),
      (party_set_slot, ":party", slot_party_cached_strength, reg0),
  ]),
  
  #script_loot_player_items:
  # INPUT: arg1 = enemy_party_no
  # Output: none
  ("loot_player_items",
    [
      (store_script_param, ":enemy_party_no", 1),
      
      (troop_get_inventory_capacity, ":inv_cap", "trp_player"),
      (try_for_range, ":i_slot", 0, ":inv_cap"),
        (troop_get_inventory_slot, ":item_id", "trp_player", ":i_slot"),
        (ge, ":item_id", 0),
        (troop_get_inventory_slot_modifier, ":item_modifier", "trp_player", ":i_slot"),
        (try_begin),
          (is_between, ":item_id", trade_goods_begin, trade_goods_end),
          (assign, ":randomness", 20),
        (else_try),
          (is_between, ":item_id", horses_begin, horses_end),
          ##        (this_or_next|is_between, ":item_id", horses_begin, horses_end),
          ##          (this_or_next|eq, ":item_id", "itm_charger"),
          ##          (eq, ":item_id", "itm_warhorse_steppe"),
          (assign, ":randomness", 15),
        (else_try),
          (this_or_next|is_between, ":item_id", weapons_begin, weapons_end),
          (is_between, ":item_id", ranged_weapons_begin, ranged_weapons_end),
          (assign, ":randomness", 5),
        (else_try),
          (this_or_next|is_between, ":item_id", armors_begin, armors_end),
          (this_or_next|eq, ":item_id", "itm_carbatinae_11qs"), #added to the end because of not breaking the save games
          (is_between, ":item_id", shields_begin, shields_end),
          (assign, ":randomness", 5),
        (try_end),
        (store_random_in_range, ":random_no", 0, 100),
        (lt, ":random_no", ":randomness"),
        (troop_remove_item, "trp_player", ":item_id"),
        
        (try_begin),
          (gt, ":enemy_party_no", 0),
          (party_get_slot, ":cur_loot_slot", ":enemy_party_no", slot_party_next_looted_item_slot),
          (val_add, ":cur_loot_slot", slot_party_looted_item_1),
          (party_set_slot, ":enemy_party_no", ":cur_loot_slot", ":item_id"),
          (val_sub, ":cur_loot_slot", slot_party_looted_item_1),
          (val_add, ":cur_loot_slot", slot_party_looted_item_1_modifier),
          (party_set_slot, ":enemy_party_no", ":cur_loot_slot", ":item_modifier"),
          (val_sub, ":cur_loot_slot", slot_party_looted_item_1_modifier),
          (val_add, ":cur_loot_slot", 1),
          (val_mod, ":cur_loot_slot", num_party_loot_slots),
          (party_set_slot, ":enemy_party_no", slot_party_next_looted_item_slot, ":cur_loot_slot"),
        (try_end),
      (try_end),
      (store_troop_gold, ":cur_gold", "trp_player"),
      (store_div, ":max_lost", ":cur_gold", 5),
      (store_div, ":min_lost", ":cur_gold", 10),
      (store_random_in_range, ":lost_gold", ":min_lost", ":max_lost"),
      (troop_remove_gold, "trp_player", ":lost_gold"),
  ]),
  
  
  #script_party_calculate_loot:
  # INPUT:
  # param1: Party-id
  # Returns num looted items in reg0
  ("party_calculate_loot",
    [
      (store_script_param_1, ":enemy_party"), #Enemy Party_id
      
      (call_script, "script_calculate_main_party_shares"),
      (assign, ":num_player_party_shares", reg0),
      
      #vc loot changes begin
      (assign, "$loot_value_before", 0),
      (try_begin),
        (eq, "$loot_option", 1),
        (val_div, ":num_player_party_shares", 4), #quadruple loot if player decides to take all for him
        (val_max, ":num_player_party_shares", 1), #avoid div by zero
      (end_try),
      #vc loot changes end
      (try_for_range, ":i_loot", 0, num_party_loot_slots),
        (store_add, ":cur_loot_slot", ":i_loot", slot_party_looted_item_1),
        (party_get_slot, ":item_no", "$g_enemy_party", ":cur_loot_slot"),
        (gt, ":item_no", 0),
        (party_set_slot, "$g_enemy_party", ":cur_loot_slot", 0),
        (val_sub, ":cur_loot_slot", slot_party_looted_item_1),
        (val_add, ":cur_loot_slot", slot_party_looted_item_1_modifier),
        (party_get_slot, ":item_modifier", "$g_enemy_party", ":cur_loot_slot"),
        (troop_add_item, "trp_temp_troop", ":item_no", ":item_modifier"),
      (try_end),
      (party_set_slot, "$g_enemy_party", slot_party_next_looted_item_slot, 0),
      
      (assign, ":num_looted_items",0),
      (try_begin),
        (this_or_next|party_slot_eq, "$g_enemy_party", slot_party_type, spt_kingdom_caravan),
        (this_or_next|party_slot_eq, "$g_enemy_party", slot_party_type, spt_bandit_lair),
        (party_slot_eq, "$g_enemy_party", slot_party_type, spt_village_farmer),
        (store_mul, ":plunder_amount", player_loot_share, 30),
        (val_mul, ":plunder_amount", "$g_strength_contribution_of_player"),
        (val_div, ":plunder_amount", 100),
        (val_div, ":plunder_amount", ":num_player_party_shares"),
        (try_begin),
          (party_slot_eq, "$g_enemy_party", slot_party_type, spt_kingdom_caravan),
          (reset_item_probabilities, 100),
          (assign, ":range_min", trade_goods_begin),
          (assign, ":range_max", trade_goods_end),
          (val_mul, ":plunder_amount", 5),	#new line for VC-1649 (more trade items and less armors for trader looting)
        (else_try),
          (party_slot_eq, "$g_enemy_party", slot_party_type, spt_bandit_lair),
          (val_div, ":plunder_amount", 2),
          (reset_item_probabilities, 1),
          (assign, ":range_min", food_begin),
          (assign, ":range_max", food_end),
        (else_try),
          (val_div, ":plunder_amount", 5),
          (reset_item_probabilities, 1),
          (assign, ":range_min", food_begin),
          (assign, ":range_max", food_end),
        (try_end),
        (store_sub, ":item_to_price_slot", slot_town_trade_good_prices_begin, trade_goods_begin),
        (try_for_range, ":cur_goods", ":range_min", ":range_max"),
          (try_begin),
            (neg|party_slot_eq, "$g_enemy_party", slot_party_type, spt_bandit_lair),
            (store_add, ":cur_price_slot", ":cur_goods", ":item_to_price_slot"),
            (party_get_slot, ":cur_price", "$g_enemy_party", ":cur_price_slot"),
          (else_try),
            (assign, ":cur_price", maximum_price_factor),
            (val_add, ":cur_price", average_price_factor),
            (val_div, ":cur_price", 3),
          (try_end),
          
          (assign, ":cur_probability", 100),
          (val_mul, ":cur_probability", average_price_factor),
          (val_div, ":cur_probability", ":cur_price"),
          (assign, reg0, ":cur_probability"),
          (set_item_probability_in_merchandise, ":cur_goods", ":cur_probability"),
        (try_end),
        (troop_add_merchandise, "trp_temp_troop", itp_type_goods, ":plunder_amount"),
        (val_add, ":num_looted_items", ":plunder_amount"),
      (try_end),
      
      #Now loot the defeated party
      (store_mul, ":loot_probability", player_loot_share, 3),
      (val_mul, ":loot_probability", "$g_strength_contribution_of_player"),
      (party_get_skill_level, ":player_party_looting", "p_main_party", "skl_looting"),
      (val_add, ":player_party_looting", 10),
      (val_mul, ":loot_probability", ":player_party_looting"),
      (val_div, ":loot_probability", 10),
      (val_div, ":loot_probability", ":num_player_party_shares"),
      #new for VC-1649 (more trade items and less armors for trader looting)
      (try_begin),
        (party_slot_eq, "$g_enemy_party", slot_party_type, spt_kingdom_caravan),
        (val_div, ":loot_probability", 2),
      (end_try),
      (try_begin),
        (store_party_size_wo_prisoners, ":party_size", "p_main_party"),
        (lt, ":party_size", 40),
        (val_mul, ":loot_probability", 2),
        (val_div, ":loot_probability", 3),
      (try_end),
      (party_get_num_companion_stacks, ":num_stacks",":enemy_party"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop",":enemy_party",":i_stack"),
        (neg|troop_is_hero, ":stack_troop"),
        (party_stack_get_size, ":stack_size",":enemy_party",":i_stack"),
        (try_for_range, ":unused", 0, ":stack_size"),
          (troop_loot_troop, "trp_temp_troop", ":stack_troop", ":loot_probability"),
        (try_end),
      (try_end),
      
      #(troop_get_inventory_capacity, ":inv_cap", "trp_temp_troop"),
      #(try_for_range, ":i_slot", 0, ":inv_cap"),
      #  (troop_get_inventory_slot, ":item_id", "trp_temp_troop", ":i_slot"),
      #  (is_between, ":item_id", horses_begin, horses_end),
      #  (troop_set_inventory_slot, "trp_temp_troop", ":i_slot", -1),
      #(try_end),
      
      (troop_get_inventory_capacity, ":inv_cap", "trp_temp_troop"),
      (try_for_range, ":i_slot", 0, ":inv_cap"),
        (troop_get_inventory_slot, ":item_id", "trp_temp_troop", ":i_slot"),
        (ge, ":item_id", 0),
        (val_add, ":num_looted_items", 1),
        #vc loot changes begin
        (troop_get_inventory_slot_modifier, ":item_mod" , "trp_temp_troop", ":i_slot"),
        (call_script, "script_store_item_price", ":item_id", ":item_mod"),
        (val_add, "$loot_value_before", reg0),
        #vc loot changes end
      (try_end),
      
      (assign, reg0, ":num_looted_items"),
  ]),
  
  #script_calculate_main_party_shares:
  # INPUT:
  # Returns number of player party shares in reg0
  ("calculate_main_party_shares",
    [
      (assign, ":num_player_party_shares", player_loot_share),
      # Add shares for player's party
      (party_get_num_companion_stacks, ":num_stacks", "p_main_party"),
      (try_for_range, ":i_stack", 1, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop", "p_main_party", ":i_stack"),
        (try_begin),
          (neg|troop_is_hero, ":stack_troop"),
          (party_stack_get_size, ":stack_size","p_main_party",":i_stack"),
          (val_add, ":num_player_party_shares", ":stack_size"),
        (else_try),
          (val_add, ":num_player_party_shares", hero_loot_share),
        (try_end),
      (try_end),
      
      (assign, reg0, ":num_player_party_shares"),
  ]),
  
  #script_party_give_xp_and_gold:
  # INPUT:
  # param1: destroyed Party-id
  # calculates and gives player paty's share of gold and xp.
  
  ("party_give_xp_and_gold",
    [
      (store_script_param_1, ":enemy_party"), #Party_id
      
      (call_script, "script_calculate_main_party_shares"),
      (assign, ":num_player_party_shares", reg0),
      
      (assign, ":total_gain", 0),
      (party_get_num_companion_stacks, ":num_stacks",":enemy_party"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id,     ":stack_troop",":enemy_party",":i_stack"),
        (neg|troop_is_hero, ":stack_troop"),
        (party_stack_get_size, ":stack_size",":enemy_party",":i_stack"),
        (store_character_level, ":level", ":stack_troop"),
        (store_add, ":gain", ":level", 10),
        (val_mul, ":gain", ":gain"),
        (val_div, ":gain", 10),
        (store_mul, ":stack_gain", ":gain", ":stack_size"),
        (val_add, ":total_gain", ":stack_gain"),
      (try_end),
      
      (val_mul, ":total_gain", "$g_strength_contribution_of_player"),
      (val_div, ":total_gain", 100),
      
      (val_min, ":total_gain", 40000), #eliminate negative results
      
      (assign, ":player_party_xp_gain", ":total_gain"),
      
      (store_random_in_range, ":r", 50, 100),
      (val_mul, ":player_party_xp_gain", ":r"),
      (val_div, ":player_party_xp_gain", 100),
      
      (party_add_xp, "p_main_party", ":player_party_xp_gain"),
      
      (store_mul, ":player_gold_gain", ":total_gain", player_loot_share),
      (val_min, ":player_gold_gain", 60000), #eliminate negative results
      (store_random_in_range, ":r", 50, 100),
      (val_mul, ":player_gold_gain", ":r"),
      (val_div, ":player_gold_gain", 100),
      (val_div, ":player_gold_gain", ":num_player_party_shares"),
      
      #add gold now
      (party_get_num_companion_stacks, ":num_stacks","p_main_party"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop","p_main_party",":i_stack"),
        (try_begin),
          (troop_is_hero, ":stack_troop"),
          (call_script, "script_troop_add_gold", ":stack_troop", ":player_gold_gain"),
        (try_end),
      (try_end),
  ]),
  
  
  #script_setup_troop_meeting:
  # INPUT:
  # param1: troop_id with which meeting will be made.
  # param2: troop_dna (optional)
  ("setup_troop_meeting",
    [
      (store_script_param_1, ":meeting_troop"),
      (store_script_param_2, ":troop_dna"),
      (call_script, "script_get_meeting_scene"),
      (assign, ":meeting_scene", reg0),
      (modify_visitors_at_site,":meeting_scene"),
      (reset_visitors),
      (set_visitor,0,"trp_player"),
      (try_begin),
        (gt, ":troop_dna", -1),
        (set_visitor,17,":meeting_troop",":troop_dna"),
      (else_try),
        (set_visitor,17,":meeting_troop"),
      (try_end),
      (set_jump_mission,"mt_conversation_encounter"),
      (jump_to_scene,":meeting_scene"),
      (change_screen_map_conversation, ":meeting_troop"),
  ]),
  
  #script_setup_party_meeting:
  # INPUT:
  # param1: Party-id with which meeting will be made.
  ("setup_party_meeting",
    [
      (store_script_param_1, ":meeting_party"),
      (try_begin),
        (lt, "$g_encountered_party_relation", 0), #hostile
        #        (call_script, "script_music_set_situation_with_culture", mtf_sit_encounter_hostile),
      (try_end),
      (call_script, "script_get_meeting_scene"),
      (assign, ":meeting_scene", reg0),
      (modify_visitors_at_site,":meeting_scene"),
      (reset_visitors),
      (set_visitor,0,"trp_player"),
      
      (try_begin),
        (is_between, ":meeting_party", walled_centers_begin, walled_centers_end),
        (assign, ":max_level", 0),
        (assign, ":meeting_troop", 0),
        (party_get_num_companion_stacks, ":num_stacks", ":meeting_party"),
        (try_for_range, ":i_stack", 0, ":num_stacks"),
          (party_stack_get_troop_id, ":stack_troop", ":meeting_party", ":i_stack"),
          (store_character_level, ":troop_level", ":stack_troop"),
          (lt, ":max_level", ":troop_level"),
          (assign, ":max_level", ":troop_level"),
          (assign, ":meeting_troop", ":stack_troop"),
        (try_end),
        
        (gt, ":max_level", 0),
        (set_visitor,17,":meeting_troop"),
        
      (else_try),
        (party_stack_get_troop_id, ":meeting_troop",":meeting_party",0),
        (party_stack_get_troop_dna,":troop_dna",":meeting_party",0),
        (set_visitor,17,":meeting_troop",":troop_dna"),
      (try_end),
      
      (set_jump_mission,"mt_conversation_encounter"),
      (jump_to_scene,":meeting_scene"),
      (change_screen_map_conversation, ":meeting_troop"),
  ]),
  
  #script_get_meeting_scene: moto chief cambia
  # INPUT: none
  # OUTPUT: reg0 contain suitable scene_no
  
  ("get_meeting_scene",
    [
      (party_get_current_terrain, ":terrain_type", "p_main_party"),
      (assign, ":scene_to_use", "scn_random_scene"),
      (try_begin),
        (eq, ":terrain_type", rt_steppe),
        (assign, ":scene_to_use", "scn_meeting_scene_plain"),
      (else_try),
        (eq, ":terrain_type", rt_plain),
        (assign, ":scene_to_use", "scn_meeting_scene_plain"),
      (else_try),
        (eq, ":terrain_type", rt_snow),
        (assign, ":scene_to_use", "scn_meeting_scene_snow"),
      (else_try),
        (eq, ":terrain_type", rt_desert),
        (assign, ":scene_to_use", "scn_meeting_scene_plain"),
      (else_try),
        (eq, ":terrain_type", rt_steppe_forest),
        (assign, ":scene_to_use", "scn_meeting_scene_plain"),
      (else_try),
        (eq, ":terrain_type", rt_forest),
        (assign, ":scene_to_use", "scn_meeting_scene_plain"),
      (else_try),
        (eq, ":terrain_type", rt_snow_forest),
        (assign, ":scene_to_use", "scn_meeting_scene_snow"),
      (else_try),
        (eq, ":terrain_type", rt_desert_forest),
        (assign, ":scene_to_use", "scn_meeting_scene_plain"),
        ### PHAIAK chief sea battle begin (
      (else_try),
        (this_or_next|eq, ":terrain_type", rt_water),
        (this_or_next|eq, ":terrain_type", rt_bridge),	#used in BW as navigable water
        (eq, ":terrain_type", rt_river),
        (assign, ":scene_to_use", "scn_meeting_scene_water"),
        ### ) PHAIAK end
      (else_try),
        (assign, ":scene_to_use", "scn_meeting_scene_plain"),
      (try_end),
      (assign, reg0, ":scene_to_use"),
  ]),
  
  
  #script_party_remove_all_companions:
  # INPUT:
  # param1: Party-id from which  companions will be removed.
  # "$g_move_heroes" : controls if heroes will also be removed.
  
  ("party_remove_all_companions",
    [
      (store_script_param_1, ":party"), #Source Party_id
      (party_get_num_companion_stacks, ":num_companion_stacks",":party"),
      (try_for_range_backwards, ":stack_no", 0, ":num_companion_stacks"),
        (party_stack_get_troop_id, ":stack_troop",":party",":stack_no"),
        
        (party_stack_get_size, ":stack_size", ":party", ":stack_no"),
        
        (try_begin),
          (troop_is_hero, ":stack_troop"),
          (neg|is_between, ":stack_troop", pretenders_begin, pretenders_end),
          (neg|is_between, ":stack_troop", companions_begin, companions_end), #chief companions dont leave player when he get defeath or prisoner
          (neq, ":stack_troop", "trp_player"),
          (eq, "$g_prison_heroes", 1),
          (eq, ":party", "p_main_party"),
          (store_random_in_range, ":succeed_escaping", 0, 2),
          (neq, ":succeed_escaping", 0), #50% chance companion stays with us.
          (troop_set_health, ":stack_troop", 100), #heal before leaving
          (store_faction_of_party, ":enemy_faction", "$g_enemy_party"),
          (assign, ":minimum_distance", 99999),
          (assign, ":prison_center", -1),
          (try_for_range, ":center", walled_centers_begin, walled_centers_end),
            (store_faction_of_party, ":center_faction", ":center"),
            (eq, ":center_faction", ":enemy_faction"),
            (store_distance_to_party_from_party, ":dist", ":center", "p_main_party"),
            (lt, ":dist", ":minimum_distance"),
            (assign, ":minimum_distance", ":dist"),
            (assign, ":prison_center", ":center"),
          (try_end),
          (assign, reg1, ":prison_center"),
          #(display_message, "@{!}DEBUG : prison center is {reg1}"),
          (try_begin),
            (ge, ":prison_center", 0),
            (store_random_in_range, ":succeed_escaping", 0, 4),
            (neq, ":succeed_escaping", 0), #25% chance companion escapes to a tavern.
            (party_add_prisoners, ":prison_center", ":stack_troop", ":stack_size"),
            (troop_set_slot, ":stack_troop", slot_troop_prisoner_of_party, ":prison_center"),
            (troop_set_slot, ":stack_troop", slot_troop_playerparty_history, pp_history_scattered),
            (troop_set_slot, ":stack_troop", slot_troop_turned_down_twice, 0),
            (troop_set_slot, ":stack_troop", slot_troop_occupation, 0),
            (party_remove_members, ":party", ":stack_troop", ":stack_size"),
            (try_begin),
              (eq, "$cheat_mode", 1),
              (str_store_party_name, s1, ":prison_center"),
              (display_message, "str_your_hero_prisoned_at_s1"),
            (try_end),
          (else_try),
            #bandits or deserters won and captured companion. So place it randomly in a town's tavern.
            (assign, ":end_condition", 1000),
            (try_for_range, ":unused", 0, ":end_condition"),
              (store_random_in_range, ":town_no", towns_begin, towns_end),
              (neg|troop_slot_eq, ":stack_troop", slot_troop_home, ":town_no"),
              (neg|troop_slot_eq, ":stack_troop", slot_troop_first_encountered, ":town_no"),
              (assign, ":end_condition", -1),
            (try_end),
            (troop_set_slot, ":stack_troop", slot_troop_cur_center, ":town_no"),
            (troop_set_slot, ":stack_troop", slot_troop_playerparty_history, pp_history_scattered),
            (troop_set_slot, ":stack_troop", slot_troop_turned_down_twice, 0),
            (troop_set_slot, ":stack_troop", slot_troop_occupation, 0),
            (party_remove_members, ":party", ":stack_troop", ":stack_size"),
            (try_begin),
              (eq, "$cheat_mode", 1),
              (str_store_troop_name, 4, ":stack_troop"),
              (str_store_party_name, 5, ":town_no"),
              (display_message, "@{!}{s4} is sent to {s5} after defeat"),
            (try_end),
          (try_end),
        (else_try),
          (this_or_next|neg|troop_is_hero, ":stack_troop"),
          (eq, "$g_move_heroes", 1),
          (party_remove_members, ":party", ":stack_troop", ":stack_size"),
        (try_end),
      (try_end),
  ]),
  
  #script_party_remove_all_prisoners:
  # INPUT:
  # param1: Party-id from which  prisoners will be removed.
  # "$g_move_heroes" : controls if heroes will also be removed.
  
  ("party_remove_all_prisoners",
    [
      (store_script_param_1, ":party"), #Source Party_id
      (party_get_num_prisoner_stacks, ":num_prisoner_stacks",":party"),
      (try_for_range_backwards, ":stack_no", 0, ":num_prisoner_stacks"),
        (party_prisoner_stack_get_troop_id, ":stack_troop",":party",":stack_no"),
        (this_or_next|neg|troop_is_hero, ":stack_troop"),
        (eq, "$g_move_heroes", 1),
        (party_prisoner_stack_get_size, ":stack_size",":party",":stack_no"),
        (party_remove_prisoners, ":party", ":stack_troop", ":stack_size"),
      (try_end),
  ]),
  
  #script_party_add_party_companions:
  # INPUT:
  # param1: Party-id to add the second part
  # param2: Party-id which will be added to the first one.
  # "$g_move_heroes" : controls if heroes will also be added.
  
  ("party_add_party_companions",
    [
      (store_script_param_1, ":target_party"), #Target Party_id
      (store_script_param_2, ":source_party"), #Source Party_id
      (party_get_num_companion_stacks, ":num_stacks",":source_party"),
      (try_for_range, ":stack_no", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop",":source_party",":stack_no"),
        (this_or_next|neg|troop_is_hero, ":stack_troop"),
        (eq, "$g_move_heroes", 1),
        (party_stack_get_size, ":stack_size",":source_party",":stack_no"),
        (party_add_members, ":target_party", ":stack_troop", ":stack_size"),
        (party_stack_get_num_wounded, ":num_wounded", ":source_party", ":stack_no"),
        (party_wound_members, ":target_party", ":stack_troop", ":num_wounded"),
      (try_end),
  ]),
  
  #script_party_add_party_prisoners:
  # INPUT:
  # param1: Party-id to add the second party
  # param2: Party-id which will be added to the first one.
  # "$g_move_heroes" : controls if heroes will also be added.
  
  ("party_add_party_prisoners",
    [
      (store_script_param_1, ":target_party"), #Target Party_id
      (store_script_param_2, ":source_party"), #Source Party_id
      (party_get_num_prisoner_stacks, ":num_stacks",":source_party"),
      (try_for_range, ":stack_no", 0, ":num_stacks"),
        (party_prisoner_stack_get_troop_id, ":stack_troop",":source_party",":stack_no"),
        (this_or_next|neg|troop_is_hero, ":stack_troop"),
        (eq, "$g_move_heroes", 1),
        (party_prisoner_stack_get_size, ":stack_size",":source_party",":stack_no"),
        (party_add_members, ":target_party", ":stack_troop", ":stack_size"),
      (try_end),
  ]),
  
  #script_party_prisoners_add_party_companions:
  # INPUT:
  # param1: Party-id to add the second part
  # param2: Party-id which will be added to the first one.
  # "$g_move_heroes" : controls if heroes will also be added.
  
  ("party_prisoners_add_party_companions",
    [
      (store_script_param_1, ":target_party"), #Target Party_id
      (store_script_param_2, ":source_party"), #Source Party_id
      (party_get_num_companion_stacks, ":num_stacks",":source_party"),
      (try_for_range, ":stack_no", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop",":source_party",":stack_no"),
        (this_or_next|neg|troop_is_hero, ":stack_troop"),
        (eq, "$g_move_heroes", 1),
        (party_stack_get_size, ":stack_size",":source_party",":stack_no"),
        (party_add_prisoners, ":target_party", ":stack_troop", ":stack_size"),
      (try_end),
  ]),
  
  #script_party_prisoners_add_party_prisoners:
  # INPUT:
  # param1: Party-id to add the second part
  # param2: Party-id which will be added to the first one.
  # "$g_move_heroes" : controls if heroes will also be added.
  
  ("party_prisoners_add_party_prisoners",
    [
      (store_script_param_1, ":target_party"), #Target Party_id
      (store_script_param_2, ":source_party"), #Source Party_id
      (party_get_num_prisoner_stacks, ":num_stacks",":source_party"),
      (try_for_range, ":stack_no", 0, ":num_stacks"),
        (party_prisoner_stack_get_troop_id, ":stack_troop",":source_party",":stack_no"),
        (this_or_next|neg|troop_is_hero, ":stack_troop"),
        (eq, "$g_move_heroes", 1),
        (party_prisoner_stack_get_size, ":stack_size",":source_party",":stack_no"),
        (party_add_prisoners, ":target_party", ":stack_troop", ":stack_size"),
        
        (ge, ":target_party", centers_begin), #target party can take prisoners?
        (troop_is_hero, ":stack_troop"),
        (troop_set_slot, ":stack_troop", slot_troop_prisoner_of_party, ":target_party"),
      (try_end),
  ]),
  
  # script_party_add_party:
  # INPUT:
  # param1: Party-id to add the second part
  # param2: Party-id which will be added to the first one.
  # "$g_move_heroes" : controls if heroes will also be added.
  
  ("party_add_party",
    [
      (store_script_param_1, ":target_party"), #Target Party_id
      (store_script_param_2, ":source_party"), #Source Party_id
      (call_script, "script_party_add_party_companions",          ":target_party", ":source_party"),
      (call_script, "script_party_prisoners_add_party_prisoners", ":target_party", ":source_party"),
  ]),
  
  
  #script_party_copy:
  # INPUT:
  # param1: Party-id to copy the second party
  # param2: Party-id which will be copied to the first one.
  
  ("party_copy",
    [
      (assign, "$g_move_heroes", 1),
      (store_script_param_1, ":target_party"), #Target Party_id
      (store_script_param_2, ":source_party"), #Source Party_id
      (party_clear, ":target_party"),
      (call_script, "script_party_add_party", ":target_party", ":source_party"),
  ]),
  
  
  #script_clear_party_group:
  # INPUT:
  # param1: Party-id of the root of the group.
  # This script will clear the root party and all parties attached to it recursively.
  
  ("clear_party_group",
    [
      (store_script_param_1, ":root_party"),
      
      (party_clear, ":root_party"),
      (party_get_num_attached_parties, ":num_attached_parties", ":root_party"),
      (try_for_range, ":attached_party_rank", 0, ":num_attached_parties"),
        (party_get_attached_party_with_rank, ":attached_party", ":root_party", ":attached_party_rank"),
        (call_script, "script_clear_party_group", ":attached_party"),
      (try_end),
  ]),
  
  
  #script_party_add_wounded_members_as_prisoners:
  # INPUT:
  # param1: Party-id to add the second party
  # param2: Party-id which will be added to the first one.
  # "$g_move_heroes" : controls if heroes will also be added.
  
  ("party_add_wounded_members_as_prisoners",
    [
      (store_script_param_1, ":target_party"), #Target Party_id
      (store_script_param_2, ":source_party"), #Source Party_id
      (party_get_num_companion_stacks, ":num_stacks", ":source_party"),
      (try_for_range, ":stack_no", 0, ":num_stacks"),
        (party_stack_get_num_wounded, ":num_wounded", ":source_party", ":stack_no"),
        (ge, ":num_wounded", 1),
        (party_stack_get_troop_id, ":stack_troop", ":source_party", ":stack_no"),
        (this_or_next|neg|troop_is_hero, ":stack_troop"),
        (eq, "$g_move_heroes", 1),
        #(party_prisoner_stack_get_size, ":stack_size",":source_party",":stack_no"),
        (party_add_prisoners, ":target_party", ":stack_troop", ":num_wounded"),
      (try_end),
  ]),
  
  
  #script_get_nonempty_party_in_group:
  # INPUT:
  # param1: Party-id of the root of the group.
  # OUTPUT: reg0: nonempy party-id
  
  ("get_nonempty_party_in_group",
    [
      (store_script_param_1, ":party_no"),
      (party_get_num_companion_stacks, ":num_companion_stacks", ":party_no"),
      (try_begin),
        (gt, ":num_companion_stacks", 0),
        (assign, reg0, ":party_no"),
      (else_try),
        (assign, reg0, -1),
        
        (party_get_num_attached_parties, ":num_attached_parties", ":party_no"),
        (try_for_range, ":attached_party_rank", 0, ":num_attached_parties"),
          (lt, reg0, 0),
          (party_get_attached_party_with_rank, ":attached_party", ":party_no", ":attached_party_rank"),
          (call_script, "script_get_nonempty_party_in_group", ":attached_party"),
        (try_end),
      (try_end),
  ]),
  
  #script_collect_prisoners_from_empty_parties:
  # INPUT:
  # param1: Party-id of the root of the group.
  # param2: Party to collect prisoners in.
  # make sure collection party is cleared before calling this.
  
  ("collect_prisoners_from_empty_parties",
    [
      (store_script_param_1, ":party_no"),
      (store_script_param_2, ":collection_party"),
      
      (party_get_num_companions, ":num_companions", ":party_no"),
      (try_begin),
        (eq, ":num_companions", 0), #party is empty (has no companions). Collect its prisoners.
        (party_get_num_prisoner_stacks, ":num_stacks",":party_no"),
        (try_for_range, ":stack_no", 0, ":num_stacks"),
          (party_prisoner_stack_get_troop_id, ":stack_troop", ":party_no", ":stack_no"),
          (troop_is_hero, ":stack_troop"),
          (party_add_members, ":collection_party", ":stack_troop", 1),
        (try_end),
      (try_end),
      (party_get_num_attached_parties, ":num_attached_parties", ":party_no"),
      (try_for_range, ":attached_party_rank", 0, ":num_attached_parties"),
        (party_get_attached_party_with_rank, ":attached_party", ":party_no", ":attached_party_rank"),
        (call_script, "script_collect_prisoners_from_empty_parties", ":attached_party", ":collection_party"),
      (try_end),
  ]),
  
  #script_change_party_morale:
  # INPUT: party_no, morale_gained
  # OUTPUT: none
  
  ("change_party_morale",
    [
      (store_script_param_1, ":party_no"),
      (store_script_param_2, ":morale_dif"),
      
      (party_get_morale, ":cur_morale", ":party_no"),
      (store_add, ":new_morale", ":cur_morale", ":morale_dif"),
      (val_clamp, ":new_morale", 0, 100),
      (party_set_morale, ":party_no", ":new_morale"),
      (str_store_party_name, s1, ":party_no"),
      
      (try_begin),
        (lt, ":new_morale", ":cur_morale"),
        (store_sub, reg1, ":cur_morale", ":new_morale"),
      (else_try),
        (gt, ":new_morale", ":cur_morale"),
        (store_sub, reg1, ":new_morale", ":cur_morale"),
      (try_end),
  ]),
  
  #script_count_casualties_and_adjust_morale:
  # INPUT: none
  # OUTPUT: none
  
  ("count_casualties_and_adjust_morale",
    [
      (try_begin), #moto chief
        (ge, "$g_enemy_party", 0), ##moto chief change enemy morale if and only if there is a valid enemy party (with party_id >= 0)
        (call_script, "script_calculate_main_party_shares"),
        (assign, ":num_player_party_shares", reg0),
        
        (assign, ":our_loss_score", 0),
        (party_get_num_companion_stacks, ":num_stacks","p_player_casualties"),
        (try_for_range, ":i_stack", 0, ":num_stacks"),
          (party_stack_get_troop_id, ":stack_troop", "p_player_casualties", ":i_stack"),
          (party_stack_get_size, ":stack_size", "p_player_casualties", ":i_stack"),
          
          (party_stack_get_num_wounded, ":num_wounded", "p_player_casualties", ":i_stack"),
          (store_mul, ":stack_size_mul_2", ":stack_size", 2),
          (store_sub, ":stack_size_mul_2_sub_wounded", ":stack_size_mul_2", ":num_wounded"),#JuJu70 - missing minuend?
          
          (store_character_level, ":level", ":stack_troop"),
          (store_add, ":gain", ":level", 3),
          
          #if died/wounded troop is player troop then give its level +30 while calculating troop die effect on morale
          (try_begin),
            (eq, ":stack_troop", "trp_player"),
            (val_add, ":level", 75),
          (else_try),
            (troop_is_hero, ":stack_troop"),
            (val_add, ":level", 50),
          (try_end),
          
          (val_mul, ":gain", ":gain"),
          (val_div, ":gain", 10),
          (assign, reg0, ":gain"),
          (val_mul, ":gain", ":stack_size"),
          
          (try_begin),
            (neg|troop_is_hero, ":stack_troop"),
            (val_mul, ":gain", ":stack_size_mul_2_sub_wounded"),
            (val_div, ":gain", ":stack_size_mul_2"),
          (try_end),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (assign, reg1, ":stack_size"),
            (assign, reg2, ":gain"),
            (display_message, "str_our_per_person__reg0_num_people__reg1_total_gain__reg2"),
          (try_end),
          (val_add, ":our_loss_score", ":gain"),
        (try_end),
        
        (assign, ":died_enemy_population", 0),
        (assign, ":enemy_loss_score", 0),
        (party_get_num_companion_stacks, ":num_stacks","p_enemy_casualties"),
        (try_for_range, ":i_stack", 0, ":num_stacks"),
          (party_stack_get_troop_id, ":stack_troop", "p_enemy_casualties", ":i_stack"),
          (party_stack_get_size, ":stack_size", "p_enemy_casualties", ":i_stack"),
          
          (party_stack_get_num_wounded, ":num_wounded", "p_enemy_casualties", ":i_stack"),
          (store_mul, ":stack_size_mul_2", ":stack_size", 2),
          (store_sub, ":stack_size_mul_2_sub_wounded", ":stack_size_mul_2", ":num_wounded"),#JuJu70 - missing minuend?
          
          (store_character_level, ":level", ":stack_troop"),
          (store_add, ":gain", ":level", 3),
          
          #if troop is hero give extra +15 level while calculating troop die effect on morale
          (try_begin),
            (troop_is_hero, ":stack_troop"),
            (val_add, ":level", 50),
          (try_end),
          
          (val_mul, ":gain", ":gain"),
          (val_div, ":gain", 10),
          (assign, reg0, ":gain"),
          (val_mul, ":gain", ":stack_size"),
          
          (try_begin),
            (neg|troop_is_hero, ":stack_troop"),
            (val_mul, ":gain", ":stack_size_mul_2_sub_wounded"),
            (val_div, ":gain", ":stack_size_mul_2"),
          (try_end),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (assign, reg1, ":stack_size"),
            (assign, reg2, ":gain"),
            (display_message, "str_ene_per_person__reg0_num_people__reg1_total_gain__reg2"),
          (try_end),
          (val_add, ":enemy_loss_score", ":gain"),
          (val_add, ":died_enemy_population", ":stack_size"),
        (try_end),
        
        (assign, ":ally_loss_score", 0),
        (try_begin),
          (eq, "$any_allies_at_the_last_battle", 1),
          (party_get_num_companion_stacks, ":num_stacks","p_ally_casualties"),
          (try_for_range, ":i_stack", 0, ":num_stacks"),
            (party_stack_get_troop_id, ":stack_troop", "p_ally_casualties", ":i_stack"),
            (party_stack_get_size, ":stack_size", "p_ally_casualties", ":i_stack"),
            
            (party_stack_get_num_wounded, ":num_wounded", "p_ally_casualties", ":i_stack"),
            (store_mul, ":stack_size_mul_2", ":stack_size", 2),
            (store_sub, ":stack_size_mul_2_sub_wounded", ":stack_size_mul_2", ":num_wounded"),
            
            (store_character_level, ":level", ":stack_troop"),
            (store_add, ":gain", ":level", 3),
            
            #if troop is hero give extra +15 level while calculating troop die effect on morale
            (try_begin),
              (troop_is_hero, ":stack_troop"),
              (val_add, ":level", 50),
            (try_end),
            
            (val_mul, ":gain", ":gain"),
            (val_div, ":gain", 10),
            (assign, reg0, ":gain"),
            (val_mul, ":gain", ":stack_size"),
            
            (try_begin),
              (neg|troop_is_hero, ":stack_troop"),
              (val_mul, ":gain", ":stack_size_mul_2_sub_wounded"),
              (val_div, ":gain", ":stack_size_mul_2"),
            (try_end),
            
            (try_begin),
              (eq, "$cheat_mode", 1),
              (assign, reg1, ":stack_size"),
              (assign, reg2, ":gain"),
              (display_message, "str_all_per_person__reg0_num_people__reg1_total_gain__reg2"),
            (try_end),
            (val_add, ":ally_loss_score", ":gain"),
          (try_end),
        (try_end),
        
        (store_add, ":our_losses", ":our_loss_score", ":ally_loss_score"),
        (assign, ":enemy_losses", ":enemy_loss_score"),
        (val_mul, ":our_losses", 100),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (assign, reg0, ":enemy_losses"),
          (display_message, "@{!}DEBUGS : enemy_loses : {reg0}"),
        (try_end),
        
        (try_begin),
          (gt, ":enemy_losses", 0),
          (store_div, ":loss_ratio", ":our_losses", ":enemy_losses"),
        (else_try),
          (assign, ":loss_ratio", 1000),
        (try_end),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (assign, reg1, ":loss_ratio"),
          (display_message, "str_loss_ratio_is_reg1"),
        (try_end),
        
        (try_begin),
          (neg|is_between, "$g_enemy_party", centers_begin, centers_end),
          (store_sub, ":total_gain", 60, ":loss_ratio"),
        (else_try),
          (store_sub, ":total_gain", 100, ":loss_ratio"),
        (try_end),
        
        (try_begin),
          (lt, ":total_gain", 0),
          (val_div, ":total_gain", 2),
        (try_end),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (assign, reg0, ":total_gain"),
          (display_message, "@{!}DEBUGS1 : total_gain : {reg0}"),
        (try_end),
        
        (val_max, ":total_gain", -60), #total gain changes between -60(1.8+ loss ratio) and 60(0 loss ratio). We assumed average loss ratio is 0.6
        (val_mul, ":total_gain", ":enemy_losses"),
        (val_div, ":total_gain", 100),
        
        (store_mul, ":total_enemy_morale_gain", ":total_gain", -1), #enemies get totally negative of the morale we get
        (val_mul, ":total_gain", "$g_strength_contribution_of_player"),
        (val_div, ":total_gain", 100),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (assign, reg0, ":total_gain"),
          (display_message, "@{!}DEBUGS2 : total_gain : {reg0}"),
        (try_end),
        
        (try_begin),
          (party_is_active, "$g_enemy_party"), #change enemy morale if and only if there is a valid enemy party
          
          #main enemy party
          (assign, ":total_enemy_population", 0),
          (val_add, ":total_enemy_population", 10), #every part effect total population by number of agents they have plus 10
          (party_get_num_companion_stacks, ":num_stacks", "$g_enemy_party"),
          (try_for_range, ":i_stack", 0, ":num_stacks"),
            (party_stack_get_troop_id, ":stack_troop", "$g_enemy_party", ":i_stack"),
            (party_stack_get_size, ":stack_size", "$g_enemy_party", ":i_stack"),
            (val_add, ":total_enemy_population", ":stack_size"),
          (try_end),
          (assign, ":main_enemy_party_population", ":total_enemy_population"),
          
          #enemy attachers
          (party_get_num_attached_parties, ":num_attached_parties", "$g_enemy_party"),
          (try_for_range, ":attached_party_rank", 0, ":num_attached_parties"),
            (val_add, ":total_enemy_population", 10), #every part effect total population by number of agents they have plus 10
            (party_get_attached_party_with_rank, ":attached_party", "$g_enemy_party", ":attached_party_rank"),
            (party_get_num_companion_stacks, ":num_stacks", ":attached_party"),
            (try_for_range, ":i_stack", 0, ":num_stacks"),
              (party_stack_get_troop_id, ":stack_troop", ":attached_party", ":i_stack"),
              (party_stack_get_size, ":stack_size", ":attached_party", ":i_stack"),
              (val_add, ":total_enemy_population", ":stack_size"),
            (try_end),
          (try_end),
          
          #(assign, reg3, ":total_enemy_population"),
          #(assign, reg4, ":died_enemy_population"),
          #(store_sub, ":remaining_enemy_population", ":total_enemy_population", ":died_enemy_population"),
          #(val_add, ":remaining_enemy_population", 10),
          #(assign, reg5, ":remaining_enemy_population"),
          #(display_message, "@total : {reg3}, died : {reg4}, remaining : {reg5}"),
          
          #remaining enemy population has 10+remaining soldiers in enemy party
          (assign, ":remaining_enemy_population", ":total_enemy_population"),
          
          (assign, reg5, ":remaining_enemy_population"),
          (assign, reg6, ":total_enemy_morale_gain"),
          
          (set_fixed_point_multiplier, 100),
          (val_mul, ":remaining_enemy_population", 100),
          (store_sqrt, ":sqrt_remaining_enemy_population", ":remaining_enemy_population"),
          (val_div, ":sqrt_remaining_enemy_population", 100),
          (val_div, ":total_enemy_morale_gain", ":sqrt_remaining_enemy_population"),
          (val_div, ":total_enemy_morale_gain", 4),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (assign, reg7, ":total_enemy_morale_gain"),
            (display_message, "str_total_enemy_morale_gain__reg6_last_total_enemy_morale_gain__reg7_remaining_enemy_population__reg5"),
          (try_end),
          
          (store_mul, ":party_morale_gain", ":total_enemy_morale_gain", ":main_enemy_party_population"),
          (val_div, ":party_morale_gain", ":total_enemy_population"),
          
          (try_begin),
            (party_is_active, "$g_enemy_party"),
            
            (call_script, "script_change_party_morale", "$g_enemy_party", ":party_morale_gain"),
            
            (party_get_num_attached_parties, ":num_attached_parties", "$g_enemy_party"),
            (try_for_range, ":attached_party_rank", 0, ":num_attached_parties"),
              (party_get_attached_party_with_rank, ":attached_party", "$g_enemy_party", ":attached_party_rank"),
              (party_get_num_companion_stacks, ":num_stacks", ":attached_party"),
              (assign, ":party_population", 0),
              (try_for_range, ":i_stack", 0, ":num_stacks"),
                (party_stack_get_troop_id, ":stack_troop", ":attached_party", ":i_stack"),
                (party_stack_get_size, ":stack_size", ":attached_party", ":i_stack"),
                (val_add, ":party_population", ":stack_size"),
              (try_end),
              #(store_div, ":party_ratio", ":total_enemy_population_multiplied_by_100", ":party_population"), #party ratio changes between 0..100, shows population ratio of that party among all enemy parties
              (store_mul, ":party_morale_gain", ":total_enemy_morale_gain", ":party_population"),
              (val_div, ":party_morale_gain", ":total_enemy_population"),
              (call_script, "script_change_party_morale", ":attached_party", ":party_morale_gain"),
            (try_end),
          (try_end),
        (try_end),
        
        #Add morale
        (assign, ":morale_gain", ":total_gain"),
        (val_div, ":morale_gain", ":num_player_party_shares"),#if there are lots of soldiers in my party there will be less morale increase.
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (assign, reg0, ":num_player_party_shares"),
          (assign, reg1, ":total_gain"),
          (display_message, "@{!}DEBUGS3 : num_player_party_shares:{reg0}, total_gain:{reg1}"),
        (try_end),
        
        (call_script, "script_change_player_party_morale", ":morale_gain"),
        
        (store_mul, ":killed_enemies_by_our_soldiers", ":died_enemy_population", "$g_strength_contribution_of_player"),
        (store_div, ":faction_morale_change", ":killed_enemies_by_our_soldiers", 8), #each 8 killed agent with any faction decreases morale of troops belong to that faction in our party by 1.
        (try_begin),
          (gt, ":faction_morale_change", 2000),
          (assign, ":faction_morale_change", 2000),
        (try_end),
        
        (try_begin), #here we give positive morale to our troops of with same faction of ally party with 2/3x multipication.
          (ge, "$g_ally_party", 0),
          
          (store_div, ":ally_faction_morale_change", ":faction_morale_change", 3), #2/3x multipication (less than normal)
          (val_mul, ":ally_faction_morale_change", 2),
          (store_faction_of_party, ":ally_faction", "$g_ally_party"),
          (faction_get_slot, ":faction_morale", ":ally_faction",  slot_faction_morale_of_player_troops),
          (val_add, ":faction_morale", ":ally_faction_morale_change"),
          (faction_set_slot, ":ally_faction",  slot_faction_morale_of_player_troops, ":faction_morale"),
        (try_end),
        
        (try_begin), #here we give positive morale to our troops of owner of rescued village's faction after saving village from bandits by x3 bonus.
          (neg|party_is_active, "$g_enemy_party"),
          (ge, "$current_town", 0),
          
          (val_mul, ":faction_morale_change", 2), #2x bonus (more than normal)
          (store_faction_of_party, ":ally_faction", "$current_town"),
          (faction_get_slot, ":faction_morale", ":ally_faction",  slot_faction_morale_of_player_troops),
          (val_add, ":faction_morale", ":faction_morale_change"),
          (faction_set_slot, ":ally_faction",  slot_faction_morale_of_player_troops, ":faction_morale"),
        (else_try),
          (party_is_active, "$g_enemy_party"),
          (assign, ":currently_in_rebellion", 0),
          (try_begin),
            (eq, "$players_kingdom", "fac_player_supporters_faction"),
            (neg|faction_slot_eq, "fac_player_supporters_faction", slot_faction_leader, "trp_player"),
            (assign, ":currently_in_rebellion", 1),
          (try_end),
          (eq, ":currently_in_rebellion", 0),
          
          (store_div, ":faction_morale_change", ":faction_morale_change", 3), #2/3x multipication (less than normal)
          (val_mul, ":faction_morale_change", 2),
          (store_faction_of_party, ":enemy_faction", "$g_enemy_party"),
          (faction_get_slot, ":faction_morale", ":enemy_faction",  slot_faction_morale_of_player_troops),
          (val_sub, ":faction_morale", ":faction_morale_change"),
          (faction_set_slot, ":enemy_faction",  slot_faction_morale_of_player_troops, ":faction_morale"),
        (try_end),
      (try_end), #moto chief
  ]),
  
  #script_print_casualties_to_s0:
  # INPUT:
  # param1: Party_id, param2: 0 = use new line, 1 = use comma
  
  #OUTPUT:
  # string register 0.
  
  ("print_casualties_to_s0",
    [(store_script_param, ":party_no", 1),
      (store_script_param, ":use_comma", 2),
      (str_clear, s0),
      (assign, ":total_reported", 0),
      (assign, ":total_wounded", 0),
      (assign, ":total_killed", 0),
      (assign, ":total_routed", 0),
      (party_get_num_companion_stacks, ":num_stacks",":party_no"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop_id", ":party_no", ":i_stack"),
        (party_stack_get_size, ":stack_size", ":party_no", ":i_stack"),
        (party_stack_get_num_wounded, ":num_wounded", ":party_no", ":i_stack"),
        #get number of routed agent numbers
        (try_begin),
          (this_or_next|eq, ":party_no", "p_main_party"),
          (eq, ":party_no", "p_player_casualties"),
          (troop_get_slot, ":num_routed", ":stack_troop_id", slot_troop_player_routed_agents),
          (troop_set_slot, ":stack_troop_id", slot_troop_player_routed_agents, 0),
        (else_try),
          (party_get_attached_to, ":attached_to", ":party_no"),
          (this_or_next|eq, ":party_no", "p_ally_casualties"),
          (ge, ":attached_to", 0),
          (this_or_next|eq, ":party_no", "p_ally_casualties"),
          (eq, ":attached_to", "p_main_party"),
          (troop_get_slot, ":num_routed", ":stack_troop_id", slot_troop_ally_routed_agents),
          (troop_set_slot, ":stack_troop_id", slot_troop_ally_routed_agents, 0),
        (else_try),
          (troop_get_slot, ":num_routed", ":stack_troop_id", slot_troop_enemy_routed_agents),
          (troop_set_slot, ":stack_troop_id", slot_troop_enemy_routed_agents, 0),
        (try_end),
        (store_sub, ":num_killed", ":stack_size", ":num_wounded"),
        (val_sub, ":num_killed", ":num_routed"),
        (val_add, ":total_killed", ":num_killed"),
        (val_add, ":total_wounded", ":num_wounded"),
        (val_add, ":total_routed", ":num_routed"),
        (try_begin),
          (this_or_next|gt, ":num_killed", 0),
          (this_or_next|gt, ":num_wounded", 0),
          (gt, ":num_routed", 0),
          (store_add, reg3, ":num_killed", ":num_wounded"),
          (store_add, reg3, reg3, ":num_routed"),
          (str_store_troop_name_by_count, s1, ":stack_troop_id", reg3),
          (try_begin),
            (troop_is_hero, ":stack_troop_id"),
            (assign, reg3, 0),
          (try_end),
          (try_begin), #there are people who killed, wounded and routed.
            (gt, ":num_killed", 0),
            (gt, ":num_wounded", 0),
            (gt, ":num_routed", 0),
            (assign, reg4, ":num_killed"),
            (assign, reg5, ":num_wounded"),
            (assign, reg6, ":num_routed"),
            (str_store_string, s2, "str_reg4_killed_reg5_wounded_reg6_routed"),
          (else_try), #there are people who killed and routed.
            (gt, ":num_killed", 0),
            (gt, ":num_routed", 0),
            (assign, reg4, ":num_killed"),
            (assign, reg5, ":num_routed"),
            (str_store_string, s2, "str_reg4_killed_reg5_routed"),
          (else_try), #there are people who killed and wounded.
            (gt, ":num_killed", 0),
            (gt, ":num_wounded", 0),
            (assign, reg4, ":num_killed"),
            (assign, reg5, ":num_wounded"),
            (str_store_string, s2, "str_reg4_killed_reg5_wounded"),
          (else_try), #there are people who wounded and routed.
            (gt, ":num_wounded", 0),
            (gt, ":num_routed", 0),
            (assign, reg4, ":num_wounded"),
            (assign, reg5, ":num_routed"),
            (str_store_string, s2, "str_reg4_wounded_reg5_routed"),
          (else_try), #there are people who only killed.
            (gt, ":num_killed", 0),
            (assign, reg1, ":num_killed"),
            (str_store_string, s3, "@killed"),
            (str_store_string, s2, "str_reg1_blank_s3"),
          (else_try), #there are people who only wounded.
            (gt, ":num_wounded", 0),
            (assign, reg1, ":num_wounded"),
            (str_store_string, s3, "@wounded"),
            (str_store_string, s2, "str_reg1_blank_s3"),
          (else_try), #there are people who only routed.
            (assign, reg1, ":num_routed"),
            (str_store_string, s3, "str_routed"),
            (str_store_string, s2, "str_reg1_blank_s3"),
          (try_end),
          (try_begin),
            (eq, ":use_comma", 1),
            (try_begin),
              (eq, ":total_reported", 0),
              (str_store_string, s0, "@{!}{reg3?{reg3}:} {s1} ({s2})"),
            (else_try),
              (str_store_string, s0, "@{!}{s0}, {reg3?{reg3}:} {s1} ({s2})"),
            (try_end),
          (else_try),
            (str_store_string, s0, "@{!}{s0}^{reg3?{reg3}:} {s1} ({s2})"),
          (try_end),
          (val_add, ":total_reported", 1),
        (try_end),
      (try_end),
      (try_begin),
        (this_or_next|gt, ":total_killed", 0),
        (this_or_next|gt, ":total_wounded", 0),
        (gt, ":total_routed", 0),
        (store_add, ":total_agents", ":total_killed", ":total_wounded"),
        (val_add, ":total_agents", ":total_routed"),
        (assign, reg3, ":total_agents"),
        (try_begin),
          (gt, ":total_killed", 0),
          (gt, ":total_wounded", 0),
          (gt, ":total_routed", 0),
          (assign, reg4, ":total_killed"),
          (assign, reg5, ":total_wounded"),
          (assign, reg6, ":total_routed"),
          (str_store_string, s2, "str_reg4_killed_reg5_wounded_reg6_routed"),
        (else_try),
          (gt, ":total_killed", 0),
          (gt, ":total_routed", 0),
          (assign, reg4, ":total_killed"),
          (assign, reg5, ":total_routed"),
          (str_store_string, s2, "str_reg4_killed_reg5_routed"),
        (else_try),
          (gt, ":total_killed", 0),
          (gt, ":total_wounded", 0),
          (assign, reg4, ":total_killed"),
          (assign, reg5, ":total_wounded"),
          (str_store_string, s2, "str_reg4_killed_reg5_wounded"),
        (else_try),
          (gt, ":total_wounded", 0),
          (gt, ":total_routed", 0),
          (assign, reg4, ":total_wounded"),
          (assign, reg5, ":total_routed"),
          (str_store_string, s2, "str_reg4_wounded_reg5_routed"),
        (else_try),
          (gt, ":total_killed", 0),
          (str_store_string, s2, "@killed"),
        (else_try),
          (gt, ":total_wounded", 0),
          (str_store_string, s2, "@wounded"),
        (else_try),
          (str_store_string, s2, "str_routed"),
        (else_try),
        (try_end),
        (str_store_string, s0, "@{s0}^TOTAL: {reg3} ({s2})"),
      (else_try),
        (str_store_string, s0, "str_none"),
        (neq, ":use_comma", 1),
        (str_store_string, s0, "@^{s0}"),
      (try_end),
  ]),
  
  #script_write_fit_party_members_to_stack_selection
  # INPUT:
  # param1: party_no, exclude_leader
  #OUTPUT:
  # trp_stack_selection_amounts slots (slot 0 = number of stacks, 1 = number of men fit, 2..n = stack sizes (fit))
  # trp_stack_selection_ids slots (2..n = stack troops)
  ("write_fit_party_members_to_stack_selection",
    [
      (store_script_param, ":party_no", 1),
      (store_script_param, ":exclude_leader", 2),
      (party_get_num_companion_stacks, ":num_stacks", ":party_no"),
      (assign, ":slot_index", 2),
      (assign, ":total_fit", 0),
      (try_for_range, ":stack_index", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop", ":party_no", ":stack_index"),
        (assign, ":num_fit", 0),
        (try_begin),
          (troop_is_hero, ":stack_troop"),
          (try_begin),
            (neg|troop_is_wounded, ":stack_troop"),
            (this_or_next|eq, ":exclude_leader", 0),
            (neq, ":stack_index", 0),
            (assign, ":num_fit",1),
          (try_end),
        (else_try),
          (party_stack_get_size, ":num_fit", ":party_no", ":stack_index"),
          (party_stack_get_num_wounded, ":num_wounded", ":party_no", ":stack_index"),
          (val_sub, ":num_fit", ":num_wounded"),
        (try_end),
        (try_begin),
          (gt, ":num_fit", 0),
          (troop_set_slot, "trp_stack_selection_amounts", ":slot_index", ":num_fit"),
          (troop_set_slot, "trp_stack_selection_ids", ":slot_index", ":stack_troop"),
          (val_add, ":slot_index", 1),
        (try_end),
        (val_add, ":total_fit", ":num_fit"),
      (try_end),
      (val_sub, ":slot_index", 2),
      (troop_set_slot, "trp_stack_selection_amounts", 0, ":slot_index"),
      (troop_set_slot, "trp_stack_selection_amounts", 1, ":total_fit"),
  ]),
  
  #script_remove_fit_party_member_from_stack_selection
  # INPUT:
  # param1: slot_index
  #OUTPUT:
  # reg0 = troop_no
  # trp_stack_selection_amounts slots (slot 0 = number of stacks, 1 = number of men fit, 2..n = stack sizes (fit))
  # trp_stack_selection_ids slots (2..n = stack troops)
  ("remove_fit_party_member_from_stack_selection",
    [
      (store_script_param, ":slot_index", 1),
      (val_add, ":slot_index", 2),
      (troop_get_slot, ":amount", "trp_stack_selection_amounts", ":slot_index"),
      (troop_get_slot, ":troop_no", "trp_stack_selection_ids", ":slot_index"),
      (val_sub, ":amount", 1),
      (troop_set_slot, "trp_stack_selection_amounts", ":slot_index", ":amount"),
      (troop_get_slot, ":total_amount", "trp_stack_selection_amounts", 1),
      (val_sub, ":total_amount", 1),
      (troop_set_slot, "trp_stack_selection_amounts", 1, ":total_amount"),
      (try_begin),
        (le, ":amount", 0),
        (troop_get_slot, ":num_slots", "trp_stack_selection_amounts", 0),
        (store_add, ":end_cond", ":num_slots", 2),
        (store_add, ":begin_cond", ":slot_index", 1),
        (try_for_range, ":index", ":begin_cond", ":end_cond"),
          (store_sub, ":prev_index", ":index", 1),
          (troop_get_slot, ":value", "trp_stack_selection_amounts", ":index"),
          (troop_set_slot, "trp_stack_selection_amounts", ":prev_index", ":value"),
          (troop_get_slot, ":value", "trp_stack_selection_ids", ":index"),
          (troop_set_slot, "trp_stack_selection_ids", ":prev_index", ":value"),
        (try_end),
        (val_sub, ":num_slots", 1),
        (troop_set_slot, "trp_stack_selection_amounts", 0, ":num_slots"),
      (try_end),
      (assign, reg0, ":troop_no"),
  ]),
  
  #script_remove_random_fit_party_member_from_stack_selection
  # INPUT:
  # none
  #OUTPUT:
  # reg0 = troop_no
  # trp_stack_selection_amounts slots (slot 0 = number of stacks, 1 = number of men fit, 2..n = stack sizes (fit))
  # trp_stack_selection_ids slots (2..n = stack troops)
  ("remove_random_fit_party_member_from_stack_selection",
    [
      (troop_get_slot, ":total_amount", "trp_stack_selection_amounts", 1),
      (store_random_in_range, ":random_troop", 0, ":total_amount"),
      (troop_get_slot, ":num_slots", "trp_stack_selection_amounts", 0),
      (store_add, ":end_cond", ":num_slots", 2),
      (try_for_range, ":index", 2, ":end_cond"),
        (troop_get_slot, ":amount", "trp_stack_selection_amounts", ":index"),
        (val_sub, ":random_troop", ":amount"),
        (lt, ":random_troop", 0),
        (assign, ":end_cond", 0),
        (store_sub, ":slot_index", ":index", 2),
      (try_end),
      (call_script, "script_remove_fit_party_member_from_stack_selection", ":slot_index"),
  ]),
  
  
  #script_add_routed_party
  #INPUT: none
  #OUTPUT: none
  ("add_routed_party",
    [
      (party_get_num_companion_stacks, ":num_stacks", "p_routed_enemies"), #question, I changed (total_enemy_casualties) with (p_routed_enemies) because this is not prisoner in p_routed_enemies party.
      (assign, ":num_regulars", 0),
      (assign, ":deleted_stacks", 0),
      (try_for_range, ":stack_no", 0, ":num_stacks"),
        (store_sub, ":difference", ":num_stacks", ":stack_no"),
        (ge, ":difference", ":deleted_stacks"),
        (store_sub, ":stack_no_minus_deleted", ":stack_no", ":deleted_stacks"),
        (party_stack_get_troop_id, ":stack_troop", "p_routed_enemies", ":stack_no_minus_deleted"),
        (try_begin),
          (troop_is_hero, ":stack_troop"),
          (party_stack_get_size, ":stack_size", "p_routed_enemies", ":stack_no_minus_deleted"),
          (party_remove_members, "p_routed_enemies", ":stack_troop", 1),
          (try_begin),
            (le, ":stack_size", 1),
            (val_add, ":deleted_stacks", 1), #if deleted hero is the only one in his troop, now we have one less stacks
          (try_end),
        (else_try),
          (val_add, ":num_regulars", 1),
        (try_end),
      (try_end),
      
      #add new party to map if there is at least one routed agent. (new party name : routed_party, template : routed_warriors)
      (try_begin),
        (ge, ":num_regulars", 1),
        
        (set_spawn_radius, 2),
        (spawn_around_party, "p_main_party", "pt_routed_warriors"),
        (assign, ":routed_party", reg0),
        
        (party_set_slot, ":routed_party", slot_party_commander_party, -1), #we need this because 0 is player's party!
        
        (assign, ":max_routed_agents", 0),
        (assign, ":routed_party_faction", "fac_neutral"),
        (try_for_range, ":cur_faction", fac_kingdom_1, fac_kingdoms_end),
          (faction_get_slot, ":num_routed_agents_in_this_faction", ":cur_faction", slot_faction_num_routed_agents),
          (gt, ":num_routed_agents_in_this_faction", ":max_routed_agents"),
          (assign, ":max_routed_agents", ":num_routed_agents_in_this_faction"),
          (assign, ":routed_party_faction", ":cur_faction"),
        (try_end),
        
        (party_set_faction, ":routed_party", ":routed_party_faction"),
        
        (party_set_ai_behavior, ":routed_party", ai_bhvr_travel_to_party),
        
        (assign, ":minimum_distance", 1000000),
        (try_for_parties, ":party_no"),
          (party_is_active, ":party_no"),
          (party_get_slot, ":cur_party_type", ":party_no", slot_party_type),
          (this_or_next|eq, ":cur_party_type", spt_town),
          (eq, ":cur_party_type", spt_castle),
          (store_faction_of_party, ":cur_faction", ":party_no"),
          (this_or_next|eq, ":routed_party_faction", "fac_neutral"),
          (eq, ":cur_faction", ":routed_party_faction"),
          (party_get_position, pos1, ":party_no"),
          (store_distance_to_party_from_party, ":dist", ":party_no", "p_main_party"),
          (try_begin),
            (lt, ":dist", ":minimum_distance"),
            (assign, ":minimum_distance", ":dist"),
            (assign, ":nearest_ally_city", ":party_no"),
          (try_end),
        (try_end),
        
        (party_get_position, pos1, "p_main_party"), #store position information of main party in pos1
        (party_get_position, pos2, ":nearest_ally_city"), #store position information of target city in pos2
        
        (assign, ":minimum_distance", 1000000),
        (try_for_range, ":unused", 0, 10),
          (map_get_random_position_around_position, pos3, pos1, 2), #store position of found random position (possible placing position for new routed party) around battle position in pos3
          (get_distance_between_positions, ":dist", pos2, pos3), #store distance between found position and target city in ":dist".
          (try_begin),
            (lt, ":dist", ":minimum_distance"),
            (assign, ":minimum_distance", ":dist"),
            (copy_position, pos63, pos3),
          (try_end),
        (end_try),
        
        (party_set_position, ":routed_party", pos63),
        
        (party_set_ai_object, ":routed_party", ":nearest_ally_city"),
        (party_set_slot, ":routed_party", slot_party_home_center, ":nearest_ally_city"),
        (party_set_flags, ":routed_party", pf_default_behavior, 1),
        
        #adding party members of p_routed_enemies to routed_party
        (party_clear, ":routed_party"),
        (party_get_num_companion_stacks, ":num_stacks", "p_routed_enemies"), #question, I changed (total_enemy_casualties) with (p_routed_enemies) because this is not prisoner in p_routed_enemies party.
        (try_for_range, ":stack_no", 0, ":num_stacks"),
          (party_stack_get_troop_id, ":stack_troop", "p_routed_enemies", ":stack_no"),
          (try_begin),
            (neg|troop_is_hero, ":stack_troop"), #do not add routed heroes to (new created) routed party for now.
            
            (party_stack_get_size, ":stack_size", "p_routed_enemies", ":stack_no"),
            (party_add_members, ":routed_party", ":stack_troop", ":stack_size"),
          (try_end),
        (try_end),
      (try_end),
  ]), #ozan
  
  
  #script_cf_training_ground_sub_routine_1_for_melee_details
  # INPUT:
  # value
  #OUTPUT:
  # none
  ("cf_training_ground_sub_routine_1_for_melee_details",
    [
      (store_script_param, ":value", 1),
      (ge, "$temp_3", ":value"),
      (val_add, ":value", 1),
      (troop_get_slot, ":troop_id", "trp_stack_selection_ids", ":value"),
      (str_store_troop_name, s0, ":troop_id"),
  ]),
  
  #script_training_ground_sub_routine_2_for_melee_details
  # INPUT:
  # value
  #OUTPUT:
  # none
  ("training_ground_sub_routine_2_for_melee_details",
    [
      (store_script_param, ":value", 1),
      (val_sub, ":value", 1),
      (try_begin),
        (lt, ":value", 0),
        (call_script, "script_remove_random_fit_party_member_from_stack_selection"),
      (else_try),
        (call_script, "script_remove_fit_party_member_from_stack_selection", ":value"),
      (try_end),
      (assign, ":troop_id", reg0),
      (store_sub, ":slot_index", "$temp_2", 1),
      (troop_set_slot, "trp_temp_array_a", ":slot_index", ":troop_id"),
      (try_begin),
        (eq, "$temp", "$temp_2"),
        (call_script, "script_start_training_at_training_ground", -1, "$temp"),
      (else_try),
        (val_add, "$temp_2", 1),
        (jump_to_menu, "mnu_training_ground_selection_details_melee_2"),
      (try_end),
  ]),
  
  #script_cf_training_ground_sub_routine_for_training_result
  # INPUT:
  # arg1: troop_id, arg2: stack_no, arg3: troop_count, arg4: xp_ratio_to_add
  #OUTPUT:
  # none
  ("cf_training_ground_sub_routine_for_training_result",
    [
      (store_script_param, ":troop_id", 1),
      (store_script_param, ":stack_no", 2),
      (store_script_param, ":amount", 3),
      (store_script_param, ":xp_ratio_to_add", 4),
      
      (store_character_level, ":level", ":troop_id"),
      (store_add, ":level_added", ":level", 5),
      (store_mul, ":min_hardness", ":level_added", 3),
      (val_min, ":min_hardness", 100),
      (store_sub, ":hardness_dif", ":min_hardness", "$g_training_ground_training_hardness"),
      (val_max, ":hardness_dif", 0),
      (store_sub, ":hardness_dif", 100, ":hardness_dif"),
      (val_mul, ":hardness_dif", ":hardness_dif"),
      (val_div, ":hardness_dif", 10), # value over 1000
      ##     (assign, reg0, ":hardness_dif"),
      ##     (display_message, "@Hardness difference: {reg0}/1000"),
      (store_mul, ":xp_ratio_to_add_for_stack", ":xp_ratio_to_add", ":hardness_dif"),
      (val_div, ":xp_ratio_to_add_for_stack", 1000),
      (try_begin),
        (eq, ":troop_id", "trp_player"),
        (val_mul, ":xp_ratio_to_add_for_stack", 1),
      (else_try),
        (try_begin),
          (eq, "$g_mt_mode", ctm_melee),
          (try_begin),
            (this_or_next|troop_is_guarantee_ranged, ":troop_id"),
            (troop_is_guarantee_horse, ":troop_id"),
            (val_div, ":xp_ratio_to_add_for_stack", 4),
          (try_end),
        (else_try),
          (eq, "$g_mt_mode", ctm_mounted),
          (try_begin),
            (neg|troop_is_guarantee_horse, ":troop_id"),
            (assign, ":xp_ratio_to_add_for_stack", 0),
          (try_end),
        (else_try),
          (neg|troop_is_guarantee_ranged, ":troop_id"),
          (assign, ":xp_ratio_to_add_for_stack", 0),
        (try_end),
      (try_end),
      (val_add,  ":level", 1),
      (store_mul, ":xp_to_add", 100, ":level"),
      (val_mul, ":xp_to_add", ":amount"),
      (val_div, ":xp_to_add", 20),
      (val_mul, ":xp_to_add", ":xp_ratio_to_add_for_stack"),
      (val_div, ":xp_to_add", 1000),
      (store_mul, ":max_xp_to_add", ":xp_to_add", 3),
      (val_div, ":max_xp_to_add", 2),
      (store_div, ":min_xp_to_add", ":xp_to_add", 2),
      (store_random_in_range, ":random_xp_to_add", ":min_xp_to_add", ":max_xp_to_add"),
      (gt, ":random_xp_to_add", 0),
      (try_begin),
        (troop_is_hero, ":troop_id"),
        (add_xp_to_troop, ":random_xp_to_add", ":troop_id"),
        (store_div, ":proficiency_to_add", ":random_xp_to_add", 50),
        (try_begin),
          (gt, ":proficiency_to_add", 0),
          (troop_raise_proficiency, ":troop_id", "$g_training_ground_used_weapon_proficiency", ":proficiency_to_add"),
        (try_end),
      (else_try),
        (party_add_xp_to_stack, "p_main_party", ":stack_no", ":random_xp_to_add"),
      (try_end),
      (assign, reg0, ":random_xp_to_add"),
  ]),
  
  
  ##  #script_cf_print_troop_name_with_stack_index_to_s0
  ##  # INPUT:
  ##  # param1: stack_index
  ##
  ##  #OUTPUT:
  ##  # string register 0.
  ##  ("cf_print_troop_name_with_stack_index_to_s0",
  ##   [
  ##     (store_script_param_1, ":stack_index"),
  ##     (party_get_num_companion_stacks, ":num_stacks", "p_main_party"),
  ##     (lt, ":stack_index", ":num_stacks"),
  ##     (party_stack_get_troop_id, ":stack_troop", "p_main_party", ":stack_index"),
  ##     (str_store_troop_name, s0, ":stack_troop"),
  ##    ]),
  
  #script_print_troop_owned_centers_in_numbers_to_s0
  # INPUT:
  # param1: troop_no
  #OUTPUT:
  # string register 0.
  ("print_troop_owned_centers_in_numbers_to_s0",
    [
      (store_script_param_1, ":troop_no"),
      (str_store_string, s0, "@nothing"),
      (assign, ":owned_towns", 0),
      (assign, ":owned_castles", 0),
      (assign, ":owned_villages", 0),
      (try_for_range_backwards, ":cur_center", centers_begin, centers_end),
        (party_slot_eq, ":cur_center", slot_town_lord, ":troop_no"),
        (try_begin),
          (party_slot_eq, ":cur_center", slot_party_type, spt_town),
          (val_add, ":owned_towns", 1),
        (else_try),
          (party_slot_eq, ":cur_center", slot_party_type, spt_castle),
          (val_add, ":owned_castles", 1),
        (else_try),
          (val_add, ":owned_villages", 1),
        (try_end),
      (try_end),
      (assign, ":num_types", 0),
      (try_begin),
        (gt, ":owned_villages", 0),
        (assign, reg0, ":owned_villages"),
        (store_sub, reg1, reg0, 1),
        (str_store_string, s0, "@{reg0} village{reg1?s:}"),
        (val_add, ":num_types", 1),
      (try_end),
      (try_begin),
        (gt, ":owned_castles", 0),
        (assign, reg0, ":owned_castles"),
        (store_sub, reg1, reg0, 1),
        (try_begin),
          (eq, ":num_types", 0),
          (str_store_string, s0, "@{reg0} fort{reg1?s:}"),
        (else_try),
          (str_store_string, s0, "@{reg0} fort{reg1?s:} and {s0}"),
        (try_end),
        (val_add, ":num_types", 1),
      (try_end),
      (try_begin),
        (gt, ":owned_towns", 0),
        (assign, reg0, ":owned_towns"),
        (store_sub, reg1, reg0, 1),
        (try_begin),
          (eq, ":num_types", 0),
          (str_store_string, s0, "@{reg0} town{reg1?s:}"),
        (else_try),
          (eq, ":num_types", 1),
          (str_store_string, s0, "@{reg0} town{reg1?s:} and {s0}"),
        (else_try),
          (str_store_string, s0, "@{reg0} town{reg1?s:}, {s0}"),
        (try_end),
      (try_end),
      (store_add, reg0, ":owned_villages", ":owned_castles"),
      (val_add, reg0, ":owned_towns"),
  ]),
  
  #script_get_random_melee_training_weapon
  # INPUT: none
  # OUTPUT: reg0 = weapon_1, reg1 = weapon_2
  ("get_random_melee_training_weapon",
    [
      (assign, ":weapon_1", -1),
      (assign, ":weapon_2", -1),
      (store_random_in_range, ":random_no", 0, 3),
      (try_begin),
        (eq, ":random_no", 0),
        (assign, ":weapon_1", "itm_practice_staff"),
      (else_try),
        (eq, ":random_no", 1),
        (assign, ":weapon_1", "itm_practice_sword"),
        (assign, ":weapon_2", "itm_practice_shield"),
      (else_try),
        (assign, ":weapon_1", "itm_practice_sword"),#heavy_
      (try_end),
      (assign, reg0, ":weapon_1"),
      (assign, reg1, ":weapon_2"),
  ]),
  
  #script_start_training_at_training_ground
  # INPUT:
  # param1: training_weapon_type, param2: training_param
  ("start_training_at_training_ground",
    [
      (val_add, "$g_training_ground_training_count", 1),
      (store_script_param, ":mission_weapon_type", 1),
      (store_script_param, ":training_param", 2),
      
      (set_jump_mission, "mt_training_ground_training"),
      
      (assign, ":training_default_weapon_1", -1),
      (assign, ":training_default_weapon_2", -1),
      (assign, ":training_default_weapon_3", -1),
      (assign, "$scene_num_total_gourds_destroyed", 0),
      (try_begin),
        (eq, ":mission_weapon_type", itp_type_bow),
        (assign, "$g_training_ground_used_weapon_proficiency", wpt_archery),
        (assign, ":training_default_weapon_1", "itm_practice_bow"),
        (try_begin),
          (eq, "$g_mt_mode", ctm_mounted),
          (assign, ":training_default_weapon_2", "itm_practice_arrows_100_amount"),
        (else_try),
          (assign, ":training_default_weapon_2", "itm_practice_arrows_10_amount"),
        (try_end),
      (else_try),
        (eq, ":mission_weapon_type", itp_type_crossbow),
        (assign, "$g_training_ground_used_weapon_proficiency", wpt_crossbow),
        (assign, ":training_default_weapon_1", "itm_practice_crossbow"),
        (assign, ":training_default_weapon_2", "itm_practice_bolts_9_amount"),
      (else_try),
        (eq, ":mission_weapon_type", itp_type_thrown),
        (assign, "$g_training_ground_used_weapon_proficiency", wpt_throwing),
        (try_begin),
          (eq, "$g_mt_mode", ctm_mounted),
          (assign, ":training_default_weapon_2", "itm_practice_throwing_daggers_100_amount"),
        (else_try),
          (assign, ":training_default_weapon_2", "itm_practice_throwing_daggers"),
        (try_end),
      (else_try),
        (eq, ":mission_weapon_type", itp_type_one_handed_wpn),
        (assign, "$g_training_ground_used_weapon_proficiency", wpt_one_handed_weapon),
        (assign, ":training_default_weapon_1", "itm_practice_sword"),
      (else_try),
        (eq, ":mission_weapon_type", itp_type_polearm),
        (assign, "$g_training_ground_used_weapon_proficiency", wpt_polearm),
        (assign, ":training_default_weapon_1", "itm_practice_lance"),
      (else_try),
        #weapon_type comes as -1 when melee training is selected
        (assign, "$g_training_ground_used_weapon_proficiency", wpt_one_handed_weapon),
        (call_script, "script_get_random_melee_training_weapon"),
        (assign, ":training_default_weapon_1", reg0),
        (assign, ":training_default_weapon_2", reg1),
      (try_end),
      
      ##     (assign, "$g_training_ground_training_troop_stack_index", ":stack_index"),
      (try_begin),
        (eq, "$g_mt_mode", ctm_mounted),
        (assign, ":training_default_weapon_3", "itm_practice_horse"),
        (store_add, "$g_training_ground_training_scene", "scn_training_ground_horse_track_1", "$g_encountered_party"),
        (val_sub, "$g_training_ground_training_scene", training_grounds_begin),
      (else_try),
        (store_add, "$g_training_ground_training_scene", "scn_training_ground_ranged_melee_1", "$g_encountered_party"),
        (val_sub, "$g_training_ground_training_scene", training_grounds_begin),
      (try_end),
      
      (modify_visitors_at_site, "$g_training_ground_training_scene"),
      (reset_visitors),
      (set_visitor, 0, "trp_player"),
      
      (assign, ":selected_weapon", -1),
      (try_for_range, ":cur_slot", 0, 4),#equipment slots
        (troop_get_inventory_slot, ":cur_item", "trp_player", ":cur_slot"),
        (ge, ":cur_item", 0),
        (item_get_type, ":item_type", ":cur_item"),
        (try_begin),
          (eq, ":item_type", ":mission_weapon_type"),
          (eq, ":selected_weapon", -1),
          (assign, ":selected_weapon", ":cur_item"),
        (try_end),
      (try_end),
      (mission_tpl_entry_clear_override_items, "mt_training_ground_training", 0),
      (mission_tpl_entry_add_override_item, "mt_training_ground_training", 0, "itm_practice_boots"),
      (try_begin),
        (ge, ":training_default_weapon_1", 0),
        (try_begin),
          (ge, ":selected_weapon", 0),
          (mission_tpl_entry_add_override_item, "mt_training_ground_training", 0, ":selected_weapon"),
        (else_try),
          (mission_tpl_entry_add_override_item, "mt_training_ground_training", 0, ":training_default_weapon_1"),
        (try_end),
      (try_end),
      (try_begin),
        (ge, ":training_default_weapon_2", 0),
        (mission_tpl_entry_add_override_item, "mt_training_ground_training", 0, ":training_default_weapon_2"),
      (try_end),
      (try_begin),
        (ge, ":training_default_weapon_3", 0),
        (mission_tpl_entry_add_override_item, "mt_training_ground_training", 0, ":training_default_weapon_3"),
      (try_end),
      
      (assign, ":cur_visitor_point", 5),
      (troop_get_slot, ":num_fit", "trp_stack_selection_amounts", 1),
      (store_add, ":end_cond", 5, ":num_fit"),
      (val_min, ":end_cond", 13),
      (try_for_range, ":cur_visitor_point", 5, ":end_cond"),
        (call_script, "script_remove_random_fit_party_member_from_stack_selection"),
        (set_visitor, ":cur_visitor_point", reg0),
        (val_add, ":cur_visitor_point", 1),
      (try_end),
      (try_begin),
        (eq, "$g_mt_mode", ctm_melee),
        (assign, ":total_difficulty", 0),
        (try_for_range, ":i", 0, ":training_param"),
          (troop_get_slot, ":cur_troop", "trp_temp_array_a", ":i"),
          (store_add, ":cur_entry_point", ":i", 1),
          (set_visitor, ":cur_entry_point", ":cur_troop"),
          (mission_tpl_entry_clear_override_items, "mt_training_ground_training", ":cur_entry_point"),
          (mission_tpl_entry_add_override_item, "mt_training_ground_training", ":cur_entry_point", "itm_practice_boots"),
          (call_script, "script_get_random_melee_training_weapon"),
          (mission_tpl_entry_add_override_item, "mt_training_ground_training", ":cur_entry_point", reg0),
          (try_begin),
            (ge, reg1, 0),
            (mission_tpl_entry_add_override_item, "mt_training_ground_training", ":cur_entry_point", reg1),
          (try_end),
          (store_character_level, ":cur_troop_level", ":cur_troop"),
          (val_add, ":cur_troop_level", 10),
          (val_mul, ":cur_troop_level", ":cur_troop_level"),
          (val_add, ":total_difficulty", ":cur_troop_level"),
        (try_end),
        
        (assign, "$g_training_ground_training_num_enemies", ":training_param"),
        (assign, "$g_training_ground_training_hardness",  ":total_difficulty"),
        (store_add, ":number_multiplier", "$g_training_ground_training_num_enemies", 4),
        (val_mul, "$g_training_ground_training_hardness", ":number_multiplier"),
        (val_div, "$g_training_ground_training_hardness", 2400),
        (str_store_string, s0, "@Your opponents are ready for the fight."),
      (else_try),
        (eq, "$g_mt_mode", ctm_mounted),
        (try_begin),
          (eq, ":mission_weapon_type", itp_type_bow),
          (assign, "$g_training_ground_training_hardness", 350),
          (assign, "$g_training_ground_training_num_gourds_to_destroy", 30),
        (else_try),
          (eq, ":mission_weapon_type", itp_type_thrown),
          (assign, "$g_training_ground_training_hardness", 400),
          (assign, "$g_training_ground_training_num_gourds_to_destroy", 30),
        (else_try),
          (eq, ":mission_weapon_type", itp_type_one_handed_wpn),
          (assign, "$g_training_ground_training_hardness", 200),
          (assign, "$g_training_ground_training_num_gourds_to_destroy", 45),
        (else_try),
          (eq, ":mission_weapon_type", itp_type_polearm),
          (assign, "$g_training_ground_training_hardness", 280),
          (assign, "$g_training_ground_training_num_gourds_to_destroy", 35),
        (try_end),
        (str_store_string, s0, "@Try to destroy as many targets as you can. You have two and a half minutes to clear the track."),
      (else_try),
        (eq, "$g_mt_mode", ctm_ranged),
        (store_mul, "$g_training_ground_ranged_distance", ":training_param", 100),
        (assign, ":hardness_modifier", ":training_param"),
        (val_mul, ":hardness_modifier", ":hardness_modifier"),
        (try_begin),
          (eq, ":mission_weapon_type", itp_type_bow),
          (val_mul, ":hardness_modifier", 3),
          (val_div, ":hardness_modifier", 2),
        (else_try),
          (eq, ":mission_weapon_type", itp_type_thrown),
          (val_mul, ":hardness_modifier", 5),
          (val_div, ":hardness_modifier", 2),
          (val_mul, ":hardness_modifier", ":training_param"),
          (val_div, ":hardness_modifier", 2),
        (try_end),
        (store_mul, "$g_training_ground_training_hardness", 100, ":hardness_modifier"),
        (val_div, "$g_training_ground_training_hardness", 6000),
        (str_store_string, s0, "@Stay behind the line on the ground and shoot the targets. Try not to waste any shots."),
      (try_end),
      (jump_to_menu, "mnu_training_ground_description"),
  ]),
  
  #script_print_party_to_s0:
  # INPUT:
  # param1: Party-id
  
  #OUTPUT:
  # string register 0.
  
  ##  ("print_party_to_s0",
  ##    [
  ##      (store_script_param_1, ":party"), #Party_id
  ##      (party_get_num_companion_stacks, ":num_stacks",":party"),
  ##      (str_store_string, s50, "str_none"),
  ##      (try_for_range, ":i_stack", 0, ":num_stacks"),
  ##        (party_stack_get_troop_id,     ":stack_troop",":party",":i_stack"),
  ##        (party_stack_get_size,         ":stack_size",":party",":i_stack"),
  ##        (str_store_troop_name_by_count, s61, ":stack_troop", ":stack_size"),
  ##        (try_begin),
  ##          (troop_is_hero, ":stack_troop"),
  ##          (str_store_string_reg, s51, s61),
  ##        (else_try),
  ##          (assign, reg60, ":stack_size"),
  ##          (str_store_string, s63, "str_reg60_s61"),
  ##        (try_end),
  ##        (try_begin),
  ##          (eq, ":i_stack", 0),
  ##          (str_store_string_reg, s50, s51),
  ##        (else_try),
  ##          (str_store_string, s50, "str_s50_comma_s51"),
  ##        (try_end),
  ##      (try_end),
  ##      (str_store_string_reg, s0, s50),
  ##  ]),
  
  #script_party_count_fit_regulars:
  # Returns the number of unwounded regular companions in a party
  # INPUT:
  # param1: Party-id
  ("party_count_fit_regulars",
    [
      (store_script_param_1, ":party"), #Party_id
      (party_get_num_companion_stacks, ":num_stacks", ":party"),
      (assign, reg0, 0),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop", ":party", ":i_stack"),
        (neg|troop_is_hero, ":stack_troop"),
        (party_stack_get_size, ":stack_size",":party",":i_stack"),
        (party_stack_get_num_wounded, ":num_wounded",":party",":i_stack"),
        (val_sub, ":stack_size", ":num_wounded"),
        (val_add, reg0, ":stack_size"),
      (try_end),
  ]),
  
  
  #script_party_count_fit_for_battle:
  # Returns the number of unwounded companions in a party
  # INPUT:
  # param1: Party-id
  # OUTPUT: reg0 = result
  ("party_count_fit_for_battle",
    [
      (store_script_param_1, ":party"), #Party_id
      (party_get_num_companion_stacks, ":num_stacks",":party"),
      (assign, reg0, 0),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop",":party",":i_stack"),
        (assign, ":num_fit",0),
        (try_begin),
          (troop_is_hero, ":stack_troop"),
          (try_begin),
            (neg|troop_is_wounded, ":stack_troop"),
            (assign, ":num_fit", 1),
          (try_end),
        (else_try),
          (party_stack_get_size, ":num_fit",":party",":i_stack"),
          (party_stack_get_num_wounded, ":num_wounded",":party",":i_stack"),
          (val_sub, ":num_fit", ":num_wounded"),
        (try_end),
        (val_add, reg0, ":num_fit"),
      (try_end),
  ]),
  
  
  #script_party_count_members_with_full_health
  # Returns the number of unwounded regulars, and heroes other than player with 100% hitpoints in a party
  # INPUT:
  # param1: Party-id
  # OUTPUT: reg0 = result
  ("party_count_members_with_full_health",
    [
      (store_script_param_1, ":party"), #Party_id
      (party_get_num_companion_stacks, ":num_stacks",":party"),
      (assign, reg0, 0),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop",":party",":i_stack"),
        (neq, ":stack_troop", "trp_player"),
        (assign, ":num_fit",0),
        (try_begin),
          (troop_is_hero, ":stack_troop"),
          (store_troop_health, ":troop_hp", ":stack_troop"),
          (try_begin),
            (ge, ":troop_hp", 80),
            (assign, ":num_fit",1),
          (try_end),
        (else_try),
          (party_stack_get_size, ":num_fit",":party",":i_stack"),
          (party_stack_get_num_wounded, ":num_wounded",":party",":i_stack"),
          (val_sub, ":num_fit", ":num_wounded"),
          (val_max, ":num_fit", 0),
        (try_end),
        (val_add, reg0, ":num_fit"),
      (try_end),
  ]),
  
  
  ##  ("get_fit_stack_with_rank",
  ##    [
  ##      (store_script_param_1, ":party"), #Party_id
  ##      (store_script_param_2, ":rank"), #Rank
  ##      (party_get_num_companion_stacks, ":num_stacks",":party"),
  ##      (assign, reg0, -1),
  ##      (assign, ":num_total", 0),
  ##      (try_for_range, ":i_stack", 0, ":num_stacks"),
  ##        (eq, reg(0), -1), #continue only if we haven't found the result yet.
  ##        (party_stack_get_troop_id,     ":stack_troop",":party",":i_stack"),
  ##        (assign, ":num_fit",0),
  ##        (try_begin),
  ##          (troop_is_hero, ":stack_troop"),
  ##          (store_troop_health, ":troop_hp", ":stack_troop"),
  ##          (try_begin),
  ##            (ge,  ":troop_hp", 20),
  ##            (assign, ":num_fit",1),
  ##          (try_end),
  ##        (else_try),
  ##          (party_stack_get_size,         ":num_fit",":party",":i_stack"),
  ##          (party_stack_get_num_wounded, ":num_wounded",":party",":i_stack"),
  ##          (val_sub, ":num_fit", ":num_wounded"),
  ##        (try_end),
  ##        (val_add, ":num_total", ":num_fit"),
  ##        (try_begin),
  ##          (lt, ":rank", ":num_total"),
  ##          (assign, reg(0), ":i_stack"),
  ##        (try_end),
  ##      (try_end),
  ##  ]),
  
  #script_get_stack_with_rank:
  # Returns the stack no, containing unwounded regular companions with rank rank.
  # INPUT:
  # param1: Party-id
  # param2: rank
  
  # ("get_stack_with_rank",
    # [
      # (store_script_param_1, ":party"), #Party_id
      # (store_script_param_2, ":rank"), #Rank
      # (party_get_num_companion_stacks, ":num_stacks",":party"),
      # (assign, reg(0), -1),
      # (assign, ":num_total", 0),
      # (try_for_range, ":i_stack", 0, ":num_stacks"),
        # (eq, reg(0), -1), #continue only if we haven't found the result yet.
        # (party_stack_get_troop_id,     ":stack_troop",":party",":i_stack"),
        # (neg|troop_is_hero, ":stack_troop"),
        # (party_stack_get_size,         ":stack_size",":party",":i_stack"),
        # (party_stack_get_num_wounded,  ":num_wounded",":party",":i_stack"),
        # (val_sub, ":stack_size", ":num_wounded"),
        # (val_add, ":num_total", ":stack_size"),
        # (try_begin),
          # (lt, ":rank", ":num_total"),
          # (assign, reg(0), ":i_stack"),
        # (try_end),
      # (try_end),
  # ]),
  
  # script_inflict_casualties_to_party:
  # derived from script_simulate_battle_with_parties, but not dependent on a second party for damage levels (casualties are normalized to party's own strength)
  # Input: party
  #        percent of party to harm (0-100)
  # Output: string s8 with casualties
  #         "p_temp_casualties"
  # Notes: bear in mind that inflict_casualties_to_party_group returns what appears to be a normal distribution of results
  #        Also, one may want to recollect parties after this
  ("inflict_casualties_to_party", [
      (store_script_param, ":party", 1),
      (store_script_param, ":percent_to_harm", 2),
      
      (try_begin),
        (gt, ":percent_to_harm", 0),
        (party_collect_attachments_to_party, ":party", "p_temp_party"),
        (call_script, "script_party_calculate_strength", "p_temp_party", 0),
        (store_mul, ":party_strength", reg0, ":percent_to_harm"),
        (val_div, ":party_strength", 167),  #try to get accurate percent
        (gt, ":party_strength", 0),
        (inflict_casualties_to_party_group, ":party", ":party_strength", "p_temp_casualties"),
        (call_script, "script_print_casualties_to_s0", "p_temp_casualties", 0),
        (str_store_string_reg, s8, s0),
      (else_try),
        (str_clear, s8),
      (try_end),
  ]),
  
  
  #script_move_members_with_ratio:
  # INPUT:
  # param1: Source Party-id
  # param2: Target Party-id
  # pin_number = ratio of members to move, multiplied by 1000
  
  #OUTPUT:
  # This script doesn't return a value but moves some of the members of source party to target party according to the given ratio.
  ("move_members_with_ratio",
    [
      (store_script_param_1, ":source_party"), #Source Party_id
      (store_script_param_2, ":target_party"), #Target Party_id
      (party_get_num_prisoner_stacks, ":num_stacks",":source_party"),
      (try_for_range_backwards, ":stack_no", 0, ":num_stacks"),
        (party_prisoner_stack_get_troop_id,     ":stack_troop",":source_party",":stack_no"),
        (party_prisoner_stack_get_size,    ":stack_size",":source_party",":stack_no"),
        (store_mul, ":number_to_move",":stack_size","$pin_number"),
        (val_div, ":number_to_move", 1000),
        (party_remove_prisoners, ":source_party", ":stack_troop", ":number_to_move"),
        (assign, ":number_moved", reg0),
        (party_add_prisoners, ":target_party", ":stack_troop", ":number_moved"),
      (try_end),
      (party_get_num_companion_stacks, ":num_stacks",":source_party"),
      (try_for_range_backwards, ":stack_no", 0, ":num_stacks"),
        (party_stack_get_troop_id,     ":stack_troop",":source_party",":stack_no"),
        (party_stack_get_size,    ":stack_size",":source_party",":stack_no"),
        (store_mul, ":number_to_move",":stack_size","$pin_number"),
        (val_div, ":number_to_move", 1000),
        (party_remove_members, ":source_party", ":stack_troop", ":number_to_move"),
        (assign, ":number_moved", reg0),
        (party_add_members, ":target_party", ":stack_troop", ":number_moved"),
      (try_end),
  ]),
  
  
  # script_count_parties_of_faction_and_party_type:
  # counts number of active parties with a template and faction.
  # Input: arg1 = faction_no, arg2 = party_type
  # Output: reg0 = count
  
  ("count_parties_of_faction_and_party_type",
    [
      (store_script_param_1, ":faction_no"),
      (store_script_param_2, ":party_type"),
      (assign, reg0, 0),
      (try_for_parties, ":party_no"),
        (party_is_active, ":party_no"),
        (party_get_slot, ":cur_party_type", ":party_no", slot_party_type),
        (store_faction_of_party, ":cur_faction", ":party_no"),
        (eq, ":cur_party_type", ":party_type"),
        (eq, ":cur_faction", ":faction_no"),
        (val_add, reg0, 1),
      (try_end),
  ]),
  
  # # script_faction_get_number_of_armies
  # # Input: arg1 = faction_no
  # # Output: reg0 = number_of_armies
  # ("faction_get_number_of_armies",
  # [
  # (store_script_param_1, ":faction_no"),
  # (assign, ":num_armies", 0),
  # (try_for_range, ":troop_no", active_npcs_begin, active_npcs_end),
  # (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
  # (store_troop_faction, ":hero_faction_no", ":troop_no"),
  # (eq, ":hero_faction_no", ":faction_no"),
  # (troop_get_slot, ":hero_party", ":troop_no", slot_troop_leaded_party),
  # (ge, ":hero_party", 0),
  # (party_is_active, ":hero_party"),
  # (call_script, "script_party_count_fit_regulars", ":hero_party"),
  # (assign, ":party_size", reg0),
  # (call_script, "script_party_get_ideal_size", ":hero_party"),
  # (assign, ":ideal_size", reg0),
  # (val_mul, ":ideal_size", 60),
  # (val_div, ":ideal_size", 100),
  # (gt, ":party_size", ":ideal_size"),
  # (val_add, ":num_armies", 1),
  # (try_end),
  # (assign, reg0, ":num_armies"),
  # ]),
  
  
  # script_faction_recalculate_strength
  # Input: arg1 = faction_no
  # Output: reg0 = strength
  ("faction_recalculate_strength",
    [
      (store_script_param_1, ":faction_no"),
      
      # (call_script, "script_faction_get_number_of_armies", ":faction_no"),
      # (assign, ":num_armies", reg0),
      (assign, ":num_castles", 0),
      (assign, ":num_towns", 0),
      
      (try_for_range, ":center_no", centers_begin, centers_end),
        (store_faction_of_party, ":center_faction", ":center_no"),
        (eq, ":center_faction", ":faction_no"),
        (try_begin),
          (party_slot_eq, ":center_no", slot_party_type, spt_castle),
          (val_add, ":num_castles", 1),
        (else_try),
          (party_slot_eq, ":center_no", slot_party_type, spt_town),
          (val_add, ":num_towns", 1),
        (try_end),
      (try_end),
      
      # (faction_set_slot, ":faction_no", slot_faction_num_armies, ":num_armies"),
      (faction_set_slot, ":faction_no", slot_faction_num_castles, ":num_castles"),
      (faction_set_slot, ":faction_no", slot_faction_num_towns, ":num_towns"),
      
  ]),
  
  #script_select_random_town:
  # This script selects a random town in range [towns_begin, towns_end)
  # INPUTS:
  # none
  
  #OUTPUT:
  # reg0: id of the selected random town
  ##  ("select_random_town",
  ##    [
  ##      (assign, ":num_towns", towns_end),
  ##      (val_sub,":num_towns", towns_begin),
  ##      (store_random, ":random_town", ":num_towns"),
  ##      (val_add,":random_town", towns_begin),
  ##      (assign, reg0, ":random_town"),
  ##  ]),
  
  #  ("select_random_spawn_point",
  #    [
  #      (assign, reg(20), spawn_points_end),
  #      (val_sub,reg(20), spawn_points_begin),
  #      (store_random, reg(21), reg(20)),
  #      (val_add,reg(21), spawn_points_begin),
  #      (assign, "$pout_town", reg(21)),
  # ]),
  
  #script_cf_select_random_town_with_faction:
  # This script selects a random town in range [towns_begin, towns_end)
  # such that faction of the town is equal to given_faction
  # INPUT:
  # arg1 = faction_no
  
  #OUTPUT:
  # This script may return false if there is no matching town.
  # reg0 = town_no
  ("cf_select_random_town_with_faction",
    [
      (store_script_param_1, ":faction_no"),
      (assign, ":result", -1),
      # First count num matching spawn points
      (assign, ":no_towns", 0),
      (try_for_range,":cur_town", towns_begin, towns_end),
        (store_faction_of_party, ":cur_faction", ":cur_town"),
        (eq, ":cur_faction", ":faction_no"),
        (val_add, ":no_towns", 1),
      (try_end),
      (gt, ":no_towns", 0), #Fail if there are no towns
      (store_random_in_range, ":random_town", 0, ":no_towns"),
      (assign, ":no_towns", 0),
      (try_for_range,":cur_town", towns_begin, towns_end),
        (eq, ":result", -1),
        (store_faction_of_party, ":cur_faction", ":cur_town"),
        (eq, ":cur_faction", ":faction_no"),
        (val_add, ":no_towns", 1),
        (gt, ":no_towns", ":random_town"),
        (assign, ":result", ":cur_town"),
      (try_end),
      (assign, reg0, ":result"),
  ]),
  
  #script_cf_select_random_village_with_faction:
  # This script selects a random village in range [villages_begin, villages_end)
  # such that faction of the village is equal to given_faction
  # INPUT:
  # arg1 = faction_no
  
  #OUTPUT:
  # This script may return false if there is no matching village.
  # reg0 = village_no
  ("cf_select_random_village_with_faction",
    [
      (store_script_param_1, ":faction_no"),
      (assign, ":result", -1),
      # First count num matching spawn points
      (assign, ":no_villages", 0),
      (try_begin),
        (eq, "$campaign_type", camp_storyline),
        (try_for_range,":cur_village", villages_begin, "p_village_150"),
          (store_faction_of_party, ":cur_faction", ":cur_village"),
          (eq, ":cur_faction", ":faction_no"),
          (val_add, ":no_villages", 1),
        (try_end),
      (else_try),
        (try_for_range,":cur_village", villages_begin, villages_end),
          (store_faction_of_party, ":cur_faction", ":cur_village"),
          (eq, ":cur_faction", ":faction_no"),
          (val_add, ":no_villages", 1),
        (try_end),
      (try_end),
      (gt, ":no_villages", 0), #Fail if there are no villages
      (store_random_in_range, ":random_village", 0, ":no_villages"),
      (assign, ":no_villages", 0),
      (try_begin),
        (eq, "$campaign_type", camp_storyline),
        (try_for_range,":cur_village", villages_begin, "p_village_150"),
          (eq, ":result", -1),
          (store_faction_of_party, ":cur_faction", ":cur_village"),
          (eq, ":cur_faction", ":faction_no"),
          (val_add, ":no_villages", 1),
          (gt, ":no_villages", ":random_village"),
          (assign, ":result", ":cur_village"),
        (try_end),
      (else_try),
        (try_for_range,":cur_village", villages_begin, villages_end),
          (eq, ":result", -1),
          (store_faction_of_party, ":cur_faction", ":cur_village"),
          (eq, ":cur_faction", ":faction_no"),
          (val_add, ":no_villages", 1),
          (gt, ":no_villages", ":random_village"),
          (assign, ":result", ":cur_village"),
        (try_end),
      (try_end),
      (assign, reg0, ":result"),
  ]),
  
  
  #script_cf_select_random_walled_center_with_faction:
  # This script selects a random center in range [centers_begin, centers_end)
  # such that faction of the town is equal to given_faction
  # INPUT:
  # arg1 = faction_no
  # arg2 = preferred_center_no
  
  #OUTPUT:
  # This script may return false if there is no matching town.
  # reg0 = town_no (Can fail)
  ("cf_select_random_walled_center_with_faction",
    [
      (store_script_param, ":faction_no", 1),
      (store_script_param, ":preferred_center_no", 2),
      (assign, ":result", -1),
      # First count num matching spawn points
      (assign, ":no_centers", 0),
      (try_for_range,":cur_center", walled_centers_begin, walled_centers_end),
        (store_faction_of_party, ":cur_faction", ":cur_center"),
        (eq, ":cur_faction", ":faction_no"),
        (val_add, ":no_centers", 1),
        (eq, ":cur_center", ":preferred_center_no"),
        (val_add, ":no_centers", 99),
      (try_end),
      (gt, ":no_centers", 0), #Fail if there are no centers
      (store_random_in_range, ":random_center", 0, ":no_centers"),
      (try_for_range,":cur_center", walled_centers_begin, walled_centers_end),
        (eq, ":result", -1),
        (store_faction_of_party, ":cur_faction", ":cur_center"),
        (eq, ":cur_faction", ":faction_no"),
        (val_sub, ":random_center", 1),
        (try_begin),
          (eq, ":cur_center", ":preferred_center_no"),
          (val_sub, ":random_center", 99),
        (try_end),
        (lt, ":random_center", 0),
        (assign, ":result", ":cur_center"),
      (try_end),
      (assign, reg0, ":result"),
  ]),
  
  #script_cf_select_random_walled_center_with_faction_and_owner_priority_no_siege:
  # INPUT:
  # arg1 = faction_no
  # arg2 = owner_troop_no
  #OUTPUT:
  # This script may return false if there is no matching town.
  # reg0 = center_no (Can fail)
  ("cf_select_random_walled_center_with_faction_and_owner_priority_no_siege",
    [
      (store_script_param, ":faction_no", 1),
      (store_script_param, ":troop_no", 2),
      (assign, ":result", -1),
      (assign, ":no_centers", 0),
      
      (call_script, "script_lord_get_home_center", ":troop_no"),
      (assign, ":home_center", reg0),
      
      (try_for_range,":cur_center", walled_centers_begin, walled_centers_end),
        (store_faction_of_party, ":cur_faction", ":cur_center"),
        (eq, ":cur_faction", ":faction_no"),
        (party_slot_eq, ":cur_center", slot_center_is_besieged_by, -1),
        (val_add, ":no_centers", 1),
        
        #(party_slot_eq, ":cur_center", slot_town_lord, ":troop_no"),
        (eq, ":home_center", ":cur_center"), #I changed it with above line, now if lord is owner of any village its bound walled center is counted as 1000. Better this way. ozan-18.01.09
        
        (val_add, ":no_centers", 1000),
      (try_end),
      
      #if no center is available count all centers not besieged do not care its faction.
      (try_begin),
        (le, ":no_centers", 0),
        
        (assign, "$g_there_is_no_avaliable_centers", 1),
        
        (try_for_range,":cur_center", walled_centers_begin, walled_centers_end),
          (party_slot_eq, ":cur_center", slot_center_is_besieged_by, -1),
          (val_add, ":no_centers", 1),
        (try_end),
      (else_try),
        (assign, "$g_there_is_no_avaliable_centers", 0),
      (try_end),
      
      (faction_get_slot, ":faction_leader", ":faction_no", slot_faction_leader),
      (this_or_next|eq, "$g_there_is_no_avaliable_centers", 0),
      (neq, ":troop_no", ":faction_leader"), #faction leaders cannot spawn if they have no centers.
      
      (store_random_in_range, ":random_center", 0, ":no_centers"),
      (try_for_range,":cur_center", walled_centers_begin, walled_centers_end),
        (eq, ":result", -1),
        (store_faction_of_party, ":cur_faction", ":cur_center"),
        (this_or_next|eq, "$g_there_is_no_avaliable_centers", 1),
        (eq, ":cur_faction", ":faction_no"),
        (party_slot_eq, ":cur_center", slot_center_is_besieged_by, -1),
        (val_sub, ":random_center", 1),
        (try_begin),
          #(party_slot_eq, ":cur_center", slot_town_lord, ":troop_no"),
          (eq, ":home_center", ":cur_center"), #I changed it with above line, now if lord is owner of any village its bound walled center is counted as 1000. Better this way. ozan-18.01.09
          (eq, "$g_there_is_no_avaliable_centers", 0),
          
          (val_sub, ":random_center", 1000),
        (try_end),
        (lt, ":random_center", 0),
        (assign, ":result", ":cur_center"),
      (try_end),
      (assign, reg0, ":result"),
  ]),
  
  #script_cf_select_random_walled_center_with_faction_and_less_strength_priority:
  # This script selects a random center in range [centers_begin, centers_end)
  # such that faction of the town is equal to given_faction
  # INPUT:
  # arg1 = faction_no
  # arg2 = preferred_center_no
  
  #OUTPUT:
  # This script may return false if there is no matching town.
  # reg0 = town_no (Can fail)
  # ("cf_select_random_walled_center_with_faction_and_less_strength_priority",
    # [
      # (store_script_param, ":faction_no", 1),
      # (store_script_param, ":preferred_center_no", 2),
      # (assign, ":result", -1),
      # # First count num matching spawn points
      # (assign, ":no_centers", 0),
      # (try_for_range, ":cur_center", walled_centers_begin, walled_centers_end),
        # (store_faction_of_party, ":cur_faction", ":cur_center"),
        # (eq, ":cur_faction", ":faction_no"),
        # (party_slot_eq, ":cur_center", slot_center_is_besieged_by, -1),
        # (val_add, ":no_centers", 1),
        # (try_begin),
          # (eq, ":cur_center", ":preferred_center_no"),
          # (val_add, ":no_centers", 99),
        # (try_end),
        # ##        (call_script, "script_party_calculate_regular_strength", ":cur_center"),
        # ##        (assign, ":strength", reg0),
        # ##        (lt, ":strength", 80),
        # ##        (store_sub, ":strength", 100, ":strength"),
        # ##        (val_div, ":strength", 20),
        # ##        (val_add, ":no_centers", ":strength"),
      # (try_end),
      # (gt, ":no_centers", 0), #Fail if there are no centers
      # (store_random_in_range, ":random_center", 0, ":no_centers"),
      # (try_for_range, ":cur_center", walled_centers_begin, walled_centers_end),
        # (eq, ":result", -1),
        # (store_faction_of_party, ":cur_faction", ":cur_center"),
        # (eq, ":cur_faction", ":faction_no"),
        # (party_slot_eq, ":cur_center", slot_center_is_besieged_by, -1),
        # (val_sub, ":random_center", 1),
        # (try_begin),
          # (eq, ":cur_center", ":preferred_center_no"),
          # (val_sub, ":random_center", 99),
        # (try_end),
        # ##        (try_begin),
        # ##          (call_script, "script_party_calculate_regular_strength", ":cur_center"),
        # ##          (assign, ":strength", reg0),
        # ##          (lt, ":strength", 80),
        # ##          (store_sub, ":strength", 100, ":strength"),
        # ##          (val_div, ":strength", 20),
        # ##          (val_sub, ":random_center", ":strength"),
        # ##        (try_end),
        # (lt, ":random_center", 0),
        # (assign, ":result", ":cur_center"),
      # (try_end),
      # (assign, reg0, ":result"),
  # ]),
  
  
  #script_cf_select_random_town_at_peace_with_faction:
  # This script selects a random town in range [towns_begin, towns_end)
  # such that faction of the town is friendly to given_faction
  # INPUT:
  # arg1 = faction_no
  
  #OUTPUT:
  # This script may return false if there is no matching town.
  # reg0 = town_no
  ("cf_select_random_town_at_peace_with_faction",
    [
      (store_script_param_1, ":faction_no"),
      (assign, ":result", -1),
      # First count num matching towns
      (assign, ":no_towns", 0),
      (try_for_range,":cur_town", towns_begin, towns_end),
        (store_faction_of_party, ":cur_faction", ":cur_town"),
        (store_relation,":reln", ":cur_faction", ":faction_no"),
        (ge, ":reln", 0),
        (val_add, ":no_towns", 1),
      (try_end),
      (gt, ":no_towns", 0), #Fail if there are no towns
      (store_random_in_range, ":random_town", 0, ":no_towns"),
      (assign, ":no_towns", 0),
      (try_for_range,":cur_town", towns_begin, towns_end),
        (eq, ":result", -1),
        (store_faction_of_party, ":cur_faction", ":cur_town"),
        (store_relation,":reln", ":cur_faction", ":faction_no"),
        (ge, ":reln", 0),
        (val_add, ":no_towns", 1),
        (gt, ":no_towns", ":random_town"),
        (assign, ":result", ":cur_town"),
      (try_end),
      (assign, reg0, ":result"),
  ]),
  
  #script_cf_select_random_town_at_peace_with_faction_in_trade_route
  # INPUT:
  # arg1 = town_no
  # arg2 = faction_no
  
  #OUTPUT:
  # This script may return false if there is no matching town.
  # reg0 = town_no
  # ("cf_select_random_town_at_peace_with_faction_in_trade_route",
    # [
      # (store_script_param, ":town_no", 1),
      # (store_script_param, ":faction_no", 2),
      # (assign, ":result", -1),
      # (assign, ":no_towns", 0),
      # (try_for_range, ":cur_slot", slot_town_trade_routes_begin, slot_town_trade_routes_end),
        # (party_get_slot, ":cur_town", ":town_no", ":cur_slot"),
        # (gt, ":cur_town", 0),
        # (store_faction_of_party, ":cur_faction", ":cur_town"),
        # (store_relation, ":reln", ":cur_faction", ":faction_no"),
        # (ge, ":reln", 0),
        # (val_add, ":no_towns", 1),
      # (try_end),
      # (gt, ":no_towns", 0), #Fail if there are no towns
      # (store_random_in_range, ":random_town", 0, ":no_towns"),
      # (try_for_range, ":cur_slot", slot_town_trade_routes_begin, slot_town_trade_routes_end),
        # (eq, ":result", -1),
        # (party_get_slot, ":cur_town", ":town_no", ":cur_slot"),
        # (gt, ":cur_town", 0),
        # (store_faction_of_party, ":cur_faction", ":cur_town"),
        # (store_relation, ":reln", ":cur_faction", ":faction_no"),
        # (ge, ":reln", 0),
        # (val_sub, ":random_town", 1),
        # (lt, ":random_town", 0),
        # (assign, ":result", ":cur_town"),
      # (try_end),
      # (assign, reg0, ":result"),
  # ]),
  
  
  
  #the following is a very simple adjustment - it measures the difference in prices between two towns
  #all goods are weighted equally except for luxuries
  #it does not take into account the prices of the goods, nor cargo capacity
  #to do that properly, a merchant would have to virtually fill his baggage, slot by slot, for each town
  #i also found that one needed to introduce demand inelasticity -- prices should vary a lot for grain,  relatively little for iron
  
  ("cf_select_most_profitable_town_at_peace_with_faction_in_trade_route",
    [
      (store_script_param, ":town_no", 1),
      (store_script_param, ":faction_no", 2),
      
      (assign, ":result", -1),
      (assign, ":best_town_score", 0),
      (store_sub, ":item_to_price_slot", slot_town_trade_good_prices_begin, trade_goods_begin),
      
      (try_for_range, ":cur_slot", slot_town_trade_routes_begin, slot_town_trade_routes_end),
        (party_get_slot, ":cur_town", ":town_no", ":cur_slot"),
        (gt, ":cur_town", 0),
        
        (store_faction_of_party, ":cur_faction", ":cur_town"),
        (store_relation, ":reln", ":cur_faction", ":faction_no"),
        (ge, ":reln", 0),
        
        (assign, ":cur_town_score", 0),
        (try_for_range, ":cur_goods", trade_goods_begin, trade_goods_end),
          (neq, ":cur_goods", "itm_butter"), #Don't count perishables
          (neq, ":cur_goods", "itm_cattle_meat"),
          (neq, ":cur_goods", "itm_chicken"),
          (neq, ":cur_goods", "itm_pork"),
          
          (store_add, ":cur_goods_price_slot", ":cur_goods", ":item_to_price_slot"),
          (party_get_slot, ":origin_price", ":town_no", ":cur_goods_price_slot"),
          (party_get_slot, ":destination_price", ":cur_town", ":cur_goods_price_slot"),
          
          (gt, ":destination_price", ":origin_price"),
          (store_sub, ":price_dif", ":destination_price", ":origin_price"),
          
          (try_begin), #weight luxury goods double
            # Following products changed for vc:
            (this_or_next|eq, ":cur_goods", "itm_wine"),	#eco note
            (this_or_next|eq, ":cur_goods", "itm_jewelry"),
            (this_or_next|eq, ":cur_goods", "itm_ivory"),
            (this_or_next|eq, ":cur_goods", "itm_amber"),
            (eq, ":cur_goods", "itm_silver"),
            (val_mul, ":price_dif", 2),
          (try_end),
          (val_add, ":cur_town_score", ":price_dif"),
        (try_end),
        
        ##		(try_begin),
        ##			(eq, "$cheat_mode", 1),
        ##			(str_store_party_name, s10, ":town_no"),
        ##			(str_store_party_name, s11, ":cur_town"),
        ##			(assign, reg3, ":cur_town_score"),
        ##			(display_message, "str_caravan_in_s10_considers_s11_total_price_dif_=_reg3"),
        ##		(try_end),
        (assign, ":count", 0),
        (try_for_parties, ":caravans"),
          (party_slot_eq, ":caravans", slot_party_type, spt_kingdom_caravan),
          (party_get_slot, ":destination", ":caravans", slot_party_ai_object),
          (eq, ":destination", ":cur_town"),
          (val_add, ":count", 1),
        (try_end),
        (le, ":count", 3),
        (gt, ":cur_town_score", ":best_town_score"),
        (assign, ":best_town_score", ":cur_town_score"),
        (assign, ":result", ":cur_town"),
      (try_end),
      (gt, ":result", -1), #Fail if there are no towns
      (assign, reg0, ":result"),
      
      #	  (store_current_hours, ":hour"),
      #	  (party_set_slot, ":result", slot_town_caravan_last_visit, ":hour"),
      
      ##	  (try_begin),
      ##		(eq, "$cheat_mode", 1),
      ##	    (assign, reg3, ":best_town_score"),
      ##	    (str_store_party_name, s3, ":town_no"),
      ##	    (str_store_party_name, s4, ":result"),
      ##	    (display_message, "str_test__caravan_in_s3_selects_for_s4_trade_score_reg3"),
      ##	  (try_end),
      
  ]),
  
  
  ##  ("cf_select_faction_spawn_point",
  ##    [
  ##      # First count num matching spawn points
  ##      (assign, reg(24), 0),
  ##      (try_for_range,reg(25), spawn_points_begin, spawn_points_end),
  ##        (store_faction_of_party, reg(23), reg(25)),
  ##        (eq, reg(23), "$pin_faction"),
  ##        (val_add, reg(24), 1),
  ##      (end_try,0),
  ##      # reg4 now holds num towns of this faction.
  ##      (gt, reg(24), 0), #Fail if there are no towns
  ##      (store_random, reg(26), reg(24)),
  ##
  ##      (assign, reg(24), 0), # reg24 = num points of this faction.
  ##      (try_for_range,reg(25), spawn_points_begin, spawn_points_end),
  ##        (store_faction_of_party, reg(23), reg(25)),
  ##        (eq, reg(23), "$pin_faction"),
  ##        (try_begin,0),
  ##          (eq, reg(24), reg(26)),
  ##          (assign, "$pout_town", reg(25)), # result is this town
  ##        (end_try,0),
  ##        (val_add, reg(24), 1),
  ##      (end_try,0),
  ##  ]),
  
  
  #script_spawn_party_at_random_town:
  # This script selects a random town in range [towns_begin, towns_end)
  # such that faction of the town is equal to given_faction
  # and spawns a new party there.
  # INPUT:
  # $pin_faction: given_faction
  # $pin_party_template: given_party_template
  
  #OUTPUT:
  # This script may return false if party cannot be spawned.
  # $pout_party: id of the spawned party
  ##  ("spawn_party_at_random_town",
  ##    [
  ##      (call_script,"script_select_random_spawn_point"),
  ##      (set_spawn_radius,1),
  ##      (spawn_around_party,"$pout_town","$pin_party_template"),
  ##      (assign, "$pout_party", reg(0)),
  ##  ]),
  
  #script_cf_spawn_party_at_faction_town:
  # This script selects a random town in range [towns_begin, towns_end)
  # such that faction of the town is equal to given_faction
  # and spawns a new party there.
  # INPUT:
  # $pin_faction: given_faction
  # $pin_party_template: given_party_template
  
  #OUTPUT:
  # This script may return false if party cannot be spawned.
  # $pout_party: id of the spawned party
  ##  ("cf_spawn_party_at_faction_town",
  ##    [
  ##      (call_script,"script_cf_select_faction_spawn_point"),
  ##      (set_spawn_radius,1),
  ##      (spawn_around_party,"$pout_town","$pin_party_template"),
  ##      (assign, "$pout_party", reg(0)),
  ##  ]),
  
  #script_spawn_party_at_random_town_if_below_limit:
  # This script checks if number of parties
  # of specified template is less than limit,
  # If so, it selects a random town in range [towns_begin, towns_end)
  # and spawns a new party there.
  # INPUT:
  # $pin_party_template: given_party_template
  # $pin_limit: limit value
  
  #OUTPUT:
  # $pout_party: id of the spawned party
  # $pout_town: id of the selected faction town
  # Note:
  # This script may return false if number of parties
  # of specified template is greater or equal to limit,
  # or if party cannot be spawned.
  ##  ("cf_spawn_party_at_random_town_if_below_limit",
  ##    [
  ##      (store_num_parties_of_template, reg(22), "$pin_party_template"),
  ##      (lt,reg(22),"$pin_limit"), #check if we are below limit.
  ##      (call_script,"script_select_random_spawn_point"),
  ##      (set_spawn_radius,1),
  ##      (spawn_around_party,"$pout_town","$pin_party_template"),
  ##      (assign, "$pout_party", reg(0)),
  ##  ]),
  
  ##  #script_spawn_party_at_faction_town_if_below_limit:
  ##  # This script checks if number of parties
  ##  # of specified template is less than limit,
  ##  # If so, it selects a random town in range [towns_begin, towns_end)
  ##  # such that faction of the town is equal to given_faction
  ##  # and spawns a new party there.
  ##  # INPUT:
  ##  # $pin_faction: given_faction
  ##  # $pin_party_template: given_party_template
  ##  # $pin_limit: limit value
  ##
  ##  #OUTPUT:
  ##  # $pout_party: id of the spawned party
  ##  # $pout_town: id of the selected faction town
  ##  # Note:
  ##  # This script may return false if number of parties
  ##  # of specified template is greater or equal to limit,
  ##  # or if party cannot be spawned.
  ##  ("cf_spawn_party_at_faction_town_if_below_limit",
  ##    [
  ##      (store_num_parties_of_template, reg(22), "$pin_party_template"),
  ##      (lt,reg(22),"$pin_limit"), #check if we are below limit.
  ##      (call_script,"script_cf_select_faction_spawn_point"),
  ##      (set_spawn_radius,1),
  ##      (spawn_around_party,"$pout_town","$pin_party_template"),
  ##      (assign, "$pout_party", reg(0)),
  ##  ]),
  
  # script_shuffle_troop_slots:
  # Shuffles a range of slots of a given troop.
  # Used for exploiting a troop as an array.
  # Input: arg1 = troop_no, arg2 = slot_begin, arg3 = slot_end
  ("shuffle_troop_slots",
    [
      (store_script_param, ":troop_no", 1),
      (store_script_param, ":slots_begin", 2),
      (store_script_param, ":slots_end", 3),
      (try_for_range, ":cur_slot_no", ":slots_begin", ":slots_end"),
        (store_random_in_range, ":random_slot_no", ":slots_begin", ":slots_end"), #reg(58) = random slot. Now exchange slots reg(57) and reg(58)
        (troop_get_slot, ":cur_slot_value", ":troop_no", ":cur_slot_no"), #temporarily store the value in slot reg(57) in reg(59)
        (troop_get_slot, ":random_slot_value", ":troop_no", ":random_slot_no"), #temporarily store the value in slot reg(58) in reg(60)
        (troop_set_slot, ":troop_no", ":cur_slot_no", ":random_slot_value"), # Now exchange the two...
        (troop_set_slot, ":troop_no", ":random_slot_no", ":cur_slot_value"),
      (try_end),
  ]),
  
  
  # script_get_quest - combines old get_random_quest with new get_dynamic_quest
  
  # Input: arg1 = troop_no (of the troop in conversation), arg2 = min_importance (of the quest)
  # Output: reg0 = quest_no (the slots of the quest will be filled after calling this script)
  ("get_quest",
    [
      (store_script_param_1, ":giver_troop"),
      
      (assign, ":result", -1),
      (assign, ":quest_target_troop", -1),
      (assign, ":quest_target_center", -1),
      (assign, ":quest_target_faction", -1),
      (assign, ":quest_object_faction", -1),
      (assign, ":quest_object_troop", -1),
      (assign, ":quest_object_center", -1),
      (assign, ":quest_target_party", -1),
      (assign, ":quest_target_party_template", -1),
      (assign, ":quest_target_amount", -1),
      (assign, ":quest_target_dna", -1),
      (assign, ":quest_target_item", -1),
      (assign, ":quest_importance", 1),
      (assign, ":quest_xp_reward", 0),
      (assign, ":quest_gold_reward", 0),
      (assign, ":quest_convince_value", 0),
      (assign, ":quest_expiration_days", 0),
      (assign, ":quest_dont_give_again_period", 0),
      
      (try_begin), #get dynamic quest is a separate script, so that we can scan a number of different troops at once for it
        (call_script, "script_get_dynamic_quest", "$g_talk_troop"),
        
        (assign, ":result", reg0),
        (assign, ":relevant_troop", reg1),
        (assign, ":relevant_party", reg2),
        (assign, ":relevant_faction", reg3),
        (assign, ":object_party", reg4),
        #GUILDMASTER QUESTS
        (try_begin),
          (eq, ":result", "qst_track_down_bandits"),
          (is_between, ":giver_troop", mayors_begin, mayors_end),
          (assign, ":quest_target_party", ":relevant_party"),
          (assign ,":quest_expiration_days", 60),
          (assign, ":quest_dont_give_again_period", 30),
          (assign, ":quest_xp_reward", 500),
          (assign, ":quest_gold_reward", 1000),
        (else_try),
          (eq, ":result", "qst_retaliate_for_border_incident"),
          (is_between, ":giver_troop", mayors_begin, mayors_end),
          (assign, ":quest_target_troop", ":relevant_troop"),
          (assign, ":quest_target_faction", ":relevant_faction"),
          (assign, ":quest_dont_give_again_period", 15),
          (assign ,":quest_expiration_days", 30),
          (assign, ":quest_xp_reward", 1000),
          (assign, ":quest_gold_reward", 1000),
          
          #KINGDOM LORD QUESTS
        (else_try),
          (eq, ":result", "qst_cause_provocation"),
          (assign, ":quest_target_faction", ":relevant_faction"),
          (assign, ":quest_expiration_days", 30),
          (assign, ":quest_dont_give_again_period", 100),
          (assign, ":quest_importance", 15),
          (assign, ":quest_xp_reward", 1000),
          (assign, ":quest_gold_reward", 1000),
          
        (else_try),
          (eq, ":result", "qst_destroy_bandit_lair"),
          (assign, ":quest_target_party", ":relevant_party"),
          (assign ,":quest_expiration_days", 60),
          (assign, ":quest_importance", 14),
          (assign, ":quest_xp_reward", 3000),
          (assign, ":quest_gold_reward", 1500),
          (assign, ":quest_dont_give_again_period", 15),
          
          #KINGDOM LADY OR KINGDOM HERO QUESTS
        (else_try),
          (eq, ":result", "qst_rescue_prisoner"),
          (assign, ":quest_target_troop", ":relevant_troop"),
          (assign, ":quest_target_center", ":relevant_party"),
          (assign, ":quest_expiration_days", 30),
          (assign, ":quest_dont_give_again_period", 5),
          (assign, ":quest_importance", 18),
          (assign, ":quest_xp_reward", 2000),
          (assign, ":quest_gold_reward", 1500),
        (else_try),
          (eq, ":result", "qst_blank_quest_4"),
          (assign, ":quest_target_party", ":relevant_party"),
          (assign, ":quest_object_center", ":object_party"),
          (assign ,":quest_expiration_days", 30),
          (assign, ":quest_importance", 7),
        (try_end),
      (try_end),
      
      #no dynamic quest available
      (try_begin),
        (eq, ":result", -1),
        
        (store_character_level, ":player_level", "trp_player"),
        (store_troop_faction, ":giver_faction_no", ":giver_troop"),
        
        (troop_get_slot, ":giver_party_no", ":giver_troop", slot_troop_leaded_party),
        (troop_get_slot, ":giver_reputation", ":giver_troop", slot_lord_reputation_type),
        
        (assign, ":giver_center_no", -1),
        (try_begin),
          (gt, ":giver_party_no", 0),
          (party_get_attached_to, ":giver_center_no", ":giver_party_no"),
        (else_try),
          (is_between, "$g_encountered_party", centers_begin, centers_end),
          (assign, ":giver_center_no", "$g_encountered_party"),
        (try_end),
        
        (try_begin),
          (troop_slot_eq, ":giver_troop", slot_troop_occupation, slto_kingdom_hero),
          (try_begin),
            (ge, "$g_talk_troop_faction_kingdom_relation", 0),
            (assign, ":quests_begin", lord_quests_begin),
            (assign, ":quests_end", lord_quests_end),
            (assign, ":quests_begin_2", lord_quests_begin_2),
            (assign, ":quests_end_2", lord_quests_end_2),
          (else_try),
            (assign, ":quests_begin", enemy_lord_quests_begin),
            (assign, ":quests_end", enemy_lord_quests_end),
            (assign, ":quests_begin_2", 0),
            (assign, ":quests_end_2", 0),
          (try_end),
        (else_try),
          (is_between, ":giver_troop", village_elders_begin, village_elders_end),
          (assign, ":quests_begin", village_elder_quests_begin),
          (assign, ":quests_end", village_elder_quests_end),
          (assign, ":quests_begin_2", village_elder_quests_begin_2),
          (assign, ":quests_end_2", village_elder_quests_end_2),
        (else_try),
          (this_or_next|is_between, ":giver_troop", mayors_begin, mayors_end),
          (is_between, ":giver_troop", "trp_castle_1_seneschal","trp_town_1_arena_master"),
          (assign, ":quests_begin", mayor_quests_begin),
          (assign, ":quests_end", mayor_quests_end),
          (assign, ":quests_begin_2", mayor_quests_begin_2),
          (assign, ":quests_end_2", mayor_quests_end_2),
        (else_try),
          (assign, ":quests_begin", lady_quests_begin),
          (assign, ":quests_end", lady_quests_end),
          (assign, ":quests_begin_2", lady_quests_begin_2),
          (assign, ":quests_end_2", lady_quests_end_2),
        (try_end),
        
        (store_sub, ":num_possible_old_quests", ":quests_end", ":quests_begin"),
        (store_sub, ":num_possible_new_quests", ":quests_end_2", ":quests_begin_2"),
        (store_add, ":num_possible_total_quests", ":num_possible_old_quests", ":num_possible_new_quests"),
        #           (store_add. ":num_possible_total_quests_plus_1",":num_possible_total_quests", 1),
        
        (try_for_range, ":unused", 0, ":num_possible_total_quests"),
          (eq, ":result", -1),
          (assign, ":quest_target_troop", -1),
          (assign, ":quest_target_center", -1),
          (assign, ":quest_target_faction", -1),
          (assign, ":quest_object_faction", -1),
          (assign, ":quest_object_troop", -1),
          (assign, ":quest_object_center", -1),
          (assign, ":quest_target_party", -1),
          (assign, ":quest_target_party_template", -1),
          (assign, ":quest_target_amount", -1),
          (assign, ":quest_target_dna", -1),
          (assign, ":quest_target_item", -1),
          (assign, ":quest_importance", 1),
          (assign, ":quest_xp_reward", 0),
          (assign, ":quest_gold_reward", 0),
          (assign, ":quest_convince_value", 0),
          (assign, ":quest_expiration_days", 0),
          (assign, ":quest_dont_give_again_period", 0),
          
          (try_begin),
            (eq, "$cheat_mode", 0),
            (store_random_in_range, ":quest_no", 0, ":num_possible_total_quests"),
          (else_try),
            (assign, ":quest_no", "$vc_next_quest"),
            (val_add, "$vc_next_quest", 1),
            (try_begin),
              (eq, "$vc_next_quest", ":num_possible_total_quests"),
              (assign, "$vc_next_quest", 0),
            (try_end),
          (try_end),
          
          (try_begin),
            (lt, ":quest_no", ":num_possible_old_quests"),
            (val_add, ":quest_no", ":quests_begin"),
          (else_try),
            (val_sub, ":quest_no", ":num_possible_old_quests"),
            (val_add, ":quest_no", ":quests_begin_2"),
          (try_end),
          (try_begin),
            (neq, "$cheat_mode", 0),
            (assign, reg1, ":quest_no"),
            (display_message, "@trying quest {reg1}"),
          (try_end),
          (neg|check_quest_active,":quest_no"),
          (neg|quest_slot_ge, ":quest_no", slot_quest_dont_give_again_remaining_days, 1),
          (try_begin),
            # Village Elder quests
            (eq, ":quest_no", "qst_deliver_grain"),
            (try_begin),
              (is_between, ":giver_center_no", villages_begin, villages_end),
              #The quest giver is the village elder
              (call_script, "script_get_troop_item_amount", ":giver_troop", "itm_grain"),
              (eq, reg0, 0),
              (neg|party_slot_ge, ":giver_center_no", slot_town_prosperity, 40),
              (assign, ":quest_target_center", ":giver_center_no"),
              (store_random_in_range, ":quest_target_amount", 4, 8),
              (assign, ":quest_expiration_days", 30),
              (assign, ":quest_dont_give_again_period", 5),
              (assign, ":result", ":quest_no"),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_deliver_cattle"),
            (try_begin),
              (is_between, ":giver_center_no", villages_begin, villages_end),
              #The quest giver is the village elder
              (party_get_slot, ":num_cattle", ":giver_center_no", slot_village_number_of_cattle),
              (lt, ":num_cattle", 50),
              (assign, ":quest_target_center", ":giver_center_no"),
              (store_random_in_range, ":quest_target_amount", 2, 6),
              (assign, ":quest_expiration_days", 30),
              (assign, ":quest_dont_give_again_period", 20),
              (assign, ":result", ":quest_no"),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_blank_quest_7"),
            (try_begin),
              (neq, "$game_started_with_content_update", 1),	# blank_quest_7 needs a new started game in the content update
              (assign, ":quest_no", "qst_train_peasants_against_bandits"),
            (else_try),
              (eq, ":giver_center_no", "p_village_150"),		# blank_quest_7 cant be used in the storyline in Doccinga (VC-3233)
              (eq, "$campaign_type", camp_storyline),
              (assign, ":quest_no", "qst_train_peasants_against_bandits"),
            (end_try),
            (eq, 1, 0),
          (else_try),
            (eq, ":quest_no", "qst_train_peasants_against_bandits"),
            (try_begin),
              (is_between, ":giver_center_no", villages_begin, villages_end),
              #The quest giver is the village elder
              (store_skill_level, ":player_trainer", "skl_trainer", "trp_player"),
              (gt, ":player_trainer", 0),
              (store_random_in_range, ":quest_target_amount", 5, 8),
              (assign, ":quest_target_center", ":giver_center_no"),
              (assign, ":quest_expiration_days", 20),
              (assign, ":quest_dont_give_again_period", 80),
              (assign, ":result", ":quest_no"),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_blank_quest_6"),
            (try_begin),
              (is_between, ":giver_center_no", villages_begin, villages_end),
              (party_slot_ge, ":giver_center_no", slot_town_prosperity, 50),
              (call_script, "script_troop_get_relation_with_troop", "trp_player", ":giver_troop"),
              (ge, reg0, 15),
              (neg|party_slot_eq, ":giver_center_no", slot_town_lord, "trp_player"),
              (assign, ":quest_target_center", ":giver_center_no"),
              (store_random_in_range, ":quest_target_amount", 4, 8),
              (assign, ":quest_expiration_days", 30),
              (assign, ":quest_dont_give_again_period", 30),
              (assign, ":result", ":quest_no"),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_blank_quest_7"),
            (try_begin),
              (is_between, ":giver_center_no", villages_begin, villages_end),
              #The quest giver is the village elder
              (assign, ":quest_target_amount", 1),
              (assign, ":quest_target_center", ":giver_center_no"),
              (assign, ":quest_expiration_days", 4),
              (assign, ":quest_dont_give_again_period", 30),
              (assign, ":result", ":quest_no"),
            (try_end),
          (else_try),
            # Mayor quests
            (eq, ":quest_no", "qst_escort_merchant_caravan"),
            (is_between, ":giver_center_no", towns_begin, towns_end),
            (store_random_party_in_range, ":quest_target_center", towns_begin, towns_end),
            (store_distance_to_party_from_party, ":dist", ":giver_center_no",":quest_target_center"),
            (assign, ":quest_gold_reward", ":dist"),
            (val_add, ":quest_gold_reward", 25),
            (val_mul, ":quest_gold_reward", 25),
            (val_div, ":quest_gold_reward", 20),
            (store_random_in_range, ":quest_target_amount", 6, 12),
            (assign, "$escort_merchant_caravan_mode", 0),
            (assign, ":result", ":quest_no"),
          (else_try),
            (eq, ":quest_no", "qst_blank_quest_10"),
            (is_between, ":giver_center_no", centers_begin, centers_end),
            (assign, ":distance", 50),
            (assign, ":result", -1),
            (assign, ":no_villages", 0),
            (try_begin),
              (eq, "$campaign_type", camp_storyline),
              (assign, ":end", "p_village_150"),
            (else_try),
              (assign, ":end", villages_end),
            (try_end),
            (try_for_range,":cur_village", villages_begin, ":end"),
              (party_slot_eq, ":cur_village", slot_village_state, svs_normal),
              (store_distance_to_party_from_party, ":cur_distance", ":cur_village", ":giver_center_no"),
              (lt,  ":cur_distance", ":distance"),
              (val_add, ":no_villages", 1),
            (try_end),
            (gt, ":no_villages", 0), #Fail if there are no villages
            (store_random_in_range, ":random_village", 0, ":no_villages"),
            (assign, ":no_villages", 0),
            (try_for_range,":cur_village", villages_begin, ":end"),
              (party_slot_eq, ":cur_village", slot_village_state, svs_normal),
              (store_distance_to_party_from_party, ":cur_distance", ":cur_village", ":giver_center_no"),
              (lt,  ":cur_distance", ":distance"),
              (val_add, ":no_villages", 1),
              (gt, ":no_villages", ":random_village"),
              (assign, ":result", ":cur_village"),
              (assign, ":end", ":cur_village"),
            (try_end),
            (assign, ":quest_target_center", ":result"),
            (store_random_in_range, ":rand", 0, 100),
            (try_begin),
              (lt, ":rand", 50),
              (assign, ":quest_target_item", "itm_lute"),
            (else_try),
              (assign, ":quest_target_item", "itm_lyre"),
            (try_end),
            (store_faction_of_party, ":faction_no",":quest_target_center"),
            (faction_get_slot, ":quest_target_troop", ":faction_no", slot_faction_deserter_troop),
            (assign, ":quest_gold_reward", 300),
            (assign, ":quest_expiration_days", 30),
            (assign, ":quest_dont_give_again_period", 25),
            (assign, ":result", ":quest_no"),
          (else_try),
            (eq, ":quest_no", "qst_blank_quest_11"),
            (is_between, ":giver_center_no", centers_begin, centers_end),
            (store_random_in_range, ":rand", 0, 100),
            (try_begin),
              (lt, ":rand", 50),
              (assign, ":quest_target_item", "itm_salt"),
            (else_try),
              (assign, ":quest_target_item", "itm_stone"),
            (try_end),
            (store_random_in_range, ":quest_target_amount", 2, 6),
            (try_begin),
              (eq, ":quest_target_item", "itm_stone"),
              (store_mul, ":quest_gold_reward", ":quest_target_amount", 120),
            (else_try),
              (store_mul, ":quest_gold_reward", ":quest_target_amount", 250),
            (try_end),
            (assign, ":quest_expiration_days", 30),
            (assign, ":quest_dont_give_again_period", 20),
            (assign, ":result", ":quest_no"),
          (else_try),
            (eq, ":quest_no", "qst_deliver_wine"),
            (is_between, ":giver_center_no", centers_begin, centers_end),
            (store_random_party_in_range, ":quest_target_center", towns_begin, towns_end),
            (store_random_in_range, ":random_no", 0, 2),
            (try_begin),
              (eq, ":random_no", 0),
              (assign, ":quest_target_item", "itm_quest_wine"),
            (else_try),
              (assign, ":quest_target_item", "itm_quest_ale"),
            (try_end),
            (store_random_in_range, ":quest_target_amount", 6, 12),
            (store_distance_to_party_from_party, ":dist", ":giver_center_no",":quest_target_center"),
            (assign, ":quest_gold_reward", ":dist"),
            (val_add, ":quest_gold_reward", 2),
            (assign, ":multiplier", 5),
            (val_add, ":multiplier", ":quest_target_amount"),
            (val_mul, ":quest_gold_reward", ":multiplier"),
            (val_div, ":quest_gold_reward", 100),
            (val_mul, ":quest_gold_reward", 10),
            (store_item_value,"$qst_deliver_wine_debt",":quest_target_item"),
            (val_mul,"$qst_deliver_wine_debt",":quest_target_amount"),
            (val_mul,"$qst_deliver_wine_debt", 6),
            (val_div,"$qst_deliver_wine_debt",5),
            (assign, ":quest_expiration_days", 7),
            (assign, ":quest_dont_give_again_period", 20),
            (assign, ":result", ":quest_no"),
          (else_try),
            (eq, ":quest_no", "qst_troublesome_bandits"),
            (is_between, ":giver_center_no", centers_begin, centers_end),
            #	          (store_character_level, ":quest_gold_reward", "trp_player"),
            #	          (val_add, ":quest_gold_reward", 20),
            #	          (val_mul, ":quest_gold_reward", 35),
            #	          (val_div, ":quest_gold_reward",100),
            #	          (val_mul, ":quest_gold_reward", 10),
            (assign, ":quest_expiration_days", 30),
            (assign, ":quest_dont_give_again_period", 30),
            (assign, ":result", ":quest_no"),
          (else_try),
            (eq, ":quest_no", "qst_kidnapped_girl"),
            (is_between, ":giver_center_no", towns_begin, towns_end),
            (store_random_in_range, ":quest_target_center", villages_begin, villages_end),
            (store_character_level, ":quest_target_amount", "trp_player"),
            (val_add, ":quest_target_amount", 15),
            (store_distance_to_party_from_party, ":dist", ":giver_center_no", ":quest_target_center"),
            (val_add, ":dist", 15),
            (val_mul, ":dist", 2),
            (val_mul, ":quest_target_amount", ":dist"),
            (val_div, ":quest_target_amount",100),
            (val_mul, ":quest_target_amount",10),
            (assign, ":quest_gold_reward", ":quest_target_amount"),
            (val_div, ":quest_gold_reward", 40),
            (val_mul, ":quest_gold_reward", 10),
            (assign, ":quest_expiration_days", 15),
            (assign, ":quest_dont_give_again_period", 30),
            (assign, ":result", ":quest_no"),
          (else_try),
            (eq, ":quest_no", "qst_move_cattle_herd"),
            (is_between, ":giver_center_no", towns_begin, towns_end),
            #(call_script, "script_cf_select_random_town_at_peace_with_faction", ":giver_faction_no"),
            (call_script, "script_cf_get_random_kingdom_center", ":giver_center_no", towns_begin, towns_end),
            (assign, ":quest_target_center", reg0),
            (is_between, ":quest_target_center", towns_begin, towns_end),
            (store_distance_to_party_from_party, ":dist",":giver_center_no",":quest_target_center"),
            (assign, ":quest_gold_reward", ":dist"),
            (val_add, ":quest_gold_reward", 25),
            (val_mul, ":quest_gold_reward", 50),
            (val_div, ":quest_gold_reward", 20),
            (assign, ":quest_expiration_days", 30),
            (assign, ":quest_dont_give_again_period", 20),
            (assign, ":result", ":quest_no"),
          (else_try),
            (eq, ":quest_no", "qst_persuade_lords_to_make_peace"),
            (is_between, ":giver_center_no", towns_begin, towns_end),
            (store_faction_of_party, ":cur_object_faction", ":giver_center_no"),
            (call_script, "script_cf_faction_get_random_enemy_faction", ":cur_object_faction"),
            (assign, ":cur_target_faction", reg0),
            (call_script, "script_cf_get_random_lord_except_king_with_faction", ":cur_object_faction"),
            (assign, ":cur_object_troop", reg0),
            (this_or_next|troop_slot_eq, ":cur_object_troop", slot_lord_reputation_type, lrep_quarrelsome),
            (this_or_next|troop_slot_eq, ":cur_object_troop", slot_lord_reputation_type, lrep_selfrighteous),
            (this_or_next|troop_slot_eq, ":cur_object_troop", slot_lord_reputation_type, lrep_martial),
            (troop_slot_eq, ":cur_object_troop", slot_lord_reputation_type, lrep_debauched),
            
            (call_script, "script_cf_get_random_lord_except_king_with_faction", ":cur_target_faction"),
            (assign, ":quest_target_troop", reg0),
            (this_or_next|troop_slot_eq, ":quest_target_troop", slot_lord_reputation_type, lrep_quarrelsome),
            (this_or_next|troop_slot_eq, ":quest_target_troop", slot_lord_reputation_type, lrep_selfrighteous),
            (this_or_next|troop_slot_eq, ":quest_target_troop", slot_lord_reputation_type, lrep_martial),
            (troop_slot_eq, ":quest_target_troop", slot_lord_reputation_type, lrep_debauched),
            
            (assign, ":quest_object_troop", ":cur_object_troop"),
            (assign, ":quest_target_faction", ":cur_target_faction"),
            (assign, ":quest_object_faction", ":cur_object_faction"),
            (assign, ":quest_gold_reward", 12000),
            (assign, ":quest_convince_value", 7000),
            (assign, ":quest_expiration_days", 30),
            (assign, ":quest_dont_give_again_period", 90),
            (assign, ":result", ":quest_no"),
          (else_try),
            (eq, ":quest_no", "qst_deal_with_looters"),
            (is_between, ":player_level", 0, 18),
            (is_between, ":giver_center_no", towns_begin, towns_end),
            (store_faction_of_party, ":cur_object_faction", ":giver_center_no"),
            (store_num_parties_destroyed_by_player, ":num_looters_destroyed", "pt_looters"),
            (party_template_set_slot,"pt_looters",slot_party_template_num_killed,":num_looters_destroyed"),
            (quest_set_slot,":quest_no",slot_quest_current_state,0),
            (quest_set_slot,":quest_no",slot_quest_target_party_template,"pt_looters"),
            (assign, ":quest_gold_reward", 500),
            (assign, ":quest_xp_reward", 500),
            (assign, ":quest_expiration_days", 20),
            (assign, ":quest_dont_give_again_period", 30),
            (assign, ":result", ":quest_no"),
          (else_try),
            (eq, ":quest_no", "qst_deal_with_night_bandits"),
            (is_between, ":player_level", 0, 20),
            (is_between, ":giver_center_no", towns_begin, towns_end),
            (party_get_slot, ":bandit_troop", "$current_town", slot_center_has_bandits),
            (is_between, ":bandit_troop", "trp_looter", "trp_sea_raider"),
            #	          (party_slot_ge, ":giver_center_no", slot_center_has_bandits, 1),
            (assign, ":quest_target_center", ":giver_center_no"),
            (assign, ":quest_expiration_days", 4),
            (assign, ":quest_dont_give_again_period", 15),
            (assign, ":result", ":quest_no"),
          (else_try),
            # Lady quests
            (eq, ":quest_no", "qst_rescue_lord_by_replace"),
            (val_add, ":quest_no", 1),
            (eq, 1, 0),
            # (try_begin),
              # (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              # (is_between, ":player_level", 5, 25),
              
              # (assign, ":prisoner_relative", -1),
              
              # (try_begin),
                # (troop_get_slot, ":cur_target_troop", ":giver_troop", slot_troop_father), #get giver_troop's father
                # (gt, ":cur_target_troop", 0), #if giver_troop has a father as a troop in game
                # (troop_slot_ge, ":cur_target_troop", slot_troop_prisoner_of_party, 0), #if giver_troop's father is in a prison
                # (assign, ":prisoner_relative", ":cur_target_troop"),
              # (try_end),
              
              # (try_begin),
                # (eq, ":prisoner_relative", -1), #if giver_troop has no father or giver_troop's father is not in prison.
                # (troop_get_slot, ":cur_target_troop", ":giver_troop", slot_troop_spouse), #get giver_troop's spouse
                # (gt, ":cur_target_troop", 0), #if giver_troop has a spouse as a troop in game
                # (troop_slot_ge, ":cur_target_troop", slot_troop_prisoner_of_party, 0), #if giver_troop's spouse is in a prison
                # (assign, ":prisoner_relative", ":cur_target_troop"),
              # (try_end),
              
              # (try_begin),
                # (eq, ":prisoner_relative", -1), #if ((giver_troop has no father) or (giver_troop's father is not in prison)) and ((giver_troop has no spouse) or (giver_troop's spouse is not in prison)).
                # (troop_get_slot, ":cur_target_troop", ":giver_troop", slot_troop_guardian), #get giver_troop's spouse
                # (gt, ":cur_target_troop", 0), #if giver_troop has a guardian as a troop in game
                # (troop_slot_ge, ":cur_target_troop", slot_troop_prisoner_of_party, 0), #if giver_troop's guardian is in a prison
                # (assign, ":prisoner_relative", ":cur_target_troop"),
              # (try_end),
              
              # (try_begin),
                # (eq, "$cheat_mode", 1),
                # (assign, reg0, ":prisoner_relative"),
                # (display_message, "str_prisoner_relative_is_reg0"),
              # (try_end),
              
              # (gt, ":prisoner_relative", -1),
              # #(changed 2) no need to this anymore (troop_slot_ge, ":prisoner_relative", slot_troop_prisoner_of_party, 0),
              # (call_script, "script_search_troop_prisoner_of_party", ":prisoner_relative"),
              # (assign, ":cur_target_center", reg0),
              
              # #(changed 3) no need to check only towns anymore (is_between, ":cur_target_center", towns_begin, towns_end),#Skip if he is not in a town
              # (is_between, ":cur_target_center", walled_centers_begin, walled_centers_end), #Skip if he is not in a walled center
              
              # (assign, ":quest_target_center", ":cur_target_center"),
              # (assign, ":quest_target_troop", ":prisoner_relative"),
              # (assign, ":quest_expiration_days", 30),
              # (assign, ":quest_dont_give_again_period", 73),
              # (assign, ":quest_importance", 15),
              # (assign, ":quest_xp_reward", 2000),
              # (assign, ":quest_gold_reward", 1500),
              # (assign, ":result", ":quest_no"),
            # (try_end),
          (else_try),
            (eq, ":quest_no", "qst_deliver_message_to_prisoner_lord"),
            (eq, "$player_has_homage", 0),
            
            (try_begin),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (is_between, ":player_level", 5, 25),
              (troop_get_slot, ":cur_target_troop", ":giver_troop", slot_troop_father),
              (try_begin),
                (eq, ":cur_target_troop", 0),
                (troop_get_slot, ":cur_target_troop", ":giver_troop", slot_troop_spouse),
              (try_end),
              #(troop_slot_eq, ":cur_target_troop", slot_troop_is_prisoner, 1),#Skip if the lady's father/husband is not in prison
              (gt, ":cur_target_troop", -1),
              (troop_slot_ge, ":cur_target_troop", slot_troop_prisoner_of_party, 0),
              (call_script, "script_search_troop_prisoner_of_party", ":cur_target_troop"),
              (assign, ":cur_target_center", reg0),
              (is_between, ":cur_target_center", towns_begin, towns_end),#Skip if he is not in a town
              (assign, ":quest_target_center", ":cur_target_center"),
              (assign, ":quest_target_troop", ":cur_target_troop"),
              (assign, ":quest_expiration_days", 30),
              (assign, ":quest_dont_give_again_period", 30),
              (assign, ":quest_importance", 2),
              (assign, ":quest_xp_reward", 200),
              (assign, ":quest_gold_reward", 150),
              (assign, ":result", ":quest_no"),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_duel_for_lady"),
            (try_begin),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (ge, ":player_level", 10),
              (neg|troop_slot_eq, ":giver_troop", slot_troop_spouse, 0),  #in dialog lady_quest_duel_for_lady_2, refers to husband
              (call_script, "script_cf_troop_get_random_enemy_troop_with_occupation", ":giver_troop", slto_kingdom_hero),#Can fail
              (assign, ":cur_target_troop", reg0),
              (neg|troop_slot_eq, ":giver_troop", slot_troop_spouse, ":cur_target_troop"), #must not be in the family
              (neg|troop_slot_eq, ":giver_troop", slot_troop_father, ":cur_target_troop"),
              (neg|troop_slot_ge, ":cur_target_troop", slot_troop_prisoner_of_party, 0),
              (troop_slot_ge, ":cur_target_troop", slot_troop_leaded_party, 0),
              (neg|troop_slot_eq, ":cur_target_troop", slot_lord_reputation_type, lrep_goodnatured),
              (neg|troop_slot_eq, ":cur_target_troop", slot_lord_reputation_type, lrep_upstanding),
              (neg|troop_slot_eq, ":cur_target_troop", slot_lord_reputation_type, lrep_martial),
              (neg|troop_slot_eq, ":cur_target_troop", slot_lord_reputation_type, lrep_benefactor),
              (assign, ":quest_target_troop", ":cur_target_troop"),
              (assign, ":quest_expiration_days", 30),
              (assign, ":quest_dont_give_again_period", 50),
              (assign, ":quest_importance", 15),
              (assign, ":quest_xp_reward", 1000),
              (assign, ":quest_gold_reward", 0),  #lady gives item
              (assign, ":result", ":quest_no"),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_blank_quest_16"),
            (eq,reg59,tf_male),
            (is_between, ":giver_troop", kingdom_ladies_begin, kingdom_ladies_end),
            (troop_get_slot, ":player_renown", "trp_player", slot_troop_renown),
            (ge, ":player_renown", 130),
            (neg|troop_slot_eq, ":giver_troop", slot_troop_spouse, "trp_player"),
            (troop_get_slot, ":spouse", ":giver_troop", slot_troop_spouse),
            (try_begin),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (call_script, "script_troop_get_player_relation", ":giver_troop"),
              (assign, ":relation", reg0),
              (assign, ":continue", 0),
              (try_begin),
                (lt, ":spouse", 0),
                (try_begin),
                  (this_or_next|troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_conventional),
                  (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_moralist),
                  (gt, ":relation",2),
                  (assign, ":continue", 1),
                (else_try),
                  (this_or_next|troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_adventurous),
                  (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_ambitious),
                  (gt, ":relation",-5),
                  (assign, ":continue", 1),
                (else_try),
                  (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_otherworldly),
                  (ge, ":relation",0),
                  (assign, ":continue", 1),
                (try_end),
              (else_try),
                (gt, ":spouse", 0),
                (try_begin),
                  (this_or_next|troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_conventional),
                  (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_moralist),
                  (gt, ":relation",5),
                  (assign, ":continue", 1),
                (else_try),
                  (this_or_next|troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_adventurous),
                  (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_ambitious),
                  (ge, ":relation",0),
                  (assign, ":continue", 1),
                (else_try),
                  (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_otherworldly),
                  (gt, ":relation",2),
                  (assign, ":continue", 1),
                (try_end),
              (try_end),
              (eq, ":continue", 1),
              (assign, ":good", 0),
              (store_random_in_range, ":rand", 0, 80),
              (try_begin),
                (lt, ":rand", 20),
                (assign, ":good", "itm_amber"),
              (else_try),
                (is_between, ":rand", 20, 40),
                (assign, ":good", "itm_jewelry"),
              (else_try),
                (is_between, ":rand", 40, 60),
                (assign, ":good", "itm_ivory"),
              (else_try),
                (assign, ":good", "itm_soapstone"),
              (try_end),
              (store_random_in_range, ":rand", 1,4),
              (assign, ":quest_dont_give_again_period", 30),
              (assign, ":quest_expiration_days", 30),
              (assign, ":quest_target_amount", ":rand"),
              (assign, ":quest_target_item", ":good"),
              (assign, ":result", ":quest_no"),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_blank_quest_17"),
            (eq,reg59,tf_male),
            (is_between, ":giver_troop", kingdom_ladies_begin, kingdom_ladies_end),
            (troop_get_slot, ":player_renown", "trp_player", slot_troop_renown),
            (ge, ":player_renown", 180),
            (neg|troop_slot_eq, ":giver_troop", slot_troop_spouse, "trp_player"),
            (troop_get_slot, ":spouse", ":giver_troop", slot_troop_spouse),
            (try_begin),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (call_script, "script_troop_get_player_relation", ":giver_troop"),
              (assign, ":relation", reg0),
              (assign, ":continue", 0),
              (try_begin),
                (lt, ":spouse", 0),
                (try_begin),
                  (this_or_next|troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_conventional),
                  (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_moralist),
                  (gt, ":relation",10),
                  (assign, ":continue", 1),
                (else_try),
                  (this_or_next|troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_adventurous),
                  (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_ambitious),
                  (gt, ":relation",5),
                  (assign, ":continue", 1),
                (else_try),
                  (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_otherworldly),
                  (gt, ":relation",8),
                  (assign, ":continue", 1),
                (try_end),
              (else_try),
                (gt, ":spouse", 0),
                (try_begin),
                  (this_or_next|troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_conventional),
                  (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_moralist),
                  (gt, ":relation",15),
                  (assign, ":continue", 1),
                (else_try),
                  (this_or_next|troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_adventurous),
                  (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_ambitious),
                  (ge, ":relation",10),
                  (assign, ":continue", 1),
                (else_try),
                  (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_otherworldly),
                  (gt, ":relation",12),
                  (assign, ":continue", 1),
                (try_end),
              (try_end),
              (eq, ":continue", 1),
              (store_random_in_range, ":rand", 1,4),
              (assign, ":quest_dont_give_again_period", 30),
              (assign, ":quest_expiration_days", 30),
              (assign, ":quest_target_amount", ":rand"),
              (assign, ":quest_target_item", "itm_wine"),
              (assign, ":result", ":quest_no"),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_lend_surgeon"),
            (try_begin),
              (eq, "$g_defending_against_siege", 0),#Skip if the center is under siege (because of resting)
              (neq, ":giver_reputation", lrep_quarrelsome),
              (neq, ":giver_reputation", lrep_debauched),
              (assign, ":max_surgery_level", 0),
              (assign, ":best_surgeon", -1),
              (party_get_num_companion_stacks, ":num_stacks","p_main_party"),
              (try_for_range, ":i_stack", 1, ":num_stacks"),
                (party_stack_get_troop_id, ":stack_troop","p_main_party",":i_stack"),
                (troop_is_hero, ":stack_troop"),
                (store_skill_level, ":cur_surgery_skill", skl_surgery, ":stack_troop"),
                (gt, ":cur_surgery_skill", ":max_surgery_level"),
                (assign, ":max_surgery_level", ":cur_surgery_skill"),
                (assign, ":best_surgeon", ":stack_troop"),
              (try_end),
              
              (store_character_level, ":cur_level", "trp_player"),
              (assign, ":required_skill", 5),
              (val_div, ":cur_level", 10),
              (val_add, ":required_skill", ":cur_level"),
              (ge, ":max_surgery_level", ":required_skill"), #Skip if party skill level is less than the required value
              
              (assign, ":quest_object_troop", ":best_surgeon"),
              (assign, ":quest_importance", 4),
              (assign, ":quest_xp_reward", 100),
              (assign, ":quest_gold_reward", 100),
              (assign, ":quest_dont_give_again_period", 50),
              (assign, ":result", ":quest_no"),
            (try_end),
            # Lord Quests
          (else_try),
            (eq, ":quest_no", "qst_meet_spy_in_enemy_town"),
            (eq, "$player_has_homage", 0),
            
            (try_begin),
              (eq, "$players_kingdom", ":giver_faction_no"),
              (neq, ":giver_reputation", lrep_goodnatured),
              (neq, ":giver_reputation", lrep_martial),
              
              (call_script, "script_troop_get_player_relation", ":giver_troop"),
              (assign, ":giver_relation", reg0),
              (gt, ":giver_relation", 3),
              (call_script, "script_cf_faction_get_random_enemy_faction", ":giver_faction_no"),
              (assign, ":enemy_faction", reg0),
              (store_relation, ":reln", ":enemy_faction", "fac_player_faction"),
              (lt, ":reln", 0),
              (call_script, "script_cf_select_random_town_with_faction", ":enemy_faction"),
              (assign, ":cur_target_center", reg0),
              #Just to make sure that there is a free walker
              (call_script, "script_cf_center_get_free_walker", ":cur_target_center"),
              (assign, ":quest_target_center", ":cur_target_center"),
              (store_random_in_range, ":quest_target_amount", secret_signs_begin, secret_signs_end),
              (assign, ":result", ":quest_no"),
              (assign, ":quest_gold_reward", 500),
              (assign, ":quest_importance", 15),
              (assign, ":quest_xp_reward", 1000),
              (assign, ":quest_expiration_days", 30),
              (assign, ":quest_dont_give_again_period", 60),
              (quest_set_slot, "qst_meet_spy_in_enemy_town", slot_quest_gold_reward, 500),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_raid_caravan_to_start_war"),
            (val_add, ":quest_no", 1),
            (eq, 1, 0), #disable this as a random quest
            
            # (try_begin),
              # (eq, "$players_kingdom", ":giver_faction_no"),
              # (this_or_next|eq, ":giver_reputation", lrep_cunning),
              # (this_or_next|eq, ":giver_reputation", lrep_quarrelsome),
              # (             eq, ":giver_reputation", lrep_debauched),
              # (gt, ":player_level", 10),
              # (eq, 1, 0), #disable this as a random quest
              
              # (neg|faction_slot_eq, ":giver_faction_no", slot_faction_leader, ":giver_troop"),#Can not take the quest from the king
              # (call_script, "script_cf_faction_get_random_friendly_faction", ":giver_faction_no"),#Can fail
              # (assign, ":quest_target_faction", reg0),
              # (store_troop_faction, ":quest_object_faction", ":giver_troop"),
              # (assign, ":quest_target_party_template", "pt_kingdom_caravan_party"),
              # (assign, ":quest_target_amount", 2),
              # (assign, ":result", ":quest_no"),
              # (assign, ":quest_expiration_days", 30),
              # (assign, ":quest_dont_give_again_period", 110),
            # (try_end),
          (else_try),
            (eq, ":quest_no", "qst_deliver_message"),
            (try_begin),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (le, ":player_level", 20),
              (neg|troop_slot_ge, "trp_player", slot_troop_renown, 250),
              (call_script, "script_cf_get_random_lord_in_a_center_with_faction", ":giver_faction_no"),#Can fail
              (assign, ":cur_target_troop", reg0),
              (neq, ":cur_target_troop", ":giver_troop"),#Skip himself
              (call_script, "script_get_troop_attached_party", ":cur_target_troop"),
              (assign, ":cur_target_center", reg0),#cur_target_center will definitely be a valid center
              (neq,":giver_center_no", ":cur_target_center"),#Skip current center
              (assign, ":quest_target_center", ":cur_target_center"),
              (assign, ":quest_target_troop", ":cur_target_troop"),
              (assign, ":quest_xp_reward", 30),
              (assign, ":quest_gold_reward", 60),
              (assign, ":quest_dont_give_again_period", 15),
              (assign, ":result", ":quest_no"),
              (assign, ":quest_expiration_days", 20),
            (try_end),
          (else_try),
            (eq, ":quest_no","qst_blank_quest_2"),
            (try_begin),
              (is_between, ":giver_troop", active_npcs_begin, active_npcs_end),
              (troop_slot_eq, ":giver_troop", slot_troop_occupation, slto_kingdom_hero),
              (party_get_attached_to, ":attached_to",":giver_party_no"),
              (is_between, ":attached_to", walled_centers_begin, walled_centers_end),
              (party_slot_eq, ":attached_to", slot_town_lord, ":giver_troop"),
              (troop_slot_eq, ":giver_troop", slot_troop_religion, 1),#christian
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (troop_slot_ge, "trp_player", slot_troop_renown, 250),
              (store_relation,":rel","fac_christians","fac_player_faction"),
              (gt, ":rel", 50),
              (gt, "$player_honor", 30),
              (hero_can_join),
              (store_random_in_range, ":mon", "p_monasterio1", "p_yourlair"),
              (store_character_level, ":quest_target_amount", "trp_player"),
              (val_add, ":quest_target_amount", 15),
              (store_distance_to_party_from_party, ":dist", ":giver_center_no", ":mon"),
              (val_add, ":dist", 15),
              (val_mul, ":dist", 2),
              (val_mul, ":quest_target_amount", ":dist"),
              (val_div, ":quest_target_amount",100),
              (val_mul, ":quest_target_amount",10),
              (assign, ":quest_gold_reward", ":quest_target_amount"),
              (val_div, ":quest_gold_reward", 40),
              (val_mul, ":quest_gold_reward", 10),
              (assign, ":quest_xp_reward", 200),
              (assign, ":quest_expiration_days", 15),
              (assign, ":quest_dont_give_again_period", 30),
              (assign, ":quest_target_center", ":mon"),
              (assign, ":result", ":quest_no"),
            (try_end),
          (else_try),
            (eq, ":quest_no","qst_blank_quest_3"),
            (try_begin),
              (is_between, ":giver_troop", active_npcs_begin, active_npcs_end),
              (troop_slot_eq, ":giver_troop", slot_troop_occupation, slto_kingdom_hero),
              (troop_slot_eq, ":giver_troop", slot_troop_religion, 2),#pagan
              (this_or_next|troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_quarrelsome),
              (this_or_next|troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_debauched),
              (this_or_next|troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_cunning),
              (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_roguish),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (troop_slot_ge, "trp_player", slot_troop_renown, 130),
              (store_character_level, ":quest_target_amount", "trp_player"),
              (gt,":quest_target_amount", 8),
              (store_random_in_range, ":mon", "p_monasterio1", "p_yourlair"),
              (val_add, ":quest_target_amount", 15),
              (val_mul, ":quest_target_amount", 50),
              (val_div, ":quest_target_amount",100),
              (val_mul, ":quest_target_amount",100),
              (assign, ":quest_expiration_days", 30),
              (assign, ":quest_dont_give_again_period", 45),
              (assign, ":quest_importance", 7),
              (assign, ":quest_gold_reward", 0),
              (assign, ":quest_xp_reward", 1000),
              (assign, ":quest_target_center", ":mon"),
              (assign, ":result", ":quest_no"),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_escort_lady"),
            (try_begin),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (ge, ":player_level", 10),
              
              (ge, ":giver_troop", 0), #skip troops without fathers in range
              
              (assign, ":cur_object_troop", -1),
              (try_for_range, ":lady", kingdom_ladies_begin, kingdom_ladies_end),
                (troop_slot_eq, ":lady", slot_troop_father, ":giver_troop"),
                (neg|troop_slot_eq, "trp_player", slot_troop_spouse, ":lady"),#avoid escorting your wife
                (assign, ":cur_object_troop", ":lady"),
              (try_end),
              
              (ge, ":cur_object_troop", 0),
              
              (troop_get_slot, ":giver_troop_confirm", ":cur_object_troop", slot_troop_father),  # just to make sure
              (eq, ":giver_troop", ":giver_troop_confirm"), # just to make sure
              
              (store_random_in_range, ":random_no", 0, 2),
              (try_begin),
                (eq, ":random_no", 0),
                (troop_get_slot, ":cur_object_troop_2", ":giver_troop", slot_troop_spouse),
                (is_between, ":cur_object_troop_2", kingdom_ladies_begin, kingdom_ladies_end),
                (troop_get_slot, ":giver_troop_confirm", ":cur_object_troop_2", slot_troop_spouse),  # just to make sure
                (eq, ":giver_troop", ":giver_troop_confirm"), # just to make sure
                (assign, ":cur_object_troop", ":cur_object_troop_2"),
              (try_end),
              (gt, ":cur_object_troop", 0),#Skip lords without a lady
              (troop_get_type, ":cur_troop_gender", ":cur_object_troop"),
              (val_mod, ":cur_troop_gender", 2),
              (eq, ":cur_troop_gender", 1),#Skip if it is not female
              (gt, ":giver_center_no", 0),#Skip if lord is outside the center
              (troop_slot_eq, ":cur_object_troop", slot_troop_cur_center, ":giver_center_no"),#Skip if the lady is not at the same center
              (call_script, "script_cf_select_random_town_with_faction", ":giver_faction_no"),#Can fail
              (assign, ":cur_target_center", reg0),
              (neq, ":cur_target_center", ":giver_center_no"),
              (hero_can_join),#Skip if player has no available slots
              
              (assign, ":quest_object_troop", ":cur_object_troop"),
              (assign, ":quest_target_center", ":cur_target_center"),
              (assign, ":quest_expiration_days", 20),
              (assign, ":quest_dont_give_again_period", 50),
              (assign, ":result", ":quest_no"),
            (try_end),
            ##        (else_try),
            ##          (eq, ":quest_no", "qst_hunt_down_raiders"),
            ##          (try_begin),
            ##            (gt, ":player_level", 10),
            ##            (faction_slot_eq, ":giver_faction_no", slot_faction_leader, ":giver_troop"),
            ##            (call_script, "script_cf_select_random_town_with_faction", ":giver_faction_no"),#Can fail
            ##            (assign, ":cur_object_center", reg0),
            ##            (neq, ":cur_object_center", ":giver_center_no"),#Skip current center
            ##            (call_script, "script_get_random_enemy_center", ":giver_party_no"),
            ##            (assign, ":cur_target_center", reg0),
            ##            (ge, ":cur_target_center", 0),
            ##            (store_faction_of_party, ":cur_target_faction", ":cur_target_center"),
            ##            (is_between,  ":cur_target_faction", kingdoms_begin, kingdoms_end),
            ##
            ##            (assign, ":quest_object_center", ":cur_object_center"),
            ##            (assign, ":quest_target_center", ":cur_target_center"),
            ##            (assign, ":quest_importance", 1),
            ##            (assign, ":quest_xp_reward", 1500),
            ##            (assign, ":quest_gold_reward", 1000),
            ##            (assign, ":result", ":quest_no"),
            ##          (try_end),
            ##        (else_try),
            ##          (eq, ":quest_no", "qst_bring_back_deserters"),
            ##          (try_begin),
            ##            (gt, ":player_level", 5),
            ##            (faction_get_slot, ":cur_target_party_template", ":giver_faction_no", slot_faction_deserter_party_template),
            ##            (faction_get_slot, ":cur_target_troop", ":giver_faction_no", slot_faction_deserter_troop),
            ##            (gt, ":cur_target_party_template", 0),#Skip factions with no deserter party templates
            ##            (store_num_parties_of_template, ":num_deserters", ":cur_target_party_template"),
            ##            (ge, ":num_deserters", 2),#Skip if there are less than 2 active deserter parties
            ##
            ##            (assign, ":quest_target_troop", ":cur_target_troop"),
            ##            (assign, ":quest_target_party_template", ":cur_target_party_template"),
            ##            (assign, ":quest_target_amount", 5),
            ##            (assign, ":quest_importance", 1),
            ##            (assign, ":quest_xp_reward", 500),
            ##            (assign, ":quest_gold_reward", 300),
            ##            (assign, ":result", ":quest_no"),
            ##          (try_end),
            ##        (else_try),
            ##          (eq, ":quest_no", "qst_deliver_supply_to_center_under_siege"),
            ##          (try_begin),
            ##            (gt, ":player_level", 10),
            ##            (gt, ":giver_center_no", 0),#Skip if lord is outside the center
            ##            (call_script, "script_cf_get_random_siege_location_with_faction", ":giver_faction_no"),#Can fail
            ##            (assign, ":quest_target_center", reg0),
            ##            (assign, ":quest_target_amount", 10),
            ##            (assign, ":quest_importance", 1),
            ##            (assign, ":quest_xp_reward", 500),
            ##            (assign, ":quest_gold_reward", 300),
            ##            (assign, ":result", ":quest_no"),
            ##          (try_end),
            ##        (else_try),
            ##          (eq, ":quest_no", "qst_rescue_lady_under_siege"),
            ##          (try_begin),
            ##            (gt, ":player_level", 15),
            ##            (troop_get_slot, ":cur_object_troop", ":giver_troop", slot_troop_daughter),
            ##            (store_random_in_range, ":random_no", 0, 2),
            ##            (try_begin),
            ##              (this_or_next|eq,  ":cur_object_troop", 0),
            ##              (eq, ":random_no", 0),
            ##              (troop_get_slot, ":cur_object_troop_2", ":giver_troop", slot_troop_spouse),
            ##              (gt, ":cur_object_troop_2", 0),
            ##              (assign, ":cur_object_troop", ":cur_object_troop_2"),
            ##            (try_end),
            ##            (gt, ":cur_object_troop", 0),#Skip lords without a lady
            ##            (troop_get_type, ":cur_troop_gender", ":cur_object_troop"),
            ##            (val_mod, ":cur_troop_gender", 2),
            ##            (eq, ":cur_troop_gender", 1),#Skip if lady is not female
            ##            (troop_get_slot, ":cur_target_center", ":cur_object_troop", slot_troop_cur_center),
            ##            (is_between, ":cur_target_center", centers_begin, centers_end),#Skip if she is not in a center
            ##            (neq,":giver_center_no", ":cur_target_center"),#Skip current center
            ##            (call_script, "script_cf_get_random_siege_location_with_faction", ":giver_faction_no"),#Can fail
            ##            (assign, ":cur_target_center", reg0),
            ##            (troop_set_slot, ":cur_object_troop", slot_troop_cur_center, ":cur_target_center"),#Move lady to the siege location
            ##            (assign, ":quest_object_troop", ":cur_object_troop"),
            ##            (assign, ":quest_target_center", ":cur_target_center"),
            ##            (assign, ":quest_target_troop", ":giver_troop"),
            ##            (assign, ":quest_importance", 1),
            ##            (assign, ":quest_xp_reward", 200),
            ##            (assign, ":quest_gold_reward", 750),
            ##            (assign, ":result", ":quest_no"),
            ##          (try_end),
          # (else_try),
            # (eq, ":quest_no", "qst_blank_quest_18"),
            # (try_begin),
              # (is_between, ":player_level", 5, 30),
              # (gt, "$player_honor", 30),
              # (is_between, ":giver_troop", kingdom_ladies_begin, kingdom_ladies_end),
              # (troop_get_slot, ":cur_target_troop", ":giver_troop", slot_troop_lover),
              # (gt, ":cur_target_troop", 0),#Skip ladies without a lover
              # (troop_get_slot, ":cur_target_center", ":cur_target_troop", slot_troop_cur_center),
              # (neq,":giver_center_no", ":cur_target_center"),#Skip current center
              # (neg|troop_slot_eq, "trp_player", slot_troop_spouse, "$g_talk_troop"),
              # (neg|troop_slot_eq, ":cur_target_troop", slot_troop_spouse, "$g_talk_troop"),
              # (call_script, "script_troop_get_player_relation", ":giver_troop"),
              # (gt, reg0, 2),
              # (assign, ":quest_target_troop", ":cur_target_troop"),
              # (assign, ":quest_expiration_days", 30),
              # (assign, ":quest_dont_give_again_period", 30),
              # (assign, ":result", ":quest_no"),
            # (try_end),
            ##        (else_try),
            ##          (eq, ":quest_no", "qst_bring_reinforcements_to_siege"),
            ##          (try_begin),
            ##            (gt, ":player_level", 10),
            ##            (call_script, "script_cf_get_random_siege_location_with_attacker_faction", ":giver_faction_no"),#Can fail
            ##            (assign, ":cur_target_center", reg0),
            ##            (store_random_in_range, ":random_no", 5, 11),
            ##            (troops_can_join, ":random_no"),#Skip if the player doesn't have enough room
            ##            (call_script, "script_cf_get_number_of_random_troops_from_party", ":giver_party_no", ":random_no"),#Can fail
            ##            (assign, ":cur_object_troop", reg0),
            ##            (party_get_battle_opponent, ":cur_target_party", ":cur_target_center"),
            ##            (party_get_num_companion_stacks, ":num_stacks", ":cur_target_party"),
            ##            (gt, ":num_stacks", 0),#Skip if the besieger party has no troops
            ##            (party_stack_get_troop_id, ":cur_target_troop", ":cur_target_party", 0),
            ##            (troop_is_hero, ":cur_target_troop"),#Skip if the besieger party has no heroes
            ##            (neq, ":cur_target_troop", ":giver_troop"),#Skip if the quest giver is the same troop
            ##            (assign, ":quest_target_troop", ":cur_target_troop"),
            ##            (assign, ":quest_object_troop", ":cur_object_troop"),
            ##            (assign, ":quest_target_party", ":cur_target_party"),
            ##            (assign, ":quest_target_center", ":cur_target_center"),
            ##            (assign, ":quest_target_amount", ":random_no"),
            ##            (assign, ":quest_importance", 1),
            ##            (assign, ":quest_xp_reward", 400),
            ##            (assign, ":quest_gold_reward", 200),
            ##            (assign, ":result", ":quest_no"),
            ##          (try_end),
          (else_try),
            (eq, ":quest_no", "qst_deliver_message_to_enemy_lord"),
            (try_begin),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (is_between, ":player_level", 5,25),
              (call_script, "script_cf_get_random_lord_from_another_faction_in_a_center", ":giver_faction_no"),#Can fail
              (assign, ":cur_target_troop", reg0),
              (call_script, "script_get_troop_attached_party", ":cur_target_troop"),
              (assign, ":quest_target_center", reg0),#quest_target_center will definitely be a valid center
              (assign, ":quest_target_troop", ":cur_target_troop"),
              (assign, ":quest_importance", 1),
              (assign, ":quest_xp_reward", 200),
              (assign, ":quest_gold_reward", 0),
              (assign, ":result", ":quest_no"),
              (assign, ":quest_expiration_days", 70),
            (try_end),
            ##        (else_try),
            ##          (eq, ":quest_no", "qst_bring_prisoners_to_enemy"),
            ##          (try_begin),
            ##            (gt, ":player_level", 10),
            ##            (is_between, ":giver_center_no", centers_begin, centers_end),#Skip if the quest giver is not at a center
            ##            (store_random_in_range, ":random_no", 5, 11),
            ##            (troops_can_join_as_prisoner, ":random_no"),#Skip if the player doesn't have enough room
            ##            (call_script, "script_get_random_enemy_town", ":giver_center_no"),
            ##            (assign, ":cur_target_center", reg0),
            ##            (ge, ":cur_target_center", 0),#Skip if there are no enemy towns
            ##            (store_faction_of_party, ":cur_target_faction", ":cur_target_center"),
            ##            (faction_get_slot, ":cur_object_troop", ":cur_target_faction", slot_faction_tier_5_troop),
            ##            (assign, ":quest_target_center", ":cur_target_center"),
            ##            (assign, ":quest_object_troop", ":cur_object_troop"),
            ##            (assign, ":quest_target_amount", ":random_no"),
            ##            (assign, ":quest_importance", 1),
            ##            (assign, ":quest_xp_reward", 300),
            ##            (assign, ":quest_gold_reward", 200),
            ##            (assign, ":result", ":quest_no"),
            ##          (try_end),
          (else_try),
            (eq, ":quest_no", "qst_deal_with_bandits_at_lords_village"),
            (try_begin),
              (neq, ":giver_reputation", lrep_debauched),
              (neq, ":giver_reputation", lrep_quarrelsome),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (assign, ":end_cond", villages_end),
              (assign, ":cur_target_center", -1),
              (try_for_range, ":cur_village", villages_begin, ":end_cond"),
                (party_slot_eq, ":cur_village", slot_town_lord, ":giver_troop"),
                (party_slot_eq, ":cur_village", slot_village_infested_by_bandits, 1),
                (party_slot_eq, ":cur_village", slot_village_state, svs_normal),
                (assign, ":cur_target_center", ":cur_village"),
                (assign, ":end_cond", 0),
              (try_end),
              (ge, ":cur_target_center", 0),
              (neg|check_quest_active, "qst_eliminate_bandits_infesting_village"),
              (assign, ":quest_target_center", ":cur_target_center"),
              (assign, ":result", ":quest_no"),
              (assign, ":quest_expiration_days", 60),
              (assign, ":quest_importance", 25),
              (assign, ":quest_xp_reward", 350),
              (store_character_level, ":level", "trp_player"),
              (store_mul, ":quest_gold_reward", ":level", 20),
              (val_add, ":quest_gold_reward", 300),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_raise_troops"),
            (try_begin),
              (neq, ":giver_reputation", lrep_martial),
              (neq, ":giver_faction_no", "fac_player_supporters_faction"), #we need tier_1_troop a valid value
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (store_character_level, ":cur_level", "trp_player"),
              (gt, ":cur_level", 35), #level 35 need. This quest is very difficult for low level players
              (troop_slot_ge, "trp_player", slot_troop_renown, 800), #renow 800 need.
              
              (store_random_in_range, ":quest_target_amount", 5, 8),
              (party_get_free_companions_capacity, ":free_capacity", "p_main_party"),
              (le, ":quest_target_amount", ":free_capacity"),
              (faction_get_slot, ":quest_object_troop", ":giver_faction_no", slot_faction_tier_1_troop),
              (store_random_in_range, ":level_up", 20, 40),
              (val_add, ":level_up", ":cur_level"),
              (val_div, ":level_up", 10),
              
              (store_mul, ":quest_gold_reward", ":quest_target_amount", 10),
              
              (assign, ":quest_target_troop", ":quest_object_troop"),
              (try_for_range, ":unused", 0, ":level_up"),
                (troop_get_upgrade_troop, ":level_up_troop", ":quest_target_troop", 0),
                (gt, ":level_up_troop", 0),
                (assign, ":quest_target_troop", ":level_up_troop"),
                (val_mul, ":quest_gold_reward", ":quest_gold_reward", 7),
                (val_div, ":quest_gold_reward", ":quest_gold_reward", 4),
              (try_end),
              
              (assign, ":quest_xp_reward", ":quest_gold_reward"),
              (val_mul, ":quest_xp_reward", 3),
              (val_div, ":quest_xp_reward", 10),
              (assign, ":result", ":quest_no"),
              (assign, ":quest_expiration_days", 120),
              (assign, ":quest_dont_give_again_period", 20),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_collect_taxes"),
            (eq, "$player_has_homage", 0),
            
            (try_begin),
              (neq, ":giver_reputation", lrep_goodnatured),
              (neq, ":giver_reputation", lrep_upstanding),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (call_script, "script_cf_troop_get_random_leaded_town_or_village_except_center", ":giver_troop", ":giver_center_no"),
              (assign, ":quest_target_center", reg0),
              (assign, ":quest_importance", 1),
              (assign, ":quest_gold_reward", 0),
              (assign, ":quest_xp_reward", 100),
              (assign, ":result", ":quest_no"),
              (assign, ":quest_expiration_days", 50),
              (assign, ":quest_dont_give_again_period", 30),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_hunt_down_fugitive"),
            (eq, "$player_has_homage", 0),
            (try_begin),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (call_script, "script_cf_select_random_village_with_faction", ":giver_faction_no"),
              (assign, ":quest_target_center", reg0),
              #					(try_begin),
              #						(eq, "$campaign_type", camp_storyline),
              #						(eq, ":quest_target_center", "p_village_150"),
              #						(call_script, "script_cf_select_random_village_with_faction", ":giver_faction_no"),
              #						(assign, ":quest_target_center", reg0),
              #					(try_end),
              #					(try_begin),
              #						(eq, "$campaign_type", camp_storyline),
              #						(neq, ":quest_target_center", "p_village_150"),
              (store_random_in_range, ":quest_target_dna", 0, 1000000),
              (assign, ":result", ":quest_no"),
              (assign, ":quest_expiration_days", 30),
              (assign, ":quest_dont_give_again_period", 30),
              #					(else_try),
              #						(neq, "$campaign_type", camp_storyline),
              #						(store_random_in_range, ":quest_target_dna", 0, 1000000),
              #						(assign, ":result", ":quest_no"),
              #						(assign, ":quest_expiration_days", 30),
              #						(assign, ":quest_dont_give_again_period", 40),
              #					(try_end),
            (try_end),
            ##        (else_try),
            ##          (eq, ":quest_no", "qst_capture_messenger"),
            ##          (try_begin),
            ##            (call_script, "script_cf_faction_get_random_enemy_faction", ":giver_faction_no"),
            ##            (assign, ":cur_target_faction", reg0),
            ##            (faction_get_slot, ":cur_target_troop", ":cur_target_faction", slot_faction_messenger_troop),
            ##            (gt, ":cur_target_troop", 0),#Checking the validiy of cur_target_troop
            ##            (store_num_parties_destroyed_by_player, ":quest_target_amount", "pt_messenger_party"),
            ##
            ##            (assign, ":quest_target_troop", ":cur_target_troop"),
            ##            (assign, ":quest_target_party_template", ":cur_target_party_template"),
            ##            (assign, ":quest_importance", 1),
            ##            (assign, ":quest_xp_reward", 700),
            ##            (assign, ":quest_gold_reward", 400),
            ##            (assign, ":result", ":quest_no"),
            ##          (try_end),
          (else_try),
            (eq, ":quest_no", "qst_kill_local_merchant"),
            (eq, "$player_has_homage", 0),
            
            (try_begin),
              (this_or_next|eq, ":giver_reputation", lrep_quarrelsome),
              (this_or_next|eq, ":giver_reputation", lrep_cunning),
              (eq, ":giver_reputation", lrep_debauched),
              (neg|faction_slot_eq, ":giver_faction_no", slot_faction_leader, ":giver_troop"),#Can not take the quest from the king
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (gt, ":player_level", 5),
              (is_between, ":giver_center_no", towns_begin, towns_end),
              (assign, ":quest_importance", 40),
              (assign, ":quest_xp_reward", 300),
              (assign, ":quest_gold_reward", 1600),
              (assign, ":result", ":quest_no"),
              (assign, ":quest_expiration_days", 10),
              (assign, ":quest_dont_give_again_period", 40),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_bring_back_runaway_serfs"),
            (try_begin),
              (neq, ":giver_reputation", lrep_goodnatured),
              (neq, ":giver_reputation", lrep_upstanding),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (ge, ":player_level", 5),
              (gt, ":giver_center_no", 0),#Skip if lord is outside the center
              (eq, "$g_defending_against_siege", 0),#Skip if the center is under siege (because of resting)
              
              (assign, ":cur_object_center", -1),
              (try_for_range, ":cur_village", villages_begin, villages_end),
                (party_slot_eq, ":cur_village", slot_town_lord, ":giver_troop"),
                (store_distance_to_party_from_party, ":dist", ":cur_village", ":giver_center_no"),
                (lt, ":dist", 25),
                (assign, ":cur_object_center", ":cur_village"),
              (try_end),
              (ge, ":cur_object_center", 0),#Skip if the quest giver is not the owner of any villages around the center
              (call_script, "script_cf_select_random_town_with_faction", ":giver_faction_no"),
              (assign, ":cur_target_center", reg0),
              (neq, ":cur_target_center", ":giver_center_no"),#Skip current center
              (store_distance_to_party_from_party, ":dist", ":cur_target_center", ":giver_center_no"),
              (ge, ":dist", 20),
              (assign, ":quest_target_party_template", "pt_runaway_serfs"),
              (assign, ":quest_object_center", ":cur_object_center"),
              (assign, ":quest_target_center", ":cur_target_center"),
              (assign, ":quest_importance", 10),
              (assign, ":quest_xp_reward", 300),
              (assign, ":quest_gold_reward", 300),
              (assign, ":result", ":quest_no"),
              (assign, ":quest_expiration_days", 30),
              (assign, ":quest_dont_give_again_period", 20),
              (assign, "$qst_bring_back_runaway_serfs_num_parties_returned", 0),
              (assign, "$qst_bring_back_runaway_serfs_num_parties_fleed", 0),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_follow_spy"),
            (eq, "$player_has_homage", 0),
            
            (try_begin),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (neq, ":giver_reputation", lrep_goodnatured),
              (party_get_skill_level, ":tracking_skill", "p_main_party", "skl_tracking"),
              (ge, ":tracking_skill", 2),
              (ge, ":player_level", 10),
              (eq, "$g_defending_against_siege", 0), #Skip if the center is under siege (because of resting)
              (gt, ":giver_party_no", 0), #Skip if the quest giver doesn't have a party
              (gt, ":giver_center_no", 0), #skip if the quest giver is not in a center
              (party_slot_eq, "$g_encountered_party", slot_party_type, spt_town), #skip if we are not in a town.
              (party_get_position, pos2, "p_main_party"),
              #(assign, ":min_distance", 99999),
              (assign, ":cur_object_center", -1),
              (assign,":end_loop",10),
              (try_for_range, ":unused_2", 0, ":end_loop"),
                (call_script, "script_cf_get_random_kingdom_center", ":giver_party_no", centers_begin, centers_end),
                (assign, ":random_object_center", reg0),
                (gt, ":random_object_center", -1),
                (party_get_position, pos3, ":random_object_center"),
                (map_get_random_position_around_position, pos4, pos3, 5),
                #(get_distance_between_positions, ":cur_distance", pos2, pos4),
                #(lt, ":cur_distance", ":min_distance"),
                #(assign, ":min_distance", ":cur_distance"),
                (assign, ":cur_object_center", ":random_object_center"),
                (copy_position, pos63, pos4), #Do not change pos63 until quest is accepted
                (assign,":end_loop",0),
              (try_end),
              (gt, ":cur_object_center", 0), #Skip if there are no kingdom centers
              
              (assign, ":quest_object_center", ":cur_object_center"),
              (assign, ":quest_dont_give_again_period", 70),
              (assign, ":quest_importance", 20),
              (assign, ":quest_xp_reward", 4000),
              (assign, ":quest_gold_reward", 2000),
              (assign, ":result", ":quest_no"),
              (assign, "$qst_follow_spy_run_away", 0),
              (assign, "$qst_follow_spy_meeting_state", 0),
              (assign, "$qst_follow_spy_meeting_counter", 0),
              (assign, "$qst_follow_spy_spy_back_in_town", 0),
              (assign, "$qst_follow_spy_partner_back_in_town", 0),
              (assign, "$qst_follow_spy_no_active_parties", 0),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_capture_enemy_hero"),
            (try_begin),
              (eq, "$players_kingdom", ":giver_faction_no"),
              (neg|faction_slot_eq, "$players_kingdom", slot_faction_marshal, "trp_player"),
              (ge, ":player_level", 15),
              (call_script, "script_cf_faction_get_random_enemy_faction", ":giver_faction_no"),#Can fail
              (assign, ":quest_target_faction", reg0),
              (assign, ":quest_expiration_days", 30),
              (assign, ":quest_dont_give_again_period", 70),
              (assign, ":quest_importance", 20),
              (assign, ":quest_xp_reward", 2500),
              (assign, ":quest_gold_reward", 2000),
              (assign, ":result", ":quest_no"),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_lend_companion"),
            (try_begin),
              (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              (assign, ":total_heroes", 0),
              (party_get_num_companion_stacks, ":num_stacks", "p_main_party"),
              (try_for_range, ":i_stack", 0, ":num_stacks"),
                (party_stack_get_troop_id, ":stack_troop","p_main_party",":i_stack"),
                (troop_is_hero, ":stack_troop"),
                (is_between, ":stack_troop", companions_begin, companions_end),
                (store_character_level, ":stack_level", ":stack_troop"),
                (ge, ":stack_level", 15),
                (assign, ":is_quest_hero", 0),
                (try_for_range, ":i_quest", 0, all_quests_end),
                  (check_quest_active, ":i_quest"),
                  (this_or_next|quest_slot_eq, ":i_quest", slot_quest_target_troop, ":stack_troop"),
                  (quest_slot_eq, ":i_quest", slot_quest_object_troop, ":stack_troop"),
                  (assign, ":is_quest_hero", 1),
                (try_end),
                (eq, ":is_quest_hero", 0),
                (val_add, ":total_heroes", 1),
              (try_end),
              (gt, ":total_heroes", 0),#Skip if party has no eligible heroes
              (store_random_in_range, ":random_hero", 0, ":total_heroes"),
              (assign, ":total_heroes", 0),
              (assign, ":cur_target_troop", -1),
              (try_for_range, ":i_stack", 0, ":num_stacks"),
                (eq, ":cur_target_troop", -1),
                (party_stack_get_troop_id, ":stack_troop","p_main_party",":i_stack"),
                (troop_is_hero, ":stack_troop"),
                (is_between, ":stack_troop", companions_begin, companions_end),
                (neq, ":stack_troop", "trp_player"),
                (store_character_level, ":stack_level", ":stack_troop"),
                (ge, ":stack_level", 15),
                (assign, ":is_quest_hero", 0),
                (try_for_range, ":i_quest", 0, all_quests_end),
                  (check_quest_active, ":i_quest"),
                  (this_or_next|quest_slot_eq, ":i_quest", slot_quest_target_troop, ":stack_troop"),
                  (quest_slot_eq, ":i_quest", slot_quest_object_troop, ":stack_troop"),
                  (assign, ":is_quest_hero", 1),
                (try_end),
                (eq, ":is_quest_hero", 0),
                (val_add, ":total_heroes", 1),
                (gt, ":total_heroes", ":random_hero"),
                (assign, ":cur_target_troop", ":stack_troop"),
              (try_end),
              (is_between, ":cur_target_troop", companions_begin, companions_end),
              
              (assign, ":quest_target_troop", ":cur_target_troop"),
              (store_current_day, ":quest_target_amount"),
              (val_add, ":quest_target_amount", 8),
              
              (assign, ":quest_importance", 3),
              (assign, ":quest_xp_reward", 200),
              (assign, ":quest_gold_reward", 0),
              (assign, ":result", ":quest_no"),
              (assign, ":quest_dont_give_again_period", 40),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_collect_debt"),
            (val_add, ":quest_no", 1),
            (eq, 1, 0), #disable this quest pending talk with armagan
            # (try_begin),
              # (ge, "$g_talk_troop_faction_kingdom_relation", 0),
              # # Find a vassal (within the same kingdom?)
              # (call_script, "script_cf_get_random_lord_in_a_center_with_faction", ":giver_faction_no"),#Can fail
              # (assign, ":quest_target_troop", reg0),
              # (neq, ":quest_target_troop", ":giver_troop"),#Skip himself
              # (call_script, "script_get_troop_attached_party", ":quest_target_troop"),
              # (assign, ":quest_target_center", reg0),#cur_target_center will definitely be a valid center
              # (neq,":giver_center_no", ":quest_target_center"),#Skip current center
              
              # (assign, ":quest_importance", 10),
              # (assign, ":quest_xp_reward", 100),
              # (assign, ":quest_gold_reward", 0),
              # (assign, ":result", ":quest_no"),
              # (store_random_in_range, ":quest_target_amount", 6, 9),
              # (val_mul, ":quest_target_amount", 500),
              # (store_div, ":quest_convince_value", ":quest_target_amount", 5),
              # (assign, ":quest_expiration_days", 90),
              # (assign, ":quest_dont_give_again_period", 30),
            # (try_end),
            ##        (else_try),
            ##          (eq, ":quest_no", "qst_capture_conspirators"),
            ##          (try_begin),
            ##            (eq, 1,0), #TODO: disable this for now
            ##            (ge, ":player_level", 10),
            ##            (is_between, ":giver_center_no", towns_begin, towns_end),#Skip if quest giver's center is not a town
            ##            (party_slot_eq, ":giver_center_no", slot_town_lord, ":giver_troop"),#Skip if the current center is not ruled by the quest giver
            ##            (call_script, "script_cf_get_random_kingdom_hero", ":giver_faction_no"),#Can fail
            ##
            ##            (assign, ":quest_target_troop", reg0),
            ##            (assign, ":quest_target_center", ":giver_center_no"),
            ##            (assign, ":quest_importance", 1),
            ##            (assign, ":quest_xp_reward", 10),
            ##            (assign, ":quest_gold_reward", 10),
            ##            (assign, ":result", ":quest_no"),
            ##            (store_character_level, ":cur_level"),
            ##            (val_div, ":cur_level", 5),
            ##            (val_max, ":cur_level", 3),
            ##            (store_add, ":max_parties", 4, ":cur_level"),
            ##            (store_random_in_range, "$qst_capture_conspirators_num_parties_to_spawn", 4, ":max_parties"),
            ##            (assign, "$qst_capture_conspirators_num_troops_to_capture", 0),
            ##            (assign, "$qst_capture_conspirators_num_parties_spawned", 0),
            ##            (assign, "$qst_capture_conspirators_leave_meeting_counter", 0),
            ##            (assign, "$qst_capture_conspirators_party_1", 0),
            ##            (assign, "$qst_capture_conspirators_party_2", 0),
            ##            (assign, "$qst_capture_conspirators_party_3", 0),
            ##            (assign, "$qst_capture_conspirators_party_4", 0),
            ##            (assign, "$qst_capture_conspirators_party_5", 0),
            ##            (assign, "$qst_capture_conspirators_party_6", 0),
            ##            (assign, "$qst_capture_conspirators_party_7", 0),
            ##          (try_end),
            ##        (else_try),
            ##          (eq, ":quest_no", "qst_defend_nobles_against_peasants"),
            ##          (try_begin),
            ##            (eq, 1,0), #TODO: disable this for now
            ##            (ge, ":player_level", 10),
            ##            (is_between, ":giver_center_no", towns_begin, towns_end),#Skip if quest giver's center is not a town
            ##            (party_slot_eq, ":giver_center_no", slot_town_lord, ":giver_troop"),#Skip if the current center is not ruled by the quest giver
            ##
            ##            (assign, ":quest_target_center", ":giver_center_no"),
            ##            (assign, ":quest_importance", 1),
            ##            (assign, ":quest_xp_reward", 10),
            ##            (assign, ":quest_gold_reward", 10),
            ##            (assign, ":result", ":quest_no"),
            ##            (store_character_level, ":cur_level"),
            ##            (val_div, ":cur_level", 5),
            ##            (val_max, ":cur_level", 4),
            ##            (store_add, ":max_parties", 4, ":cur_level"),
            ##            (store_random_in_range, "$qst_defend_nobles_against_peasants_num_peasant_parties_to_spawn", 4, ":cur_level"),
            ##            (store_random_in_range, "$qst_defend_nobles_against_peasants_num_noble_parties_to_spawn", 4, ":cur_level"),
            ##            (assign, "$qst_defend_nobles_against_peasants_num_nobles_to_save", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_num_nobles_saved", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_peasant_party_1", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_peasant_party_2", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_peasant_party_3", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_peasant_party_4", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_peasant_party_5", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_peasant_party_6", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_peasant_party_7", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_peasant_party_8", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_noble_party_1", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_noble_party_2", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_noble_party_3", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_noble_party_4", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_noble_party_5", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_noble_party_6", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_noble_party_7", 0),
            ##            (assign, "$qst_defend_nobles_against_peasants_noble_party_8", 0),
            ##          (try_end),
          (else_try),
            (eq, ":quest_no", "qst_incriminate_loyal_commander"),
            (eq, "$player_has_homage", 0),
            
            (try_begin),
              (neq, ":giver_reputation", lrep_upstanding),
              (neq, ":giver_reputation", lrep_goodnatured),
              (eq, "$players_kingdom", ":giver_faction_no"),
              (ge, ":player_level", 10),
              (faction_slot_eq, ":giver_faction_no", slot_faction_leader, ":giver_troop"),
              (assign, ":try_times", 1),
              (assign, ":found", 0),
              (try_for_range, ":unused", 0, ":try_times"),
                (call_script, "script_cf_faction_get_random_enemy_faction", ":giver_faction_no"),#Can fail
                (assign, ":cur_target_faction", reg0),
                
                (faction_get_slot, ":cur_target_troop", ":cur_target_faction", slot_faction_leader),
                (assign, ":num_centerless_heroes", 0),
                (try_for_range, ":cur_kingdom_hero", active_npcs_begin, active_npcs_end),
                  (troop_slot_eq, ":cur_kingdom_hero", slot_troop_occupation, slto_kingdom_hero),
                  #(troop_slot_eq, ":cur_kingdom_hero", slot_troop_is_prisoner, 0),
                  (neg|troop_slot_ge, ":cur_kingdom_hero", slot_troop_prisoner_of_party, 0),
                  (neq, ":cur_target_troop", ":cur_kingdom_hero"),
                  (store_troop_faction, ":cur_kingdom_hero_faction", ":cur_kingdom_hero"),
                  (eq, ":cur_target_faction", ":cur_kingdom_hero_faction"),
                  ##                (call_script, "script_get_number_of_hero_centers", ":cur_kingdom_hero"),
                  ##                (eq, reg0, 0),
                  (val_add, ":num_centerless_heroes", 1),
                (try_end),
                (gt, ":num_centerless_heroes", 0),
                (assign, ":cur_object_troop", -1),
                (store_random_in_range, ":random_kingdom_hero", 0, ":num_centerless_heroes"),
                (try_for_range, ":cur_kingdom_hero", active_npcs_begin, active_npcs_end),
                  (eq, ":cur_object_troop", -1),
                  (troop_slot_eq, ":cur_kingdom_hero", slot_troop_occupation, slto_kingdom_hero),
                  (neq, ":cur_target_troop", ":cur_kingdom_hero"),
                  (store_troop_faction, ":cur_kingdom_hero_faction", ":cur_kingdom_hero"),
                  (eq, ":cur_target_faction", ":cur_kingdom_hero_faction"),
                  ##                (call_script, "script_get_number_of_hero_centers", ":cur_kingdom_hero"),
                  ##                (eq, reg0, 0),
                  (val_sub, ":random_kingdom_hero", 1),
                  (lt, ":random_kingdom_hero", 0),
                  (assign, ":cur_object_troop", ":cur_kingdom_hero"),
                (try_end),
                
                (assign, ":cur_target_center", -1),
                (call_script, "script_get_troop_attached_party", ":cur_target_troop"),
                (is_between, reg0, towns_begin, towns_end),
                (party_slot_eq, reg0, slot_town_lord, ":cur_target_troop"),
                (assign, ":cur_target_center", reg0),
                
                (assign, ":try_times", -1),#Exit the second loop
                (assign, ":found", 1),
              (try_end),
              (eq, ":found", 1),
              
              (assign, "$incriminate_quest_sacrificed_troop", 0),
              
              (party_get_num_companion_stacks, ":num_stacks", "p_main_party"),
              (try_for_range, ":i_stack", 1, ":num_stacks"),
                (eq ,"$incriminate_quest_sacrificed_troop", 0),
                (party_stack_get_troop_id, ":stack_troop","p_main_party",":i_stack"),
                (neg|troop_is_hero, ":stack_troop"),
                (store_character_level, ":stack_troop_level", ":stack_troop"),
                (ge, ":stack_troop_level", 25),
                (assign, "$incriminate_quest_sacrificed_troop", ":stack_troop"),
              (try_end),
              (gt, "$incriminate_quest_sacrificed_troop", 0),
              
              (assign, ":quest_target_troop", ":cur_target_troop"),
              (assign, ":quest_object_troop", ":cur_object_troop"),
              (assign, ":quest_target_center", ":cur_target_center"),
              (assign, ":quest_target_faction", ":cur_target_faction"),
              
              (assign, ":quest_importance", 25),
              (assign, ":quest_xp_reward", 700),
              (assign, ":quest_gold_reward", 1000),
              (assign, ":result", ":quest_no"),
              (assign, ":quest_expiration_days", 30),
              (assign, ":quest_dont_give_again_period", 140),
            (try_end),
          (else_try),
            (eq, ":quest_no", "qst_capture_prisoners"),
            (eq, "$player_has_homage", 0),
            
            (try_begin),
              (eq, "$players_kingdom", ":giver_faction_no"),
              (call_script, "script_cf_faction_get_random_enemy_faction", ":giver_faction_no"),#Can fail
              (assign, ":cur_target_faction", reg0),
              (store_add, ":max_tier_no", slot_faction_tier_5_troop, 1),
              (store_random_in_range, ":random_tier_no", slot_faction_tier_2_troop, ":max_tier_no"),
              (faction_get_slot, ":cur_target_troop", ":cur_target_faction", ":random_tier_no"),
              (gt, ":cur_target_troop", 0),
              (store_random_in_range, ":quest_target_amount", 3, 7),
              (assign, ":quest_target_troop", ":cur_target_troop"),
              (assign, ":quest_target_faction", ":cur_target_faction"),
              (assign, ":quest_importance", 1),
              (store_character_level, ":quest_gold_reward", ":cur_target_troop"),
              (val_add, ":quest_gold_reward", 5),
              (val_mul, ":quest_gold_reward", ":quest_gold_reward"),
              (val_div, ":quest_gold_reward", 5),
              (val_mul, ":quest_gold_reward", ":quest_target_amount"),
              (assign, ":quest_xp_reward", ":quest_gold_reward"),
              (assign, ":result", ":quest_no"),
              (assign, ":quest_expiration_days", 90),
              (assign, ":quest_dont_give_again_period", 30),
            (try_end),
          (try_end),
        (try_end),
      (try_end),
      #end of quest finding
      
      
      (try_begin),
        (neq, ":result", -1),
        
        (try_begin),
          (party_is_active, ":quest_target_center"),
          (store_faction_of_party, ":quest_target_faction", ":quest_target_center"),
        (try_end),
        
        (quest_set_slot, ":result", slot_quest_target_troop, ":quest_target_troop"),
        (quest_set_slot, ":result", slot_quest_target_center, ":quest_target_center"),
        (quest_set_slot, ":result", slot_quest_object_troop, ":quest_object_troop"),
        (quest_set_slot, ":result", slot_quest_target_faction, ":quest_target_faction"),
        (quest_set_slot, ":result", slot_quest_object_faction, ":quest_object_faction"),
        (quest_set_slot, ":result", slot_quest_object_center, ":quest_object_center"),
        (quest_set_slot, ":result", slot_quest_target_party, ":quest_target_party"),
        (quest_set_slot, ":result", slot_quest_target_party_template, ":quest_target_party_template"),
        (quest_set_slot, ":result", slot_quest_target_amount, ":quest_target_amount"),
        (quest_set_slot, ":result", slot_quest_importance, ":quest_importance"),
        (quest_set_slot, ":result", slot_quest_xp_reward, ":quest_xp_reward"),
        (quest_set_slot, ":result", slot_quest_gold_reward, ":quest_gold_reward"),
        (quest_set_slot, ":result", slot_quest_convince_value, ":quest_convince_value"),
        (quest_set_slot, ":result", slot_quest_expiration_days, ":quest_expiration_days"),
        (quest_set_slot, ":result", slot_quest_dont_give_again_period, ":quest_dont_give_again_period"),
        (quest_set_slot, ":result", slot_quest_current_state, 0),
        (quest_set_slot, ":result", slot_quest_giver_troop, ":giver_troop"),
        (quest_set_slot, ":result", slot_quest_giver_center, ":giver_center_no"),
        (quest_set_slot, ":result", slot_quest_target_dna, ":quest_target_dna"),
        (quest_set_slot, ":result", slot_quest_target_item, ":quest_target_item"),
      (try_end),
      
      (assign, reg0, ":result"),
  ]),
  
  ("get_dynamic_quest",
    #Dynamic quests are rarer, more important quests
    #this is a separate script from get_quest, so that tavern keepers can scan all NPCs for quests
    [
      (store_script_param_1, ":giver_troop"),
      
      (assign, ":result", -1),
      (assign, ":relevant_troop", -1),
      (assign, ":relevant_party", -1),
      (assign, ":relevant_faction", -1),
      (assign, ":object_party", -1),
      
      (try_begin),
        (eq, ":giver_troop", -1),
        
      (else_try),
        #1 rescue prisoner
        (neg|check_quest_active, "qst_rescue_prisoner"),
        (this_or_next|troop_slot_eq, ":giver_troop", slot_troop_occupation, slto_kingdom_hero),
        (troop_slot_eq, ":giver_troop", slot_troop_occupation, slto_kingdom_lady),
        
        (assign, ":target_troop", -1),
        (try_for_range, ":possible_prisoner", active_npcs_begin, active_npcs_end),
          (troop_get_slot, ":captor_location", ":possible_prisoner", slot_troop_prisoner_of_party),
          (is_between, ":captor_location", walled_centers_begin, walled_centers_end),
          (store_troop_faction, ":giver_troop_faction_no", ":giver_troop"),
          (store_faction_of_party, ":captor_location_faction_no", ":captor_location"),
          (store_relation, ":giver_captor_relation", ":giver_troop_faction_no", ":captor_location_faction_no"),
          (lt, ":giver_captor_relation", 0),
          
          (call_script, "script_troop_get_family_relation_to_troop", ":giver_troop", ":possible_prisoner"),
          (ge, reg0, 10),
          
          (assign, ":offered_parole", 0),
          (try_begin),
            (call_script, "script_cf_prisoner_offered_parole", ":possible_prisoner"),
            (assign, ":offered_parole", 1),
          (try_end),
          (eq, ":offered_parole", 0),
          
          (neg|party_slot_eq, ":captor_location", slot_town_lord, "trp_player"),
          (assign, ":target_troop", ":possible_prisoner"),
          (assign, ":target_party", ":captor_location"),
        (try_end),
        (gt, ":target_troop", -1),
        (assign, ":result", "qst_rescue_prisoner"),
        (assign, ":relevant_troop", ":target_troop"),
        (assign, ":relevant_party", ":target_party"),
        
      (else_try),
        #2 retaliate for border incident
        (is_between, ":giver_troop", mayors_begin, mayors_end),
        (store_faction_of_troop, ":giver_faction", ":giver_troop"),
        
        (neg|check_quest_active, "qst_retaliate_for_border_incident"),
        (quest_slot_eq, "qst_retaliate_for_border_incident", slot_quest_dont_give_again_remaining_days, 0),
        (assign, ":target_leader", 0),
        
        (try_for_range, ":kingdom", "fac_kingdom_1", kingdoms_end),
          (call_script, "script_diplomacy_faction_get_diplomatic_status_with_faction", ":giver_faction", ":kingdom"),
          (assign, ":diplomatic_status", reg0),
          (eq, ":diplomatic_status", -1),
          (assign, ":duration", reg1),
          (ge, ":duration", 10),
          
          
          (try_for_range, ":lord", active_npcs_begin, active_npcs_end),
            (store_faction_of_troop, ":lord_faction", ":lord"),
            (eq, ":lord_faction", ":kingdom"),
            
            (troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_debauched),
            
            (assign, ":target_leader", ":lord"),
            (assign, ":target_faction", ":kingdom"),
          (try_end),
        (try_end),
        (is_between, ":target_leader", active_npcs_begin, active_npcs_end),
        
        (assign, ":result", "qst_retaliate_for_border_incident"),
        (assign, ":relevant_troop", ":target_leader"),
        (assign, ":relevant_faction", ":target_faction"),
      (else_try), #Find bandit hideout
        (troop_slot_eq, ":giver_troop", slot_troop_occupation, slto_kingdom_hero),
        (neg|check_quest_active, "qst_destroy_bandit_lair"),
        (quest_slot_eq, "qst_destroy_bandit_lair", slot_quest_dont_give_again_remaining_days, 0),
        
        #		(display_message, "@Checking for bandit lair quest"),
        
        (assign, ":lair_found", -1),
        
        (try_for_parties, ":bandit_lair"),
          (gt, ":bandit_lair", "p_spawn_points_end"),
          (party_get_template_id, ":bandit_type", ":bandit_lair"),
          (is_between, ":bandit_type", "pt_steppe_bandit_lair", "pt_looter_lair"),  #exclude looter lair, as always
          (assign, ":closest_town", -1),
          (assign, ":score_to_beat", 99999),
          
          (try_for_range, ":town_no", towns_begin, towns_end),
            (store_distance_to_party_from_party, ":distance", ":bandit_lair", ":town_no"),
            (lt, ":distance", ":score_to_beat"),
            (assign, ":closest_town", ":town_no"),
            (assign, ":score_to_beat", ":distance"),
          (try_end),
          
          #(str_store_party_name, s7, ":closest_town"),
          #(party_get_slot, ":closest_town_lord", ":closest_town", slot_town_lord),
          #(str_store_troop_name, s8, ":closest_town_lord"),
          
          (party_slot_eq, ":closest_town", slot_town_lord, ":giver_troop"),
          (assign, ":lair_found", ":bandit_lair"),
        (try_end),
        
        (gt, ":lair_found", "p_spawn_points_end"),
        
        (assign ,":result", "qst_destroy_bandit_lair"),
        (assign, ":relevant_party", ":lair_found"),
      (else_try),  #3 - bounty on bandit party
        (is_between, ":giver_troop", mayors_begin, mayors_end),
        (neg|check_quest_active, "qst_track_down_bandits"),
        (quest_slot_eq, "qst_track_down_bandits", slot_quest_dont_give_again_remaining_days, 0),
        
        (assign, ":cur_town", -1),
        (try_for_range, ":town", towns_begin, towns_end),
          (party_slot_eq, ":town", slot_town_elder, ":giver_troop"),
          (assign, ":cur_town", ":town"),
        (try_end),
        (gt, ":cur_town", -1),
        
        (call_script, "script_merchant_road_info_to_s42", ":cur_town"),
        (assign, ":bandit_party_found", reg0),
        (party_is_active, ":bandit_party_found"),
        (gt, ":bandit_party_found", 0),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "str_traveller_attack_found"),
        (try_end),
        
        (assign ,":result", "qst_track_down_bandits"),
        (assign, ":relevant_party", ":bandit_party_found"),
      (else_try),  #raid a caravan to start war
        (troop_slot_eq, ":giver_troop", slot_lord_reputation_type, lrep_debauched),
        (store_faction_of_troop, ":giver_troop_faction", ":giver_troop"),
        
        (assign, ":junior_debauched_lord_in_faction", -1),
        (try_for_range, ":lord_in_faction", active_npcs_begin, active_npcs_end),
          (troop_slot_eq, ":lord_in_faction", slot_lord_reputation_type, lrep_debauched),
          (store_faction_of_troop, ":debauched_lord_faction", ":lord_in_faction"),
          (eq, ":debauched_lord_faction", ":giver_troop_faction"),
          (assign, ":junior_debauched_lord_in_faction", ":lord_in_faction"),
        (try_end),
        (eq, ":giver_troop", ":junior_debauched_lord_in_faction"),
        
        (assign, ":faction_to_attack", -1),
        (assign, ":faction_to_attack_score", -1),
        
        (try_for_range, ":faction_candidate", kingdoms_begin, kingdoms_end),
          (neq, ":faction_candidate", ":giver_troop_faction"),
          (faction_slot_eq, ":faction_candidate", slot_faction_state, sfs_active),
          (neq, ":faction_candidate", "$players_kingdom"),
          
          (store_relation, ":relation", ":faction_candidate", ":giver_troop_faction"),
          
          (store_add, ":provocation_slot", ":giver_troop_faction", slot_faction_provocation_days_with_factions_begin),
          (val_sub, ":provocation_slot", kingdoms_begin),
          (faction_get_slot, ":provocation_days", ":faction_candidate", ":provocation_slot"),
          
          (ge, ":relation", 0), #disqualifies if the faction is already at war
          (le, ":provocation_days", 0), #disqualifies if the faction has already provoked someone
          
          (store_random_in_range, ":faction_candidate_score", 0, 100),
          #add in scores - no truce?
          #				(store_add, ":truce_slot", ":giver_troop_faction", slot_faction_truce_days_with_factions_begin),
          #				(store_add, ":provocation_slot", ":giver_troop_faction", slot_faction_provocation_days_with_factions_begin),
          #				(val_sub, ":truce_slot", kingdoms_begin),
          #				(val_sub, ":provocation_slot", kingdoms_begin),
          #				(faction_slot_eq, ":faction_candidate", ":provocation_slot", 0),
          #				(try_begin),
          #					(faction_slot_ge, ":faction_candidate", ":truce_slot", 1),
          #					(val_sub, ":faction_to_attack_temp_score", 1),
          #				(try_end),
          
          (gt, ":faction_candidate_score", ":faction_to_attack_score"),
          (assign, ":faction_to_attack", ":faction_candidate"),
          (assign, ":faction_to_attack_score", ":faction_candidate_score"),
        (try_end),
        
        (is_between, ":faction_to_attack", kingdoms_begin, kingdoms_end),
        
        (assign ,":result", "qst_cause_provocation"),
        (assign, ":relevant_faction", ":faction_to_attack"),
      (else_try), #track masterless men
        (troop_slot_eq, ":giver_troop", slot_troop_occupation, slto_kingdom_hero),
        (neg|check_quest_active, "qst_blank_quest_4"),
        (quest_slot_eq, "qst_blank_quest_4", slot_quest_dont_give_again_remaining_days, 0),
        (assign, ":last_bandit_party_found", -1),
        (try_for_range_backwards, ":log_entry_iterator", 0, "$num_log_entries"),
          (store_sub, ":log_entry_no", "$num_log_entries", ":log_entry_iterator"),
          (troop_slot_eq, "trp_log_array_entry_type", ":log_entry_no",  logent_village_raided),
          (troop_get_slot, ":center_object", "trp_log_array_center_object", ":log_entry_no"),
          (party_slot_eq, ":center_object", slot_town_lord, ":giver_troop"),
          (troop_get_slot, ":event_time", "trp_log_array_entry_time", ":log_entry_no"),
          (store_current_hours, ":cur_hour"),
          (store_sub, ":hours_ago", ":cur_hour", ":event_time"),
          #			(assign, reg3, ":hours_ago"),
          #			(display_message, "@{reg3} hrs ago"),
          (lt, ":hours_ago", 672), #four weeks
          (gt, ":hours_ago", 48),
          (assign, ":object_party", ":center_object"),
          (troop_get_slot, ":actor",  "trp_log_array_actor", ":log_entry_no"),
          (ge, ":actor", 0),
          (neg|is_between, ":actor", active_npcs_begin, active_npcs_end),
          (try_begin),
            (party_is_active, ":actor"),
            (store_faction_of_party, ":faction", ":actor"),
            (eq, ":faction", "fac_deserters"),
            (assign, ":assailants_known", -1),
            (try_begin),
              (party_is_active, ":actor"),
              (assign, ":assailants_known", ":actor"),
              (gt, ":assailants_known", 0),
              (assign, ":last_bandit_party_found", ":assailants_known"),
            (try_end),
          (else_try),
            (try_for_parties, ":party_no"),
              (store_faction_of_party, ":faction", ":party_no"),
              (eq, ":faction", "fac_deserters"),
              (party_stack_get_troop_id, ":leader", ":party_no", 0),
              (eq, ":leader", ":actor"),
              (assign, ":last_bandit_party_found", ":party_no"),
            (try_end),
          (try_end),
          (gt, ":last_bandit_party_found", -1),
          (assign ,":result", "qst_blank_quest_4"),
          (assign, ":relevant_party", ":last_bandit_party_found"),
        (try_end),
      (try_end),
      (assign, reg0, ":result"),
      (assign, reg1, ":relevant_troop"),
      (assign, reg2, ":relevant_party"),
      (assign, reg3, ":relevant_faction"),
      (assign, reg4, ":object_party"),
  ]),
  
  ("get_political_quest",
    # Political quests are given by the player's political "coach" -- ie, a spouse or the minister -- to improve standing in the faction
    [
      (store_script_param, ":giver_troop", 1),
      
      (assign, ":result", -1),
      (assign, ":quest_target_troop", -1),
      (assign, ":quest_object_troop", -1),
      (assign, ":quest_dont_give_again_period", 7), #one week on average
      
      
      
      (try_begin), #this for kingdom hero, "we have a mutual enemy"
        (neg|check_quest_active, "qst_denounce_lord"),
        (try_begin),
          (ge, "$cheat_mode", 1),
          (quest_get_slot, reg4, "qst_denounce_lord", slot_quest_dont_give_again_remaining_days),
          (display_message, "@{!}DEBUG -- Checking for denounce lord, eligible in {reg4} days"),
        (try_end),
        
        (neg|quest_slot_ge, "qst_denounce_lord", slot_quest_dont_give_again_remaining_days, 1),
        (neq, ":giver_troop", "$g_player_minister"),
        (neg|troop_slot_eq, "trp_player", slot_troop_spouse, ":giver_troop"),
        (neg|faction_slot_eq, "$players_kingdom", slot_faction_leader, "trp_player"),
        
        
        #		(neg|troop_slot_eq, "$g_talk_troop", slot_lord_reputation_type, lrep_martial),
        (neg|troop_slot_eq, "$g_talk_troop", slot_lord_reputation_type, lrep_quarrelsome),
        (neg|troop_slot_eq, "$g_talk_troop", slot_lord_reputation_type, lrep_goodnatured),
        
        #		(neg|troop_slot_ge, "trp_player", slot_troop_controversy, 10),
        
        
        (assign, ":target_lord", -1),
        (assign, ":score_to_beat", 1),
        
        (try_for_range, ":potential_target", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":potential_target_faction", ":potential_target"),
          (eq, ":potential_target_faction", "$players_kingdom"),
          (neq, ":potential_target", ":giver_troop"),
          (neg|faction_slot_eq, ":potential_target_faction", slot_faction_leader, ":potential_target"),
          
          #cannot denounce if you also have an intrigue against lord active
          (this_or_next|neg|check_quest_active, "qst_intrigue_against_lord"),
          (neg|quest_slot_eq, "qst_intrigue_against_lord", slot_quest_target_troop, ":potential_target"),
          
          (call_script, "script_troop_get_relation_with_troop", ":potential_target", ":giver_troop"),
          (assign, ":relation_with_giver_troop", reg0),
          (lt, ":relation_with_giver_troop", ":score_to_beat"),
          
          (str_store_troop_name, s4, ":potential_target"),
          (try_begin),
            (ge, "$cheat_mode", 1),
            (display_message, "@{!}DEBUG -- Rival found in {s4}"),
          (try_end),
          
          (try_begin),
            (troop_slot_eq, "$g_talk_troop", slot_lord_reputation_type, lrep_debauched),
            (assign, ":max_rel_w_player", 60),
          (else_try),
            (troop_slot_eq, "$g_talk_troop", slot_lord_reputation_type, lrep_quarrelsome),
            (assign, ":max_rel_w_player", 50),
          (else_try),
            (assign, ":max_rel_w_player", 45),
          (try_end),
          
          (call_script, "script_troop_get_relation_with_troop", ":potential_target", "trp_player"),
          (assign, ":relation_with_player", reg0),
          (lt, ":relation_with_player", ":max_rel_w_player"),
          
          (str_store_troop_name, s4, ":potential_target"),
          (try_begin),
            (ge, "$cheat_mode", 1),
            (display_message, "@{!}DEBUG -- {s4} is not close friend of player"),
          (try_end),
          
          (assign, ":enemies_in_faction", 0),
          (try_for_range, ":other_lord", active_npcs_begin, active_npcs_end),
            (store_faction_of_troop, ":other_lord_faction", ":other_lord"),
            (eq, ":other_lord_faction", "$players_kingdom"),
            (call_script, "script_troop_get_relation_with_troop", ":potential_target", ":other_lord"),
            (lt, reg0, 0),
            (val_add, ":enemies_in_faction", 1),
          (try_end),
          
          (str_store_troop_name, s4, ":potential_target"),
          (try_begin),
            (ge, "$cheat_mode", 1),
            (assign, reg3, ":enemies_in_faction"),
            (display_message, "@{!}DEBUG -- {s4} has {reg3} rivals"),
          (try_end),
          
          (this_or_next|ge, ":enemies_in_faction", 3),
          (ge, "$cheat_mode", 1),
          
          (assign, ":score_to_beat", ":relation_with_giver_troop"),
          (assign, ":target_lord", ":potential_target"),
        (try_end),
        
        (is_between, ":target_lord", active_npcs_begin, active_npcs_end),
        
        
        (assign, ":result", "qst_denounce_lord"),
        (assign, ":quest_target_troop", ":target_lord"),
        
      (else_try),
        (neg|check_quest_active, "qst_intrigue_against_lord"),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (quest_get_slot, reg4, "qst_intrigue_against_lord", slot_quest_dont_give_again_remaining_days),
          (display_message, "@{!}DEBUG -- Checking for intrigue, eligible in {reg4} days"),
        (try_end),
        
        (neg|quest_slot_ge, "qst_intrigue_against_lord", slot_quest_dont_give_again_remaining_days, 1),
        
        
        
        (neq, ":giver_troop", "$g_player_minister"),
        (neg|troop_slot_eq, "trp_player", slot_troop_spouse, ":giver_troop"),
        (neg|faction_slot_eq, "$players_kingdom", slot_faction_leader, "trp_player"),
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (display_message, "@{!}DEBUG -- Trying for intrigue against lord"),
        (try_end),
        
        
        (assign, ":target_lord", -1),
        (assign, ":score_to_beat", 10),
        
        (try_for_range, ":potential_target", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":potential_target_faction", ":potential_target"),
          (eq, ":potential_target_faction", "$players_kingdom"),
          (neq, ":potential_target", ":giver_troop"),
          (neg|faction_slot_eq, ":potential_target_faction", slot_faction_leader, ":potential_target"),
          
          
          (this_or_next|neg|check_quest_active, "qst_denounce_lord"),
          (neg|quest_slot_eq, "qst_denounce_lord", slot_quest_target_troop, ":potential_target"),
          
          (faction_get_slot, ":faction_liege", "$players_kingdom", slot_faction_leader),
          (call_script, "script_troop_get_relation_with_troop", ":potential_target", ":faction_liege"),
          (assign, ":relation_with_liege", reg0),
          (lt, ":relation_with_liege", ":score_to_beat"),
          
          (str_store_troop_name, s4, ":potential_target"),
          (try_begin),
            (ge, "$cheat_mode", 1),
            (display_message, "@{!}DEBUG -- {s4} has sufficiently low relation with liege"),
          (try_end),
          
          
          (call_script, "script_troop_get_relation_with_troop", ":potential_target", ":giver_troop"),
          (assign, ":relation_with_giver_troop", reg0),
          (lt, ":relation_with_giver_troop", 0),
          
          (str_store_troop_name, s4, ":potential_target"),
          (try_begin),
            (ge, "$cheat_mode", 1),
            (display_message, "@{!}DEBUG -- {s4} has sufficiently low relation with giver troop"),
          (try_end),
          
          
          (call_script, "script_troop_get_relation_with_troop", ":potential_target", "trp_player"),
          (assign, ":relation_with_player", reg0),
          (lt, ":relation_with_player", 0),
          
          (str_store_troop_name, s4, ":potential_target"),
          (try_begin),
            (ge, "$cheat_mode", 1),
            (display_message, "@{!}DEBUG -- {s4} has sufficiently low relation with player"),
          (try_end),
          
          (assign, ":score_to_beat", ":relation_with_liege"),
          (assign, ":target_lord", ":potential_target"),
        (try_end),
        
        (is_between, ":target_lord", active_npcs_begin, active_npcs_end),
        
        
        (assign, ":result", "qst_intrigue_against_lord"),
        (assign, ":quest_target_troop", ":target_lord"),
        
        
      (else_try),
        #Resolve dispute, if there is a good chance of achieving the result
        (try_begin),
          (ge, "$cheat_mode", 1),
          (quest_get_slot, reg4, "qst_resolve_dispute", slot_quest_dont_give_again_remaining_days),
          (display_message, "@{!}DEBUG -- Checking for resolve dispute, eligible in {reg4} days"),
        (try_end),
        
        (neg|quest_slot_ge, "qst_resolve_dispute", slot_quest_dont_give_again_remaining_days, 1),
        
        
        
        (this_or_next|troop_slot_eq, "trp_player", slot_troop_spouse, "$g_talk_troop"),
        (eq, "$g_talk_troop", "$g_player_minister"),
        
        (assign, ":target_lord", -1),
        (assign, ":object_lord", -1),
        (assign, ":best_chance_of_success", 20),
        
        (try_for_range, ":lord_1", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":lord_1_faction", ":lord_1"),
          (eq, ":lord_1_faction", "$players_kingdom"),
          (neq, ":lord_1", "$g_talk_troop"),
          
          (try_for_range, ":lord_2", active_npcs_begin, active_npcs_end),
            (store_faction_of_troop, ":lord_2_faction", ":lord_2"),
            (eq, ":lord_2_faction", "$players_kingdom"),
            
            (neq, ":lord_1", ":lord_2"),
            (neq, ":lord_2", "$g_talk_troop"),
            
            (call_script, "script_troop_get_relation_with_troop", ":lord_1", ":lord_2"),
            (assign, ":lord_1_relation_with_lord_2", reg0),
            (lt, ":lord_1_relation_with_lord_2", -5),
            
            (call_script, "script_troop_get_relation_with_troop", ":lord_1", "trp_player"),
            (assign, ":relation_with_lord_1", reg0),
            
            (call_script, "script_troop_get_relation_with_troop", ":lord_2", "trp_player"),
            (assign, ":relation_with_lord_2", reg0),
            
            (gt, ":relation_with_lord_1", 0),
            (gt, ":relation_with_lord_2", 0),
            
            (store_mul, ":chance_of_success", ":relation_with_lord_1", ":relation_with_lord_2"),
            
            
            (gt, ":chance_of_success", ":best_chance_of_success"),
            (assign, ":best_chance_of_success", ":chance_of_success"),
            (assign, ":target_lord", ":lord_1"),
            (assign, ":object_lord", ":lord_2"),
          (try_end),
        (try_end),
        
        
        (is_between, ":target_lord", active_npcs_begin, active_npcs_end),
        
        (assign, ":result", "qst_resolve_dispute"),
        (assign, ":quest_target_troop", ":target_lord"),
        (assign, ":quest_object_troop", ":object_lord"),
        
      (else_try),
        (try_begin),
          (ge, "$cheat_mode", 1),
          (quest_get_slot, reg4, "qst_offer_gift", slot_quest_dont_give_again_remaining_days),
          (display_message, "@{!}DEBUG -- Checking for offer gift, eligible in {reg4} days"),
        (try_end),
        
        (neg|quest_slot_ge, "qst_offer_gift", slot_quest_dont_give_again_remaining_days, 1),
        
        (assign, ":relative_found", -1),
        (assign, ":score_to_beat", 5),
        
        (try_for_range, ":potential_relative", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":relative_faction", ":potential_relative"),
          (eq, ":relative_faction", "$players_kingdom"),
          (neq, ":potential_relative", ":giver_troop"),
          (neg|faction_slot_eq, ":relative_faction", slot_faction_leader, ":potential_relative"),
          
          (call_script, "script_troop_get_family_relation_to_troop", ":giver_troop", ":potential_relative"),
          (assign, ":family_relation", reg0),
          (ge, ":family_relation", ":score_to_beat"),
          
          (store_sub, ":min_relation_w_player", 0, ":family_relation"),
          
          (call_script, "script_troop_get_relation_with_troop", "trp_player", ":potential_relative"),
          (assign, ":relation_with_player", reg0),
          (is_between, ":relation_with_player", ":min_relation_w_player", 0),
          
          (assign, ":score_to_beat", ":family_relation"),
          (assign, ":relative_found", ":potential_relative"),
          
        (try_end),
        
        (is_between, ":relative_found", active_npcs_begin, active_npcs_end),
        
        (assign, ":result", "qst_offer_gift"),
        (assign, ":quest_target_troop", ":relative_found"),
      (try_end),
      
      
      (try_begin),
        (gt, ":result", -1),
        (quest_set_slot, ":result", slot_quest_target_troop, ":quest_target_troop"),
        (quest_set_slot, ":result", slot_quest_object_troop, ":quest_object_troop"), ##CABA - bugfix was slot_quest_target_troop chief
        
        (quest_set_slot, ":result", slot_quest_giver_troop, ":giver_troop"),
        (quest_set_slot, ":result", slot_quest_dont_give_again_period, ":quest_dont_give_again_period"),
      (try_end),
      
      (assign, reg0, ":result"),
      (assign, reg1, ":quest_target_troop"),
      (assign, reg2, ":quest_object_troop"),
  ]),
  
  ("npc_find_quest_for_player_to_s11",
    [
      (store_script_param, ":faction", 1),
      
      (assign, ":quest_giver_found", -1),
      (try_for_range, ":quest_giver", active_npcs_begin, mayors_end),
        (eq, ":quest_giver_found", -1),
        
        (neg|troop_slot_eq, "trp_player", slot_troop_spouse, ":quest_giver"),
        
        (gt, ":quest_giver", "$g_troop_list_no"),
        
        (assign, "$g_troop_list_no", ":quest_giver"),
        
        (this_or_next|troop_slot_eq, ":quest_giver", slot_troop_occupation, slto_kingdom_hero),
        (is_between, ":quest_giver", mayors_begin, mayors_end),
        
        (neg|troop_slot_ge, ":quest_giver", slot_troop_prisoner_of_party, centers_begin),
        
        (try_begin),
          (is_between, ":quest_giver", mayors_begin, mayors_end),
          (assign, ":quest_giver_faction", -1),
          (try_for_range,":town", towns_begin, towns_end),
            (party_slot_eq, ":town", slot_town_elder, ":quest_giver"),
            (store_faction_of_party, ":quest_giver_faction", ":town"),
          (try_end),
        (else_try),
          (store_faction_of_troop, ":quest_giver_faction", ":quest_giver"),
        (try_end),
        (eq, ":faction", ":quest_giver_faction"),
        
        (call_script, "script_get_dynamic_quest", ":quest_giver"),
        (gt, reg0, -1),
        
        (assign, ":quest_giver_found", ":quest_giver"),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (str_store_troop_name, s4, ":quest_giver_found"),
          (display_message, "str_test_diagnostic_quest_found_for_s4"),
        (try_end),
        
      (try_end),
      
      (assign, reg0, ":quest_giver_found"),
      
  ]),
  
  # script_cf_get_random_enemy_center_within_range
  # Input: arg1 = party_no, arg2 = range (in kms)
  # Output: reg0 = center_no
  ("cf_get_random_enemy_center_within_range",
    [
      (store_script_param, ":party_no", 1),
      (store_script_param, ":range", 2),
      
      (assign, ":num_centers", 0),
      (store_faction_of_party, ":faction_no", ":party_no"),
      (try_for_range, ":cur_center", centers_begin, centers_end),
        (store_faction_of_party, ":cur_faction", ":cur_center"),
        (store_relation, ":cur_relation", ":faction_no", ":cur_faction"),
        (lt, ":cur_relation", 0),
        (store_distance_to_party_from_party, ":dist", ":party_no", ":cur_center"),
        (le, ":dist", ":range"),
        (val_add, ":num_centers", 1),
      (try_end),
      (gt, ":num_centers", 0),
      (store_random_in_range, ":random_center", 0, ":num_centers"),
      (assign, ":end_cond", centers_end),
      (try_for_range, ":cur_center", centers_begin, ":end_cond"),
        (store_faction_of_party, ":cur_faction", ":cur_center"),
        (store_relation, ":cur_relation", ":faction_no", ":cur_faction"),
        (lt, ":cur_relation", 0),
        (store_distance_to_party_from_party, ":dist", ":party_no", ":cur_center"),
        (le, ":dist", ":range"),
        (val_sub, ":random_center", 1),
        (lt, ":random_center", 0),
        (assign, ":result", ":cur_center"),
        (assign, ":end_cond", 0),#break
      (try_end),
      (assign, reg0, ":result"),
  ]),
  
  # script_cf_faction_get_random_enemy_faction
  # Input: arg1 = faction_no
  # Output: reg0 = faction_no (Can fail)
  ("cf_faction_get_random_enemy_faction",
    [
      (store_script_param_1, ":faction_no"),
      
      (assign, ":result", -1),
      (assign, ":count_factions", 0),
      (try_for_range, ":cur_faction", kingdoms_begin, kingdoms_end),
        (faction_slot_eq, ":cur_faction", slot_faction_state, sfs_active),
        (store_relation, ":cur_relation", ":faction_no", ":cur_faction"),
        (le, ":cur_relation", -1),
        (val_add, ":count_factions", 1),
      (try_end),
      (store_random_in_range,":random_faction",0,":count_factions"),
      (assign, ":count_factions", 0),
      (try_for_range, ":cur_faction", kingdoms_begin, kingdoms_end),
        (eq, ":result", -1),
        (faction_slot_eq, ":cur_faction", slot_faction_state, sfs_active),
        (store_relation, ":cur_relation", ":faction_no", ":cur_faction"),
        (le, ":cur_relation", -1),
        (val_add, ":count_factions", 1),
        (gt, ":count_factions", ":random_faction"),
        (assign, ":result", ":cur_faction"),
      (try_end),
      
      (neq, ":result", -1),
      (assign, reg0, ":result"),
  ]),
  
  # script_cf_faction_get_random_friendly_faction
  # Input: arg1 = faction_no
  # Output: reg0 = faction_no (Can fail)
  # ("cf_faction_get_random_friendly_faction",
    # [
      # (store_script_param_1, ":faction_no"),
      
      # (assign, ":result", -1),
      # (assign, ":count_factions", 0),
      # (try_for_range, ":cur_faction", kingdoms_begin, kingdoms_end),
        # (faction_slot_eq, ":cur_faction", slot_faction_state, sfs_active),
        # (neq, ":cur_faction", ":faction_no"),
        # (store_relation, ":cur_relation", ":faction_no", ":cur_faction"),
        # (ge, ":cur_relation", 0),
        # (val_add, ":count_factions", 1),
      # (try_end),
      # (store_random_in_range,":random_faction",0,":count_factions"),
      # (assign, ":count_factions", 0),
      # (try_for_range, ":cur_faction", kingdoms_begin, kingdoms_end),
        # (eq, ":result", -1),
        # (faction_slot_eq, ":cur_faction", slot_faction_state, sfs_active),
        # (neq, ":cur_faction", ":faction_no"),
        # (store_relation, ":cur_relation", ":faction_no", ":cur_faction"),
        # (ge, ":cur_relation", 0),
        # (val_add, ":count_factions", 1),
        # (gt, ":count_factions", ":random_faction"),
        # (assign, ":result", ":cur_faction"),
      # (try_end),
      
      # (neq, ":result", -1),
      # (assign, reg0, ":result"),
  # ]),
  
  # script_cf_troop_get_random_enemy_troop_with_occupation
  # Input: arg1 = troop_no,
  # Output: reg0 = enemy_troop_no (Can fail)
  ("cf_troop_get_random_enemy_troop_with_occupation",
    [
      (store_script_param_1, ":troop_no"),
      (store_script_param_2, ":occupation"),
      
      (assign, ":result", -1),
      (assign, ":count_enemies", 0),
      (try_for_range, ":enemy_troop_no", active_npcs_begin, active_npcs_end),
        (troop_slot_eq, ":enemy_troop_no", slot_troop_occupation, ":occupation"),
        (call_script, "script_troop_get_relation_with_troop", ":troop_no", ":enemy_troop_no"),
        (lt, reg0, -10),
        (val_add, ":count_enemies", 1),
      (try_end),
      
      (gt, ":count_enemies", 0),
      (store_random_in_range,":random_enemy",0,":count_enemies"),
      
      (assign, ":count_enemies", 0),
      (try_for_range, ":enemy_troop_no", active_npcs_begin, active_npcs_end),
        (troop_slot_eq, ":enemy_troop_no", slot_troop_occupation, ":occupation"),
        (call_script, "script_troop_get_relation_with_troop", ":troop_no", ":enemy_troop_no"),
        (lt, reg0, -10),
        (val_add, ":count_enemies", 1),
        (eq, ":random_enemy", ":count_enemies"),
        (assign, ":result", ":enemy_troop_no"),
      (try_end),
      
      (neq, ":result", -1),
      (assign, reg0, ":result"),
  ]),
  
  
  ##  # script_cf_troop_get_random_enemy_troop_as_a_town_lord
  ##  # Input: arg1 = troop_no
  ##  # Output: reg0 = enemy_troop_no (Can fail)
  ##  ("cf_troop_get_random_enemy_troop_as_a_town_lord",
  ##    [
  ##      (store_script_param_1, ":troop_no"),
  ##
  ##      (assign, ":result", -1),
  ##      (assign, ":count_enemies", 0),
  ##      (try_for_range, ":cur_slot", slot_troop_enemies_begin, slot_troop_enemies_end),
  ##        (troop_get_slot, ":cur_enemy", ":troop_no", ":cur_slot"),
  ##        (gt, ":cur_enemy", 0),
  ##        (troop_slot_eq, ":cur_enemy", slot_troop_occupation, slto_kingdom_hero),
  ##        (call_script, "script_get_number_of_hero_centers", ":cur_enemy"),
  ##        (gt, reg0, 0),
  ##        (val_add, ":count_enemies", 1),
  ##      (try_end),
  ##      (store_random_in_range,":random_enemy",0,":count_enemies"),
  ##      (assign, ":count_enemies", 0),
  ##      (try_for_range, ":cur_slot", slot_troop_enemies_begin, slot_troop_enemies_end),
  ##        (eq, ":result", -1),
  ##        (troop_get_slot, ":cur_enemy", ":troop_no", ":cur_slot"),
  ##        (gt, ":cur_enemy", 0),
  ##        (troop_slot_eq, ":cur_enemy", slot_troop_occupation, slto_kingdom_hero),
  ##        (call_script, "script_get_number_of_hero_centers", ":cur_enemy"),
  ##        (gt, reg0, 0),
  ##        (val_add, ":count_enemies", 1),
  ##        (gt, ":count_enemies", ":random_enemy"),
  ##        (assign, ":result", ":cur_enemy"),
  ##      (try_end),
  ##      (neq, ":result", -1),
  ##      (assign, reg0, ":result"),
  ##  ]),
  
  
  ##  # script_cf_get_random_enemy_with_valid_slot
  ##  # Input: arg1 = faction_no, arg2 = slot_no
  ##  # Output: reg0 = faction_no (Can fail)
  ##  ("cf_get_random_enemy_with_valid_slot",
  ##    [
  ##      (store_script_param_1, ":faction_no"),
  ##      (store_script_param_2, ":slot_no"),
  ##
  ##      (assign, ":result", -1),
  ##      (assign, ":count_factions", 0),
  ##      (try_for_range, ":cur_faction", kingdoms_begin, kingdoms_end),
  ##        (store_relation, ":cur_relation", ":faction_no", ":cur_faction"),
  ##        (le, ":cur_relation", -10),
  ##        (faction_get_slot, ":cur_value", ":cur_faction", ":slot_no"),
  ##        (gt, ":cur_value", 0),#Checking validity
  ##        (val_add, ":count_factions", 1),
  ##      (try_end),
  ##      (store_random_in_range,":random_faction",0,":count_factions"),
  ##      (assign, ":count_factions", 0),
  ##      (try_for_range, ":cur_faction", kingdoms_begin, kingdoms_end),
  ##        (eq, ":result", -1),
  ##        (store_relation, ":cur_relation", ":faction_no", ":cur_faction"),
  ##        (le, ":cur_relation", -10),
  ##        (faction_get_slot, ":cur_value", ":cur_faction", ":slot_no"),
  ##        (gt, ":cur_value", 0),#Checking validity
  ##        (val_add, ":count_factions", 1),
  ##        (gt, ":count_factions", ":random_faction"),
  ##        (assign, ":result", ":cur_faction"),
  ##      (try_end),
  ##
  ##      (neq, ":result", -1),
  ##      (assign, reg0, ":result"),
  ##  ]),
  
  
  ##  # script_cf_get_random_kingdom_hero
  ##  # Input: arg1 = faction_no
  ##  # Output: reg0 = troop_no (Can fail)
  ##  ("cf_get_random_kingdom_hero",
  ##    [
  ##      (store_script_param_1, ":faction_no"),
  ##      (assign, ":count_heroes", 0),
  ##      (try_for_range, ":center_no", centers_begin, centers_end),
  ##        (store_faction_of_party, ":cur_faction", ":center_no"),
  ##        (eq, ":cur_faction", ":faction_no"),
  ##        (party_get_slot, ":cur_lord", ":center_no", slot_town_lord),
  ##        (is_between, ":cur_lord", heroes_begin, heroes_end),
  ##        (val_add, ":count_heroes", 1),
  ##      (try_end),
  ##      (store_random_in_range, ":random_hero", 0, ":count_heroes"),
  ##      (assign, ":result", -1),
  ##      (assign, ":count_heroes", 0),
  ##      (try_for_range, ":center_no", centers_begin, centers_end),
  ##        (eq, ":result", -1),
  ##        (store_faction_of_party, ":cur_faction", ":center_no"),
  ##        (eq, ":cur_faction", ":faction_no"),
  ##        (party_get_slot, ":cur_lord", ":center_no", slot_town_lord),
  ##        (is_between, ":cur_lord", heroes_begin, heroes_end),
  ##        (val_add, ":count_heroes", 1),
  ##        (lt, ":random_hero", ":count_heroes"),
  ##        (assign, ":result", ":cur_lord"),
  ##      (try_end),
  ##      (neq, ":result", -1),
  ##      (assign, reg0, ":result"),
  ##  ]),
  
  
  # script_cf_get_random_kingdom_hero_as_lover - removed
  
  
  
  ##  # script_cf_get_random_siege_location_with_faction
  ##  # Input: arg1 = faction_no
  ##  # Output: reg0 = center_no, Can Fail!
  ##  ("cf_get_random_siege_location_with_faction",
  ##    [
  ##      (store_script_param_1, ":faction_no"),
  ##      (assign, ":result", -1),
  ##      (assign, ":count_sieges", 0),
  ##      (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
  ##        (party_get_battle_opponent, ":besieger_party", ":center_no"),
  ##        (gt, ":besieger_party", 0),
  ##        (store_faction_of_party, ":cur_faction_no", ":center_no"),
  ##        (eq, ":cur_faction_no", ":faction_no"),
  ##        (val_add, ":count_sieges", 1),
  ##      (try_end),
  ##      (store_random_in_range,":random_center",0,":count_sieges"),
  ##      (assign, ":count_sieges", 0),
  ##      (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
  ##        (eq, ":result", -1),
  ##        (party_get_battle_opponent, ":besieger_party", ":center_no"),
  ##        (gt, ":besieger_party", 0),
  ##        (store_faction_of_party, ":cur_faction_no", ":center_no"),
  ##        (eq, ":cur_faction_no", ":faction_no"),
  ##        (val_add, ":count_sieges", 1),
  ##        (gt, ":count_sieges", ":random_center"),
  ##        (assign, ":result", ":center_no"),
  ##      (try_end),
  ##      (neq, ":result", -1),
  ##      (assign, reg0, ":result"),
  ##  ]),
  
  ##  # script_cf_get_random_siege_location_with_attacker_faction
  ##  # Input: arg1 = faction_no
  ##  # Output: reg0 = center_no, Can Fail!
  ##  ("cf_get_random_siege_location_with_attacker_faction",
  ##    [
  ##      (store_script_param_1, ":faction_no"),
  ##      (assign, ":result", -1),
  ##      (assign, ":count_sieges", 0),
  ##      (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
  ##        (party_get_battle_opponent, ":besieger_party", ":center_no"),
  ##        (gt, ":besieger_party", 0),
  ##        (store_faction_of_party, ":cur_faction_no", ":besieger_party"),
  ##        (eq, ":cur_faction_no", ":faction_no"),
  ##        (val_add, ":count_sieges", 1),
  ##      (try_end),
  ##      (store_random_in_range,":random_center",0,":count_sieges"),
  ##      (assign, ":count_sieges", 0),
  ##      (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
  ##        (eq, ":result", -1),
  ##        (party_get_battle_opponent, ":besieger_party", ":center_no"),
  ##        (gt, ":besieger_party", 0),
  ##        (store_faction_of_party, ":cur_faction_no", ":besieger_party"),
  ##        (eq, ":cur_faction_no", ":faction_no"),
  ##        (val_add, ":count_sieges", 1),
  ##        (gt, ":count_sieges", ":random_center"),
  ##        (assign, ":result", ":center_no"),
  ##      (try_end),
  ##      (neq, ":result", -1),
  ##      (assign, reg0, ":result"),
  ##  ]),
  
  
  
  ##  # script_cf_get_number_of_random_troops_from_party
  ##  # Input: arg1 = party_no, arg2 = number of troops to remove
  ##  # Output: reg0 = troop_no, Can fail if there are no slots having the required number of units!
  ##  ("cf_get_number_of_random_troops_from_party",
  ##    [
  ##      (store_script_param_1, ":party_no"),
  ##      (store_script_param_2, ":no_to_remove"),
  ##
  ##      (assign, ":result", -1),
  ##      (assign, ":count_stacks", 0),
  ##
  ##      (party_get_num_companion_stacks, ":num_stacks",":party_no"),
  ##      (try_for_range, ":i_stack", 0, ":num_stacks"),
  ##        (party_stack_get_size, ":stack_size",":party_no",":i_stack"),
  ##        (party_stack_get_num_wounded, ":num_wounded",":party_no",":i_stack"),
  ##        (val_sub, ":stack_size", ":num_wounded"),
  ##        (ge, ":stack_size", ":no_to_remove"),
  ##        (party_stack_get_troop_id, ":stack_troop",":party_no",":i_stack"),
  ##        (neg|troop_is_hero, ":stack_troop"),
  ##        (val_add, ":count_stacks", 1),
  ##      (try_end),
  ##      (store_random_in_range,":random_stack",0,":count_stacks"),
  ##      (assign, ":count_stacks", 0),
  ##      (try_for_range, ":i_stack", 0, ":num_stacks"),
  ##        (eq, ":result", -1),
  ##        (party_stack_get_size, ":stack_size",":party_no",":i_stack"),
  ##        (party_stack_get_num_wounded, ":num_wounded",":party_no",":i_stack"),
  ##        (val_sub, ":stack_size", ":num_wounded"),
  ##        (ge, ":stack_size", ":no_to_remove"),
  ##        (party_stack_get_troop_id, ":stack_troop",":party_no",":i_stack"),
  ##        (neg|troop_is_hero, ":stack_troop"),
  ##        (val_add, ":count_stacks", 1),
  ##        (gt, ":count_stacks", ":random_stack"),
  ##        (assign, ":result", ":stack_troop"),
  ##      (try_end),
  ##
  ##      (neq, ":result", -1),
  ##      (assign, reg0, ":result"),
  ##  ]),
  
  
  
  
  # script_cf_get_random_lord_in_a_center_with_faction
  # Input: arg1 = faction_no
  # Output: reg0 = troop_no, Can Fail!
  ("cf_get_random_lord_in_a_center_with_faction",
    [
      (store_script_param_1, ":faction_no"),
      (assign, ":result", -1),
      (assign, ":count_lords", 0),
      (try_for_range, ":lord_no", heroes_begin, heroes_end),
        (store_troop_faction, ":lord_faction_no", ":lord_no"),
        (eq, ":faction_no", ":lord_faction_no"),
        (troop_slot_eq, ":lord_no", slot_troop_occupation, slto_kingdom_hero),
        #(troop_slot_eq, ":lord_no", slot_troop_is_prisoner, 0),
        (neg|troop_slot_ge, ":lord_no", slot_troop_prisoner_of_party, 0),
        (troop_get_slot, ":lord_party", ":lord_no", slot_troop_leaded_party),
        (ge, ":lord_party", 0),
        (party_get_attached_to, ":lord_attachment", ":lord_party"),
        (is_between, ":lord_attachment", centers_begin, centers_end), #is troop in a center?
        (val_add, ":count_lords", 1),
      (try_end),
      (store_random_in_range, ":random_lord", 0, ":count_lords"),
      (assign, ":count_lords", 0),
      (try_for_range, ":lord_no", heroes_begin, heroes_end),
        (eq, ":result", -1),
        (store_troop_faction, ":lord_faction_no", ":lord_no"),
        (eq, ":faction_no", ":lord_faction_no"),
        (troop_slot_eq, ":lord_no", slot_troop_occupation, slto_kingdom_hero),
        #(troop_slot_eq, ":lord_no", slot_troop_is_prisoner, 0),
        (neg|troop_slot_ge, ":lord_no", slot_troop_prisoner_of_party, 0),
        (troop_get_slot, ":lord_party", ":lord_no", slot_troop_leaded_party),
        (ge, ":lord_party", 0),
        (party_get_attached_to, ":lord_attachment", ":lord_party"),
        (is_between, ":lord_attachment", centers_begin, centers_end), #is troop in a center?
        (val_add, ":count_lords", 1),
        (lt, ":random_lord", ":count_lords"),
        (assign, ":result", ":lord_no"),
      (try_end),
      (neq, ":result", -1),
      (assign, reg0, ":result"),
  ]),
  
  # script_cf_get_random_lord_except_king_with_faction
  # Input: arg1 = faction_no
  # Output: reg0 = troop_no, Can Fail!
  ("cf_get_random_lord_except_king_with_faction",
    [
      (store_script_param_1, ":faction_no"),
      (assign, ":result", -1),
      (assign, ":count_lords", 0),
      (try_for_range, ":lord_no", heroes_begin, heroes_end),
        (store_troop_faction, ":lord_faction_no", ":lord_no"),
        (eq, ":faction_no", ":lord_faction_no"),
        (neg|faction_slot_eq, ":faction_no", slot_faction_leader, ":lord_no"),
        (troop_slot_eq, ":lord_no", slot_troop_occupation, slto_kingdom_hero),
        #(troop_slot_eq, ":lord_no", slot_troop_is_prisoner, 0),
        (neg|troop_slot_ge, ":lord_no", slot_troop_prisoner_of_party, 0),
        (troop_get_slot, ":lord_party", ":lord_no", slot_troop_leaded_party),
        (ge, ":lord_party", 0),
        (val_add, ":count_lords", 1),
      (try_end),
      (store_random_in_range, ":random_lord", 0, ":count_lords"),
      (assign, ":count_lords", 0),
      (try_for_range, ":lord_no", heroes_begin, heroes_end),
        (eq, ":result", -1),
        (store_troop_faction, ":lord_faction_no", ":lord_no"),
        (eq, ":faction_no", ":lord_faction_no"),
        (neg|faction_slot_eq, ":faction_no", slot_faction_leader, ":lord_no"),
        (troop_slot_eq, ":lord_no", slot_troop_occupation, slto_kingdom_hero),
        #(troop_slot_eq, ":lord_no", slot_troop_is_prisoner, 0),
        (neg|troop_slot_ge, ":lord_no", slot_troop_prisoner_of_party, 0),
        (troop_get_slot, ":lord_party", ":lord_no", slot_troop_leaded_party),
        (ge, ":lord_party", 0),
        (val_add, ":count_lords", 1),
        (lt, ":random_lord", ":count_lords"),
        (assign, ":result", ":lord_no"),
      (try_end),
      (neq, ":result", -1),
      (assign, reg0, ":result"),
  ]),
  
  
  # script_cf_get_random_lord_from_another_faction_in_a_center
  # Input: arg1 = faction_no
  # Output: reg0 = troop_no, Can Fail!
  ("cf_get_random_lord_from_another_faction_in_a_center",
    [
      (store_script_param_1, ":faction_no"),
      (assign, ":result", -1),
      (assign, ":count_lords", 0),
      (try_for_range, ":lord_no", heroes_begin, heroes_end),
        (store_troop_faction, ":lord_faction_no", ":lord_no"),
        (neq, ":lord_faction_no", ":faction_no"),
        (store_relation, ":our_relation", ":lord_faction_no", "fac_player_faction"),
        (store_relation, ":lord_relation", ":lord_faction_no", ":faction_no"),
        (lt, ":lord_relation", 0),
        (ge, ":our_relation", 0),
        (troop_slot_eq, ":lord_no", slot_troop_occupation, slto_kingdom_hero),
        #(troop_slot_eq, ":lord_no", slot_troop_is_prisoner, 0),
        (neg|troop_slot_ge, ":lord_no", slot_troop_prisoner_of_party, 0),
        (troop_get_slot, ":lord_party", ":lord_no", slot_troop_leaded_party),
        (ge, ":lord_party", 0),
        (party_get_attached_to, ":lord_attachment", ":lord_party"),
        (is_between, ":lord_attachment", centers_begin, centers_end), #is troop in a center?
        (val_add, ":count_lords", 1),
      (try_end),
      (store_random_in_range, ":random_lord", 0, ":count_lords"),
      (assign, ":count_lords", 0),
      (try_for_range, ":lord_no", heroes_begin, heroes_end),
        (eq, ":result", -1),
        (store_troop_faction, ":lord_faction_no", ":lord_no"),
        (neq, ":lord_faction_no", ":faction_no"),
        (store_relation, ":our_relation", ":lord_faction_no", "fac_player_faction"),
        (store_relation, ":lord_relation", ":lord_faction_no", ":faction_no"),
        (lt, ":lord_relation", 0),
        (ge, ":our_relation", 0),
        (troop_slot_eq, ":lord_no", slot_troop_occupation, slto_kingdom_hero),
        #(troop_slot_eq, ":lord_no", slot_troop_is_prisoner, 0),
        (neg|troop_slot_ge, ":lord_no", slot_troop_prisoner_of_party, 0),
        (troop_get_slot, ":lord_party", ":lord_no", slot_troop_leaded_party),
        (ge, ":lord_party", 0),
        (party_get_attached_to, ":lord_attachment", ":lord_party"),
        (is_between, ":lord_attachment", centers_begin, centers_end), #is troop in a center?
        (val_add, ":count_lords", 1),
        (lt, ":random_lord", ":count_lords"),
        (assign, ":result", ":lord_no"),
      (try_end),
      (neq, ":result", -1),
      (assign, reg0, ":result"),
  ]),
  
  # script_get_closest_walled_center
  # Input: arg1 = party_no
  # Output: reg0 = center_no (closest)
  ("get_closest_walled_center",
    [
      (store_script_param_1, ":party_no"),
      (assign, ":min_distance", 9999999),
      (assign, reg0, -1),
      (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
        (store_distance_to_party_from_party, ":party_distance", ":party_no", ":center_no"),
        (lt, ":party_distance", ":min_distance"),
        (assign, ":min_distance", ":party_distance"),
        (assign, reg0, ":center_no"),
      (try_end),
  ]),
  
  # script_get_closest_center
  # Input: arg1 = party_no
  # Output: reg0 = center_no (closest)
  ("get_closest_center",
    [
      (store_script_param_1, ":party_no"),
      (assign, ":min_distance", 9999999),
      (assign, reg0, -1),
      (try_for_range, ":center_no", centers_begin, centers_end),
        (store_distance_to_party_from_party, ":party_distance", ":party_no", ":center_no"),
        (lt, ":party_distance", ":min_distance"),
        (assign, ":min_distance", ":party_distance"),
        (assign, reg0, ":center_no"),
      (try_end),
  ]),
  
  # script_get_closest_center_of_faction
  # Input: arg1 = party_no, arg2 = kingdom_no
  # Output: reg0 = center_no (closest)
  # ("get_closest_center_of_faction",
    # [
      # (store_script_param_1, ":party_no"),
      # (store_script_param_2, ":kingdom_no"),
      # (assign, ":min_distance", 99999),
      # (assign, ":result", -1),
      # (try_for_range, ":center_no", centers_begin, centers_end),
        # (store_faction_of_party, ":faction_no", ":center_no"),
        # (eq, ":faction_no", ":kingdom_no"),
        # (store_distance_to_party_from_party, ":party_distance", ":party_no", ":center_no"),
        # (lt, ":party_distance", ":min_distance"),
        # (assign, ":min_distance", ":party_distance"),
        # (assign, ":result", ":center_no"),
      # (try_end),
      # (assign, reg0, ":result"),
  # ]),
  
  # script_get_closest_walled_center_of_faction
  # Input: arg1 = party_no, arg2 = kingdom_no
  # Output: reg0 = center_no (closest)
  ("get_closest_walled_center_of_faction",
    [
      (store_script_param_1, ":party_no"),
      (store_script_param_2, ":kingdom_no"),
      (assign, ":min_distance", 99999),
      (assign, ":result", -1),
      (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
        (store_faction_of_party, ":faction_no", ":center_no"),
        (eq, ":faction_no", ":kingdom_no"),
        (store_distance_to_party_from_party, ":party_distance", ":party_no", ":center_no"),
        (lt, ":party_distance", ":min_distance"),
        (assign, ":min_distance", ":party_distance"),
        (assign, ":result", ":center_no"),
      (try_end),
      (assign, reg0, ":result"),
  ]),
  
  ##  # script_get_closest_town_of_faction
  ##  # Input: arg1 = party_no, arg2 = kingdom_no
  ##  # Output: reg0 = center_no (closest)
  ##  ("get_closest_town_of_faction",
  ##    [
  ##      (store_script_param_1, ":party_no"),
  ##      (store_script_param_2, ":kingdom_no"),
  ##      (assign, ":min_distance", 9999999),
  ##      (assign, ":result", -1),
  ##      (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
  ##        (store_faction_of_party, ":faction_no", ":center_no"),
  ##        (eq, ":faction_no", ":kingdom_no"),
  ##        (party_slot_eq, ":center_no", slot_party_type, spt_town),
  ##        (store_distance_to_party_from_party, ":party_distance", ":party_no", ":center_no"),
  ##        (lt, ":party_distance", ":min_distance"),
  ##        (assign, ":min_distance", ":party_distance"),
  ##        (assign, ":result", ":center_no"),
  ##      (try_end),
  ##      (assign, reg0, ":result"),
  ##  ]),
  
  
  # script_let_nearby_parties_join_current_battle
  # Input: arg1 = besiege_mode, arg2 = dont_add_friends_other_than_accompanying
  # Output: none
  ("let_nearby_parties_join_current_battle",
    [
      (store_script_param, ":besiege_mode", 1),
      (store_script_param, ":dont_add_friends_other_than_accompanying", 2),
      (store_character_level, ":player_level", "trp_player"),
      (try_for_parties, ":party_no"),
        (neq, ":party_no", "p_main_party"),
        (neq, ":party_no", "$g_enemy_party"),
        (party_is_active, ":party_no"),
        (neg|is_between, ":party_no", villages_begin, villages_end),
        (party_get_battle_opponent, ":opponent",":party_no"),
        (lt, ":opponent", 0), #party is not itself involved in a battle
        (party_get_attached_to, ":attached_to",":party_no"),
        (lt, ":attached_to", 0), #party is not attached to another party
        (get_party_ai_behavior, ":ai_bhvr", ":party_no"),
        (neq, ":ai_bhvr", ai_bhvr_in_town),
        
        #Quest bandits do not join battle
        (this_or_next|neg|check_quest_active, "qst_track_down_bandits"),
        (neg|quest_slot_eq, "qst_track_down_bandits", slot_quest_target_party, ":party_no"),
        (this_or_next|neg|check_quest_active, "qst_troublesome_bandits"),
        (neg|quest_slot_eq, "qst_troublesome_bandits", slot_quest_target_party, ":party_no"),
        (this_or_next|neg|check_quest_active, "qst_blank_quest_4"),
        (neg|quest_slot_eq, "qst_blank_quest_4", slot_quest_target_party, ":party_no"),
        
        (party_get_template_id, ":template_id", ":party_no"),	#VC-3211
        (neq, ":template_id", "pt_port"),
        (neq, ":template_id", "pt_jetty_port"),
        (neq, ":template_id", "pt_ferry_port"),
        (neq, ":template_id", "pt_village_farmers"),	#VC-3336
        (neq, ":template_id", "pt_kingdom_caravan_party"),	#VC-3336
        (neq, ":template_id", "pt_sacerdotes_party"),
        (neq, ":template_id", "pt_paganos_party"),
        (neq, ":template_id", "pt_troublesome_bandits"),
        (neg|is_between, ":template_id", "pt_steppe_bandit_lair", "pt_bandit_lair_templates_end"),
        
        (assign,	":join_distance", 0),
        (try_begin),
          (is_between, ":party_no", walled_centers_begin, walled_centers_end),
          (assign, ":join_distance", 3), #day
          (try_begin),
            (is_currently_night),
            (assign, ":join_distance", 2), #night
          (try_end),
          
        (else_try),
          (this_or_next|eq, ":party_no", "p_yourlair"),
          (this_or_next|eq, ":party_no","p_troop_camp_1"),
          (eq, ":party_no","p_troop_camp_2"),
          (assign, ":join_distance", 4), #day
          (try_begin),
            (is_currently_night),
            (assign, ":join_distance", 2), #night
          (try_end),
          
        (else_try),
          (this_or_next|eq, ":template_id", "pt_deserters"),
          (neg|party_slot_eq, ":party_no", slot_party_spawn_point, 0),
          (assign, ":join_distance", 3), #day/bandit
          (try_begin),
            (is_currently_night),
            (assign, ":join_distance", 2), #night/bandit
          (try_end),
          
          #default
        (else_try),
          (assign, ":join_distance", 5), #day
          (try_begin),
            (is_currently_night),
            (assign, ":join_distance", 3), #night
          (try_end),
        (try_end),
        
        (store_distance_to_party_from_party, ":distance", ":party_no", "p_main_party"),
        (lt, ":distance", ":join_distance"),
        
        (assign, ":is_bandit_party_with_level_greater_than_6", 0),
        (try_begin),
          (this_or_next|eq, ":template_id", "pt_deserters"),
          (neg|party_slot_eq, ":party_no", slot_party_spawn_point, 0),
          (gt, ":player_level", 2), #chief reduce to 2
          (assign, ":is_bandit_party_with_level_greater_than_6", 1),
        (try_end),
        
        #relations
        (try_begin),
          (neq, ":party_no", "p_yourlair"),
          (neq, ":party_no","p_troop_camp_1"),
          (neq, ":party_no","p_troop_camp_2"),
          (store_faction_of_party, ":faction_no", ":party_no"),
        (else_try),
          (assign, ":faction_no", "fac_player_faction"),
        (try_end),
        
        (store_faction_of_party, ":enemy_faction", "$g_enemy_party"),
        (party_get_slot, ":party_type", ":party_no",slot_party_type),
        
        (try_begin),
          (this_or_next|eq, ":faction_no", "fac_player_supporters_faction"),
          (this_or_next|eq, ":faction_no", "fac_player_faction"),
          (this_or_next|eq, ":faction_no", "$players_kingdom"),
          (eq, ":party_type", spt_levy),	#party the player has levied
          (assign, ":reln_with_player", 100),
          (assign, ":reln_with_enemy", -100),
        (else_try),
          (eq, ":faction_no", ":enemy_faction"),
          (assign, ":reln_with_player", -100),
          (assign, ":reln_with_enemy", 100),
        (else_try),
          (store_relation, ":reln_with_enemy", ":faction_no", ":enemy_faction"),
          (try_begin),
            (is_between, "$players_kingdom", kingdoms_begin, kingdoms_end),
            (store_relation, ":reln_with_player", ":faction_no", "$players_kingdom"),
          (else_try),
            (store_relation, ":reln_with_player", ":faction_no", "fac_player_faction"),
          (try_end),
        (try_end),
        
        (assign, ":enemy_side", 1),
        (try_begin),
          (neq, "$g_enemy_party", "$g_encountered_party"),
          (assign, ":enemy_side", 2),
        (try_end),
        
        (party_stack_get_troop_id, ":leader", ":party_no", 0),
        (try_begin),
          (lt, ":reln_with_player", 0),	#at war with player
          
          (try_begin),
            (ge, ":reln_with_enemy", 0),	#not at war with enemy
            (eq, ":besiege_mode", 0),	#don't jump into siege defense
            
            (this_or_next|eq, ":party_type", spt_kingdom_hero_party),
            (eq, ":is_bandit_party_with_level_greater_than_6", 1),
            
            (this_or_next|party_slot_eq, ":party_no", slot_party_on_water, 1),
            (party_slot_eq, "p_main_party", slot_party_on_water, 0),
            
            # (neq, ":ai_bhvr", ai_bhvr_avoid_party),
            (assign, ":cont", 0),
            (try_begin),
              (eq, ":party_type", spt_kingdom_hero_party),
              (troop_is_hero, ":leader"),
              (party_stack_get_troop_id, ":enemy_leader", "$g_enemy_party", 0),
              (gt, ":enemy_leader", 0),
              (troop_is_hero, ":enemy_leader"),
              (call_script, "script_troop_get_relation_with_troop", ":leader", ":enemy_leader"),
              (lt, reg0, -15),	#really hates enemy leader
              (neg|faction_slot_eq, ":enemy_faction", slot_faction_leader, ":enemy_leader"),	#who is not their lord
              (assign, ":cont", 1),	#won't help
            (try_end),
            
            (eq, ":cont", 0),
            (party_quick_attach_to_current_battle, ":party_no", ":enemy_side"), #attach as enemy
            (str_store_party_name, s1, ":party_no"),
            (display_message, "str_s1_joined_battle_enemy", color_bad_news), #chief anade color
          (try_end),
          
          #not at war with player
        (else_try),
          (try_begin),
            (party_slot_eq, ":party_no", slot_party_ai_state, spai_accompanying_army),
            (party_slot_eq, ":party_no", slot_party_ai_object, "p_main_party"),
            (assign, ":party_is_accompanying_player", 1),
          (else_try),
            (assign, ":party_is_accompanying_player", 0),
          (try_end),
          
          (this_or_next|eq, ":dont_add_friends_other_than_accompanying", 0),
          (eq, ":party_is_accompanying_player", 1),
          
          (this_or_next|eq, ":party_is_accompanying_player", 1),	#allow accompanying parties on land to join player in water
          (this_or_next|party_slot_eq, ":party_no", slot_party_on_water, 1),
          (party_slot_eq, "p_main_party", slot_party_on_water, 0),
          
          #cases for joining
          (assign, ":join_even_you_do_not_like_player", 0),
          (assign, ":check", 0),
          
          #player's troops
          (try_begin),
            (this_or_next|eq, ":party_no", "p_yourlair"),
            (this_or_next|eq, ":party_no","p_troop_camp_1"),
            (eq, ":party_no","p_troop_camp_2"),
            (assign, ":check", 1),
            
            #towns/castles
          (else_try),
            (is_between, ":party_no", walled_centers_begin, walled_centers_end),
            
            ##only player's garrisons join in
            (try_begin),
              (party_slot_eq, ":party_no", slot_town_lord, "trp_player"),
              (assign, ":check", 1),
            (try_end),
            
            #lords
          (else_try),
            (eq, ":party_type", spt_kingdom_hero_party),
            
            #player's vassals
            (try_begin),
              (this_or_next|eq, ":faction_no", "fac_player_faction"),
              (eq, ":faction_no", "fac_player_supporters_faction"),
              (try_begin),
                (lt, ":reln_with_enemy", 0),	#at war with enemy
                (assign, ":join_even_you_do_not_like_player", 1),
              (try_end),
              (assign, ":check", 1),
              
              #other lords
            (else_try),
              (lt, ":reln_with_enemy", 0),	#at war with enemy
              (try_begin),
                (eq, ":faction_no", "$players_kingdom"),
                (faction_slot_eq, "$players_kingdom", slot_faction_marshal, "trp_player"),
                (assign, ":join_even_you_do_not_like_player", 1),
              (try_end),
              (assign, ":check", 1),
            (try_end),
            
            #any other party
          (else_try),
            (lt, ":reln_with_enemy", 0),	#at war with enemy
            (assign, ":check", 1),
          (try_end),
          (eq, ":check", 1),
          
          #exceptions
          (assign, ":do_join", 1),
          (try_begin),
            (eq, ":besiege_mode", 1),	#player besieging
            (eq, ":party_is_accompanying_player", 0),	#but party wasn't part of besieging force
            (eq, ":join_even_you_do_not_like_player", 0),	#player is not leading a war effort
            (assign, ":do_join", 0),
          (try_end),
          (eq, ":do_join", 1),
          
          (assign, ":player_relation", 0),
          (try_begin),
            (eq, ":party_type", spt_kingdom_hero_party),
            (party_stack_get_troop_id, ":leader", ":party_no", 0),
            (assign, ":player_relation", -1),
            (troop_is_hero, ":leader"),
            (is_between, ":leader", active_npcs_begin, active_npcs_end),
            (call_script, "script_troop_get_player_relation", ":leader"),
            (assign, ":player_relation", reg0),
          (try_end),
          
          (try_begin),
            (this_or_next|eq, ":is_bandit_party_with_level_greater_than_6", 1), #bandot can fight to our side
            (this_or_next|ge, ":player_relation", 0),
            # (this_or_next|ge,  ":reln_with_player", 0),
            (eq, ":join_even_you_do_not_like_player", 1),
            (party_quick_attach_to_current_battle, ":party_no", 0), #attach as friend
            (try_begin),
              (is_between, ":party_no", walled_centers_begin, walled_centers_end),
              (str_store_string, s1, "@Garrison Troops"),
            (else_try),
              (this_or_next|eq, ":party_no", "p_troop_camp_1"),
              (eq, ":party_no", "p_troop_camp_2"),
              (str_store_string, s1, "@Camp Troops"),
            (else_try),
              (eq, ":party_no", "p_yourlair"),
              (str_store_string, s1, "@Refuge Troops"),
            (else_try),
              (str_store_party_name, s1, ":party_no"),
            (try_end),
            (display_message, "str_s1_joined_battle_friend", color_good_news), #chief anade color
            
          (else_try),
            (eq, ":party_type", spt_kingdom_hero_party),
            (str_store_party_name, s1, ":party_no"),
            (display_message, "@Even though nearby, {s1} does not come to your aid."),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  # script_party_wound_all_members_aux
  # Input: arg1 = party_no
  ("party_wound_all_members_aux",
    [
      (store_script_param_1, ":party_no"),
      
      (party_get_num_companion_stacks, ":num_stacks",":party_no"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop",":party_no",":i_stack"),
        (try_begin),
          (neg|troop_is_hero, ":stack_troop"),
          (party_stack_get_size, ":stack_size",":party_no",":i_stack"),
          (party_wound_members, ":party_no", ":stack_troop", ":stack_size"),
        (else_try),
          (troop_set_health, ":stack_troop", 0),
        (try_end),
      (try_end),
      (party_get_num_attached_parties, ":num_attached_parties", ":party_no"),
      (try_for_range, ":attached_party_rank", 0, ":num_attached_parties"),
        (party_get_attached_party_with_rank, ":attached_party", ":party_no", ":attached_party_rank"),
        (call_script, "script_party_wound_all_members_aux", ":attached_party"),
      (try_end),
  ]),
  
  # script_party_wound_all_members
  # Input: arg1 = party_no
  ("party_wound_all_members",
    [
      (store_script_param_1, ":party_no"),
      
      (call_script, "script_party_wound_all_members_aux", ":party_no"),
  ]),
  
  # script_calculate_battle_advantage
  # Output: reg0 = battle advantage
  ("calculate_battle_advantage",
    [
      (call_script, "script_party_count_fit_for_battle", "p_collective_friends"),
      (assign, ":friend_count", reg(0)),
      
      (party_get_skill_level, ":player_party_tactics",  "p_main_party", skl_tactics),
      (party_get_skill_level, ":ally_party_tactics",  "p_collective_friends", skl_tactics),
      (val_max, ":player_party_tactics", ":ally_party_tactics"),
      
      (call_script, "script_party_count_fit_for_battle", "p_collective_enemy"),
      (assign, ":enemy_count", reg(0)),
      
      (party_get_skill_level, ":enemy_party_tactics",  "p_collective_enemy", skl_tactics),
      
      (val_add, ":friend_count", 1),
      (val_add, ":enemy_count", 1),
      
      (try_begin),
        (ge, ":friend_count", ":enemy_count"),
        (val_mul, ":friend_count", 100),
        (store_div, ":ratio", ":friend_count", ":enemy_count"),
        (store_sub, ":raw_advantage", ":ratio", 100),
      (else_try),
        (val_mul, ":enemy_count", 100),
        (store_div, ":ratio", ":enemy_count", ":friend_count"),
        (store_sub, ":raw_advantage", 100, ":ratio"),
      (try_end),
      (val_mul, ":raw_advantage", 2),
      
      (val_mul, ":player_party_tactics", 30),
      (val_mul, ":enemy_party_tactics", 30),
      (val_add, ":raw_advantage", ":player_party_tactics"),
      (val_sub, ":raw_advantage", ":enemy_party_tactics"),
      (val_div, ":raw_advantage", 100),
      
      
      (assign, reg0, ":raw_advantage"),
      (display_message, "@Battle Advantage = {reg0}.", 0xFFFFFFFF),
  ]),
  
  # script_cf_check_enemies_nearby
  # Input: none
  # Output: none, fails when enemies are nearby
  ("cf_check_enemies_nearby",
    [
      (get_player_agent_no, ":player_agent"),
      (agent_is_alive, ":player_agent"),
      (agent_get_position, pos1, ":player_agent"),
      (assign, ":result", 0),
      (set_fixed_point_multiplier, 100),
      (try_for_agents,":cur_agent"),
        (neq, ":cur_agent", ":player_agent"),
        (agent_is_alive, ":cur_agent"),
        (agent_is_human, ":cur_agent"),
        (neg|agent_is_ally, ":cur_agent"),
        #
        (this_or_next|neq, "$dog_companion", 1),#VC-2111
        (neq, ":cur_agent", "$player_dog_agent_no"),
        #
        (agent_get_position, pos2, ":cur_agent"),
        (get_distance_between_positions, ":distance", pos1, pos2),
        (lt, ":distance", 1500),
        (assign, ":result", 1),
      (try_end),
      (eq, ":result", 0),
  ]),
  
  # script_get_heroes_attached_to_center_aux
  # For internal use only
  ("get_heroes_attached_to_center_aux",
    [
      (store_script_param_1, ":center_no"),
      (store_script_param_2, ":party_no_to_collect_heroes"),
      (party_get_num_companion_stacks, ":num_stacks",":center_no"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop",":center_no",":i_stack"),
        (troop_is_hero, ":stack_troop"),
        (party_add_members, ":party_no_to_collect_heroes", ":stack_troop", 1),
      (try_end),
      (party_get_num_attached_parties, ":num_attached_parties", ":center_no"),
      (try_for_range, ":attached_party_rank", 0, ":num_attached_parties"),
        (party_get_attached_party_with_rank, ":attached_party", ":center_no", ":attached_party_rank"),
        (call_script, "script_get_heroes_attached_to_center_aux", ":attached_party", ":party_no_to_collect_heroes"),
      (try_end),
  ]),
  
  # script_get_heroes_attached_to_center
  # Input: arg1 = center_no, arg2 = party_no_to_collect_heroes
  # Output: none, adds heroes to the party_no_to_collect_heroes party
  ("get_heroes_attached_to_center",
    [
      (store_script_param_1, ":center_no"),
      (store_script_param_2, ":party_no_to_collect_heroes"),
      (party_clear, ":party_no_to_collect_heroes"),
      (call_script, "script_get_heroes_attached_to_center_aux", ":center_no", ":party_no_to_collect_heroes"),
      
      #rebellion changes begin -Arma
      (try_for_range, ":pretender", pretenders_begin, pretenders_end),
        (neq, ":pretender", "$supported_pretender"),
        (troop_slot_eq, ":pretender", slot_troop_cur_center, ":center_no"),
        (party_add_members, ":party_no_to_collect_heroes", ":pretender", 1),
      (try_end),
      
      #     (try_for_range, ":rebel_faction", rebel_factions_begin, rebel_factions_end),
      #        (faction_slot_eq, ":rebel_faction", slot_faction_state, sfs_inactive_rebellion),
      #        (faction_slot_eq, ":rebel_faction", slot_faction_inactive_leader_location, ":center_no"),
      #        (faction_get_slot, ":pretender", ":rebel_faction", slot_faction_leader),
      #        (party_add_members, ":party_no_to_collect_heroes", ":pretender", 1),
      #     (try_end),
      #rebellion changes end
      
      
  ]),
  
  
  # script_get_heroes_attached_to_center_as_prisoner_aux
  # For internal use only
  ("get_heroes_attached_to_center_as_prisoner_aux",
    [
      (store_script_param_1, ":center_no"),
      (store_script_param_2, ":party_no_to_collect_heroes"),
      (party_get_num_prisoner_stacks, ":num_stacks",":center_no"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_prisoner_stack_get_troop_id, ":stack_troop",":center_no",":i_stack"),
        (troop_is_hero, ":stack_troop"),
        (party_add_members, ":party_no_to_collect_heroes", ":stack_troop", 1),
      (try_end),
      (party_get_num_attached_parties, ":num_attached_parties", ":center_no"),
      (try_for_range, ":attached_party_rank", 0, ":num_attached_parties"),
        (party_get_attached_party_with_rank, ":attached_party", ":center_no", ":attached_party_rank"),
        (call_script, "script_get_heroes_attached_to_center_as_prisoner_aux", ":attached_party", ":party_no_to_collect_heroes"),
      (try_end),
  ]),
  
  
  # script_get_heroes_attached_to_center_as_prisoner
  # Input: arg1 = center_no, arg2 = party_no_to_collect_heroes
  # Output: none, adds heroes to the party_no_to_collect_heroes party
  ("get_heroes_attached_to_center_as_prisoner",
    [
      (store_script_param_1, ":center_no"),
      (store_script_param_2, ":party_no_to_collect_heroes"),
      (party_clear, ":party_no_to_collect_heroes"),
      (call_script, "script_get_heroes_attached_to_center_as_prisoner_aux", ":center_no", ":party_no_to_collect_heroes"),
  ]),
  
  ##
  ##  # script_cf_get_party_leader
  ##  # Input: arg1 = party_no
  ##  # Output: reg0 = troop_no of the leader (Can fail)
  ##  ("cf_get_party_leader",
  ##    [
  ##      (store_script_param_1, ":party_no"),
  ##
  ##      (party_get_num_companion_stacks, ":num_stacks",":party_no"),
  ##      (gt, ":num_stacks", 0),
  ##      (party_stack_get_troop_id, ":stack_troop", ":party_no", 0),
  ##      (troop_is_hero, ":stack_troop"),
  ##      (assign, reg0, ":stack_troop"),
  ##  ]),
  
  # script_give_center_to_faction
  # Input: arg1 = center_no, arg2 = faction
  ("give_center_to_faction",
    [
      (store_script_param_1, ":center_no"),
      (store_script_param_2, ":faction_no"),
      
      (try_begin),
        (eq, ":faction_no", "fac_player_supporters_faction"),
        (faction_get_slot, ":player_faction_king", "fac_player_supporters_faction", slot_faction_leader),
        (eq, ":player_faction_king", "trp_player"),
        
        (try_begin),
          (is_between, ":center_no", walled_centers_begin, walled_centers_end),
          (assign, ":number_of_walled_centers_players_kingdom_has", 1),
        (else_try),
          (assign, ":number_of_walled_centers_players_kingdom_has", 0),
        (try_end),
        
        (try_for_range, ":walled_center", walled_centers_begin, walled_centers_end),
          (store_faction_of_party, ":owner_faction_no", ":walled_center"),
          (eq, ":owner_faction_no", "fac_player_supporters_faction"),
          (val_add, ":number_of_walled_centers_players_kingdom_has", 1),
        (try_end),
        
        (ge, ":number_of_walled_centers_players_kingdom_has", 10),
        (unlock_achievement, ACHIEVEMENT_VICTUM_SEQUENS),
      (try_end),
      
      (try_begin),
        (check_quest_active, "qst_join_siege_with_army"),
        (quest_slot_eq, "qst_join_siege_with_army", slot_quest_target_center, ":center_no"),
        (call_script, "script_abort_quest", "qst_join_siege_with_army", 0),
        #Reactivating follow army quest
        (faction_get_slot, ":faction_marshal", "$players_kingdom", slot_faction_marshal),
        (str_store_troop_name_link, s9, ":faction_marshal"),
        (setup_quest_text, "qst_follow_army"),
        (str_store_string, s2, "@{s9} wants you to resume following his army until further notice."),
        (call_script, "script_start_quest", "qst_follow_army", ":faction_marshal"),
        (assign, "$g_player_follow_army_warnings", 0),
      (try_end),
      
      #(store_faction_of_party, ":old_faction", ":center_no"),
      (call_script, "script_give_center_to_faction_aux", ":center_no", ":faction_no"),
      (call_script, "script_update_village_market_towns"),
      
      (try_for_range, ":cur_faction", kingdoms_begin, kingdoms_end),
        (call_script, "script_faction_recalculate_strength", ":cur_faction"),
      (try_end),
      (assign, "$g_recalculate_ais", 1),
      
      (try_begin),
        (eq, ":faction_no", "fac_player_supporters_faction"),
        (faction_slot_eq, "fac_player_supporters_faction", slot_faction_state, sfs_inactive),
        (call_script, "script_activate_player_faction", "trp_player"),
      (try_end),
      
      #(call_script, "script_activate_deactivate_player_faction", ":old_faction"),
      #(try_begin),
      #(eq, ":faction_no", "fac_player_supporters_faction"),
      #(faction_slot_eq, "fac_player_supporters_faction", slot_faction_leader, "trp_player"),
      #(call_script, "script_give_center_to_lord", ":center_no", "trp_player", 0),
      
      #check with Armagan -- what is this here for?
      #(try_for_range, ":cur_village", villages_begin, villages_end),
      #(store_faction_of_party, ":cur_village_faction", ":cur_village"),
      #(eq, ":cur_village_faction", "fac_player_supporters_faction"),
      #(neg|party_slot_eq, ":cur_village", slot_town_lord, "trp_player"),
      #(call_script, "script_give_center_to_lord", ":cur_village", "trp_player", 0),
      #(try_end),
      #(try_end),
  ]),
  
  # script_give_center_to_faction_aux
  # Input: arg1 = center_no, arg2 = faction
  ("give_center_to_faction_aux",
    [
      (store_script_param_1, ":center_no"),
      (store_script_param_2, ":faction_no"),
      
      (store_faction_of_party, ":old_faction", ":center_no"),
      (party_set_faction, ":center_no", ":faction_no"),
      
      (try_begin),
        (party_slot_eq, ":center_no", slot_party_type, spt_village),
        
        # (party_get_slot, ":farmer_party", ":center_no", slot_village_farmer_party),
        # (try_begin),
          # (gt, ":farmer_party", 0),
          # (party_is_active, ":farmer_party"),
          # (party_set_faction, ":farmer_party", ":faction_no"),
        # (try_end),
        
        # (try_begin),
          (party_slot_eq, ":center_no", slot_village_state, svs_being_raided),
          (call_script, "script_village_set_state", ":center_no", svs_normal),
        # (try_end),
      (try_end),
      
      #update all attached parties VC-3829
      (try_for_parties, ":cur_party"),
        (party_get_slot, ":cur_center", ":cur_party", slot_party_home_center),
        (eq, ":cur_center", ":center_no"),
        (party_set_faction, ":cur_party", ":faction_no"),
      (try_end),
      
      (try_begin),
        #This bit of seemingly redundant code (the neq condition) is designed to prevent a bug that occurs when a player first conquers a center -- apparently this script is called again AFTER it is handed to a lord
        #Without this line, then the player's dialog selection does not have any affect, because town_lord is set again to stl_unassigned after the player makes his or her choice
        (neq, ":faction_no", ":old_faction"),
        
        (party_set_slot, ":center_no", slot_center_ex_faction, ":old_faction"),
        (party_get_slot, ":old_town_lord", ":center_no", slot_town_lord),
        (party_set_slot, ":center_no", slot_town_lord, stl_unassigned),
        (party_set_banner_icon, ":center_no", 0),#Removing banner
        (call_script, "script_update_faction_notes", ":old_faction"),
      (try_end),
      
      (call_script, "script_update_faction_notes", ":faction_no"),
      (call_script, "script_update_center_notes", ":center_no"),
      
      (try_begin),
        (ge, ":old_town_lord", 0),
        (neq, ":faction_no", "fac_player_supporters_faction"),
        (call_script, "script_update_troop_notes", ":old_town_lord"),
      (try_end),
      
      (try_for_range, ":other_center", centers_begin, centers_end),
        (party_slot_eq, ":other_center", slot_village_bound_center, ":center_no"),
        (call_script, "script_give_center_to_faction_aux", ":other_center", ":faction_no"),
      (try_end),
  ]),
  
  # script_change_troop_faction
  # Input: arg1 = troop_no, arg2 = faction
  ("change_troop_faction",
    [
      (store_script_param_1, ":troop_no"),
      (store_script_param_2, ":faction_no"),
      
      #Clean up old faction
      (store_faction_of_troop, ":orig_faction", ":troop_no"),
      (troop_get_slot, ":leaded_party", ":troop_no", slot_troop_leaded_party),
      
      #remove if he is marshal
      (try_begin),
        (faction_slot_eq, ":orig_faction", slot_faction_marshal, ":troop_no"),
        (call_script, "script_check_and_finish_active_army_quests_for_faction", ":orig_faction"),
        
        #No current issue on the agenda
        (try_begin),
          (neg|faction_slot_ge, ":orig_faction", slot_faction_political_issue, 1),
          
          (faction_set_slot, ":orig_faction", slot_faction_political_issue, 1), #Appointment of marshal
          (store_current_hours, ":hours"),
          (val_max, ":hours", 0),
          (faction_set_slot, ":orig_faction", slot_faction_political_issue_time, ":hours"), #Appointment of marshal
          (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
            (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
            (eq, ":active_npc_faction", ":orig_faction"),
            (troop_set_slot, ":active_npc", slot_troop_stance_on_faction_issue, -1),
          (try_end),
          (try_begin),
            (eq, "$players_kingdom", ":orig_faction"),
            (troop_set_slot, "trp_player", slot_troop_stance_on_faction_issue, -1),
          (try_end),
        (try_end),
        
        (try_begin),
          (party_is_active, ":leaded_party"),
          (party_set_marshal, ":leaded_party", 0),
        (try_end),
        
        (faction_set_slot, ":orig_faction", slot_faction_marshal, -1),
      (try_end),
      #Removal as marshal ends
      
      #Other political ramifications
      (troop_set_slot, ":troop_no", slot_troop_stance_on_faction_issue, -1),
      (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
        (troop_slot_eq, ":active_npc", slot_troop_stance_on_faction_issue, ":troop_no"),
        (troop_set_slot, ":active_npc", slot_troop_stance_on_faction_issue, -1),
      (try_end),
      #Political ramifications end
      
      #set up in new faction
      (try_begin),
        (ge, "$cheat_mode", 1),
        (str_store_troop_name, s4, ":troop_no"),
        (display_message, "@{!}DEBUG - {s4} faction changed in normal faction change"),
      (try_end),
      
      (troop_set_faction, ":troop_no", ":faction_no"),
      (troop_set_slot, ":troop_no", slot_troop_recruitment_random, 0),
      (troop_set_slot, ":troop_no", slot_lord_recruitment_argument, 0),
      (troop_set_slot, ":troop_no", slot_lord_recruitment_candidate, 0),
      (troop_set_slot, ":troop_no", slot_troop_promised_fief, 0),
      (troop_set_slot, ":troop_no", slot_troop_last_faction, ":orig_faction"),
      (call_script, "script_troop_set_title_according_to_faction", ":troop_no", ":faction_no"), #new title system
      
      (try_begin),
        (eq, ":faction_no", "fac_outlaws"),
        
        (try_begin),
          (party_is_active, ":leaded_party"),
          (neq, ":leaded_party", "p_main_party"),
          (remove_party, ":leaded_party"),
          (troop_set_slot, ":troop_no", slot_troop_leaded_party, -1),
        (try_end),
        
      (else_try),
          (is_between, ":faction_no", kingdoms_begin, kingdoms_end),
          
          (faction_set_slot, ":faction_no", slot_faction_state, sfs_active),  #Reactivating inactive or defeated kingdom
          (troop_set_slot, ":troop_no", slot_troop_occupation, slto_kingdom_hero),  #in case was applying to player kingdom
          
          (try_begin),
            (this_or_next|eq, ":faction_no", "$players_kingdom"),
            (eq, ":faction_no", "fac_player_supporters_faction"),
            (call_script, "script_check_concilio_calradi_achievement"),
          (try_end),
          
          #Takes walled centers and dependent villages with him
          (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
            (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
            (call_script, "script_give_center_to_faction_while_maintaining_lord", ":center_no", ":faction_no"),
          (try_end),
          
          #Dependant kingdom ladies switch faction
          (try_for_range, ":kingdom_lady", kingdom_ladies_begin, kingdom_ladies_end),
            (call_script, "script_get_kingdom_lady_social_determinants", ":kingdom_lady"),
            (assign, ":closest_male_relative", reg0),
            (assign, ":new_center", reg1),
            
            (eq, ":closest_male_relative", ":troop_no"),
            
            (try_begin),
              (ge, "$cheat_mode", 1),
              (str_store_troop_name, s4, ":kingdom_lady"),
              (display_message, "@{!}DEBUG - {s4} faction changed by guardian moving"),
            (try_end),
            
            (troop_set_faction, ":kingdom_lady", ":faction_no"),
            (troop_slot_eq, ":kingdom_lady", slot_troop_prisoner_of_party, -1),
            (troop_set_slot, ":kingdom_lady", slot_troop_cur_center, ":new_center"),
          (try_end),
          
          #Remove his control over villages under another fortress
          (try_for_range, ":village_no", villages_begin, villages_end),
            (party_slot_eq, ":village_no", slot_town_lord, ":troop_no"),
            (store_faction_of_party, ":village_faction", ":village_no"),
            (try_begin),
              (neq, ":village_faction", ":faction_no"),
              (party_set_slot, ":village_no", slot_town_lord, stl_unassigned),
            (try_end),
          (try_end),
          
          #Free prisoners
          (try_begin),
            (troop_get_slot, ":leaded_party", ":troop_no", slot_troop_leaded_party),
            (gt, ":leaded_party", 0),
            (party_set_faction, ":leaded_party", ":faction_no"),
            (party_get_num_prisoner_stacks, ":num_stacks", ":leaded_party"),
            (try_for_range_backwards, ":troop_iterator", 0, ":num_stacks"),
              (party_prisoner_stack_get_troop_id, ":cur_troop_id", ":leaded_party", ":troop_iterator"),
              (store_troop_faction, ":cur_faction", ":cur_troop_id"),
              (troop_is_hero, ":cur_troop_id"),
              (eq, ":cur_faction", ":faction_no"),
              (call_script, "script_remove_troop_from_prison", ":cur_troop_id"),
              (party_remove_prisoners, ":leaded_party", ":cur_troop_id", 1),
            (try_end),
          (try_end),
          
          #Annull all quests of which the lord is giver
          (try_for_range, ":quest", all_quests_begin, all_quests_end),
            (check_quest_active, ":quest"),
            (quest_slot_eq, ":quest", slot_quest_giver_troop, ":troop_no"),
            
            (str_store_troop_name, s4, ":troop_no"),
            (try_begin),
              (eq, "$cheat_mode", 1),
              (display_message, "str_s4_changing_sides_aborts_quest"),
            (try_end),
            (call_script, "script_abort_quest", ":quest", 0),
          (try_end),
          
          #Boot all lords out of centers whose faction has changed
          (try_for_range, ":lord_to_move", active_npcs_begin, active_npcs_end),
            (troop_get_slot, ":lord_led_party", ":lord_to_move", slot_troop_leaded_party),
            (party_is_active, ":lord_led_party"),
            (party_get_attached_to, ":led_party_attached", ":lord_led_party"),
            (is_between, ":led_party_attached", walled_centers_begin, walled_centers_end),
            (store_faction_of_party, ":led_party_faction", ":lord_led_party"),
            (store_faction_of_party, ":attached_party_faction", ":led_party_attached"),
            (neq, ":led_party_faction", ":attached_party_faction"),
            
            (party_detach, ":lord_led_party"),
          (try_end),
          
          #Increase relation with lord in new faction by 5
          #Or, if player kingdom, make inactive pending confirmation
          (faction_get_slot, ":faction_liege", ":faction_no", slot_faction_leader),
          (try_begin),
            (eq, ":faction_liege", "trp_player"),
            (neq, ":troop_no", "$g_talk_troop"),
            (troop_set_slot, ":troop_no", slot_troop_occupation, slto_inactive), #POSSIBLE REASON 1
            (call_script, "script_troop_set_title_according_to_faction", ":troop_no", ":faction_no"), #recall with inactive flag now set
            # (try_begin), fails
              # (gt, ":leaded_party", 0),
              # (party_set_faction, ":leaded_party", "fac_adventurers"),  #distinguish pending confirm
            # (try_end),
            #VC-2674
            (try_begin),
              (troop_get_slot, ":prisoner_of_party", ":troop_no", slot_troop_prisoner_of_party),
              (gt,":prisoner_of_party", -1),
              (try_begin),
                (eq, ":prisoner_of_party", "p_main_party"),
                (party_remove_prisoners, "p_main_party", ":troop_no", 1),
                (remove_troops_from_prisoners,  ":troop_no", 1),
                (troop_set_slot, ":troop_no", slot_troop_prisoner_of_party, -1),
              (else_try),
                (is_between, ":prisoner_of_party", walled_centers_begin, walled_centers_end),
                (store_faction_of_party, ":prison_faction", ":prisoner_of_party"),
                (eq, ":prison_faction", ":faction_no"),
                (party_remove_prisoners, ":prisoner_of_party", ":troop_no",1),
                (remove_troops_from_prisoners,  ":troop_no", 1),
                (troop_set_slot, ":troop_no", slot_troop_prisoner_of_party, -1),
                (call_script, "script_remove_troop_from_prison", ":troop_no"),
              (try_end),
            (try_end),
            #VC-2674
          (else_try),
            (is_between, ":faction_liege", active_npcs_begin, active_npcs_end),
            (is_between, ":troop_no", active_npcs_begin, active_npcs_end),
            (call_script, "script_troop_change_relation_with_troop", ":faction_liege", ":troop_no", 5),
            (val_add, "$total_indictment_changes", 5),
          (try_end),
          
          #Break courtship relations
          (try_begin),
            #	  	(troop_slot_ge, ":troop_no", slot_troop_spouse, 0),
            #		(call_script, "script_assign_troop_love_interests", ":troop_no"),
            #Already married, do nothing
            #	  (else_try),
            (is_between, ":troop_no", active_npcs_begin, active_npcs_end),
            (try_for_range, ":love_interest_slot", slot_troop_love_interest_1, slot_troop_love_interests_end),
              (troop_get_slot, ":courted_lady", ":troop_no", ":love_interest_slot"),
              (call_script, "script_courtship_event_lady_break_relation_with_suitor", ":courted_lady", ":troop_no"),
            (try_end),
            (call_script, "script_assign_troop_love_interests", ":troop_no"),
          (else_try),
            (is_between, ":troop_no", kingdom_ladies_begin, kingdom_ladies_end),
            (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
              (try_for_range, ":love_interest_slot", slot_troop_love_interest_1, slot_troop_love_interests_end),
                (troop_slot_eq, ":active_npc", ":love_interest_slot", ":troop_no"),
                (call_script, "script_courtship_event_lady_break_relation_with_suitor", ":troop_no", ":active_npc"),
              (try_end),
            (try_end),
          (try_end),
          
          #Stop raidings/sieges of new faction's fief if there is any
          (troop_get_slot, ":troop_party", ":troop_no", slot_troop_leaded_party),
          (try_for_range, ":center_no", villages_begin, villages_end),
            (party_slot_eq, ":center_no", slot_village_raided_by, ":troop_party"),
            (call_script, "script_village_set_state", ":center_no", svs_normal),
          (try_end),
          (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
            (party_slot_eq, ":center_no", slot_center_is_besieged_by, ":troop_party"),
            (call_script, "script_village_set_state", ":center_no", svs_normal),
            # Chief siege camp around town
            (party_set_extra_icon, ":center_no", 0, 0, 0, 0, 0),
            # chief siege camp acaba
          (try_end),
      (try_end),    #new faction is kingdom
      
      (call_script, "script_update_all_notes"),
      (call_script, "script_update_village_market_towns"),
      (assign, "$g_recalculate_ais", 1),
  ]),
  
  # script_troop_set_title_according_to_faction
  # Input: arg1 = troop_no, arg2 = faction_no
  ("troop_set_title_according_to_faction",
    [
      (store_script_param, ":troop_no", 1),
      (store_script_param, ":faction_no", 2),
      
      #VC-3907 restore untitled name for non-kingdom heroes
      (str_store_troop_name_plural, s0, ":troop_no"),
      (troop_set_name, ":troop_no", s0),
      
      #  (faction_get_slot, ":culture", ":faction_no", slot_faction_culture),
      (try_begin),
        (is_between, ":faction_no", kingdoms_begin, kingdoms_end),
        (neg|troop_slot_eq, ":troop_no", slot_troop_occupation, slto_inactive),  #VC-3909 avoid title if hasn't been admitted to player faction
        (faction_get_slot, ":faction_leader", ":faction_no", slot_faction_leader),
        # (troop_get_type, ":gender", ":troop_no"),
        #  (val_mod, ":gender", 2),
        
        (assign, ":lord_have_fief", 0),
        (try_begin),
          (neq, ":troop_no", ":faction_leader"), # exclude research for ruler
          (try_for_range, ":cur_center", walled_centers_begin, walled_centers_end),
            (party_slot_eq, ":cur_center", slot_town_lord, ":troop_no"),
            (val_add, ":lord_have_fief", 1),
          (try_end),
        (try_end),
        
        #(store_sub, ":title_index", ":faction_no", kingdoms_begin),
        (try_begin),
          (faction_slot_eq, ":faction_no", slot_faction_culture, "fac_culture_norse"),
          (try_begin),
            (eq, ":troop_no", ":faction_leader"), # he is king
            (assign, ":title_index", "str_faction_title_male_player"),
          (else_try),
            (gt, ":lord_have_fief", 0), #he has town or forts.
            (assign, ":title_index", "str_faction_title_male_1"),
          (else_try),
            (assign, ":title_index", "str_faction_title_male_2"),
          (try_end),
        (else_try),
          (this_or_next|faction_slot_eq, ":faction_no", slot_faction_culture, "fac_culture_saxon"),
          (faction_slot_eq, ":faction_no", slot_faction_culture, "fac_culture_angle"),
          (try_begin),
            (eq, ":troop_no", ":faction_leader"), # he is king
            (assign, ":title_index", "str_faction_title_male_3"),
          (else_try),
            (gt, ":lord_have_fief", 0), #he has town or forts.
            (assign, ":title_index", "str_faction_title_male_4"),
          (else_try),
            (assign, ":title_index", "str_faction_title_male_5"),
          (try_end),
        (else_try),
          (faction_slot_eq, ":faction_no", slot_faction_culture, "fac_culture_welsh"),
          (try_begin),
            (eq, ":troop_no", ":faction_leader"), # he is king
            (assign, ":title_index", "str_faction_title_male_6"),
          (else_try),
            (gt, ":lord_have_fief", 0), #he has town or forts.
            (assign, ":title_index", "str_faction_title_female_player"),
          (else_try),
            (assign, ":title_index", "str_faction_title_female_1"),
          (try_end),
        (else_try),
          (this_or_next|faction_slot_eq, ":faction_no", slot_faction_culture, "fac_culture_scotch"),
          (faction_slot_eq, ":faction_no", slot_faction_culture, "fac_culture_irish"),
          (try_begin),
            (eq, ":troop_no", ":faction_leader"), # he is king
            (assign, ":title_index", "str_faction_title_female_2"),
          (else_try),
            (gt, ":lord_have_fief", 0), #he has town or forts.
            (assign, ":title_index", "str_faction_title_female_3"),
          (else_try),
            (assign, ":title_index", "str_faction_title_female_4"),
          (try_end),
        (else_try), #other
          (try_begin),
            (eq, ":troop_no", ":faction_leader"), # he is king
            (assign, ":title_index", "str_faction_title_female_5"),
          (else_try),
            (gt, ":lord_have_fief", 0), #he has town or forts.
            (assign, ":title_index", "str_faction_title_female_5"),
          (else_try),
            (assign, ":title_index", "str_faction_title_female_6"),
          (try_end),
          
          #(eq, ":gender", 0), #male
          ##          (val_add, ":title_index", kingdom_titles_male_begin),
          ##        (else_try),
          ##          (val_add, ":title_index", kingdom_titles_female_begin),
        (try_end),
        (str_store_string, s1, ":title_index"),
        (troop_set_name, ":troop_no", s1),
      (try_end),
      
      #VC-3907 restore untitled name for non-kingdom heroes
      (troop_get_slot, ":troop_party", ":troop_no", slot_troop_leaded_party),
      (try_begin),
        (gt, ":troop_party", 0),
        (str_store_troop_name, s5, ":troop_no"),
        (party_set_name, ":troop_party", "str_s5_s_party"),
      (try_end),
  ]),
  
  # script_give_center_to_lord
  # Input: arg1 = center_no, arg2 = lord_troop, arg3 = add_garrison_to_center
  ("give_center_to_lord",
    [
      (store_script_param, ":center_no", 1),
      (store_script_param, ":lord_troop_id", 2), #-1 only in the case of a player deferring ownership of a center
      (store_script_param, ":add_garrison", 3),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (ge, ":lord_troop_id", 0),
        (str_store_party_name, s4, ":center_no"),
        (str_store_troop_name, s5, ":lord_troop_id"),
        (display_message, "@{!}DEBUG -- {s4} awarded to {s5}"),
      (try_end),
      
      (troop_set_slot, "trp_player", slot_troop_num_fiefs, 0),
      (try_for_range, ":other_lord", active_npcs_begin, active_npcs_end),
        (troop_set_slot, ":other_lord", slot_troop_num_fiefs, 0),
      (try_end),
      
      (try_for_range, ":cur_center", centers_begin, centers_end),
        (try_begin),
          (eq, ":cur_center", ":center_no"),
          (ge, ":lord_troop_id", 0),
          (troop_get_slot, ":number_of_fiefs_lord_has", ":lord_troop_id", slot_troop_num_fiefs),
          (val_add, ":number_of_fiefs_lord_has", 1),
          (troop_set_slot, ":lord_troop_id", slot_troop_num_fiefs, ":number_of_fiefs_lord_has"),
        (else_try),
          (party_get_slot, ":other_lord", ":cur_center", slot_town_lord),
          
          (this_or_next|eq, ":other_lord", "trp_player"),
          (is_between, ":other_lord", active_npcs_including_player_begin, active_npcs_end),
          (troop_get_slot, ":number_of_fiefs_lord_has", ":other_lord", slot_troop_num_fiefs),
          (val_add, ":number_of_fiefs_lord_has", 1),
          (troop_set_slot, ":other_lord", slot_troop_num_fiefs, ":number_of_fiefs_lord_has"),
        (try_end),
      (try_end),
    
      (try_begin),
        (eq, ":lord_troop_id", "trp_player"),
        (unlock_achievement, ACHIEVEMENT_ROYALITY_PAYMENT),
        
        (troop_slot_ge, "trp_player", slot_troop_num_fiefs, 5),
        (unlock_achievement, ACHIEVEMENT_MEDIEVAL_EMLAK),
      (try_end),
      
      (try_begin), #This script is ONLY called with lord_troop_id = -1 when it is the player faction
        (eq, ":lord_troop_id", -1),
        (assign, ":lord_troop_faction", "fac_player_supporters_faction"),
        (party_set_banner_icon, ":center_no", 0),#Removing banner
        
      (else_try),
        (eq, ":lord_troop_id", "trp_player"),
        (assign, ":lord_troop_faction", "$players_kingdom"), #was changed on Apr 27 from fac_plyr_sup_fac
        (party_set_slot,":center_no",recruit_permission_need, 0),
        
      (else_try),
        (store_troop_faction, ":lord_troop_faction", ":lord_troop_id"),
        (party_set_slot,":center_no",recruit_permission_need, 1),
      (try_end),
      
      (faction_get_slot, ":faction_leader", ":lord_troop_faction", slot_faction_leader),
      
      (try_begin),
        (eq, ":faction_leader", "trp_player"),
        
        (try_begin),
          (troop_get_type, ":is_female", "trp_player"),
          (val_mod, ":is_female", 2),
          (eq, ":is_female", 1),
          (unlock_achievement, ACHIEVEMENT_QUEEN),
        (try_end),
      (try_end),
      
      (party_get_slot, ":old_lord_troop_id", ":center_no", slot_town_lord),
      
      (try_begin),
        (eq, ":faction_leader", ":old_lord_troop_id"),
        (call_script, "script_add_log_entry", logent_liege_grants_fief_to_vassal, ":faction_leader", ":center_no", ":lord_troop_id", ":lord_troop_faction"),
        (troop_set_slot, ":lord_troop_id", slot_troop_promised_fief, 0),
      (try_end),
      
      (try_begin),
        (eq, ":lord_troop_id", -1), #Lord troop ID -1 is only used when a player is deferring assignment of a fief
        (party_set_faction, ":center_no", "$players_kingdom"),
      (else_try),
        (eq, ":lord_troop_id", "trp_player"),
        (gt, "$players_kingdom", 0),
        (party_set_faction, ":center_no", "$players_kingdom"),
      (else_try),
        (eq, ":lord_troop_id", "trp_player"),
        (neg|is_between, "$players_kingdom", kingdoms_begin, kingdoms_end),
        (party_set_faction, ":center_no", "fac_player_supporters_faction"),
      (else_try),
        (party_set_faction, ":center_no", ":lord_troop_faction"),
      (try_end),
      (party_set_slot, ":center_no", slot_town_lord, ":lord_troop_id"),
      
      #update all attached parties VC-3829
      (store_faction_of_party, ":center_faction", ":center_no"),
      (try_for_parties, ":cur_party"),
        (party_get_slot, ":cur_center", ":cur_party", slot_party_home_center),
        (eq, ":cur_center", ":center_no"),
        (party_set_faction, ":cur_party", ":center_faction"),
      (try_end),
      
      (try_begin),
        (this_or_next|party_slot_eq, ":center_no", slot_party_type, spt_town),
        (party_slot_eq, ":center_no", slot_party_type, spt_castle),
        (gt, ":lord_troop_id", -1),
        
        #normal_banner_begin
        (troop_get_slot, ":cur_banner", ":lord_troop_id", slot_troop_banner_scene_prop),
        (gt, ":cur_banner", 0),
        (val_sub, ":cur_banner", banner_scene_props_begin),
        (val_add, ":cur_banner", banner_map_icons_begin),
        (party_set_banner_icon, ":center_no", ":cur_banner"),
        # custom_banner_begin
        #        (troop_get_slot, ":flag_icon", ":lord_troop_id", slot_troop_custom_banner_map_flag_type),
        #        (ge, ":flag_icon", 0),
        #        (val_add, ":flag_icon", custom_banner_map_icons_begin),
        #        (party_set_banner_icon, ":center_no", ":flag_icon"),
      (try_end),
      
      #    (try_begin),
      #		(eq, 1, 0),
      #       (eq, ":lord_troop_id", "trp_player"),
      #       (neq, ":old_lord_troop_id", "trp_player"),
      #       (party_get_slot, ":center_relation", ":center_no", slot_center_player_relation),
      #       (is_between, ":center_relation", -4, 5),
      #       (call_script, "script_change_player_relation_with_center", ":center_no", 5),
      #       (gt, ":old_lord_troop_id", 0),
      #       (call_script, "script_change_player_relation_with_troop", ":old_lord_troop_id", -25),
      #   (try_end),
      (try_begin),
        (gt, ":lord_troop_id", -1),
        (call_script, "script_update_troop_notes", ":lord_troop_id"),
      (try_end),
      
      (call_script, "script_update_center_notes", ":center_no"),
      
      (try_begin),
        (gt, ":lord_troop_faction", 0),
        (call_script, "script_update_faction_notes", ":lord_troop_faction"),
      (try_end),
      
      (try_begin),
        (ge, ":old_lord_troop_id", 0),
        (call_script, "script_update_troop_notes", ":old_lord_troop_id"),
        (store_troop_faction, ":old_lord_troop_faction", ":old_lord_troop_id"),
        (call_script, "script_update_faction_notes", ":old_lord_troop_faction"),
      (try_end),
      
      (try_begin),
        (eq, ":add_garrison", 1),
        (this_or_next|party_slot_eq, ":center_no", slot_party_type, spt_town),
        (party_slot_eq, ":center_no", slot_party_type, spt_castle),
        (assign, ":garrison_strength", 3),
        (try_begin),
          (party_slot_eq, ":center_no", slot_party_type, spt_town),
          (assign, ":garrison_strength", 9),
        (try_end),
        (try_for_range, ":unused", 0, ":garrison_strength"),
          (call_script, "script_cf_reinforce_party", ":center_no"),
        (try_end),
        ## ADD some XP initially
        (try_for_range, ":unused", 0, 7),
          (store_mul, ":xp_range_min", 150, ":garrison_strength"),
          (store_mul, ":xp_range_max", 200, ":garrison_strength"),
          (store_random_in_range, ":xp", ":xp_range_min", ":xp_range_max"),
          (party_upgrade_with_xp, ":center_no", ":xp", 0),
        (try_end),
      (try_end),
      
      (faction_get_slot, ":faction_leader", ":lord_troop_faction", slot_faction_leader),
      (store_current_hours, ":hours"),
      
      #the next block handles gratitude, objections and jealousies
      (try_begin),
        (gt, ":hours", 0),
        (gt, ":lord_troop_id", 0),
        
        (call_script, "script_troop_change_relation_with_troop", ":lord_troop_id", ":faction_leader", 10),
        (val_add, "$total_promotion_changes", 10),
        
        #smaller factions are more dramatically influenced by internal jealousies
        #Disabled as of NOV 2010
        #		(try_begin),
        #			(neg|faction_slot_ge, ":lord_troop_faction", slot_faction_number_of_parties, 4),
        #			(assign, ":faction_size_multiplier", 6),
        #		(else_try),
        #			(neg|faction_slot_ge, ":lord_troop_faction", slot_faction_number_of_parties, 8),
        #			(assign, ":faction_size_multiplier", 5),
        #		(else_try),
        #			(neg|faction_slot_ge, ":lord_troop_faction", slot_faction_number_of_parties, 16),
        #			(assign, ":faction_size_multiplier", 4),
        #		(else_try),
        #			(neg|faction_slot_ge, ":lord_troop_faction", slot_faction_number_of_parties, 32),
        #			(assign, ":faction_size_multiplier", 3),
        #		(else_try),
        #			(assign, ":faction_size_multiplier", 2),
        #		(try_end),
        
        #factional politics -- each lord in the faction adjusts his relation according to the relation with the lord receiving the faction
        (try_for_range, ":other_lord", active_npcs_begin, active_npcs_end),
          (troop_slot_eq, ":other_lord", slot_troop_occupation, slto_kingdom_hero),
          (neq, ":other_lord", ":lord_troop_id"),
          
          (store_troop_faction, ":other_troop_faction", ":other_lord"),
          (eq, ":lord_troop_faction", ":other_troop_faction"),
          
          (neq, ":other_lord", ":faction_leader"),
          
          (call_script, "script_troop_get_relation_with_troop", ":other_lord", ":lord_troop_id"),
          (assign, ":relation_with_troop", reg0),
          
          #original relation impact started at 8 but was effectively clamped at 24
          #we want to keep that impact because most lord relations have such small magnitudes
          #here we use non-linear function to accomplish the same in continuous fashion
          #sqrt (9 sine (x clamped at 90))
          #but simply use case statement to avoid difficulties with negative, etc.
          
          (try_begin),
            (ge, ":relation_with_troop", 90),
            (assign, ":relation_with_liege_change", 3),
          (else_try),
            (ge, ":relation_with_troop", 27),
            (assign, ":relation_with_liege_change", 2),
          (else_try),
            (ge, ":relation_with_troop", 7),
            (assign, ":relation_with_liege_change", 1),
          (else_try),
            (ge, ":relation_with_troop", -7),
            (assign, ":relation_with_liege_change", 0),
          (else_try),
            (ge, ":relation_with_troop", -27),
            (assign, ":relation_with_liege_change", -1),
          (else_try),
            (ge, ":relation_with_troop", -90),
            (assign, ":relation_with_liege_change", -2),
          (else_try),
            (assign, ":relation_with_liege_change", -3),
          (end_try),
          
          (try_begin),
            #penalty is decreased for lords who are upstanding or goodnatured
            (this_or_next|troop_slot_eq, ":other_lord", slot_lord_reputation_type, lrep_upstanding),
            (troop_slot_eq, ":other_lord", slot_lord_reputation_type, lrep_goodnatured),
            (val_add, ":relation_with_liege_change", 1),
          (else_try),
            #penalty is increased for lords who have the more unpleasant reputation types
            (this_or_next|troop_slot_eq, ":other_lord", slot_lord_reputation_type, lrep_selfrighteous),
            (this_or_next|troop_slot_eq, ":other_lord", slot_lord_reputation_type, lrep_debauched),
            (troop_slot_eq, ":other_lord", slot_lord_reputation_type, lrep_quarrelsome),
            (val_sub, ":relation_with_liege_change", 1),
          (try_end),
          
          (try_begin),
            (troop_slot_eq, ":other_lord", slot_troop_stance_on_faction_issue, ":lord_troop_id"),
            (val_add, ":relation_with_liege_change", 1),
            # (val_max, ":relation_with_liege_change", 1),
          (try_end),
          
          (try_begin),
            (troop_slot_eq, ":other_lord", slot_troop_num_fiefs, 0),
            (val_sub, ":relation_with_liege_change", 2),
          (try_end),
          
          (neq, ":relation_with_liege_change", 0),
          (call_script, "script_troop_change_relation_with_troop", ":other_lord", ":faction_leader", ":relation_with_liege_change"),
          (val_add, "$total_promotion_changes", ":relation_with_liege_change"),
          
          (try_begin),
            (this_or_next|le, ":relation_with_liege_change", -3),
            (this_or_next|troop_slot_eq, ":other_lord", slot_troop_promised_fief, 1), #1 is any fief
            (troop_slot_eq, ":other_lord", slot_troop_promised_fief, ":center_no"),
            (call_script, "script_add_log_entry", logent_troop_feels_cheated_by_troop_over_land, ":other_lord", ":center_no", ":lord_troop_id", ":lord_troop_faction"),
          (try_end),
          
        (try_end),
      (try_end),
      
      #Villages from another faction will also be transferred along with a fortress
      (try_begin),
        (is_between, ":center_no", walled_centers_begin, walled_centers_end),
        (try_for_range, ":cur_village", villages_begin, villages_end),
          (party_slot_eq, ":cur_village", slot_village_bound_center, ":center_no"),
          (store_faction_of_party, ":cur_village_faction", ":cur_village"),
          (neq, ":cur_village_faction", ":lord_troop_faction"),
          
          (call_script, "script_give_center_to_lord", ":cur_village", ":lord_troop_id", 0),
        (try_end),
      (try_end),
  ]),
  
  ##  # script_give_town_to_besiegers
  ##  # Input: arg1 = center_no, arg2 = besieger_party
  ##  ("give_town_to_besiegers",
  ##    [
  ##      (store_script_param_1, ":center_no"),
  ##      (store_script_param_2, ":besieger_party"),
  ##      (store_faction_of_party, ":besieger_faction", ":besieger_party"),
  ##
  ##      (try_begin),
  ##        (call_script, "script_cf_get_party_leader", ":besieger_party"),
  ##        (assign, ":new_leader", reg0),
  ##      (else_try),
  ##        (call_script, "script_select_kingdom_hero_for_new_center", ":besieger_faction"),
  ##        (assign, ":new_leader", reg0),
  ##      (try_end),
  ##
  ##      (call_script, "script_give_center_to_lord", ":center_no", ":new_leader"),
  ##
  ##      (try_for_parties, ":party_no"),
  ##        (get_party_ai_object, ":object", ":party_no"),
  ##        (get_party_ai_behavior, ":behavior", ":party_no"),
  ##        (eq, ":object", ":center_no"),
  ##        (this_or_next|eq, ":behavior", ai_bhvr_travel_to_party),
  ##        (eq, ":behavior", ai_bhvr_attack_party),
  ##        (party_set_ai_behavior, ":party_no", ai_bhvr_hold),
  ##        (party_set_slot, ":party_no", slot_party_ai_state, spai_undefined),
  ##        (party_set_flags, ":party_no", pf_default_behavior, 0),
  ##      (try_end),
  ##
  ##      #Staying at the center for a while
  ##      (party_set_ai_behavior, ":besieger_party", ai_bhvr_hold),
  ##      (party_set_slot, ":besieger_party", slot_party_ai_state, spai_undefined),
  ##      (party_set_flags, ":besieger_party", pf_default_behavior, 0),
  ##
  ##      (faction_get_slot, ":reinforcement_a", ":besieger_faction", slot_faction_reinforcements_a),
  ##      (faction_get_slot, ":reinforcement_b", ":besieger_faction", slot_faction_reinforcements_b),
  ##      (party_add_template, ":center_no", ":reinforcement_a"),
  ##      (party_add_template, ":center_no", ":reinforcement_b"),
  ##  ]),
  ##
  
  # script_get_number_of_hero_centers
  # Input: arg1 = troop_no
  # Output: reg0 = number of centers that are ruled by the hero
  ("get_number_of_hero_centers",
    [
      (store_script_param_1, ":troop_no"),
      (assign, ":result", 0),
      (try_for_range, ":center_no", centers_begin, centers_end),
        (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
        (val_add, ":result", 1),
      (try_end),
      (assign, reg0, ":result"),
  ]),
  
  
  ##  # script_cf_get_new_center_leader_chance_for_troop
  ##  # Input: arg1 = troop_no
  ##  # Output: reg0 = chance of the troop to rule a new center
  ##  ("cf_get_new_center_leader_chance_for_troop",
  ##    [
  ##      (store_script_param_1, ":troop_no"),
  ##      (troop_get_slot, ":troop_rank", ":troop_no", slot_troop_kingdom_rank),
  ##      (try_begin),
  ##        (eq, ":troop_rank", 4),
  ##        (assign, ":troop_chance", 1000),
  ##      (else_try),
  ##        (eq, ":troop_rank", 3),
  ##        (assign, ":troop_chance", 800),
  ##      (else_try),
  ##        (eq, ":troop_rank", 2),
  ##        (assign, ":troop_chance", 400),
  ##      (else_try),
  ##        (eq, ":troop_rank", 1),
  ##        (assign, ":troop_chance", 100),
  ##      (else_try),
  ##        (assign, ":troop_chance", 10),
  ##      (try_end),
  ##
  ##      (call_script, "script_get_number_of_hero_centers", ":troop_no"),
  ##      (assign, ":number_of_hero_centers", reg0),
  ##      (try_begin),
  ##        (gt, ":number_of_hero_centers", 0),
  ##        (val_mul, ":number_of_hero_centers", 2),
  ##        (val_mul, ":number_of_hero_centers", ":number_of_hero_centers"),
  ##        (val_div, ":troop_chance", ":number_of_hero_centers"),
  ##      (try_end),
  ##      (assign, reg0, ":troop_chance"),
  ##      (eq, reg0, 0),
  ##      (assign, reg0, 1),
  ##  ]),
  
  
  ##  # script_select_kingdom_hero_for_new_center
  ##  # Input: arg1 = faction_no
  ##  # Output: reg0 = troop_no as the new leader
  ##  ("select_kingdom_hero_for_new_center",
  ##    [
  ##      (store_script_param_1, ":kingdom"),
  ##
  ##      (assign, ":min_num_centers", -1),
  ##      (assign, ":min_num_centers_troop", -1),
  ##
  ##      (try_for_range, ":troop_no", kingdom_heroes_begin, kingdom_heroes_end),
  ##        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
  ##        (store_troop_faction, ":troop_faction", ":troop_no"),
  ##        (eq, ":troop_faction", ":kingdom"),
  ##        (call_script, "script_get_number_of_hero_centers", ":troop_no"),
  ##        (assign, ":num_centers", reg0),
  ##        (try_begin),
  ##          (lt, ":num_centers", ":min_num_centers"),
  ##          (assign, ":min_num_centers", ":num_centers"),
  ##          (assign, ":min_num_centers_troop", ":troop_no"),
  ##        (try_end),
  ##      (try_end),
  ##      (assign, reg0, ":min_num_centers_troop"),
  ##  ]),
  
  
  # script_cf_get_random_enemy_center
  # Input: arg1 = party_no
  # Output: reg0 = center_no
  # ("cf_get_random_enemy_center",
    # [
      # (store_script_param_1, ":party_no"),
      
      # (assign, ":result", -1),
      # (assign, ":total_enemy_centers", 0),
      # (store_faction_of_party, ":party_faction", ":party_no"),
      
      # (try_for_range, ":center_no", centers_begin, centers_end),
        # (store_faction_of_party, ":center_faction", ":center_no"),
        # (store_relation, ":party_relation", ":center_faction", ":party_faction"),
        # (lt, ":party_relation", 0),
        # (val_add, ":total_enemy_centers", 1),
      # (try_end),
      
      # (gt, ":total_enemy_centers", 0),
      # (store_random_in_range, ":random_center", 0, ":total_enemy_centers"),
      # (assign, ":total_enemy_centers", 0),
      # (try_for_range, ":center_no", centers_begin, centers_end),
        # (eq, ":result", -1),
        # (store_faction_of_party, ":center_faction", ":center_no"),
        # (store_relation, ":party_relation", ":center_faction", ":party_faction"),
        # (lt, ":party_relation", 0),
        # (val_sub, ":random_center", 1),
        # (lt, ":random_center", 0),
        # (assign, ":result", ":center_no"),
      # (try_end),
      # (assign, reg0, ":result"),
  # ]),
  
  
  ##  # script_get_random_enemy_town
  ##  # Input: arg1 = party_no
  ##  # Output: reg0 = center_no
  ##  ("get_random_enemy_town",
  ##    [
  ##      (store_script_param_1, ":party_no"),
  ##
  ##      (assign, ":result", -1),
  ##      (assign, ":total_enemy_centers", 0),
  ##      (store_faction_of_party, ":party_faction", ":party_no"),
  ##
  ##      (try_for_range, ":center_no", towns_begin, towns_end),
  ##        (store_faction_of_party, ":center_faction", ":center_no"),
  ##        (neq, ":center_faction", ":party_faction"),
  ##        (val_add, ":total_enemy_centers", 1),
  ##      (try_end),
  ##
  ##      (try_begin),
  ##        (eq, ":total_enemy_centers", 0),
  ##      (else_try),
  ##        (store_random_in_range, ":random_center", 0, ":total_enemy_centers"),
  ##        (assign, ":total_enemy_centers", 0),
  ##        (try_for_range, ":center_no", towns_begin, towns_end),
  ##          (eq, ":result", -1),
  ##          (store_faction_of_party, ":center_faction", ":center_no"),
  ##          (neq, ":center_faction", ":party_faction"),
  ##          (store_relation, ":party_relation", ":center_faction", ":party_faction"),
  ##          (le, ":party_relation", -10),
  ##          (val_add, ":total_enemy_centers", 1),
  ##          (lt, ":random_center", ":total_enemy_centers"),
  ##          (assign, ":result", ":center_no"),
  ##        (try_end),
  ##      (try_end),
  ##      (assign, reg0, ":result"),
  ##  ]),
  
  # script_find_travel_location
  # Input: arg1 = center_no
  # Output: reg0 = new_center_no (to travel within the same faction)
  # ("find_travel_location",
    # [
      # (store_script_param_1, ":center_no"),
      # (store_faction_of_party, ":faction_no", ":center_no"),
      # (assign, ":total_weight", 0),
      # (try_for_range, ":cur_center_no", centers_begin, centers_end),
        # (neq, ":center_no", ":cur_center_no"),
        # (store_faction_of_party, ":center_faction_no", ":cur_center_no"),
        # (eq, ":faction_no", ":center_faction_no"),
        
        # (store_distance_to_party_from_party, ":cur_distance", ":center_no", ":cur_center_no"),
        # (val_add, ":cur_distance", 1),
        
        # (assign, ":new_weight", 100000),
        # (val_div, ":new_weight", ":cur_distance"),
        # (val_add, ":total_weight", ":new_weight"),
      # (try_end),
      
      # (assign, reg0, -1),
      
      # (try_begin),
        # (eq, ":total_weight", 0),
      # (else_try),
        # (store_random_in_range, ":random_weight", 0 , ":total_weight"),
        # (assign, ":total_weight", 0),
        # (assign, ":done", 0),
        # (try_for_range, ":cur_center_no", centers_begin, centers_end),
          # (eq, ":done", 0),
          # (neq, ":center_no", ":cur_center_no"),
          # (store_faction_of_party, ":center_faction_no", ":cur_center_no"),
          # (eq, ":faction_no", ":center_faction_no"),
          
          # (store_distance_to_party_from_party, ":cur_distance", ":center_no", ":cur_center_no"),
          # (val_add, ":cur_distance", 1),
          
          # (assign, ":new_weight", 100000),
          # (val_div, ":new_weight", ":cur_distance"),
          # (val_add, ":total_weight", ":new_weight"),
          # (lt, ":random_weight", ":total_weight"),
          # (assign, reg0, ":cur_center_no"),
          # (assign, ":done", 1),
        # (try_end),
      # (try_end),
  # ]),
  
  
  # script_get_relation_between_parties
  # Input: arg1 = party_no_1, arg2 = party_no_2
  # Output: reg0 = relation between parties
  ("get_relation_between_parties",
    [
      (store_script_param_1, ":party_no_1"),
      (store_script_param_2, ":party_no_2"),
      
      (store_faction_of_party, ":party_no_1_faction", ":party_no_1"),
      (store_faction_of_party, ":party_no_2_faction", ":party_no_2"),
      (try_begin),
        (eq, ":party_no_1_faction", ":party_no_2_faction"),
        (assign, reg0, 100),
      (else_try),
        (store_relation, ":relation", ":party_no_1_faction", ":party_no_2_faction"),
        (assign, reg0, ":relation"),
      (try_end),
  ]),
  # script_calculate_weekly_party_wage
  # Input: arg1 = party_no
  # Output: reg0 = weekly wage
  ("calculate_weekly_party_wage",
    [
      (store_script_param_1, ":party_no"),
      
      (assign, ":result", 0),
      (party_get_num_companion_stacks, ":num_stacks",":party_no"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop",":party_no",":i_stack"),
        (party_stack_get_size, ":stack_size",":party_no",":i_stack"),
        (call_script, "script_npc_get_troop_wage", ":stack_troop", ":party_no"),
        (assign, ":cur_wage", reg0),
        (val_mul, ":cur_wage", ":stack_size"),
        (val_add, ":result", ":cur_wage"),
      (try_end),
      (assign, reg0, ":result"),
  ]),
  
  # script_calculate_player_faction_wage
  # Input: arg1 = party_no
  # Output: reg0 = weekly wage
  #         reg1 = number of troops paid #motomataru fix debt morale penalty
  ("calculate_player_faction_wage",
    [(assign, ":nongarrison_wages", 0),
      (assign, ":garrison_wages", 0),
      (assign, reg1, 0), #motomataru chief fix debt morale penalty
      (try_for_parties, ":party_no"),
        (assign, ":garrison_troop", 0),
        (try_begin),
          (this_or_next|party_slot_eq, ":party_no", slot_party_type, spt_town),
          (party_slot_eq, ":party_no", slot_party_type, spt_castle),
          (party_slot_eq, ":party_no", slot_town_lord, "trp_player"),
          (assign, ":garrison_troop", 1),
        (try_end),
        (this_or_next|eq, ":party_no", "p_main_party"),
        (eq, ":garrison_troop", 1),
        (party_get_num_companion_stacks, ":num_stacks",":party_no"),
        (try_for_range, ":i_stack", 0, ":num_stacks"),
          (party_stack_get_troop_id, ":stack_troop",":party_no",":i_stack"),
          (party_stack_get_size, ":stack_size",":party_no",":i_stack"),
          (val_add, reg1, ":stack_size"), #motomataru chief fix debt morale penalty
          (call_script, "script_game_get_troop_wage", ":stack_troop", ":party_no"),
          (assign, ":cur_wage", reg0),
          (val_mul, ":cur_wage", ":stack_size"),
          (try_begin),
            (eq, ":garrison_troop", 1),
            (val_add, ":garrison_wages", ":cur_wage"),
          (else_try),
            (val_add, ":nongarrison_wages", ":cur_wage"),
          (try_end),
        (try_end),
      (try_end),
      (val_div, ":garrison_wages", 4),#Half payment for garrisons change to 1/4
      (store_sub, ":total_payment", 14, "$g_cur_week_half_daily_wage_payments"), #between 0 and 7
      (val_mul, ":nongarrison_wages", ":total_payment"),
      (val_div, ":nongarrison_wages", 14),
      (store_add, reg0, ":nongarrison_wages", ":garrison_wages"),
  ]),
  
  # script_calculate_hero_weekly_net_income_and_add_to_wealth ###
  # Input: arg1 = troop_no
  # Output: none
  ("calculate_hero_weekly_net_income_and_add_to_wealth",
    [
      (store_script_param_1, ":troop_no"),
      
      (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
      (troop_get_slot, ":cur_wealth", ":troop_no", slot_troop_wealth),
      
      (assign, ":weekly_income", 300), #let every hero receive 750 denars by default
      
      (store_character_level, ":troop_level", ":troop_no"),
      (store_mul, ":level_income", ":troop_level", 30), #was 10
      (val_add, ":weekly_income", ":level_income"),
      
      # (try_for_range, ":center_no", centers_begin, centers_end),  needlessly and badly duplicates rents and tariffs system
      # (party_slot_eq, ":center_no", slot_village_infested_by_bandits, 0),
      # (party_get_slot, ":lord", ":center_no", slot_town_lord),
      # (try_begin), #check if troop is lord with center # now attack propierties does more damage to lords
      # (eq, ":lord", ":troop_no"),
      # (party_get_slot, ":prosperity", ":center_no", slot_town_prosperity), #prosperty changes between 0..100
      # (try_begin),
      # (party_slot_eq, ":center_no", slot_party_type, spt_village),
      # (party_slot_eq, ":center_no", slot_village_state, svs_normal),
      # (store_mul, ":prosperity_add", 40, ":prosperity"), #multiplier changes between 20..120
      # (store_add, ":lords_income", 2000, ":prosperity_add"), #multiplier changes between 20..120
      # (else_try),
      # (party_slot_eq, ":center_no", slot_party_type, spt_castle),
      # (store_mul, ":prosperity_add", 45, ":prosperity"), #multiplier changes between 20..120
      # (store_add, ":lords_income", 2500, ":prosperity_add"), #multiplier changes between 20..120
      # (else_try),
      # (party_slot_eq, ":center_no", slot_party_type, spt_town),
      # (store_mul, ":prosperity_add", 50, ":prosperity"), #multiplier changes between 20..120
      # (store_add, ":lords_income", 3000, ":prosperity_add"), #multiplier changes between 20..120
      # (try_end),
      # (try_end),
      # (try_end), # end of parties loop
      # (val_add, ":weekly_income", ":lords_income"), #extra money per propierties
      
      # (try_begin), #check if troop is kingdom leader
      # (faction_slot_eq, ":faction_no", slot_faction_leader, ":troop_no"),
      # #Kingdom Wealth, fyrd concept, the important is towns, villages, and castle number. Here faction number total
      # (assign, ":num_lords", 0),
      # (assign, ":num_caravans", 0),
      # (try_for_parties, ":cur_party"),
      # (store_faction_of_party, ":cur_faction", ":cur_party"),
      # (eq, ":cur_faction", ":faction_no"),
      
      # (try_begin),
      # (party_slot_eq, ":cur_party", slot_party_type, spt_kingdom_hero_party),
      # (val_add, ":num_lords", 1),
      # (else_try),
      # (party_slot_eq, ":cur_party", slot_party_type, spt_kingdom_caravan),
      # (val_add, ":num_caravans", 1),
      # (try_end),
      # (try_end), # end of parties loop
      # #############
      # ##kingdom wealth total:
      # (val_mul, ":num_lords", 1000), #lord taxes
      # (val_mul, ":num_caravans", 1000), #commerce taxes
      # (store_add, ":taxes_income", ":num_lords", ":num_caravans"), #total taxes.
      # (val_sub, ":taxes_income", 500), #marshal and others expenses.
      # (val_max, ":taxes_income", 100), #final
      
      # (val_add, ":weekly_income", ":taxes_income"), #minor extra income king.
      # (try_end),
      
      (store_troop_faction,":faction_no", ":troop_no"),
      (try_begin), #check if troop is kingdom leader
        (faction_slot_eq, ":faction_no", slot_faction_leader, ":troop_no"),
        (val_add, ":weekly_income", 1000),
      (try_end),
      
      (try_begin), #check if troop is marshall
        (faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
        (val_add, ":weekly_income", 1000),
      (try_end),
      (assign, ":cur_weekly_wage", 0),
      (try_begin),
        (gt, ":party_no",0),
        (call_script, "script_calculate_weekly_party_wage", ":party_no"),
        (assign, ":cur_weekly_wage", reg0),
      (try_end),
      (val_sub, ":weekly_income", ":cur_weekly_wage"),
      (val_add, ":cur_wealth", ":weekly_income"),
      
      (try_begin),
        (lt, ":cur_wealth", 0),
        (neq, ":faction_no", "fac_adventurers"),
        (store_sub, ":percent_under", 0, ":cur_wealth"),
        (val_mul, ":percent_under", 100),
        (gt, ":cur_weekly_wage", 0),# avoid division by 0
        (val_div, ":percent_under", ":cur_weekly_wage"),
        (val_div, ":percent_under", 5), #Max 20 percent
        
        (call_script, "script_party_inflict_attrition", ":party_no", ":percent_under", 1),
      (try_end),
      
      (val_max, ":cur_wealth", 0),
      (troop_set_slot, ":troop_no", slot_troop_wealth, ":cur_wealth"),
  ]),
  
  # script_cf_reinforce_party
  # Input: arg1 = party_no,
  # Output: none
  # Adds reinforcement to party according to its type and faction
  # Called from several places, simple_triggers for centers, script_hire_men_to_kingdom_hero_party for hero parties
  ("cf_reinforce_party",
    [
      (store_script_param_1, ":party_no"),
      
      (store_faction_of_party, ":party_faction", ":party_no"),
      (party_get_slot, ":party_type",":party_no", slot_party_type),
      
      #Rebellion changes begin:
      (try_begin),
        (eq, ":party_type", spt_kingdom_hero_party),
        (party_stack_get_troop_id, ":leader", ":party_no"),
        (troop_get_slot, ":party_faction",  ":leader", slot_troop_original_faction),
      (try_end),
      #Rebellion changes end
      
      (try_begin),
        (eq, ":party_no", "p_main_party"),
        (try_begin),
          (is_between, "$players_kingdom", npc_kingdoms_begin, npc_kingdoms_end),
          (assign, ":party_faction", "$players_kingdom"),
        (else_try),
          (assign, ":party_faction", "fac_player_supporters_faction"),
        (try_end),
        
      (else_try),
        (eq, ":party_faction", "fac_player_supporters_faction"),
        (is_between, ":party_no", walled_centers_begin, walled_centers_end),
        (party_get_slot, ":town_lord", ":party_no", slot_town_lord),
        (try_begin),
          (gt, ":town_lord", 0),
          (troop_get_slot, ":party_faction", ":town_lord", slot_troop_original_faction),
        (else_try),
          (party_get_slot, ":party_faction", ":party_no", slot_center_original_faction),
        (try_end),
      (try_end),
      
      (faction_get_slot, ":party_template_a", ":party_faction", slot_faction_reinforcements_a),
      (faction_get_slot, ":party_template_b", ":party_faction", slot_faction_reinforcements_b),
      (faction_get_slot, ":party_template_c", ":party_faction", slot_faction_reinforcements_c),
      
      (assign, ":party_template", 0),
      (store_random_in_range, ":rand", 0, 100),
      (try_begin),
        (this_or_next|eq, ":party_type", spt_town),
        (eq, ":party_type", spt_castle),  #CASTLE OR TOWN
        (try_begin),
          (lt, ":rand", 65),
          (assign, ":party_template", ":party_template_a"),
        (else_try),
          (assign, ":party_template", ":party_template_b"),
        (try_end),
      (else_try),
        (this_or_next|eq, ":party_no", "p_main_party"),
        (eq, ":party_type", spt_kingdom_hero_party),
        (try_begin),
          (lt, ":rand", 55),
          (assign, ":party_template", ":party_template_a"),
        (else_try),
          (lt, ":rand", 75),
          (assign, ":party_template", ":party_template_b"),
        (else_try),
          (assign, ":party_template", ":party_template_c"),
        (try_end),
      (try_end),
      
      (try_begin),
        (gt, ":party_template", 0),
        (party_add_template, ":party_no", ":party_template"),
      (try_end),
  ]),
  
  # script_hire_men_to_kingdom_hero_party
  # Input: arg1 = troop_no (hero of the party)
  # Output: none
  ("hire_men_to_kingdom_hero_party",
    [
      (store_script_param_1, ":troop_no"),
      
      (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
      (troop_get_slot, ":cur_wealth", ":troop_no", slot_troop_wealth),
      
      #while hiring reinforcements party leaders can only use 3/4 of their budget. This value is holding in ":hiring budget".
      (assign, ":hiring_budget", ":cur_wealth"),
      (val_mul, ":hiring_budget", 3),
      (val_div, ":hiring_budget", 4),
      
      (try_begin),
        (eq, ":party_no", "p_main_party"),
        (call_script, "script_game_get_party_companion_limit"),
        (store_div, ":ideal_size", reg0, 2),
      (else_try),
        (call_script, "script_party_get_ideal_size", ":party_no"),
        (assign, ":ideal_size", reg0),
      (try_end),
      
      (store_mul, ":ideal_top_size", ":ideal_size", 3),
      (val_div, ":ideal_top_size", 2),
      
      #(try_begin),
      #	(ge, "$cheat_mode", 1),
      #  (str_store_troop_name, s7, ":troop_no"),
      #  (assign, reg9, ":cur_wealth"),
      #  (display_message, "@{!}DEBUGS : {s7} total budget is {reg9}"),
      #  (assign, reg6, ":ideal_size"),
      #  (assign, reg7, ":ideal_top_size"),
      #  (assign, reg8, ":hiring_budget"),
      #  (display_message, "str_debug__hiring_men_to_s7_ideal_size__reg6_ideal_top_size__reg7_hiring_budget__reg8"),
      #(try_end),
      
      (party_get_num_companions, ":party_size", ":party_no"),
      
      (store_faction_of_party, ":party_faction", ":party_no"),
      (try_begin),
        (this_or_next|eq, ":party_faction", "fac_player_supporters_faction"),
        (eq, ":party_faction", "$players_kingdom"),
        (assign, ":reinforcement_cost", reinforcement_cost_moderate),
      (else_try),
        (options_get_campaign_ai, ":reduce_campaign_ai"), #moto chief
        (try_begin),
          (eq, ":reduce_campaign_ai", 0), #hard
          (assign, ":reinforcement_cost", reinforcement_cost_hard),
        (else_try),
          (eq, ":reduce_campaign_ai", 2), #easy
          (assign, ":reinforcement_cost", reinforcement_cost_easy),
        (else_try),
          (assign, ":reinforcement_cost", reinforcement_cost_moderate),
        (try_end),
      (try_end),
      
      (assign, ":num_rounds", 1),
      (try_for_range, ":unused", 0 , ":num_rounds"),
        (try_begin),
          (lt, ":party_size", ":ideal_size"),
          (gt, ":hiring_budget", ":reinforcement_cost"),
          (store_and, reg0, "$first_time", first_time_load_main_party),
          (this_or_next|neq, reg0, 0),
          (gt, ":party_no", 0),
          (call_script, "script_cf_reinforce_party", ":party_no"),
          (val_sub, ":cur_wealth", ":reinforcement_cost"),
          (troop_set_slot, ":troop_no", slot_troop_wealth, ":cur_wealth"),
        (else_try),
          (gt, ":party_size", ":ideal_top_size"),
          (store_troop_faction, ":troop_faction", ":troop_no"),
          (party_get_num_companion_stacks, ":num_stacks", ":party_no"),
          (assign, ":total_regulars", 0),
          (assign, ":total_regular_levels", 0),
          (try_for_range_backwards, ":i_stack", 0, ":num_stacks"),
            (party_stack_get_troop_id, ":stack_troop", ":party_no", ":i_stack"),
            (neg|troop_is_hero, ":stack_troop"),
            (party_stack_get_size, ":stack_size", ":party_no", ":i_stack"),
            (store_character_level, ":stack_level", ":stack_troop"),
            (store_troop_faction, ":stack_faction", ":stack_troop"),
            (try_begin),
              (eq, ":troop_faction", ":stack_faction"),
              (val_mul, ":stack_level", 3), #reducing the chance of the faction troops' removal
            (try_end),
            (val_mul, ":stack_level", ":stack_size"),
            (val_add, ":total_regulars", ":stack_size"),
            (val_add, ":total_regular_levels", ":stack_level"),
          (try_end),
          (gt, ":total_regulars", 0),
          (store_div, ":average_level", ":total_regular_levels", ":total_regulars"),
          (try_for_range_backwards, ":i_stack", 0, ":num_stacks"),
            (party_stack_get_troop_id, ":stack_troop", ":party_no", ":i_stack"),
            (neg|troop_is_hero, ":stack_troop"),
            (party_stack_get_size, ":stack_size", ":party_no", ":i_stack"),
            (store_character_level, ":stack_level", ":stack_troop"),
            (store_troop_faction, ":stack_faction", ":stack_troop"),
            (try_begin),
              (eq, ":troop_faction", ":stack_faction"),
              (val_mul, ":stack_level", 3),
            (try_end),
            (store_sub, ":level_dif", ":average_level", ":stack_level"),
            (val_div, ":level_dif", 3),
            (store_add, ":prune_chance", 10, ":level_dif"),
            (gt, ":prune_chance", 0),
            (call_script, "script_get_percentage_with_randomized_round", ":stack_size", ":prune_chance"),
            (gt, reg0, 0),
            (party_remove_members, ":party_no", ":stack_troop", reg0),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  # script_get_percentage_with_randomized_round
  # Input: arg1 = value, arg2 = percentage
  # Output: none
  ("get_percentage_with_randomized_round",
    [
      (store_script_param, ":value", 1),
      (store_script_param, ":percentage", 2),
      
      (store_mul, ":result", ":value", ":percentage"),
      (val_div, ":result", 100),
      (store_mul, ":used_amount", ":result", 100),
      (val_div, ":used_amount", ":percentage"),
      (store_sub, ":left_amount", ":value", ":used_amount"),
      (try_begin),
        (gt, ":left_amount", 0),
        (store_mul, ":chance", ":left_amount", ":percentage"),
        (store_random_in_range, ":random_no", 0, 100),
        (lt, ":random_no", ":chance"),
        (val_add, ":result", 1),
      (try_end),
      (assign, reg0, ":result"),
  ]),
  
  # script_create_cattle_herd
  # Input: arg1 = center_no, arg2 = amount (0 = default)
  # Output: reg0 = party_no
  ("create_cattle_herd",
    [
      (store_script_param_1, ":center_no"),
      (store_script_param_2, ":amount"),
      
      (assign, ":herd_party", -1),
      (set_spawn_radius,1),
      
      (spawn_around_party,":center_no", "pt_cattle_herd"),
      (assign, ":herd_party", reg0),
      (party_get_position, pos1, ":center_no"),
      (call_script, "script_map_get_random_position_around_position_within_range", 1, 2),
      (party_set_position, ":herd_party", pos2),
      
      (party_set_slot, ":herd_party", slot_party_type, spt_cattle_herd),
      (party_set_slot, ":herd_party", slot_party_ai_state, spai_undefined),
      (party_set_ai_behavior, ":herd_party", ai_bhvr_hold),
      
      (party_set_slot, ":herd_party", slot_party_commander_party, -1), #we need this because 0 is player's party!
      
      (try_begin),
        (gt, ":amount", 0),
        (party_clear, ":herd_party"),
        (party_add_members, ":herd_party", "trp_cattle", ":amount"),
      (try_end),
      
      (assign, reg0, ":herd_party"),
  ]),
  
  #script_buy_cattle_from_village
  # Input: arg1 = village_no, arg2 = amount, arg3 = single_cost
  # Output: reg0 = party_no
  ("buy_cattle_from_village",
    [
      (store_script_param, ":village_no", 1),
      (store_script_param, ":amount", 2),
      (store_script_param, ":single_cost", 3),
      
      #Changing price of the cattle
      (try_for_range, ":unused", 0, ":amount"),
        (call_script, "script_game_event_buy_item", "itm_cattle_meat", 0),
        (call_script, "script_game_event_buy_item", "itm_cattle_meat", 0),
      (try_end),
      
      (party_get_slot, ":num_cattle", ":village_no", slot_village_number_of_cattle),
      (val_sub, ":num_cattle", ":amount"),
      (party_set_slot, ":village_no", slot_village_number_of_cattle, ":num_cattle"),
      (store_mul, ":cost", ":single_cost", ":amount"),
      (troop_remove_gold, "trp_player", ":cost"),
      
      (assign, ":continue", 1),
      (try_for_parties, ":cur_party"),
        (eq, ":continue", 1),
        (party_slot_eq, ":cur_party", slot_party_type, spt_cattle_herd),
        (store_distance_to_party_from_party, ":dist", ":village_no", ":cur_party"),
        (lt, ":dist", 8),
        (assign, ":subcontinue", 1),
        (try_begin),
          (check_quest_active, "qst_move_cattle_herd"),
          (quest_slot_eq, "qst_move_cattle_herd", slot_quest_target_party, ":cur_party"),
          (assign, ":subcontinue", 0),
        (try_end),
        (eq, ":subcontinue", 1),
        (party_add_members, ":cur_party", "trp_cattle", ":amount"),
        (assign, ":continue", 0),
        (assign, reg0, ":cur_party"),
      (try_end),
      (try_begin),
        (eq, ":continue", 1),
        (call_script, "script_create_cattle_herd", ":village_no", ":amount"),
      (try_end),
  ]),
  
  #script_kill_cattle_from_herd
  # Input: arg1 = party_no, arg2 = amount
  # Output: none (fills trp_temp_troop's inventory) #
  ("kill_cattle_from_herd",
    [
      (store_script_param_1, ":party_no"),
      (store_script_param_2, ":amount"),
      
      (troop_clear_inventory, "trp_temp_troop"),
      (store_mul, ":meat_amount", ":amount", 2),
      (troop_add_items, "trp_temp_troop", "itm_cattle_meat", ":meat_amount"),
      #      (troop_add_items, "trp_temp_troop", "itm_raw_leather", ":meat_amount"), #chief add hides to cows
      
      (troop_get_inventory_capacity, ":inv_size", "trp_temp_troop"),
      (try_for_range, ":i_slot", 0, ":inv_size"),
        (troop_get_inventory_slot, ":item_id", "trp_temp_troop", ":i_slot"),
        (eq, ":item_id", "itm_cattle_meat"),
        (troop_set_inventory_slot_modifier, "trp_temp_troop", ":i_slot", imod_fresh),
      (try_end),
      
      (party_get_num_companions, ":num_cattle", ":party_no"),
      (try_begin),
        (ge, ":amount", ":num_cattle"),
        (remove_party, ":party_no"),
      (else_try),
        (party_remove_members, ":party_no", "trp_cattle", ":amount"),
      (try_end),
  ]),
  
  # script_create_kingdom_hero_party
  # Input: arg1 = troop_no, arg2 = center_no
  # Output: $pout_party = party_no
  ("create_kingdom_hero_party",
    [
      (store_script_param, ":troop_no", 1),
      (store_script_param, ":center_no", 2),
      
      (store_troop_faction, ":troop_faction_no", ":troop_no"),
      
      (assign, "$pout_party", -1),
      (try_begin),
        (eq, "$g_there_is_no_avaliable_centers", 0),
        (set_spawn_radius, 0),
      (else_try),
        (set_spawn_radius, 15),
      (try_end),
      (spawn_around_party, ":center_no", "pt_kingdom_hero_party"),
      
      (assign, "$pout_party", reg0),
      
      (party_set_faction, "$pout_party", ":troop_faction_no"),
      (party_set_slot, "$pout_party", slot_party_type, spt_kingdom_hero_party),
      (call_script, "script_party_set_ai_state", "$pout_party", spai_undefined, -1),
      (troop_set_slot, ":troop_no", slot_troop_leaded_party, "$pout_party"),
      (party_add_leader, "$pout_party", ":troop_no"),
      (str_store_troop_name, s5, ":troop_no"),
      (party_set_name, "$pout_party", "str_s5_s_party"),
      
      (party_set_slot, "$pout_party", slot_party_commander_party, -1), #we need this because 0 is player's party!
      
      #Setting the flag icon
      #normal_banner_begin
      (troop_get_slot, ":cur_banner", ":troop_no", slot_troop_banner_scene_prop),
      (try_begin),
        (gt, ":cur_banner", 0),
        (val_sub, ":cur_banner", banner_scene_props_begin),
        (val_add, ":cur_banner", banner_map_icons_begin),
        (val_min, ":cur_banner", "icon_map_flag_default"),	# phaiak quick fix for VC-1425
        (party_set_banner_icon, "$pout_party", ":cur_banner"),
        #custom_banner_begin
        #(troop_get_slot, ":flag_icon", ":troop_no", slot_troop_custom_banner_map_flag_type),
        #(try_begin),
        #  (ge, ":flag_icon", 0),
        #  (val_add, ":flag_icon", custom_banner_map_icons_begin),
        #  (party_set_banner_icon, "$pout_party", ":flag_icon"),
      (try_end),
      (troop_equip_items, ":troop_no"),
      (try_begin),
        #because of below two lines, lords can only hire more than one party_template(stack) at game start once a time during all game.
        (troop_slot_eq, ":troop_no", slot_troop_spawned_before, 0),
        (troop_set_slot, ":troop_no", slot_troop_spawned_before, 1),
        (assign, ":num_tries", 20),
        (try_begin),
          (store_troop_faction, ":troop_kingdom", ":troop_no"),
          (faction_slot_eq, ":troop_kingdom", slot_faction_leader, ":troop_no"),
          (assign, ":num_tries", 50),
        (try_end),
        
        #(str_store_troop_name, s0, ":troop_no"),
        #(display_message, "{!}str_debug__hiring_men_to_party_for_s0"),
        
        (try_for_range, ":unused", 0, ":num_tries"),
          (call_script, "script_hire_men_to_kingdom_hero_party", ":troop_no"),
        (try_end),
        
        (assign, ":xp_rounds", 0),
        
        (options_get_campaign_ai, ":reduce_campaign_ai"), #moto chief
        (try_begin),
          (this_or_next|eq, ":troop_faction_no", "$players_kingdom"),
          (eq, ":troop_faction_no", "fac_player_supporters_faction"),
          (assign, ":xp_rounds", 0),
        (else_try),
          (eq, ":reduce_campaign_ai", 0), #hard
          (assign, ":xp_rounds", 2),
        (else_try),
          (eq, ":reduce_campaign_ai", 1), #moderate
          (assign, ":xp_rounds", 1),
        (else_try),
          (eq, ":reduce_campaign_ai", 2), #easy
          (assign, ":xp_rounds", 0),
        (try_end),
        
        (troop_get_slot, ":renown", ":troop_no", slot_troop_renown),
        (store_div, ":renown_xp_rounds", ":renown", 100),
        (val_add, ":xp_rounds", ":renown_xp_rounds"),
        (try_for_range, ":unused", 0, ":xp_rounds"),
          (call_script, "script_upgrade_hero_party", "$pout_party", 4000),
        (try_end),
      (try_end),
  ]),
  
  # script_create_kingdom_party_if_below_limit
  # Input: arg1 = faction_no, arg2 = party_type (variables beginning with spt_)
  # Output: reg0 = party_no
  ("create_kingdom_party_if_below_limit",
    [
      (store_script_param_1, ":faction_no"),
      (store_script_param_2, ":party_type"),
      
      (call_script, "script_count_parties_of_faction_and_party_type", ":faction_no", ":party_type"),
      (assign, ":party_count", reg0),
      
      (assign, ":party_count_limit", 0),
      
      (faction_get_slot, ":num_towns", ":faction_no", slot_faction_num_towns),
      
      (try_begin),
        ##        (eq, ":party_type", spt_forager),
        ##        (assign, ":party_count_limit", 1),
        ##      (else_try),
        ##        (eq, ":party_type", spt_scout),
        ##        (assign, ":party_count_limit", 1),
        ##      (else_try),
        ##        (eq, ":party_type", spt_patrol),
        ##        (assign, ":party_count_limit", 1),
        ##      (else_try),
        ##        (eq, ":party_type", spt_messenger),
        ##        (assign, ":party_count_limit", 1),
        ##      (else_try),
        # (eq, ":party_type", spt_kingdom_caravan),
        # (try_begin),
          # (eq, ":num_towns", 0),
          # (assign, ":party_count_limit", 0),
        # (else_try),
          # (eq, ":num_towns", 1),
          # (assign, ":party_count_limit", 1),
        # (else_try),
          # (eq, ":num_towns", 2),
          # (assign, ":party_count_limit", 3),
        # (else_try),
          # (assign, ":party_count_limit", 5),
        # (try_end),
        
        # VC-3912 adjust to number of towns, split between two factions (WB was four)
        # this means, when the player is down to one rival, chances are they will be economically as powerful
        # beyond that limit, growing a faction becomes progressively harder
        (store_sub, ":total_towns", towns_end, towns_begin),
        (val_div, ":total_towns", 2),   #clamp to 1/2 of total towns
        (store_div, ":degrees_per_town", 90, ":total_towns"),
        
        (store_mul, ":total_degrees", ":num_towns", ":degrees_per_town"),
        (val_min, ":total_degrees", 90),    #clamp
        
        (set_fixed_point_multiplier, ":total_towns"),
        (val_mul, ":total_degrees", ":total_towns"),    #must apply FPM to domain
        (store_sin, ":party_count_limit", ":total_degrees"),    #sine favors smaller factions
        
        ##      (else_try),
        ##        (eq, ":party_type", spt_prisoner_train),
        ##        (assign, ":party_count_limit", 1),
      (try_end),
      
      (assign, reg0, -1),
      (try_begin),
        (lt, ":party_count", ":party_count_limit"),
        (call_script,"script_cf_create_kingdom_party", ":faction_no", ":party_type"),
      (try_end),
  ]),
  
  
  # script_cf_create_kingdom_party
  # Input: arg1 = faction_no, arg2 = party_type (variables beginning with spt_)
  # Output: reg0 = party_no
  ("cf_create_kingdom_party",
    [
      (store_script_param_1, ":faction_no"),
      (store_script_param_2, ":party_type"),
      
      (str_store_faction_name, s7, ":faction_no"),
      (assign, ":party_name_str", "str_no_string"),
      
      ##      (faction_get_slot, ":reinforcements_a", ":faction_no", slot_faction_reinforcements_a),
      (faction_get_slot, ":reinforcements_b", ":faction_no", slot_faction_reinforcements_b),
      ##      (faction_get_slot, ":reinforcements_c", ":faction_no", slot_faction_reinforcements_c),
      
      (try_begin),
        ##        (eq, ":party_type", spt_forager),
        ##        (assign, ":party_template", "pt_forager_party"),
        #        (assign, ":party_name_str", "str_s7_foragers"),
        ##      (else_try),
        ##        (eq, ":party_type", spt_scout),
        ##        (assign, ":party_template", "pt_scout_party"),
        #        (assign, ":party_name_str", "str_s7_scouts"),
        ##      (else_try),
        ##        (eq, ":party_type", spt_patrol),
        ##        (assign, ":party_template", "pt_patrol_party"),
        #        (assign, ":party_name_str", "str_s7_patrol"),
        ##      (else_try),
        (eq, ":party_type", spt_kingdom_caravan),
        (assign, ":party_template", "pt_kingdom_caravan_party"),
        #        (assign, ":party_name_str", "str_s7_caravan"),
        ##      (else_try),
        ##        (eq, ":party_type", spt_messenger),
        ##        (assign, ":party_template", "pt_messenger_party"),
        #        (assign, ":party_name_str", "str_s7_messenger"),
        ##      (else_try),
        ##        (eq, ":party_type", spt_raider),
        ##        (assign, ":party_template", "pt_raider_party"),
        ##        (assign, ":party_name_str", "str_s7_raiders"),
        ##      (else_try),
        ##        (eq, ":party_type", spt_prisoner_train),
        ##        (assign, ":party_template", "pt_prisoner_train_party"),
        #        (assign, ":party_name_str", "str_s7_prisoner_train"),
      (try_end),
      
      (assign, ":result", -1),
      (try_begin),
        (try_begin),
          (eq, ":party_type", spt_kingdom_caravan),
          (call_script,"script_cf_select_random_town_with_faction", ":faction_no", -1),
          (set_spawn_radius, 0),
        (else_try), #not used at the moment
          (call_script,"script_cf_select_random_walled_center_with_faction", ":faction_no", -1),
          (set_spawn_radius, 1),
        (try_end),
        (assign, ":spawn_center", reg0),
        (is_between, ":spawn_center", centers_begin, centers_end),
        (spawn_around_party,":spawn_center",":party_template"),
        (assign, ":result", reg0),
        (party_set_faction, ":result", ":faction_no"),
        (try_begin),
          (eq, ":party_type", spt_kingdom_caravan),
          (party_set_slot, ":result", slot_party_home_center, ":spawn_center"),
          (party_set_slot, ":result", slot_party_last_traded_center, ":spawn_center"),
        (try_end),
        (party_set_slot, ":result", slot_party_type, ":party_type"),
        (party_set_slot, ":result", slot_party_ai_state, spai_undefined),
        (try_begin),
          (neq, ":party_name_str", "str_no_string"),
          (party_set_name, ":result", ":party_name_str"),
        (try_end),
        
        (try_begin),
          ##          (eq, ":party_type", spt_forager),
          ##          (party_add_template, ":result", ":reinforcements_a"),
          ##        (else_try),
          ##          (eq, ":party_type", spt_scout),
          ##          (party_add_template, ":result", ":reinforcements_c"),
          ##        (else_try),
          ##          (eq, ":party_type", spt_patrol),
          ##          (party_add_template, ":result", ":reinforcements_a"),
          ##          (party_add_template, ":result", ":reinforcements_b"),
          ##        (else_try),
          (eq, ":party_type", spt_kingdom_caravan),
          (try_begin),
            (eq, ":faction_no", "fac_player_supporters_faction"),
            (party_get_slot, ":reinforcement_faction", ":spawn_center", slot_center_original_faction),
            (faction_get_slot, ":reinforcements_b", ":reinforcement_faction", slot_faction_reinforcements_b),
          (try_end),
          (party_add_template, ":result", ":reinforcements_b"),
          (party_add_template, ":result", ":reinforcements_b"),
          (party_set_ai_behavior,":result",ai_bhvr_travel_to_party),
          (party_set_ai_object,":result",":spawn_center"),
          (party_set_flags, ":result", pf_default_behavior, 1),
          (store_sub, ":item_to_price_slot", slot_town_trade_good_prices_begin, trade_goods_begin),
          (try_for_range, ":cur_goods", trade_goods_begin, trade_goods_end),
            (store_add, ":cur_goods_price_slot", ":cur_goods", ":item_to_price_slot"),
            (party_set_slot, ":result", ":cur_goods_price_slot", average_price_factor),
          (try_end),
          ##        (else_try),
          ##          (eq, ":party_type", spt_messenger),
          ##          (faction_get_slot, ":messenger_troop", ":faction_no", slot_faction_messenger_troop),
          ##          (party_add_leader, ":result", ":messenger_troop"),
          ##          (party_set_ai_behavior,":result",ai_bhvr_travel_to_party),
          ##          (party_set_ai_object,":result",":spawn_center"),
          ##          (party_set_flags, ":result", pf_default_behavior, 0),
          ##        (else_try),
          ##          (eq, ":party_type", spt_raider),
          ##          (party_add_template, ":result", ":reinforcements_c"),
          ##          (party_add_template, ":result", ":reinforcements_b"),
          ##          (party_add_template, ":result", "pt_raider_captives"),
          ##        (else_try),
          ##          (eq, ":party_type", spt_prisoner_train),
          ##          (party_add_template, ":result", ":reinforcements_b"),
          ##          (party_add_template, ":result", ":reinforcements_a"),
          ##          (try_begin),
          ##            (call_script,"script_cf_faction_get_random_enemy_faction",":faction_no"),
          ##            (store_random_in_range,":r",0,3),
          ##            (try_begin),
          ##              (lt, ":r", 1),
          ##              (faction_get_slot, ":captive_reinforcements", reg0, slot_faction_reinforcements_b),
          ##            (else_try),
          ##              (faction_get_slot, ":captive_reinforcements", reg0, slot_faction_reinforcements_a),
          ##            (try_end),
          ##            (party_add_template, ":result", ":captive_reinforcements",1),
          ##          (else_try),
          ##            (party_add_template, ":result", "pt_default_prisoners"),
          ##          (try_end),
        (try_end),
      (try_end),
      (ge, ":result", 0),
      (assign, reg0, ":result"),
  ]),
  
  # script_get_troop_attached_party
  # Input: arg1 = troop_no
  # Output: reg0 = party_no (-1 if troop's party is not attached to a party)
  ("get_troop_attached_party",
    [
      (store_script_param_1, ":troop_no"),
      
      (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
      (assign, ":attached_party_no", -1),
      (try_begin),
        (ge, ":party_no", 0),
        (party_get_attached_to, ":attached_party_no", ":party_no"),
      (try_end),
      (assign, reg0, ":attached_party_no"),
  ]),
  
  # script_center_get_food_consumption
  # Input: arg1 = center_no
  # Output: reg0: food consumption (1 food item counts as 100 units)
  ("center_get_food_consumption",
    [
      (store_script_param_1, ":center_no"),
      
      (party_get_slot, reg0, ":center_no", slot_town_prosperity), #prosperity changes between 0..100
      (val_mul, reg0, 125),
      (val_div, reg0, 100),
      (val_add, reg0, 875),
  ]),
  
  # script_center_get_food_store_limit
  # Input: arg1 = center_no
  # Output: reg0: food consumption (1 food item counts as 100 units)
  ("center_get_food_store_limit",
    [
      (store_script_param_1, ":center_no"),
      
      (call_script, "script_center_get_food_consumption", ":center_no"),
      
      (try_begin),
        (party_slot_eq, ":center_no", slot_party_type, spt_town),
        (val_mul, reg0, town_food_days),
      (else_try),
        (val_mul, reg0, castle_food_days),
      (try_end),
  ]),
  
  # VC 2.0 versions
  # script_center_get_food_consumption
  # Input: arg1 = center_no
  # Output: reg0: food consumption (1 food item counts as 100 units)
  ("center_get_food_consumption_20",
    [
      (store_script_param_1, ":center_no"),
      
      (party_get_slot, reg0, ":center_no", slot_town_prosperity), #prosperity changes between 0..100
      (val_mul, reg0, 100),	#average 5000
      
      #garrison
      (try_begin),
        (party_get_num_companions, ":garrison_consumption", ":center_no"),
        (val_div, ":garrison_consumption", 3), #3 men = 1 food
        (val_add, reg0, ":garrison_consumption"),
      (try_end),
  ]),
  
  # script_refresh_village_merchant_inventory
  # Input: arg1 = village_no
  # Output: none
  ("refresh_village_merchant_inventory",
    [
      (store_script_param_1, ":village_no"),
      (party_get_slot, ":merchant_troop", ":village_no", slot_town_elder),
      (reset_item_probabilities,0),
      
      (party_get_slot, ":bound_center", ":village_no", slot_village_bound_center),
      
      (assign, ":total_probability", 0),
      (try_for_range, ":cur_good", trade_goods_begin, trade_goods_end),
        (call_script, "script_center_get_production", ":village_no", ":cur_good"),
        (assign, ":cur_probability", reg0),
        
        (call_script, "script_center_get_production", ":bound_center", ":cur_good"),
        (val_div, reg0, 5), #also add 1/5 of bound center production to village's inventory.
        (val_add, ":cur_probability", reg0),
        
        (val_max, ":cur_probability", 5),
        (val_add, ":total_probability", ":cur_probability"),
      (try_end),
      #	  	  (assign, reg34, ":total_probability"),
      #	  (display_message, "@Total probability is {reg34}"),
      (try_begin),
        (party_get_slot, ":prosperity", ":village_no", slot_town_prosperity),
        (val_div, ":prosperity", 15), #up to 6
        (store_add, ":number_of_items_in_village", ":prosperity", 1),
      (try_end),
      
      (try_for_range, ":cur_good", trade_goods_begin, trade_goods_end),
        (call_script, "script_center_get_production", ":village_no", ":cur_good"),
        (assign, ":cur_probability", reg0),
        
        (call_script, "script_center_get_production", ":bound_center", ":cur_good"),
        (val_div, reg0, 5), #also add 1/5 of bound center production to village's inventory.
        (val_add, ":cur_probability", reg0),
        
        (val_max, ":cur_probability", 5),
        (val_mul, ":cur_probability", ":number_of_items_in_village"),
        (val_mul, ":cur_probability", 100),
        (val_div, ":cur_probability", ":total_probability"),
        #		(str_store_item_name,s33, ":cur_good"),
        #		(assign, reg35, ":cur_probability"),
        #		(display_message, "@Current probability of {s33} is {reg35}"),
        #       (set_item_probability_in_merchandise, ":cur_good", ":cur_probability"),
        #		(is_between, ":cur_good",food_begin, food_end),
        (set_item_probability_in_merchandise, ":cur_good", ":cur_probability"),
      (try_end),
      
      (set_item_probability_in_merchandise, "itm_jewelry", 0),	#phaiak #avoiding jewellery in villages
      
      (troop_clear_inventory, ":merchant_troop"),
      (troop_add_merchandise, ":merchant_troop", itp_type_goods, ":number_of_items_in_village"),
      (troop_ensure_inventory_space, ":merchant_troop", 80),
      
      #Adding 1 prosperity to the village while reducing each 3000 gold from the elder
      (store_troop_gold, ":gold",":merchant_troop"),
      (try_begin),
        (gt, ":gold", 3500),
        (store_div, ":prosperity_added", ":gold", 3000),
        (store_mul, ":gold_removed", ":prosperity_added", 3000),
        (troop_remove_gold, ":merchant_troop", ":gold_removed"),
        (call_script, "script_change_center_prosperity", ":village_no", ":prosperity_added"),
      (try_end),
  ]),
  
  # script_refresh_village_defenders
  # Input: arg1 = village_no
  # Output: none
  ("refresh_village_defenders",
    [
      (store_script_param_1, ":village_no"),
      (party_get_slot, ":prosperity", ":village_no", slot_town_prosperity),
      (try_begin),
        (gt, ":prosperity", 40),
        (assign, ":ideal_size", ":prosperity"),
      (else_try),
        (store_add, ":ideal_size", ":prosperity", 20),
      (try_end),
      (try_begin),
        (party_get_num_companions, ":party_size", ":village_no"),
        (lt, ":party_size", ":ideal_size"),
        (party_add_template, ":village_no", "pt_village_defenders"),
      (try_end),
  ]),
  
  # script_village_set_state
  # Input: arg1 = center_no arg2:new_state
  # Output: reg0: food consumption (1 food item counts as 100 units)
  ("village_set_state",
    [
      (store_script_param_1, ":village_no"),
      (store_script_param_2, ":new_state"),
      (try_begin),
        (eq, ":new_state", 0),
        (party_set_extra_text, ":village_no", "str_empty_string"),
        (party_set_slot, ":village_no", slot_village_raided_by, -1),
      (else_try),
        (eq, ":new_state", svs_being_raided),
        (party_set_extra_text, ":village_no", "@(Being Raided)"),
      (else_try),
        (eq, ":new_state", svs_looted),
        (party_set_extra_text, ":village_no", "@(Looted)"),
        
        (party_set_slot, ":village_no", slot_village_raided_by, -1),
        (party_get_slot, ":prosperity_change", ":village_no", slot_town_prosperity),
        (val_mul, ":prosperity_change", -6),
        (val_div, ":prosperity_change", 10),
        (call_script, "script_change_center_prosperity", ":village_no", ":prosperity_change"),
        (val_add, "$newglob_total_prosperity_from_villageloot", ":prosperity_change"),
        
        (try_begin), #optional - lowers the relationship between a lord and his liege if his fief is looted
          (eq, 5, 0),
          (party_get_slot, ":town_lord", ":village_no", slot_town_lord),
          (is_between, ":town_lord", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":town_lord_faction", ":town_lord"),
          (faction_get_slot, ":faction_leader", ":town_lord_faction", slot_faction_leader),
          (call_script, "script_troop_change_relation_with_troop", ":town_lord", ":faction_leader", -1),
          (val_add, "$total_battle_ally_changes", -1),
        (try_end),
      (else_try),
        (eq, ":new_state", svs_under_siege),
        (party_set_extra_text, ":village_no", "@(Under Siege)"),
        
        #Divert all caravans heading to the center
        #Note that occasionally, no alternative center will be found. In that case, the caravan will try to run the blockade
        (try_for_parties, ":party_no"),
          (gt, ":party_no", "p_spawn_points_end"),
          (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_caravan),
          (party_slot_eq, ":party_no", slot_party_ai_object, ":village_no"),
          
          (party_get_slot, ":origin", ":party_no", slot_party_last_traded_center),
          (store_faction_of_party, ":merchant_faction", ":party_no"),
          
          (call_script, "script_cf_select_most_profitable_town_at_peace_with_faction_in_trade_route", ":origin", ":merchant_faction"),
          (assign, ":target_center", reg0),
          (is_between, ":target_center", centers_begin, centers_end),
          
          (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
          (party_set_ai_object, ":party_no", ":target_center"),
          (party_set_flags, ":party_no", pf_default_behavior, 0),
          (party_set_slot, ":party_no", slot_party_ai_state, spai_trading_with_town),
          (party_set_slot, ":party_no", slot_party_ai_object, ":target_center"),
        (try_end),
      (try_end),
      (party_set_slot, ":village_no", slot_village_state, ":new_state"),
  ]),
  
  
  # script_process_village_raids
  # Input: none
  # Output: none
  # called from triggers every two hours
  ("process_village_raids",
    [
      (try_for_range, ":village_no", villages_begin, villages_end),
        (assign, ":normal_village_icon", "icon_village_a"),
        (assign, ":burnt_village_icon", "icon_village_burnt_a"),
        (assign, ":deserted_village_icon", "icon_village_deserted_a"),
        (party_get_slot, ":village_raid_progress", ":village_no", slot_village_raid_progress),
        (try_begin),
          (party_slot_eq, ":village_no", slot_village_state, 0), #village is normal
          (val_sub, ":village_raid_progress", 5),
          (val_max, ":village_raid_progress", 0),
          (party_set_slot, ":village_no", slot_village_raid_progress, ":village_raid_progress"),
          (try_begin),
            (lt, ":village_raid_progress", 50),
            
            (try_begin),
              (party_get_icon, ":village_icon", ":village_no"),
              (neq, ":village_icon", ":normal_village_icon"), ##CABA FIX
              (party_set_icon, ":village_no", ":normal_village_icon"), ##CABA FIX
            (try_end),
            
            (party_slot_ge, ":village_no", slot_village_smoke_added, 1),
            (party_set_slot, ":village_no", slot_village_smoke_added, 0),
            (party_clear_particle_systems, ":village_no"),
          (try_end),
        (else_try),
          (party_slot_eq, ":village_no", slot_village_state, svs_being_raided), #village is being raided
          #End raid unless there is an enemy party nearby
          (assign, ":raid_ended", 1),
          (party_get_slot, ":looter_party", ":village_no", slot_village_raided_by),
          
          (try_begin),
            (ge, ":looter_party", 0),
            (party_is_active, ":looter_party"),
            (this_or_next|neq, ":looter_party", "p_main_party"),
            (eq, "$g_player_is_captive", 0),
            (store_distance_to_party_from_party, ":distance", ":village_no", ":looter_party"),
            (lt, ":distance", raid_distance),
            (assign, ":raid_ended", 0),
          (try_end),
          
          (try_begin),
            (eq, ":raid_ended", 1),
            (call_script, "script_village_set_state", ":village_no", svs_normal), #clear raid flag
            (party_set_slot, ":village_no", slot_village_smoke_added, 0),
            (party_clear_particle_systems, ":village_no"),
          (else_try),
            (assign, ":raid_progress_increase", 11),
            (try_begin),
              (party_get_skill_level, ":looting_skill", ":looter_party", "skl_looting"),
              (val_add, ":raid_progress_increase", ":looting_skill"),
            (try_end),
            (try_begin),
              (party_slot_eq, ":village_no", slot_center_has_watch_tower, 1),
              (val_mul, ":raid_progress_increase", 2),
              (val_div, ":raid_progress_increase", 3),
            (try_end),
            (val_add, ":village_raid_progress", ":raid_progress_increase"),
            (party_set_slot, ":village_no", slot_village_raid_progress, ":village_raid_progress"),
            (try_begin),
              (ge, ":village_raid_progress", 50),
              (party_slot_eq, ":village_no", slot_village_smoke_added, 0),
              (party_add_particle_system, ":village_no", "psys_map_village_fire"),
              (party_add_particle_system, ":village_no", "psys_map_village_fire_smoke"),
              (party_set_icon, ":village_no", ":burnt_village_icon"), ##CABA FIX
              (party_set_slot, ":village_no", slot_village_smoke_added, 1),
            (try_end),
            (try_begin),
              (gt, ":village_raid_progress", 100),
              (str_store_party_name_link, s1, ":village_no"),
              (party_stack_get_troop_id, ":raid_leader", ":looter_party", 0),
              (ge, ":raid_leader", 0),
              (str_store_party_name, s2, ":looter_party"),
              (store_faction_of_party, ":village_faction", ":village_no"),
              (try_begin),
                (this_or_next|ge, "$vc_show_all_messages", 1),	#VC-2479
                (this_or_next|eq, ":village_faction", "fac_player_supporters_faction"),
                (this_or_next|eq, ":village_faction", "$players_kingdom"),
                (party_slot_eq, ":village_no", slot_town_lord, "trp_player"),
                (faction_get_color, ":faction_color", ":village_faction"),
                (display_log_message, "@The village of {s1} has been looted by {s2}.", ":faction_color"),
              (end_try),
              #(display_log_message, "@The village of {s1} has been looted by {s2}."),
              
              (try_begin),
                (party_get_slot, ":village_lord", ":village_no", slot_town_lord),
                (is_between, ":village_lord", active_npcs_begin, active_npcs_end),
                (call_script, "script_troop_change_relation_with_troop", ":raid_leader", ":village_lord", -5),
                (val_add, "$total_battle_enemy_changes", -5),
              (try_end),
              
              #give loot gold to raid leader
              (troop_get_slot, ":raid_leader_gold", ":raid_leader", slot_troop_wealth),
              (party_get_slot, ":village_prosperity", ":village_no"),
              (store_mul, ":value_of_loot", ":village_prosperity", 60), #average is 3000
              (val_add, ":raid_leader_gold", ":value_of_loot"),
              (troop_set_slot, ":raid_leader", slot_troop_wealth, ":raid_leader_gold"),
              
              #take loot gold from village lord #new 1.126 - JuJu70 seems too harsh why penalize lord if villagers pay up?
              # Since villages are looted on a weekly basis lord have no real income and draining money weekly
              (try_begin),
                (is_between, ":village_lord", active_npcs_begin, active_npcs_end),
                (troop_get_slot, ":village_lord_gold", ":village_lord", slot_troop_wealth),
                (store_div, ":lord_penalty", ":value_of_loot", 5), #JuJu70 changed to 1/5
                (val_sub, ":village_lord_gold", ":lord_penalty"),
                (val_max, ":village_lord_gold", 0),
                (troop_set_slot, ":village_lord", slot_troop_wealth, ":village_lord_gold"),
              (try_end),
              
              (call_script, "script_village_set_state",  ":village_no", svs_looted),
              (party_set_slot, ":village_no", slot_center_accumulated_rents, 0), #new 1.126
              (party_set_slot, ":village_no", slot_center_accumulated_tariffs, 0), #new 1.126
              
              (party_set_slot, ":village_no", slot_village_raid_progress, 0),
              (party_set_slot, ":village_no", slot_village_recover_progress, 0),
              (try_begin),
                (store_faction_of_party, ":village_faction", ":village_no"),
                (this_or_next|party_slot_eq, ":village_no", slot_town_lord, "trp_player"),
                (eq, ":village_faction", "fac_player_supporters_faction"),
                (call_script, "script_add_notification_menu", "mnu_notification_village_raided", ":village_no", ":raid_leader"),
              (try_end),
              (store_faction_of_party, ":looter_faction", ":looter_party"),
              (try_begin),
                (is_between, ":raid_leader", active_npcs_begin, active_npcs_end),
                (call_script, "script_add_log_entry", logent_village_raided, ":raid_leader",  ":village_no",-1, -1),
              (else_try),
                (call_script, "script_add_log_entry", logent_village_raided, ":looter_party",  ":village_no",-1, -1),
              (try_end),
              (try_begin),
                (neq, ":looter_faction", "fac_adventurers"),
                (call_script, "script_faction_inflict_war_damage_on_faction", ":looter_faction", ":village_faction", 5),
              (try_end),
            (try_end),
          (try_end),
        (else_try),
          (party_slot_eq, ":village_no", slot_village_state, svs_looted), #village is looted
          (party_get_slot, ":recover_progress", ":village_no", slot_village_recover_progress),
          (val_add, ":recover_progress", 1),
          (party_set_slot, ":village_no", slot_village_recover_progress, ":recover_progress"), #village looted
          (try_begin),
            (ge, ":recover_progress", 10),
            (party_slot_eq, ":village_no", slot_village_smoke_added, 1),
            (party_clear_particle_systems, ":village_no"),
            (party_add_particle_system, ":village_no", "psys_map_village_looted_smoke"),
            (party_set_slot, ":village_no", slot_village_smoke_added, 2),
          (try_end),
          (try_begin),
            (gt, ":recover_progress", 50),
            (party_slot_eq, ":village_no", slot_village_smoke_added, 2),
            (party_clear_particle_systems, ":village_no"),
            (party_set_slot, ":village_no", slot_village_smoke_added, 3),
            (party_set_icon, ":village_no", ":deserted_village_icon"), ##CABA FIX
          (try_end),
          (try_begin),
            (gt, ":recover_progress", 100),
            (call_script, "script_village_set_state",  ":village_no", svs_normal),#village back to normal
            (party_set_slot, ":village_no", slot_village_recover_progress, 0),
            (party_clear_particle_systems, ":village_no"),
            (party_set_slot, ":village_no", slot_village_smoke_added, 0),
            (party_set_icon, ":village_no", ":normal_village_icon"), ##CABA FIX
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  
  # script_process_sieges
  # Input: center_no
  # Output: none
  #called from triggers
  ("process_sieges",
    [
      (store_script_param, ":center_no", 1),
      
      # (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
      #Reducing siege hardness every day by 20
      (party_get_slot, ":siege_hardness", ":center_no", slot_center_siege_hardness),
      (val_sub, ":siege_hardness", 20),
      (val_max, ":siege_hardness", 0),
      (party_set_slot, ":center_no", slot_center_siege_hardness, ":siege_hardness"),
      
      (try_begin),
        (party_get_slot, ":besieger_party", ":center_no", slot_center_is_besieged_by),
        (ge, ":besieger_party", 0), #town is under siege
        
        #Reduce prosperity of besieged castle/town by -0.33/-4 every day.
        (try_begin),
          (try_begin),
            (is_between, ":center_no", castles_begin, castles_end),
            (store_random_in_range, ":random_value", 0, 3),
            (try_begin),
              (eq, ":random_value", 0),
              (assign, ":daily_siege_effect_on_prosperity", -1),
            (else_try),
              (assign, ":daily_siege_effect_on_prosperity", 0),
            (try_end),
          (else_try),
            (assign, ":daily_siege_effect_on_prosperity", -4),
          (try_end),
          
          (call_script, "script_change_center_prosperity", ":center_no", ":daily_siege_effect_on_prosperity"),
          (val_add, "$newglob_total_prosperity_from_townloot", ":daily_siege_effect_on_prosperity"),
        (try_end),
        
        (store_faction_of_party, ":center_faction", ":center_no"),
        # Lift siege unless there is an enemy party nearby
        (assign, ":siege_lifted", 0),
        (try_begin),
          (try_begin),
            (neg|party_is_active, ":besieger_party"),
            (assign, ":siege_lifted", 1),
          (else_try),
            (store_distance_to_party_from_party, ":besieger_distance", ":center_no", ":besieger_party"),
            (gt, ":besieger_distance", 5),
            (assign, ":siege_lifted", 1),
          (else_try),
            (store_faction_of_party, ":besieger_faction", ":besieger_party"),
            (store_relation, ":reln", ":besieger_faction", ":center_faction"),
            (ge, ":reln", 0),
            (assign, ":siege_lifted", 1),
          (try_end),
          (eq, ":siege_lifted", 1),
          #If another lord can take over the siege, it isn't lifted
          (try_for_range, ":enemy_hero", active_npcs_begin, active_npcs_end),
            (troop_slot_eq, ":enemy_hero", slot_troop_occupation, slto_kingdom_hero),
            (troop_get_slot, ":enemy_party", ":enemy_hero", slot_troop_leaded_party),
            (ge, ":enemy_party", 0),
            (party_is_active, ":enemy_party"),
            (store_faction_of_party, ":party_faction", ":enemy_party"),
            (store_relation, ":reln", ":party_faction", ":center_faction"),
            (lt, ":reln", 0),
            (store_distance_to_party_from_party, ":distance", ":center_no", ":enemy_party"),
            (lt, ":distance", 4),
            (assign, ":besieger_party", ":enemy_party"),
            (party_set_slot, ":center_no", slot_center_is_besieged_by, ":enemy_party"),
            (assign, ":siege_lifted", 0),
            # Chief siege camp around town
            
            # (try_begin),
            # (party_slot_eq, ":center_no",slot_town_port, 1),
            # (party_set_extra_icon, ":center_no", "icon_siege_camp_ports", 0, 0, 0, 0),
            # (else_try),
            # (party_set_extra_icon, ":center_no", "icon_siege_camp", 0, 0, 0, 0),
            # (try_end),
            
            #phaiak begin
            (party_get_icon, ":icon", ":center_no"),
            (try_begin),
              (eq, ":icon", "icon_town_port"),
              (party_set_extra_icon, ":center_no", "icon_siege_camp_ports", 0, 0, 0, 0),
            (else_try),
              (eq, ":icon", "icon_town_walled"),
              (party_set_extra_icon, ":center_no", "icon_siege_camp_town", 0, 0, 0, 0),
            (else_try),
              (eq, ":icon", "icon_fort1"),
              (party_set_extra_icon, ":center_no", "icon_siege_camp_fort", 0, 0, 0, 0),
            (else_try),
              (eq, ":icon", "icon_fort2"),
              (party_set_extra_icon, ":center_no", "icon_siege_camp_fort", 0, 0, 0, 0),
            (end_try),
            #phaiak end
            # chief siege camp acaba
          (try_end),
        (try_end),
        
        #food supply
        (party_get_slot, ":town_food_store", ":center_no", slot_party_food_store),
        (call_script, "script_center_get_food_store_limit", ":center_no"),
        (assign, ":food_store_limit", reg0),
        
        (try_begin),
          (eq, ":siege_lifted", 1),
          (call_script, "script_lift_siege", ":center_no", 1),
        (else_try),
          (call_script, "script_center_get_food_consumption", ":center_no"),
          (assign, ":food_consumption", reg0),
          (val_sub, ":town_food_store", ":food_consumption"), # reduce food only under siege???
          (try_begin),
            (lt, ":town_food_store", 0), #town is starving
            (store_random_in_range, ":r", 0, 100),
            (lt, ":r", 10),
            (call_script, "script_party_wound_all_members", ":center_no"), # town falls with 10% chance if starving
            
          (try_end),
        (try_end),
        # (else_try),	done by village farmers now
        # #town is not under siege...
        # (val_add, ":town_food_store", 30), #add 30 food (significant for castles only.
        # (try_end),
        
        (val_min, ":town_food_store", ":food_store_limit"),
        (val_max, ":town_food_store", 0),
        (party_set_slot, ":center_no", slot_party_food_store, ":town_food_store"),
      (try_end),
  ]),
  
  # script_lift_siege
  # Input: arg1 = center_no, arg2 = display_message
  # Output: none
  #called from triggers
  ("lift_siege",
    [
      (store_script_param, ":center_no", 1),
      (store_script_param, ":display_message", 2),
      (party_set_slot, ":center_no", slot_center_is_besieged_by, -1), #clear siege
      # Chief siege camp around town
      (party_set_extra_icon, ":center_no", 0, 0, 0, 0, 0),
      # chief siege camp acaba
      (call_script, "script_village_set_state",  ":center_no", svs_normal), #clear siege flag
      (try_begin),
        (eq, ":center_no", "$g_player_besiege_town"),
        (assign, "$g_siege_method", 0), #remove siege progress
        
        #siege warfare chief
        (assign, "$g_empieza_asedio", 0),
        (party_set_slot,":center_no",slot_center_blockaded,0),
        (party_set_slot,":center_no",slot_center_blockaded_time,0),
        (party_set_slot,":center_no", slot_center_mantlets_placed, 0),
        (party_set_slot,":center_no",slot_center_ladder_time,0),
        (party_set_slot,":center_no",slot_center_latrines,0),
        (party_set_slot,":center_no",slot_center_infiltration_type,0),
        
        (assign, "$g_siege_saneamiento", 0),
        (assign, "$g_traicion_interna", 0),
        (assign, "$g_infiltracion_interna", 0),
        (assign, "$g_campos_cercanos", 0),
        (assign, "$g_listos_para_asalto", 0),
        (assign, "$g_mantlets_1", 0),
        (assign, "$g_cabezas_dentro", 0), #event
        (assign, "$g_siege_sallied_out_once", 0),
        (assign, "$g_days_spent_starving", 0), #siege warfare, important, we use this in dialogs
        (assign, "$g_next_sally_at", 0), #sally more common siege warfare chief
        #siege warfare acaba
        
      (try_end),
      (try_begin),
        (eq, ":display_message", 1),
        (str_store_party_name_link, s3, ":center_no"),
        (display_message, "@{s3} is no longer under siege."),
      (try_end),
  ]),
  
  
  # script_process_alarms
  # Input: none
  # Output: none
  #called from triggers
  ("process_alarms",
    [
      # (assign, ":current_modula", "$g_alarm_modula"),
      # (val_add, "$g_alarm_modula", 1),
      # (try_begin),
      # (eq, "$g_alarm_modula", 3),
      # (assign, "$g_alarm_modula", 0),
      # (try_end),
      
      # (try_for_range, ":center_no", centers_begin, centers_end),
      # (store_mod, ":center_modula", ":center_no", 3),
      # (eq, ":center_modula", ":current_modula"),
      
      (store_random_in_range, ":center_no", centers_begin, centers_end),
      (party_set_slot, ":center_no", slot_center_last_spotted_enemy, -1),
      (party_set_slot, ":center_no", slot_center_sortie_strength, 0),
      (party_set_slot, ":center_no", slot_center_sortie_enemy_strength, 0),
      
      (assign, ":spotting_range", 3),
      (try_begin),
        (is_currently_night),
        (assign, ":spotting_range", 2),
      (try_end),
      
      (try_begin),
        (party_slot_eq, ":center_no", slot_center_has_watch_tower, 1),
        (val_mul, ":spotting_range", 2),
      (else_try),
        (neg|is_between, ":center_no", villages_begin, villages_end),
        (val_add, ":spotting_range", 1),
        (val_mul, ":spotting_range", 2),
      (try_end),
      
      (store_faction_of_party, ":center_faction", ":center_no"),
      
      (try_for_parties, ":party_no"),
        (this_or_next|party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
        (eq, ":party_no", "p_main_party"),
        
        (store_faction_of_party, ":party_faction", ":party_no"),
        
        (try_begin),
          (eq, ":party_no", "p_main_party"),
          (assign, ":party_faction", "$players_kingdom"),
        (try_end),
        
        (try_begin),
          (eq, ":party_faction", ":center_faction"),
          
          (store_distance_to_party_from_party, ":distance", ":party_no", ":center_no"),
          (le, ":distance", ":spotting_range"),
          
          (party_get_slot, ":cached_strength", ":party_no", slot_party_cached_strength),
          (party_get_slot, ":sortie_strength", ":center_no", slot_center_sortie_strength),
          (val_add, ":sortie_strength", ":cached_strength"),
          (party_set_slot, ":center_no", slot_center_sortie_strength, ":sortie_strength"),
        (else_try),
          (neq, ":party_faction", ":center_faction"),
          
          (store_distance_to_party_from_party, ":distance", ":party_no", ":center_no"),
          
          (try_begin),
            (lt, ":distance", 10),
            (store_current_hours, ":hours"),
            (store_sub, ":faction_recce_slot", ":party_faction", kingdoms_begin),
            (val_add, ":faction_recce_slot", slot_center_last_reconnoitered_by_faction_time),
            (ge, ":faction_recce_slot", slot_center_last_reconnoitered_by_faction_time),	# VC-2361
            (party_set_slot, ":center_no", ":faction_recce_slot", ":hours"),
            
            #(eq, "$cheat_mode", 1),
            #(str_store_faction_name, s4, ":party_faction"),
            #(str_store_party_name, s5, ":center_no"),
            #(display_message, "@{!}DEBUG -- {s4} reconnoiters {s5}"),
          (try_end),
          
          (store_relation, ":reln", ":center_faction", ":party_faction"),
          (lt, ":reln", 0),
          (try_begin),
            (le, ":distance", ":spotting_range"),
            
            (party_get_slot, ":cached_strength", ":party_no", slot_party_cached_strength),
            (party_get_slot, ":enemy_strength", ":center_no", slot_center_sortie_enemy_strength),
            (val_add, ":enemy_strength", ":cached_strength"),
            (party_set_slot, ":center_no", slot_center_sortie_enemy_strength, ":enemy_strength"),
            (party_set_slot, ":center_no", slot_center_last_spotted_enemy, ":party_no"),
          (try_end),
          
        (try_end),
      (try_end),
      # (try_end),
      
      # (try_for_range, ":center_no", centers_begin, centers_end),	why not do for the one just calculated?
      # (store_mod, ":center_modula", ":center_no", 3),
      # (eq, ":center_modula", ":current_modula"),
      
      (try_begin), #eligible units sortie out of castle
        (is_between, ":center_no", walled_centers_begin, walled_centers_end),
        (party_slot_ge, ":center_no", slot_center_last_spotted_enemy, 0),
        
        (party_get_slot, ":sortie_strength", ":center_no", slot_center_sortie_strength),
        (party_get_slot, ":enemy_strength", ":center_no", slot_center_sortie_enemy_strength),
        
        #Below two lines are new added by ozan. While AI want to drive nearby besieging enemy parties by making sortie them, they give up current battle if they are already joining one.
        #Lets assume there is a battle inside the castle, because enemies are inside castle and they are so close to castle they will be also added to slot_center_sortie_enemy_strength
        #But in this scenario, they are not outside the castle, so searching/patrolling enemy outside the castle is useless at this point.
        #So if there is already a battle inside the center, do not sortie and search enemy outside.
        (party_get_battle_opponent, ":center_battle_opponent", ":center_no"),
        (try_begin),
          (ge, "$cheat_mode", 1),
          (ge, ":center_battle_opponent", 0),
          (str_store_party_name, s7, ":center_no"),
          (str_store_party_name, s6, ":center_battle_opponent"),
          (display_message, "@{!}DEBUG : There are already enemies ({s6}) inside {s7}."),
        (try_end),
        (lt, ":center_battle_opponent", 0),
        #New added by ozan ended.
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (str_store_party_name, s4, ":center_no"),
          (assign, reg3, ":sortie_strength"),
          (assign, reg4, ":enemy_strength"),
          (display_message, "@{!}DEBUG -- Calculating_sortie for {s4} strength of {reg3} vs {reg4} enemies"),
        (try_end),
        
        (store_mul, ":enemy_strength_mul_14_div_10", ":enemy_strength", 14),
        (val_div, ":enemy_strength_mul_14_div_10", 10),
        (gt, ":sortie_strength", ":enemy_strength_mul_14_div_10"),
        
        (assign, ":at_least_one_party_sorties", 0),
        (try_for_parties, ":sortie_party"),
          (party_get_attached_to, ":town", ":sortie_party"),
          (eq, ":town", ":center_no"),
          
          (party_slot_eq, ":sortie_party", slot_party_type, spt_kingdom_hero_party),
          
          (party_get_slot, ":cached_strength", ":sortie_party", slot_party_cached_strength),
          (ge, ":cached_strength", 100),
          
          (party_detach, ":sortie_party"),
          (call_script, "script_party_set_ai_state", ":sortie_party",  spai_patrolling_around_center, ":center_no"),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_party_name, s4, ":sortie_party"),
            (display_message, "str_s4_sorties"),
          (try_end),
          
          (eq, ":at_least_one_party_sorties", 0),
          (assign, ":at_least_one_party_sorties", ":sortie_party"),
        (try_end),
        
        (try_begin),
          (party_is_in_town, "p_main_party", ":center_no"),
          (eq, "$g_player_is_captive", 0),
          (gt, ":at_least_one_party_sorties", 0),
          (call_script, "script_add_notification_menu", "mnu_notification_sortie_possible", ":center_no", ":sortie_party"),
        (try_end),
      (try_end),
      
      (store_faction_of_party, ":center_faction", ":center_no"),
      
      #Send message
      (try_begin),
        (this_or_next|eq, "$cheat_mode", 1), #this is message
        (this_or_next|party_slot_eq, ":center_no", slot_town_lord, "trp_player"),
        (eq, ":center_faction", "$players_kingdom"),
        
        (party_get_slot, ":enemy_party", ":center_no", slot_center_last_spotted_enemy),
        (ge, ":enemy_party", 0),
        (store_distance_to_party_from_party, ":dist", "p_main_party", ":center_no"),
        (assign, ":has_messenger", 0),
        (try_begin),
          (this_or_next|party_slot_eq, ":center_no", slot_town_lord, "trp_player"),
          (eq, ":center_faction", "fac_player_supporters_faction"),
          (party_slot_eq, ":center_no", slot_center_has_messenger_post, 1),
          (assign, ":has_messenger", 1),
        (try_end),
        
        (this_or_next|eq, "$cheat_mode", 1),
        (this_or_next|lt, ":dist", 30),
        (eq, ":has_messenger", 1),
        
        (str_store_party_name_link, s1, ":center_no"),
        (party_get_slot, ":exact_enemy_strength", ":center_no", slot_center_sortie_enemy_strength),
        
        (try_begin), #moto chief cambia
          (lt, ":exact_enemy_strength", 500),
          (display_message, "@Small bands of enemies have been spotted near {s1}.", color_bad_news),
        (else_try),
          (lt, ":exact_enemy_strength", 1000),
          (display_message, "@Enemy patrols have been spotted near {s1}.", color_bad_news),
        (else_try),
          (lt, ":exact_enemy_strength", 2000),
          (display_message, "@A medium-sized group of enemies has been spotted near {s1}.", color_bad_news),
        (else_try),
          (lt, ":exact_enemy_strength", 4000),
          (display_message, "@A significant group of enemies has been spotted near {s1}.", color_bad_news),
        (else_try),
          (lt, ":exact_enemy_strength", 8000),
          (display_message, "@An army of enemies has been spotted near {s1}.", color_bad_news),
        (else_try),
          (lt, ":exact_enemy_strength", 16000),
          (display_message, "@A large army of enemies has been spotted near {s1}.", color_bad_news),
        (else_try),
          (display_message, "@A great host of enemies has been spotted near {s1}.", color_bad_news),
        (try_end),
        #maybe do audio sound?
        
      (try_end),
  ]),
  
  # # script_allow_vassals_to_join_indoor_battle
  # # Input: none
  # # Output: none
  # ("allow_vassals_to_join_indoor_battle",
  # [
  # #if our commander attacks an enemy army
  # (try_for_range, ":troop_no", active_npcs_begin, active_npcs_end),
  # (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
  # (neg|troop_slot_ge, ":troop_no", slot_troop_prisoner_of_party, 0),
  # (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
  # (gt, ":party_no", 0),
  # (party_is_active, ":party_no"),
  
  # (party_get_attached_to, ":party_is_attached_to", ":party_no"),
  # (lt, ":party_is_attached_to", 0),
  
  # (store_troop_faction, ":faction_no", ":troop_no"),
  
  # (try_begin),
  # #(faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemies_around_center),
  # (party_slot_eq, ":party_no", slot_party_ai_state, spai_accompanying_army),
  # (party_get_slot, ":commander_party", ":party_no", slot_party_ai_object),
  # (gt, ":commander_party", 0),
  # (party_is_active, ":commander_party"),
  
  # (assign, ":besieged_center", -1),
  # (try_begin),
  # (party_slot_eq, ":commander_party", slot_party_ai_state, spai_holding_center), #if commander is holding a center
  # (party_get_slot, ":commander_object", ":commander_party", slot_party_ai_object), #get commander's ai object (center they are holding)
  # (party_get_battle_opponent, ":besieger_enemy", ":commander_object"), #get this object's battle opponent
  # (party_is_active, ":besieger_enemy"),
  # (assign, ":besieged_center", ":commander_object"),
  # (assign, ":commander_object", ":besieger_enemy"),
  # (else_try),
  # (party_slot_eq, ":commander_party", slot_party_ai_state, spai_engaging_army), #if commander is engaging an army
  # (party_get_slot, ":commander_object", ":commander_party", slot_party_ai_object), #get commander's ai object (army which they engaded)
  # (ge, ":commander_object", 0), #if commander has an object
  # (neg|is_between, ":commander_object", centers_begin, centers_end), #if this object is not a center, so it is a party
  # (party_is_active, ":commander_object"),
  # (party_get_battle_opponent, ":besieged_center", ":commander_object"), #get this object's battle opponent
  # (else_try),
  # (assign, ":besieged_center", -1),
  # (try_end),
  
  # (is_between, ":besieged_center", walled_centers_begin, walled_centers_end), #if battle opponent of our commander's ai object is a walled center
  
  # (party_get_attached_to, ":attached_to_party", ":commander_party"), #if commander is attached to besieged center already.
  # (eq, ":attached_to_party", ":besieged_center"),
  
  # (store_faction_of_party, ":besieged_center_faction", ":besieged_center"),#get (battle opponent of our commander's ai object)'s faction
  # (eq, ":besieged_center_faction", ":faction_no"), #if battle opponent of our commander's ai object is from same faction with current party
  # (party_is_active, ":commander_object"),
  # #make also follow_or_not check if needed
  
  # (call_script, "script_party_set_ai_state", ":party_no", spai_engaging_army, ":commander_object"), #go and help commander
  
  # (try_begin),
  # (eq, "$cheat_mode", 1),
  # (str_store_party_name, s7, ":party_no"),
  # (str_store_party_name, s6, ":commander_object"),
  # (display_message, "@{!}DEBUG : {s7} is helping his commander by fighting with {s6}."),
  # (try_end),
  # (else_try),
  # #(faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_center),
  
  # (party_slot_eq, ":party_no", slot_party_ai_state, spai_accompanying_army),
  # (party_get_slot, ":commander_party", ":party_no", slot_party_ai_object),
  # (gt, ":commander_party", 0),
  # (party_is_active, ":commander_party"),
  
  # (party_get_battle_opponent, ":besieged_center", ":commander_party"), #get this object's battle opponent
  
  # #make also follow_or_not check if needed
  
  # (is_between, ":besieged_center", walled_centers_begin, walled_centers_end), #if this object is a center
  # (party_get_attached_to, ":attached_to_party", ":party_no"),
  # (neq, ":attached_to_party", ":besieged_center"),
  # (party_is_active, ":besieged_center"),
  
  # (call_script, "script_party_set_ai_state", ":party_no", spai_engaging_army, ":besieged_center"), #go and help commander
  
  # #(try_begin),
  # #  (eq, "$cheat_mode", 1),
  # #  (str_store_party_name, s7, ":party_no"),
  # #  (str_store_party_name, s6, ":besieged_center"),
  # #  (display_message, "@{!}DEBUG : {s7} is helping his commander by attacking {s6}."),
  # #(try_end),
  
  # #(party_set_ai_behavior, ":party_no", ai_bhvr_attack_party),
  # #(party_set_ai_object, ":party_no", ":besieged_center"),
  # #(party_set_flags, ":party_no", pf_default_behavior, 1), #is these needed?
  # #(party_set_slot, ":party_no", slot_party_ai_substate, 1), #is these needed?
  # (try_end),
  # (try_end),
  # ]),
  
  # script_party_set_ai_state
  # Input: arg1 = party_no, arg2 = new_ai_state, arg3 = action_object (if necessary)
  # Output: none (Can fail)
  
  #Redone somewhat on Feb 18 to make sure that initative is set properly
  
  ("party_set_ai_state",
    [
      (store_script_param, ":party_no", 1),
      (store_script_param, ":new_ai_state", 2),
      (store_script_param, ":new_ai_object", 3),
      
      #dunno where these invalid parties are coming from, but this will prevent error
      (try_begin),
        (ge, ":new_ai_object", 0),
        (neg|party_is_active, ":new_ai_object"),
        (neg|is_between, ":new_ai_object", spawn_points_begin, spawn_points_end), #cases where parties DO interact with inactive parties
        (neg|party_slot_eq, ":new_ai_object", slot_party_type, spt_bandit_lair),
        (assign, ":new_ai_object", -1),
        (assign, ":new_ai_state", spai_undefined),
      (try_end),
      
      (party_get_slot, ":old_ai_state", ":party_no", slot_party_ai_state),
      (party_get_slot, ":old_ai_object", ":party_no", slot_party_ai_object),
      (party_get_attached_to, ":attached_to_party", ":party_no"),
      (assign, ":party_is_in_town", 0),
      (try_begin),
        (is_between, ":attached_to_party", centers_begin, centers_end),
        (assign, ":party_is_in_town", ":attached_to_party"),
        #Fix for VC-1904 and VC-1931:
        (party_slot_eq, ":party_no", slot_party_on_water, 1),
        (party_set_slot, ":party_no", slot_party_on_water, 0),
        (party_set_flags, ":party_no", pf_is_ship, 0),
        (call_script, "script_update_party_icon", ":party_no"),
        
        (gt, ":party_no", "p_spawn_points_end"),#VC-3204
        (party_set_slot, ":party_no", slot_party_ai_state_backup, -1),
        (party_set_slot, ":party_no", slot_party_ai_object_backup, -1),
      (try_end),
      
      (assign, ":commander", -1),
      (try_begin),
        (party_is_active, ":party_no"),
        (party_stack_get_troop_id, ":commander", ":party_no", 0),
        (store_faction_of_party, ":faction_no", ":party_no"),
      (try_end),
      
      (try_begin),
        (lt, ":commander", 0),
        #sometimes 0 sized parties enter "party_set_ai_state" script. So only discard them
        #(try_begin),
        #  (eq, "$cheat_mode", 1),
        #  (str_store_troop_name, s6, ":party_no"),
        #  (party_get_num_companions, reg6, ":party_no"),
        #  (display_message, "@{!}DEBUGS : party name is : {s6}, party size is : {reg6}, new ai discarded."),
        #(try_end),
      (else_try),
        #Party does any business in town
        (try_begin),
          (is_between, ":party_is_in_town", walled_centers_begin, walled_centers_end),
          (party_slot_eq, ":party_is_in_town", slot_center_is_besieged_by, -1),
          (call_script, "script_troop_does_business_in_center", ":commander", ":party_is_in_town"),
        (else_try),
          (party_slot_eq, ":party_no", slot_party_ai_state, spai_visiting_village),
          (party_get_slot, ":party_is_in_village", ":party_no", slot_party_ai_object),
          (is_between, ":party_is_in_village", villages_begin, villages_end),
          #(party_slot_eq, ":party_is_in_village", slot_center_is_looted_by, -1),
          (neg|party_slot_eq, ":party_is_in_village", slot_village_state, svs_being_raided),
          (neg|party_slot_eq, ":party_is_in_village", slot_village_state, svs_looted),
          (store_distance_to_party_from_party, ":distance", ":party_no", ":party_is_in_village"),
          (lt, ":distance", 3),
          (call_script, "script_troop_does_business_in_center", ":commander", ":party_is_in_village"),
        (try_end),
        
        (party_set_slot, ":party_no", slot_party_follow_me, 0),
        
        (try_begin),
          (eq, ":old_ai_state", ":new_ai_state"),
          (eq, ":old_ai_object", ":new_ai_object"),
          (party_slot_eq, ":party_no", slot_party_ai_substate, 0),	#capture substate reset
          #do nothing. Nothing is changed.
        (else_try),
          #phaiak begin
          #Changes for the storyline:
          (eq, "$campaign_type", camp_storyline),
          (troop_get_slot, ":leaded_party", "trp_knight_4_3", slot_troop_leaded_party),
          (eq, ":party_no", ":leaded_party"),
          (assign, ":hold", 0),
          (try_for_range, ":quest_no", "qst_kennemer_jarl", "qst_notes_companions"),
            (check_quest_active, ":quest_no"),
            (assign, ":hold", 1),
          (end_try),
          (try_begin),
            (check_quest_active, "qst_collect_men"),		# ("collect_men", "The Revenge: Visit Doccinga", 0,
            (assign, ":hold", 1),
          (end_try),
          (eq, ":hold", 1),
          (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
          (party_set_ai_object, ":party_no", "p_castle_63"),	# Kennemer
          (party_set_flags, ":party_no", pf_default_behavior, 0),
          
          (party_set_slot, ":party_no", slot_party_ai_state, spai_undefined),
          (party_set_slot, ":party_no", slot_party_ai_object, "p_castle_63"),
          
        (else_try),
          #Changes for making parties landing
          (party_slot_eq, ":party_no", slot_party_on_water, 1),
          #(get_party_ai_behavior, ":ai_behavior", ":party_no"),
          (try_begin),
            #scenario 1: ai_object = port party
            (call_script, "script_cf_is_port_party", ":new_ai_object"),
            (assign, ":block", 1),
          (else_try),
            #scenario 2: ai_object on land
            (neq, ":new_ai_object", -1),
            (party_is_active, ":new_ai_object"),
            (neq, ":new_ai_state", spai_visiting_village),			#because this means ai_bhvr_travel_to_point
            (neq, ":new_ai_state", spai_patrolling_around_center),	#because this means ai_bhvr_travel_to_point
            (neq, ":new_ai_state", spai_besieging_center),			#because this means ai_bhvr_travel_to_point
            (party_slot_eq, ":new_ai_object", slot_party_on_water, 0),
            (assign, ":block", 0),
            (call_script, "script_get_closest_port", ":new_ai_object"),
            (party_get_slot, ":port_party", reg0, slot_party_port_party),
          (else_try),
            #scenario 3: target_position on land (this could cause lag)
            (this_or_next|eq, ":new_ai_state", spai_visiting_village),			#because this means ai_bhvr_travel_to_point
            (this_or_next|eq, ":new_ai_state", spai_patrolling_around_center),	#because this means ai_bhvr_travel_to_point
            (			  eq, ":new_ai_state", spai_besieging_center),			#because this means ai_bhvr_travel_to_point
            
            (party_get_ai_target_position, pos1, ":party_no"),
            (party_set_position, "p_temp_party", pos1),
            (party_get_current_terrain, ":terrain_type", "p_temp_party"),
            (neq, ":terrain_type", 0),
            (neq, ":terrain_type", 7),
            (neq, ":terrain_type", 8),
            (assign, ":block", 0),
            (call_script, "script_get_closest_port", "p_temp_party"),
            (party_get_slot, ":port_party", reg0, slot_party_port_party),
          (else_try),
            #scenario 4: rest
            (assign, ":block", 1),
          (end_try),
          (eq, ":block", 0),
          
          (try_begin),
            (gt, ":party_no", "p_spawn_points_end"),#VC-3204
            (party_set_slot, ":party_no", slot_party_ai_state_backup, ":new_ai_state"),
            (party_set_slot, ":party_no", slot_party_ai_object_backup, ":new_ai_object"),
          (end_try),
          
          # (party_get_slot, ":port_party", ":closest_town", slot_party_port_party),
          (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
          (party_set_ai_object, ":party_no", ":port_party"),
          ### ????:
          # (party_set_slot, ":party_no", slot_party_ai_state, 0),	# outcommented to try fixing VC-1824
          (party_set_slot, ":party_no", slot_party_ai_object, ":port_party"),
        (else_try),
          (party_slot_eq, ":party_no", slot_party_on_water, 0),
          (eq, ":new_ai_state", spai_accompanying_army),
          (party_get_slot, ":ai_object", ":party_no", slot_party_ai_object),
          (gt, ":ai_object", -1),
          (party_is_active, ":ai_object"),
          (party_slot_eq, ":ai_object", slot_party_on_water, 1),
          (try_begin),
            (gt, ":party_no", "p_spawn_points_end"),#VC-3204
            (party_set_slot, ":party_no", slot_party_ai_state_backup, ":new_ai_state"),
            (party_set_slot, ":party_no", slot_party_ai_object_backup, ":new_ai_object"),
          (end_try),
          #
          (call_script, "script_get_closest_port", ":party_no"),
          (party_set_slot, ":party_no", slot_party_ai_embarking_port, reg0),
          (party_get_position, pos1, reg0),
          (party_set_ai_target_position, ":party_no", pos1),
          (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_point),
        (else_try),
          #phaiak end
          (assign, ":initiative", 80),	#MOTO chief change to 80 from 100 - not at peak mobilization
          (assign, ":aggressiveness", 5),	#MOTO change to average from 8
          (assign, ":courage", 5),	#MOTO change to average from 8
          
          (try_begin),
            (this_or_next|eq, ":new_ai_state", spai_accompanying_army),
            (eq, ":new_ai_state", spai_screening_army),
            (party_is_active, ":new_ai_object"),
            (party_set_ai_behavior, ":party_no", ai_bhvr_escort_party),
            (party_set_ai_object, ":party_no", ":new_ai_object"),
            (party_set_flags, ":party_no", pf_default_behavior, 0),
            
            (try_begin),
              (gt, ":party_is_in_town", 0),
              (party_detach, ":party_no"),
            (try_end),
            
            (try_begin),
              (eq, ":new_ai_state", spai_screening_army),
              (assign, ":aggressiveness", 8),	#MOTO chief drop to 8 (maximum) from 9
              (assign, ":courage", 8),	#MOTO drop to 8 (maximum) from 9
              (assign, ":initiative", 100),	#MOTO change to 100 from 80, army is in high state of mobilization
              (assign, ":new_ai_state", spai_accompanying_army),	#MOTO use substate to represent screening properly
              (party_set_slot, ":party_no", slot_party_ai_substate, 1),
            (else_try),
              (assign, ":aggressiveness", 3),	#MOTO drop to 3 from 6
              (assign, ":courage", 8),	#MOTO drop to 8 (maximum) from 9
              (assign, ":initiative", 50),	#MOTO increase to average from 10 (not really more tied down than in siege)
              (party_set_slot, ":party_no", slot_party_ai_substate, 0),
            (try_end),
          (else_try),
            (eq, ":new_ai_state", spai_besieging_center),
            
            (party_get_position, pos1, ":new_ai_object"),
            (map_get_random_position_around_position, pos2, pos1, 2),
            (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_point),
            (party_set_ai_target_position, ":party_no", pos2),
            (party_set_ai_object, ":party_no", ":new_ai_object"),
            (party_set_flags, ":party_no", pf_default_behavior, 0),
            (party_set_slot, ":party_no", slot_party_follow_me, 1),
            (party_set_slot, ":party_no", slot_party_ai_substate, 0),
            
            (try_begin),
              (gt, ":party_is_in_town", 0),
              (neq, ":party_is_in_town", ":new_ai_object"),
              (party_detach, ":party_no"),
            (try_end),
            
            (assign, ":aggressiveness", 3),	#MOTO chief raise to 3 from 1 for travel
            (assign, ":courage", 5),	#MOTO drop to average from 9 - in enemy territory
            (assign, ":initiative", 100),
            #(assign, ":initiative", 100),
          (else_try),
            (eq, ":new_ai_state", spai_holding_center),
            
            (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
            (party_set_ai_object, ":party_no", ":new_ai_object"),
            (party_set_flags, ":party_no", pf_default_behavior, 0),
            
            (try_begin),
              (gt, ":party_is_in_town", 0),
              (neq, ":party_is_in_town", ":new_ai_object"),
              (party_detach, ":party_no"),
            (try_end),
            #moto chief changes de aqui a final de script
            (assign, ":aggressiveness", 3),	#MOTO decrease to 3 from 7 - focused on getting home
            (assign, ":courage", 8),	#MOTO decrease to 8 (maximum) from 9
            (assign, ":initiative", 80),	#MOTO decrease to 80 from 100 - not at peak mobilization
          (else_try),
            (eq, ":new_ai_state", spai_patrolling_around_center),
            
            #MOTO move this functionality from npc_decision_checklist_party_ai to generalize
            (try_begin),
              (neg|is_between, ":new_ai_object", villages_begin, villages_end),
              (party_get_battle_opponent, ":besieger_party", ":new_ai_object"),
            (else_try),
              (party_slot_eq, ":new_ai_object", slot_village_state, svs_being_raided),
              (party_get_slot, ":besieger_party", ":new_ai_object", slot_village_raided_by),
            (else_try),
              (assign, ":besieger_party", -1),
            (try_end),
            
            (try_begin),
              (gt, ":besieger_party", 0),
              (party_is_active, ":besieger_party"),
              
              (assign, ":new_ai_state", spai_engaging_army),
              (assign, ":new_ai_object", ":besieger_party"),
            (try_end),
            
            (eq, ":new_ai_state", spai_patrolling_around_center),
            #MOTO end move this functionality from npc_decision_checklist_party_ai to generalize
            
            (party_get_position, pos1, ":new_ai_object"),
            (map_get_random_position_around_position, pos2, pos1, 1),
            (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_point),
            (party_set_ai_target_position, ":party_no", pos2),
            (party_set_ai_object, ":party_no", ":new_ai_object"),
            
            #MOTO do in process_hero_ai
            # (try_begin),
            # (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemies_around_center),
            # (party_set_ai_patrol_radius, ":party_no", 1), #line 100
            # (else_try),
            # (party_set_ai_patrol_radius, ":party_no", 5), #line 100
            # (try_end),
            #MOTO end do in process_hero_ai
            
            (party_set_flags, ":party_no", pf_default_behavior, 0),
            (party_set_slot, ":party_no", slot_party_follow_me, 1),
            (party_set_slot, ":party_no", slot_party_ai_substate, 0),
            
            (try_begin),
              (gt, ":party_is_in_town", 0),
              (party_detach, ":party_no"),
            (try_end),
            
            # (try_begin),	MOTO move this part after section to apply to ALL defensive actions
            # #new to avoid losing time of marshal with attacking unimportant targets while there is a threat in our centers.
            # (ge, ":commander", 0),
            # (faction_slot_eq, ":faction_no", slot_faction_marshal, ":commander"),
            # (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemies_around_center),
            
            # (party_get_position, pos3, ":party_no"),
            # (get_distance_between_positions, ":distance_to_center", pos1, pos3),
            
            # (try_begin),
            # (ge, ":distance_to_center", 800), #added new (1.122)
            # (assign, ":initiative", 10),
            # (assign, ":aggressiveness", 1),
            # (assign, ":courage", 8),
            # (else_try), #below added new (1.122)
            # (assign, ":initiative", 100),
            # (assign, ":aggressiveness", 8),
            # (assign, ":courage", 8),
            # (try_end),
            # (else_try),
            (assign, ":aggressiveness", 3),	#MOTO set to 3 from 8 for travel
            (assign, ":courage", 8),
            (assign, ":initiative", 80),		#MOTO change to 80 from 100
            # (try_end),
          (else_try),
            (eq, ":new_ai_state", spai_visiting_village),
            (party_get_position, pos1, ":new_ai_object"),
            (map_get_random_position_around_position, pos2, pos1, 2),
            (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_point),
            (party_set_ai_target_position, ":party_no", pos2),
            (party_set_ai_object, ":party_no", ":new_ai_object"),
            (party_set_flags, ":party_no", pf_default_behavior, 0),
            (party_set_slot, ":party_no", slot_party_ai_substate, 0),
            (try_begin),
              (gt, ":party_is_in_town", 0),
              (neq, ":party_is_in_town", ":new_ai_object"),
              (party_detach, ":party_no"),
            (try_end),
            
            (assign, ":aggressiveness", 3),	#MOTO set to 3 from 8 for travel
            (assign, ":courage", 8),
            (assign, ":initiative", 80),	#MOTO set to 80 from 100
          (else_try), #0.660: this is where the 1625/1640 bugs happen with an improper ai_object
            (eq, ":new_ai_state", spai_raiding_around_center),
            (party_get_position, pos1, ":new_ai_object"),
            (map_get_random_position_around_position, pos2, pos1, 1),
            (party_set_ai_behavior, ":party_no", ai_bhvr_patrol_location),
            (party_set_ai_patrol_radius, ":party_no", 2),    #MOTO double new defensive patrol radius
            (party_set_ai_target_position, ":party_no", pos2),
            (party_set_ai_object, ":party_no", ":new_ai_object"),
            (party_set_flags, ":party_no", pf_default_behavior, 0),
            (party_set_slot, ":party_no", slot_party_follow_me, 1),
            (party_set_slot, ":party_no", slot_party_ai_substate, 0),
            (try_begin),
              (gt, ":party_is_in_town", 0),
              (neq, ":party_is_in_town", ":new_ai_object"),
              (party_detach, ":party_no"),
            (try_end),
            
            # (try_begin),	MOTO not an emergency
            # (ge, ":commander", 0),
            # (faction_slot_eq, ":faction_no", slot_faction_marshal, ":commander"),
            # (assign, ":aggressiveness", 1),
            # (assign, ":courage", 8),
            # (assign, ":initiative", 20),
            # (else_try),
            (assign, ":aggressiveness", 7),
            (assign, ":courage", 5),	#MOTO set to average from 8 - in enemy territory
            (assign, ":initiative", 100),
            # (try_end),
          (else_try),
            (eq, ":new_ai_state", spai_engaging_army),
            
            (party_set_ai_behavior, ":party_no", ai_bhvr_attack_party),
            (party_set_ai_object, ":party_no", ":new_ai_object"),
            (party_set_flags, ":party_no", pf_default_behavior, 0),
            (try_begin),
              (gt, ":party_is_in_town", 0),
              (party_detach, ":party_no"),
            (try_end),
            
            # (try_begin),
            # #new to avoid losing time of marshal with attacking unimportant targets while there is a threat in our centers.
            # (ge, ":commander", 0),
            # (faction_slot_eq, ":faction_no", slot_faction_marshal, ":commander"),
            # (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemies_around_center),
            # (assign, ":initiative", 10),
            # (assign, ":aggressiveness", 1),
            # (assign, ":courage", 8),
            # (else_try),
            (assign, ":aggressiveness", 8),
            (assign, ":courage", 5),	#MOTO drop to 5 from 8 - in enemy territory
            (assign, ":initiative", 100),
            # (try_end),
          (else_try),
            (eq, ":new_ai_state", spai_retreating_to_center),
            (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
            (party_set_ai_object, ":party_no", ":new_ai_object"),
            (party_set_flags, ":party_no", pf_default_behavior, 1),
            (party_set_slot, ":party_no", slot_party_commander_party, -1),
            (try_begin),
              (gt, ":party_is_in_town", 0),
              (neq, ":party_is_in_town", ":new_ai_object"),
              (party_detach, ":party_no"),
            (try_end),
            
            (assign, ":aggressiveness", 3),
            (assign, ":courage", 4),
            (assign, ":initiative", 80),	#MOTO change to 80 from 100, army is in high state of mobilization but scrambling
          (else_try),
            (eq, ":new_ai_state", spai_undefined),
            (party_set_ai_behavior, ":party_no", ai_bhvr_hold),
            (party_set_flags, ":party_no", pf_default_behavior, 0),
          (try_end),
          
          #MOTO defensive emergency
          #moved from 1.122 mods above and modified
          (try_begin),
            #new to avoid losing time of marshal with attacking unimportant targets while there is a threat in our centers.
            # (ge, ":commander", 0),	MOTO everyone hurry to defense!
            # (faction_slot_eq, ":faction_no", slot_faction_marshal, ":commander"),
            (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemies_around_center),
            
            (this_or_next|eq, ":new_ai_state", spai_holding_center),
            (this_or_next|eq, ":new_ai_state", spai_engaging_army),
            (eq, ":new_ai_state", spai_patrolling_around_center),
            
            (party_get_position, pos1, ":new_ai_object"),
            (party_get_position, pos3, ":party_no"),
            (get_distance_between_positions, ":distance", pos1, pos3),
            
            (try_begin),
              # (ge, ":distance_to_center", 800), #added new (1.122)
              (ge, ":distance", 1500),    #1.5 x patrol radius, see script_process_hero_ai below
              (assign, ":initiative", 80),	#MOTO change to 80 from 10, army is in high state of mobilization but scrambling
              (assign, ":aggressiveness", 1),
              (assign, ":courage", 8),
            (else_try), #below added new (1.122)
              (assign, ":initiative", 100),
              (assign, ":aggressiveness", 8),
              (assign, ":courage", 8),
            (try_end),
          (try_end),
          #MOTO end defensive emergency
          
          (try_begin),
            (troop_slot_eq, ":commander", slot_lord_reputation_type, lrep_martial),
            (val_add, ":aggressiveness", 2),
            (val_add, ":courage", 2),
          (else_try),
            (this_or_next|troop_slot_eq, ":commander", slot_lord_reputation_type, lrep_selfrighteous),
            (troop_slot_eq, ":commander", slot_lord_reputation_type, lrep_roguish),
            (val_add, ":aggressiveness", 1),
            (val_add, ":courage", 1),
          (else_try),
            (troop_slot_eq, ":commander", slot_lord_reputation_type, lrep_debauched),
            (val_sub, ":aggressiveness", 1),
            (val_sub, ":courage", 1),
          (try_end),
          
          (party_set_slot, ":party_no", slot_party_ai_state, ":new_ai_state"),
          (party_set_slot, ":party_no", slot_party_ai_object, ":new_ai_object"),
          (party_set_aggressiveness, ":party_no", ":aggressiveness"),
          (party_set_courage, ":party_no", ":courage"),
          (party_set_ai_initiative, ":party_no", ":initiative"),
        (try_end),
      (try_end),
      
      #Helpfulness
      (try_begin),
        (ge, ":commander", 0),
        
        (party_set_helpfulness, ":party_no", 101),
        (try_begin),
          (troop_slot_eq, ":commander", slot_lord_reputation_type, lrep_martial),
          (party_set_helpfulness, ":party_no", 200),
        (else_try),
          (troop_slot_eq, ":commander", slot_lord_reputation_type, lrep_upstanding),
          (party_set_helpfulness, ":party_no", 150),
        (else_try),
          (party_slot_eq, ":party_no", slot_party_ai_state, spai_accompanying_army),
          (party_set_helpfulness, ":party_no", 110),
        (else_try),
          (this_or_next|troop_slot_eq, ":commander", slot_lord_reputation_type, lrep_quarrelsome),
          (troop_slot_eq, ":commander", slot_lord_reputation_type, lrep_roguish),
          (party_set_helpfulness, ":party_no", 90),
        (else_try),
          (troop_slot_eq, ":commander", slot_lord_reputation_type, lrep_selfrighteous),
          (party_set_helpfulness, ":party_no", 80),
        (else_try),
          #(troop_slot_eq, ":commander", slot_lord_reputation_type, lrep_debauched),
          (party_set_helpfulness, ":party_no", 50),
        (try_end),
      (try_end),
  ]),
  
  ("cf_party_under_player_suggestion",
    [
      (store_script_param, ":party_no", 1),
      
      (party_slot_eq, ":party_no", slot_party_following_orders_of_troop, "trp_kingdom_heroes_including_player_begin"),
      
      (party_get_slot, ":ai_state", ":party_no", slot_party_ai_state),
      (party_slot_eq, ":party_no", slot_party_orders_type, ":ai_state"),
      
      (party_get_slot, ":ai_object", ":party_no", slot_party_ai_object),
      (party_slot_eq, ":party_no", slot_party_orders_object, ":ai_object"),
      
      (store_current_hours, ":hours_since_orders_given"),
      (party_get_slot, ":orders_time", ":party_no", slot_party_orders_time),
      
      (val_sub, ":hours_since_orders_given", ":orders_time"),
      (lt, ":hours_since_orders_given", 36),
  ]),
  
  #Currently called from process_ai_state, could be called from elsewhere
  #It is used for lord to (1)Court ladies (2)Collect rents (3)Look for volunteers
  ("troop_does_business_in_center",
    [
      (store_script_param, ":troop_no", 1),
      (store_script_param, ":center_no", 2),
      
      (troop_get_slot, ":led_party", ":troop_no", slot_troop_leaded_party),
      
      (store_current_hours, ":current_time"),
      (try_begin),
        #      (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"), #this was added to get lords in centers out and visiting their fiefs, but I've adjusted the decision checklist
        (is_between, ":center_no", walled_centers_begin, walled_centers_end),
        (party_set_slot, ":led_party", slot_party_last_in_any_center, ":current_time"),
        (try_begin),
          (call_script, "script_lord_get_home_center", ":troop_no"),
          (eq, ":center_no", reg0),
          (party_set_slot, ":led_party", slot_party_last_in_home_center, ":current_time"),
        (try_end),
      (try_end),
      
      #Collect the rents
      (try_begin),
        (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
        
        (party_get_slot, ":accumulated_rents", ":center_no", slot_center_accumulated_rents),
        (party_get_slot, ":accumulated_tariffs", ":center_no", slot_center_accumulated_tariffs),
        (troop_get_slot, ":troop_wealth", ":troop_no", slot_troop_wealth),
        (val_add, ":troop_wealth", ":accumulated_rents"),
        (val_add, ":troop_wealth", ":accumulated_tariffs"),
        
        (troop_set_slot, ":troop_no", slot_troop_wealth, ":troop_wealth"),
        (party_set_slot, ":center_no", slot_center_accumulated_rents, 0),
        (party_set_slot, ":center_no", slot_center_accumulated_tariffs, 0),
        
        (try_begin),
          (this_or_next|eq, "$cheat_mode", 1),
          (eq, "$cheat_mode", 3),
          (assign, reg1, ":troop_wealth"),
          (str_store_party_name, s4, ":center_no"),
          (add_troop_note_from_sreg, ":troop_no", 1, "str_current_wealth_reg1_taxes_last_collected_from_s4", 0),
        (try_end),
      (try_end),
      
      #Recruit volunteers
      (try_begin),
        (is_between, ":center_no", villages_begin, villages_end),
        
        (party_get_slot, ":troop_type", ":center_no", slot_center_npc_volunteer_troop_type),
        (party_get_slot, ":troop_amount", ":center_no", slot_center_npc_volunteer_troop_amount),
        (party_set_slot, ":center_no", slot_center_npc_volunteer_troop_amount, -1),
        (party_add_members, ":led_party", ":troop_type", ":troop_amount"),
      (try_end),
      
      #Courtship
      (try_begin),
        (party_get_slot, ":time_of_last_courtship", ":led_party", slot_party_leader_last_courted),
        (store_sub, ":hours_since_last_courtship", ":current_time", ":time_of_last_courtship"),
        (gt, ":hours_since_last_courtship", 72),
        
        (troop_slot_eq, ":troop_no", slot_troop_spouse, -1),
        (try_for_range, ":love_interest_slot", slot_troop_love_interest_1, slot_troop_love_interests_end),
          (troop_get_slot, ":love_interest", ":troop_no", ":love_interest_slot"),
          (gt, ":love_interest", 0),
          (troop_get_slot, ":love_interest_town", ":love_interest", slot_troop_cur_center),
          (eq, ":center_no", ":love_interest_town"),
          
          (call_script, "script_courtship_event_troop_court_lady", ":troop_no", ":love_interest"),
          (party_set_slot, ":led_party", slot_party_leader_last_courted, ":current_time"),
        (try_end),
      (try_end),
  ]),
  
  # script_process_kingdom_parties_ai MOVED to stochastic trigger
  # This is called more frequently than decide_kingdom_parties_ai
  # Input: none
  # Output: none
  #called from triggers
  # ("process_kingdom_parties_ai",
  # [
  # (try_for_range, ":troop_no", active_npcs_begin, active_npcs_end),
  # (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
  # (neg|troop_slot_ge, ":troop_no", slot_troop_prisoner_of_party, 0),
  # (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
  # (gt, ":party_no", 0),
  # (call_script, "script_process_hero_ai", ":troop_no"),
  # (try_end),
  # ]),
  
  # script_process_hero_ai
  # This is called more frequently than script_decide_kingdom_party_ais
  #Handles sieges, raids, etc -- does not change the party's basic mission.
  # Input: none
  # Output: none
  #called from triggers
  ("process_hero_ai",
    [
      (store_script_param_1, ":troop_no"),
      (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
      (try_begin),
        (party_is_active, ":party_no"),
        (store_faction_of_party, ":faction_no", ":party_no"),
        (party_get_slot, ":ai_state", ":party_no", slot_party_ai_state),
        (party_get_slot, ":ai_object", ":party_no", slot_party_ai_object),
        (try_begin),
          (eq, ":ai_state", spai_besieging_center),
          (is_between, ":ai_object", centers_begin, centers_end), #do not besiege port parties (VC-3070)
          (try_begin),
            (party_slot_eq, ":ai_object", slot_center_is_besieged_by, -1),
            (store_distance_to_party_from_party, ":distance", ":party_no", ":ai_object"),
            (lt, ":distance", 3),
            (try_begin),
              (party_slot_eq, ":party_no", slot_party_ai_state, spai_accompanying_army),
              (party_get_slot, ":commander_party", ":party_no", slot_party_ai_object),
              (party_set_slot, ":ai_object", slot_center_is_besieged_by, ":commander_party"),
            (else_try),
              (party_set_slot, ":ai_object", slot_center_is_besieged_by, ":party_no"),
            (try_end),
            
            (party_get_slot, reg1, ":ai_object", slot_town_siege_count),
            (val_add, reg1, 1),
            (party_set_slot, ":ai_object", slot_town_siege_count, reg1),
            
            # Chief siege camp around town
            # (try_begin),
            # (party_slot_eq, ":ai_object",slot_town_port, 1),
            # (party_set_extra_icon, ":ai_object", "icon_siege_camp_ports", 0, 0, 0, 0),
            # (else_try),
            # (party_set_extra_icon, ":ai_object", "icon_siege_camp", 0, 0, 0, 0),
            # (try_end),
            #phaiak begin
            (try_begin),
              #(party_is_active,":ai_object"),
              (party_get_icon, ":icon", ":ai_object"),
              (try_begin),
                (eq, ":icon", "icon_town_port"),
                (party_set_extra_icon, ":ai_object", "icon_siege_camp_ports", 0, 0, 0, 0),
              (else_try),
                (eq, ":icon", "icon_town_walled"),
                (party_set_extra_icon, ":ai_object", "icon_siege_camp_town", 0, 0, 0, 0),
              (else_try),
                (eq, ":icon", "icon_fort1"),
                (party_set_extra_icon, ":ai_object", "icon_siege_camp_fort", 0, 0, 0, 0),
              (else_try),
                (eq, ":icon", "icon_fort2"),
                (party_set_extra_icon, ":ai_object", "icon_siege_camp_fort", 0, 0, 0, 0),
              (end_try),
            (try_end),
            #phaiak end
            # chief siege camp acaba
            (store_current_hours, ":cur_hours"),
            (party_set_slot, ":ai_object", slot_center_siege_begin_hours, ":cur_hours"),
            
            (str_store_party_name_link, s1, ":ai_object"),
            (str_store_troop_name_link, s2, ":troop_no"),
            (str_store_faction_name_link, s3, ":faction_no"),
            (display_message, "@{s1} has been besieged by {s2} of the {s3}."),
            (try_begin),
              (store_faction_of_party, ":ai_object_faction", ":ai_object"),
              (this_or_next|party_slot_eq, ":ai_object", slot_town_lord, "trp_player"),
              (eq, ":ai_object_faction", "fac_player_supporters_faction"),
              (call_script, "script_add_notification_menu", "mnu_notification_center_under_siege", ":ai_object", ":troop_no"),
            (try_end),
            (call_script, "script_village_set_state", ":ai_object", svs_under_siege),
            (assign, "$g_recalculate_ais", 1),
          (try_end),
        (else_try),
          (eq, ":ai_state", spai_raiding_around_center),
          (party_slot_eq, ":party_no", slot_party_ai_substate, 0),
          (assign, ":selected_village", 0),
          (try_for_range, ":enemy_village_no", villages_begin, villages_end),
            (eq, ":selected_village", 0),
            (store_faction_of_party, ":enemy_village_faction", ":enemy_village_no"),
            (try_begin),
              (party_slot_eq, ":enemy_village_no", slot_town_lord, "trp_player"),
              (store_relation, ":reln", "fac_player_faction", ":faction_no"),
            (else_try),
              (store_relation, ":reln", ":enemy_village_faction", ":faction_no"),
            (try_end),
            (lt, ":reln", 0),
            (store_distance_to_party_from_party, ":dist", ":enemy_village_no", ":party_no"),
            (lt, ":dist", 15),
            (party_slot_eq, ":enemy_village_no", slot_village_state, 0), #village is not already raided
            #CHANGE STATE TO RAID THIS VILLAGE
            (assign, ":selected_village", ":enemy_village_no"),
          (try_end),
          (try_begin),
            (eq, ":selected_village", 0),
            (is_between, ":ai_object", villages_begin, villages_end),
            (assign, ":selected_village", ":ai_object"),
          (try_end),
          (try_begin),
            (gt, ":selected_village", 0),
            (call_script, "script_party_set_ai_state", ":party_no", spai_raiding_around_center, ":selected_village"),
            (try_begin),
              (faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
              (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_raiding_village),
              (faction_set_slot, ":faction_no", slot_faction_ai_object, ":selected_village"),
            (try_end),
            (party_get_position, pos1, ":selected_village"),
            (map_get_random_position_around_position, pos2, pos1, 1),
            (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_point),
            (party_set_ai_target_position, ":party_no", pos2),
            (party_set_ai_object, ":party_no", ":selected_village"),
            (party_set_slot, ":party_no", slot_party_ai_substate, 1),
          (try_end),
        (else_try),
          (eq, ":ai_state", spai_raiding_around_center),#substate is 1
          (gt, ":ai_object", -1),
          (try_begin),
            (store_distance_to_party_from_party, ":distance", ":party_no", ":ai_object"),
            (lt, ":distance", 2),
            (try_begin),
              (party_slot_eq, ":ai_object", slot_village_state, 0),
              (party_slot_eq, ":ai_object", slot_party_type, spt_village), # phaiak try fixing VC-2085
              # (party_get_template_id, ":party_template_id", ":ai_object"),
              # (neq, ":party_template_id", "pt_port"),
              (call_script, "script_village_set_state", ":ai_object", svs_being_raided),
              (party_set_slot, ":ai_object", slot_village_raided_by, ":party_no"),
              (try_begin),
                (store_faction_of_party, ":village_faction", ":ai_object"),
                (this_or_next|party_slot_eq, ":ai_object", slot_town_lord, "trp_player"),
                (eq, ":village_faction", "fac_player_supporters_faction"),
                (store_distance_to_party_from_party, ":dist", "p_main_party", ":ai_object"),
                (this_or_next|lt, ":dist", 30),
                (party_slot_eq, ":ai_object", slot_center_has_messenger_post, 1),
                (call_script, "script_add_notification_menu", "mnu_notification_village_raid_started", ":ai_object", ":troop_no"),
              (try_end),
            (else_try),
              (party_slot_eq, ":ai_object", slot_village_state, svs_being_raided),
            (else_try),
              #if anything other than being_raided leave
              (party_set_slot, ":party_no", slot_party_ai_substate, 0),
            (try_end),
          (try_end),
        (else_try),
          (eq, ":ai_state", spai_retreating_to_center),
          (try_begin),
            (party_get_battle_opponent, ":enemy_party", ":party_no"),
            (ge, ":enemy_party", 0), #we are in a battle! we may be caught in a loop!
            (call_script, "script_party_set_ai_state", ":party_no", spai_undefined, -1),
            (party_set_flags, ":party_no", pf_default_behavior, 0),
            (party_set_slot, ":party_no", slot_party_commander_party, -1),
          (try_end),
          
          #MOTO chief rewrite to update ALL states from script_party_set_ai_state when objective reached or eliminated
          # (else_try),
          # (eq, ":ai_state", spai_patrolling_around_center),
          
          # (try_begin),
          # (party_slot_eq, ":party_no", slot_party_ai_substate, 0),
          # (store_distance_to_party_from_party, ":distance", ":party_no", ":ai_object"),
          # (lt, ":distance", 6),
          # (party_set_slot, ":party_no", slot_party_ai_substate, 1),
          
          # (party_set_aggressiveness, ":party_no", 8),
          # (party_set_courage, ":party_no", 8),
          # (party_set_ai_initiative, ":party_no", 100),
          
          # (party_set_ai_behavior, ":party_no", ai_bhvr_patrol_party),
          # (party_set_ai_object, ":party_no", ":ai_object"),
          # (try_end),
          # (else_try),
          # (eq, ":ai_state", spai_holding_center),
        (try_end),
        
        (try_begin),
          (ge, ":ai_object", 0),
          (neg|party_is_active, ":ai_object"),	#object was eliminated
          (call_script, "script_npc_decision_checklist_party_ai", ":troop_no"),
          (assign, ":ai_state", reg0),
          (assign, ":ai_object", reg1),
          (call_script, "script_party_set_ai_state", ":party_no", ":ai_state", ":ai_object"),
        (try_end),
        
        #state changes
        (assign, ":initiative", 0),	#test variable
        
        #start fights between passing armies
        (try_begin),
          (faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
          (assign, ":enemy_distance", 1000),
          
          (try_for_range, ":enemy_faction", kingdoms_begin, kingdoms_end),
            (store_relation, ":reln", ":faction_no", ":enemy_faction"),
            (lt, ":reln", 0),
            (faction_get_slot, ":enemy_marshal", ":enemy_faction", slot_faction_marshal),
            (ge, ":enemy_marshal", 0),
            (troop_get_slot, ":enemy_marshal_party", ":enemy_marshal", slot_troop_leaded_party),
            (party_is_active, ":enemy_marshal_party"),
            (store_distance_to_party_from_party, ":distance", ":party_no", ":enemy_marshal_party"),
            (gt, ":enemy_distance", ":distance"),
            (assign, ":enemy_distance", ":distance"),
          (try_end),
          
          (lt, ":distance", 25),
          
          (try_begin),
            (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemies_around_center),
            (assign, ":aggressiveness", 8),
            (assign, ":courage", 8),
            (assign, ":initiative", 100),
          (else_try),
            (assign, ":aggressiveness", 8),
            (assign, ":courage", 5),	#in enemy territory
            (assign, ":initiative", 100),
          (try_end),
          
        (else_try),
          (ge, ":ai_object", 0),
          (party_is_active, ":ai_object"),
          
          (try_begin),
            (eq, ":ai_state", spai_accompanying_army),
            (party_get_slot, ":commander_ai_object", ":ai_object", slot_party_ai_object),
            (party_get_battle_opponent, ":commander_opponent", ":ai_object"),
            (lt, ":commander_opponent", 0),	#marshal not in a fight?
            (try_begin),
              (party_is_active, ":commander_ai_object"),
              (store_distance_to_party_from_party, ":distance", ":party_no", ":commander_ai_object"),
            (else_try),
              (assign, ":distance", 1000),
            (try_end),
          (else_try),
            (store_distance_to_party_from_party, ":distance", ":party_no", ":ai_object"),
          (try_end),
          
          #defensive emergency
          (try_begin),
            (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemies_around_center),
            (try_begin),
              (le, ":distance", 15),	#1.5 x patrol radius
              
              (try_begin),
                (this_or_next|eq, ":ai_state", spai_engaging_army),
                (eq, ":ai_state", spai_patrolling_around_center),
                (assign, ":aggressiveness", 8),
                (assign, ":courage", 8),
                (assign, ":initiative", 100),
                (try_begin),
                  (eq, ":ai_state", spai_patrolling_around_center),
                  (party_slot_eq, ":party_no", slot_party_ai_substate, 0),
                  (party_set_slot, ":party_no", slot_party_ai_substate, 1),
                  (party_set_ai_behavior, ":party_no", ai_bhvr_patrol_party),
                  (party_set_ai_object, ":party_no", ":ai_object"),
                  (party_set_ai_patrol_radius, ":party_no", 1),
                (try_end),
                
              (else_try),
                (eq, ":ai_state", spai_accompanying_army),
                (party_get_slot, ":commander_ai_state", ":ai_object", slot_party_ai_state),
                (this_or_next|ge, ":commander_opponent", 0),	#commander in a fight?
                (this_or_next|eq, ":commander_ai_state", spai_engaging_army),
                (eq, ":commander_ai_state", spai_patrolling_around_center),
                (assign, ":aggressiveness", 8),
                (assign, ":courage", 8),
                (assign, ":initiative", 100),
              (try_end),
            (try_end),
            
            #reach objective
          (else_try),
            (eq, ":ai_state", spai_holding_center),
            (party_is_in_any_town, ":party_no"),
            (assign, ":aggressiveness", 5),
            (assign, ":courage", 8),
            (assign, ":initiative", 50),
            
          (else_try),
            (eq, ":ai_state", spai_besieging_center),
            (le, ":distance", 3),
            (assign, ":aggressiveness", 1),
            (assign, ":courage", 5),
            (assign, ":initiative", 20),
            
          (else_try),
            (eq, ":ai_state", spai_visiting_village),
            (le, ":distance", 3),
            (assign, ":aggressiveness", 5),
            (assign, ":courage", 8),
            (assign, ":initiative", 50),
            
          (else_try),
            (eq, ":ai_state", spai_patrolling_around_center),
            (le, ":distance", 15),	#1.5 x patrol radius
            (party_slot_eq, ":party_no", slot_party_ai_substate, 0),
            (party_set_slot, ":party_no", slot_party_ai_substate, 1),
            (assign, ":aggressiveness", 8),
            (assign, ":courage", 8),
            (assign, ":initiative", 80),
            (party_set_ai_behavior, ":party_no", ai_bhvr_patrol_party),
            (party_set_ai_object, ":party_no", ":ai_object"),
            (party_set_ai_patrol_radius, ":party_no", 1),
            
          (else_try),
            (eq, ":ai_state", spai_accompanying_army),
            (party_get_slot, ":commander_ai_state", ":ai_object", slot_party_ai_state),
            (try_begin),
              (this_or_next|ge, ":commander_opponent", 0),	#commander in a fight?
              (this_or_next|eq, ":commander_ai_state", spai_engaging_army),
              (eq, ":commander_ai_state", spai_raiding_around_center),
              (try_begin),
                (le, ":distance", 15),	#1.5 x patrol radius
                (assign, ":aggressiveness", 8),
                (assign, ":courage", 5),
                (assign, ":initiative", 100),
              (try_end),
              
              #spread besiegers around center
            (else_try),
              (le, ":distance", 5),	#marshal's distance (see above) was 3
              (eq, ":commander_ai_state", spai_besieging_center),
              (party_is_active, ":commander_ai_object"),
              (store_distance_to_party_from_party, ":distance", ":party_no", ":commander_ai_object"),
              (gt, ":distance", 3),	#not yet at siege?
              (party_get_position, pos1, ":commander_ai_object"),
              (map_get_land_position_around_position, pos2, pos1, 3),
              (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_point),
              (party_set_ai_target_position, ":party_no", pos2),
              # (party_set_ai_object, ":party_no", ":new_ai_object"),
              # (party_set_flags, ":party_no", pf_default_behavior, 0),
            (try_end),
          (try_end),
        (try_end),
        
        #set new values
        (try_begin),
          (gt, ":initiative", 0),
          
          (try_begin),
            (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_martial),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_selfrighteous),
            (val_add, ":aggressiveness", 2),
            (val_add, ":courage", 2),
          (else_try),
            (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_roguish),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_quarrelsome),
            (val_add, ":aggressiveness", 1),
            (val_sub, ":courage", 1),
          (else_try),
            ##			(this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_debauched),
            ##			(this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_custodian),
            ##			(troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_benefactor),
            (val_sub, ":aggressiveness", 1),
            (val_sub, ":courage", 1),
          (try_end),
          
          (party_set_aggressiveness, ":party_no", ":aggressiveness"),
          (party_set_courage, ":party_no", ":courage"),
          (party_set_ai_initiative, ":party_no", ":initiative"),
        (try_end),
        #MOTO chief end rewrite to update ALL states from script_party_set_ai_state when objective reached or eliminated
      (try_end),
  ]),
  
  # script_begin_assault_on_center
  # Input: arg1: faction_no
  # Output: none
  #called from triggers
  ("begin_assault_on_center",
    [
      (store_script_param, ":center_no", 1),
      
      (try_for_range, ":troop_no", active_npcs_begin, active_npcs_end),
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
        (neg|troop_slot_ge, ":troop_no", slot_troop_prisoner_of_party, 0),
        (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
        (gt, ":party_no", 0),
        (party_is_active, ":party_no"),
        
        (assign, ":continue", 0),
        (try_begin),
          (party_slot_eq, ":party_no", slot_party_ai_state, spai_besieging_center),
          (party_slot_eq, ":party_no", slot_party_ai_object, ":center_no"),
          (party_slot_eq, ":party_no", slot_party_ai_substate, 0),
          (assign, ":continue", 1),
        (else_try),
          (party_slot_eq, ":party_no", slot_party_ai_state, spai_accompanying_army),
          (party_get_slot, ":commander_party", ":party_no", slot_party_ai_object),
          (gt, ":commander_party", 0),
          (party_is_active, ":commander_party"),
          (party_slot_eq, ":commander_party", slot_party_ai_state, spai_besieging_center),
          (party_slot_eq, ":commander_party", slot_party_ai_object, ":center_no"),
          (call_script, "script_party_set_ai_state", ":party_no", spai_besieging_center, ":center_no"),
          (assign, ":continue", 1),
        (try_end),
        
        (eq, ":continue", 1),
        
        (party_set_ai_behavior, ":party_no", ai_bhvr_attack_party),
        (party_set_ai_object, ":party_no", ":center_no"),
        (party_set_flags, ":party_no", pf_default_behavior, 1),
        (party_set_slot, ":party_no", slot_party_ai_substate, 1),
      (try_end),
  ]),
  

  # script_get_center_faction_relation_including_player
  # Input: arg1: center_no, arg2: target_faction_no
  # Output: reg0: relation
  #called from triggers
  # ("get_center_faction_relation_including_player",
    # [
      # (store_script_param, ":center_no", 1),
      # (store_script_param, ":target_faction_no", 2),
      # (store_faction_of_party, ":center_faction", ":center_no"),
      # (store_relation, ":relation", ":center_faction", ":target_faction_no"),
      # (try_begin),
        # (party_slot_eq, ":center_no", slot_town_lord, "trp_player"),
        # (store_relation, ":relation", "fac_player_faction", ":target_faction_no"),
      # (try_end),
      # (assign, reg0, ":relation"),
  # ]),
  
  #script_update_report_to_army_quest_note
  ("update_report_to_army_quest_note",
    [
      (store_script_param, ":faction_no", 1),
      (store_script_param, ":new_strategy", 2),
      (store_script_param, ":old_faction_ai_state", 3),
      
      (try_begin),
        (le, "$number_of_report_to_army_quest_notes", 13),
        
        (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
        
        (try_begin), #updating quest notes for only report to army quest
          (eq, ":faction_no", "$players_kingdom"),
          (neq, ":new_strategy", ":old_faction_ai_state"),
          (check_quest_active, "qst_report_to_army"),
          (ge, ":faction_marshal", 0),
          
          (str_store_troop_name_link, s11, ":faction_marshal"),
          (store_current_hours, ":hours"),
          (call_script, "script_game_get_date_text", 0, ":hours"),
          
          (try_begin),
            (this_or_next|eq, ":new_strategy", sfai_attacking_enemies_around_center),
            (this_or_next|eq, ":new_strategy", sfai_attacking_center),
            (eq, ":new_strategy", sfai_gathering_army),
            (faction_get_slot, ":faction_object", ":faction_no", slot_faction_ai_object),
            (ge, ":faction_object", 0),
            (str_store_party_name_link, s21, ":faction_object"),
          (try_end),
          
          (try_begin),
            (eq, ":new_strategy", sfai_gathering_army),
            
            (try_begin),
              (ge, "$g_gathering_reason", 0),
              (str_store_party_name_link, s21, "$g_gathering_reason"),
              (str_store_string, s14, "str_we_should_prepare_to_defend_s21_but_we_should_gather_our_forces_until_we_are_strong_enough_to_engage_them"),
            (else_try),
              (str_store_string, s14, "str_it_is_time_to_go_on_the_offensive_and_we_must_first_assemble_the_army"),
            (try_end),
            
            (str_store_string, s14, "@({s1}) {s11}: {s14}"),
            (add_quest_note_from_sreg, "qst_report_to_army", "$number_of_report_to_army_quest_notes", s14, 0),
            (val_add, "$number_of_report_to_army_quest_notes", 1),
          (else_try),
            (eq, ":new_strategy", sfai_attacking_enemies_around_center),
            
            (try_begin),
              (is_between, ":faction_object", walled_centers_begin, walled_centers_end),
              (str_store_string, s14, "str_we_should_ride_to_break_the_siege_of_s21"),
              (str_store_string, s14, "@({s1}) {s11}: {s14}"),
              (add_quest_note_from_sreg, "qst_report_to_army", "$number_of_report_to_army_quest_notes", s14, 0),
              (val_add, "$number_of_report_to_army_quest_notes", 1),
            (else_try),
              (is_between, ":faction_object", villages_begin, villages_end),
              (str_store_string, s14, "str_we_should_ride_to_defeat_the_enemy_gathered_near_s21"),
              (str_store_string, s14, "@({s1}) {s11}: {s14}"),
              (add_quest_note_from_sreg, "qst_report_to_army", "$number_of_report_to_army_quest_notes", s14, 0),
              (val_add, "$number_of_report_to_army_quest_notes", 1),
            (try_end),
          (else_try),
            (this_or_next|eq, ":new_strategy", sfai_attacking_center),
            (eq, ":new_strategy", sfai_raiding_village),
            
            (try_begin),
              (is_between, ":faction_object", walled_centers_begin, walled_centers_end),
              (str_store_string, s14, "str_we_believe_the_fortress_will_be_worth_the_effort_to_take_it"),
              (str_store_string, s14, "@{s14} ({s21})"),
              (str_store_string, s14, "@({s1}) {s11}: {s14}"),
              (add_quest_note_from_sreg, "qst_report_to_army", "$number_of_report_to_army_quest_notes", s14, 0),
              (val_add, "$number_of_report_to_army_quest_notes", 1),
            (else_try),
              (is_between, ":faction_object", villages_begin, villages_end),
              (str_store_string, s14, "str_we_shall_leave_a_fiery_trail_through_the_heart_of_the_enemys_lands_targeting_the_wealthy_settlements_if_we_can"),
              (str_store_string, s14, "@{s14} ({s21})"),
              (str_store_string, s14, "@({s1}) {s11}: {s14}"),
              (add_quest_note_from_sreg, "qst_report_to_army", "$number_of_report_to_army_quest_notes", s14, 0),
              (val_add, "$number_of_report_to_army_quest_notes", 1),
            (try_end),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  
  # script_decide_faction_ai
  # Input: arg1: faction_no
  # Output: none
  #called from triggers
  ("decide_faction_ai",
    #This handles political issues and faction issues
    [
      (store_script_param_1, ":faction_no"),
      
      (faction_get_slot, ":old_faction_ai_state", ":faction_no", slot_faction_ai_state),
      (faction_get_slot, ":old_faction_ai_object", ":faction_no", slot_faction_ai_object),
      (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
      (try_begin), # a special case to end long-running feasts
        (eq, ":faction_no", "fac_player_supporters_faction"),
        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        (eq, ":old_faction_ai_state", sfai_feast),
        (store_current_hours, ":hours_at_current_state"),
        (faction_get_slot, ":current_state_started", ":faction_no", slot_faction_ai_current_state_started),
        (val_sub, ":hours_at_current_state", ":current_state_started"),
        (ge, ":hours_at_current_state", 72),
        (faction_set_slot, ":faction_no", slot_faction_ai_state, sfai_default),
        (faction_set_slot, ":faction_no", slot_faction_ai_object, -1),
        (assign, "$player_marshal_ai_state", sfai_default),
        (assign, "$player_marshal_ai_object", -1),
        (display_message, "@Feast concludes", color_hero_news),
      (try_end),
      
      #Remove marshal if he has become too controversial,, or he has defected, or has been taken prisoner
      (try_begin),
        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        (neq, ":faction_no", "fac_player_supporters_faction"),
        (ge, ":faction_marshal", "trp_player"),
        
        (store_faction_of_troop, ":marshal_faction", ":faction_marshal"),
        (try_begin),
          (eq, ":faction_marshal", "trp_player"),
          (assign, ":marshal_faction", "$players_kingdom"),
        (try_end),
        
        
        (assign, ":player_marshal_is_prisoner", 0),
        (try_begin),
          (eq, ":faction_marshal", "trp_player"),
          (eq, "$g_player_is_captive", 1),
          (assign, ":player_marshal_is_prisoner", 1),
        (try_end),
        
        
        #High controversy level, or marshal has defected, or is prisoner
        (this_or_next|neq, ":marshal_faction", ":faction_no"),
        (this_or_next|troop_slot_ge, ":faction_marshal", slot_troop_controversy, 80),
        (this_or_next|eq, ":player_marshal_is_prisoner", 1),
        (troop_slot_ge, ":faction_marshal", slot_troop_prisoner_of_party, 0),
        
        (assign, ":few_following_player_campaign", 0),
        (try_begin),
          (eq, ":faction_marshal", "trp_player"),
          (assign, ":vassals_following_player_campaign", 0),
          (gt, "$g_player_days_as_marshal", 1),
          (try_for_range, ":vassal", active_npcs_begin, active_npcs_end),
            (troop_slot_eq, ":vassal", slot_troop_occupation, slto_kingdom_hero),
            (store_faction_of_troop, ":vassal_faction", ":vassal"),
            (eq, ":vassal_faction", ":faction_no"),
            (call_script, "script_npc_decision_checklist_troop_follow_or_not", ":vassal"),
            (eq, reg0, 1),
            (val_add, ":vassals_following_player_campaign", 1),
          (try_end),
          (lt, ":vassals_following_player_campaign", 4),
          (assign, ":few_following_player_campaign", 1),
        (try_end),
        
        #Only remove marshal for controversy if offensive campaign in progress
        (this_or_next|eq, ":old_faction_ai_state", sfai_default),
        (this_or_next|eq, ":old_faction_ai_state", sfai_feast),
        (this_or_next|neq, ":marshal_faction", ":faction_no"),
        (this_or_next|eq, ":few_following_player_campaign", 1),
        (this_or_next|eq, ":player_marshal_is_prisoner", 1),
        (troop_slot_ge, ":faction_marshal", slot_troop_prisoner_of_party, 0),
        
        #No current issue on the agenda
        (this_or_next|eq, ":player_marshal_is_prisoner", 1),
        (this_or_next|troop_slot_ge, ":faction_marshal", slot_troop_prisoner_of_party, 0),
        (neg|faction_slot_ge, ":faction_no", slot_faction_political_issue, 1),
        
        (faction_set_slot, ":faction_no", slot_faction_political_issue, 1), #Appointment of marshal
        (store_current_hours, ":hours"),
        (val_max, ":hours", 0),
        (faction_set_slot, ":faction_no", slot_faction_political_issue_time, ":hours"), #Appointment of marshal
        
        (faction_get_slot, ":old_marshal", ":faction_no", slot_faction_marshal),
        (try_begin),
          (ge, ":old_marshal", 0),
          (troop_get_slot, ":old_marshal_party", ":old_marshal", slot_troop_leaded_party),
          (party_is_active, ":old_marshal_party"),
          (party_set_marshal, ":old_marshal_party", 0),
        (try_end),
        
        (try_begin),
          (eq, "$players_kingdom", ":faction_no"),
          (faction_slot_eq, "$players_kingdom", slot_faction_marshal, "trp_player"),
          (call_script, "script_add_notification_menu", "mnu_notification_relieved_as_marshal", 0, 0),
        (else_try),
          (neq, ":old_marshal", "trp_player"),
          (call_script, "script_change_troop_renown", ":old_marshal", 15),
        (try_end),
        (faction_set_slot, ":faction_no", slot_faction_marshal, -1),
        (assign, ":faction_marshal", -1),
        
        
        
        (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
          (eq, ":active_npc_faction", ":faction_no"),
          (troop_set_slot, ":active_npc", slot_troop_stance_on_faction_issue, -1),
        (try_end),
        (try_begin),
          (eq, "$players_kingdom", ":faction_no"),
          (troop_set_slot, "trp_player", slot_troop_stance_on_faction_issue, -1),
        (try_end),
        
      (else_try),	 #If marshal not present, and not already on agenda, make political issue
        (eq, ":faction_marshal", -1),
        (neg|faction_slot_ge, ":faction_no", slot_faction_political_issue, 1), #This to avoid resetting votes every time
        
        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        (neq, ":faction_no", "fac_player_supporters_faction"),
        
        (faction_set_slot, ":faction_no", slot_faction_political_issue, 1), #Appointment of marshal
        (store_current_hours, ":hours"),
        (val_max, ":hours", 0),
        (faction_set_slot, ":faction_no", slot_faction_political_issue_time, ":hours"), #Appointment of marshal
        
        (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
          (eq, ":active_npc_faction", ":faction_no"),
          (troop_set_slot, ":active_npc", slot_troop_stance_on_faction_issue, -1),
        (try_end),
        (try_begin),
          (eq, "$players_kingdom", ":faction_no"),
          (troop_set_slot, "trp_player", slot_troop_stance_on_faction_issue, -1),
        (try_end),
        
        
      (else_try),	#If player is marshal, but not part of faction
        (eq, ":faction_marshal", "trp_player"),
        (neq, "$players_kingdom", ":faction_no"),
        
        (faction_set_slot, ":faction_no", slot_faction_political_issue, 1), #Appointment of marshal
        (store_current_hours, ":hours"),
        (val_max, ":hours", 0),
        (faction_set_slot, ":faction_no", slot_faction_political_issue_time, ":hours"), #Appointment of marshal
        
        (faction_get_slot, ":old_marshal", ":faction_no", slot_faction_marshal),
        (try_begin),
          (ge, ":old_marshal", 0),
          (troop_get_slot, ":old_marshal_party", ":old_marshal", slot_troop_leaded_party),
          (party_is_active, ":old_marshal_party"),
          (party_set_marshal, ":old_marshal_party", 0),
        (try_end),
        
        (faction_set_slot, ":faction_no", slot_faction_marshal, -1),
        (assign, ":faction_marshal", -1),
        
        (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
          (eq, ":active_npc_faction", ":faction_no"),
          (troop_set_slot, ":active_npc", slot_troop_stance_on_faction_issue, -1),
        (try_end),
        (try_begin),
          (eq, "$players_kingdom", ":faction_no"),
          (troop_set_slot, "trp_player", slot_troop_stance_on_faction_issue, -1),
        (try_end),
        
      (try_end),
      
      #If the faction issue is a center no longer under faction control, remove and reset
      (try_begin),
        (faction_get_slot, ":faction_political_issue", ":faction_no", slot_faction_political_issue),
        (is_between, ":faction_political_issue", centers_begin, centers_end),
        (store_faction_of_party, ":disputed_center_faction", ":faction_political_issue"),
        (neq, ":disputed_center_faction", ":faction_no"),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (str_store_faction_name, s4, ":faction_no"),
          (str_store_party_name, s5, ":disputed_center_faction"),
          (display_message, "@{!}DEBUG -- {s4} drops {s5} as issue as it has changed hands"),
        (try_end),
        
        #Reset political issue
        (faction_set_slot, ":faction_no", slot_faction_political_issue, 0),
        (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
          (eq, ":active_npc_faction", ":faction_no"),
          (troop_set_slot, ":active_npc", slot_troop_stance_on_faction_issue, -1),
        (try_end),
        (try_begin),
          (eq, "$players_kingdom", ":faction_no"),
          (troop_set_slot, "trp_player", slot_troop_stance_on_faction_issue, -1),
        (try_end),
        
      (try_end),
      
      
      #Resolve the political issue on the agenda
      (try_begin),
        (faction_slot_ge, ":faction_no", slot_faction_political_issue, 1),
        (neq, ":faction_no", "fac_player_supporters_faction"),
        
        #Do not switch marshals during a campaign
        (this_or_next|faction_slot_ge, ":faction_no", slot_faction_political_issue, centers_begin),
        (this_or_next|faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_default),
        (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_feast),
        
        
        (faction_get_slot, ":faction_leader", ":faction_no", slot_faction_leader),
        
        (assign, ":total_lords", 0),
        (assign, ":lords_who_have_voted", 0),
        (assign, ":popular_favorite", -1),
        
        #Reset number of votes
        (troop_set_slot, "trp_player", slot_troop_temp_slot, 0),
        (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
          (troop_set_slot, ":active_npc", slot_troop_temp_slot, 0),
        (try_end),
        
        #Tabulate votes
        (try_for_range, ":voting_lord", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":voting_lord_faction", ":voting_lord"),
          (eq, ":voting_lord_faction", ":faction_no"),
          (val_add, ":total_lords", 1),
          (troop_get_slot, ":lord_chosen_candidate", ":voting_lord", slot_troop_stance_on_faction_issue),
          (gt, ":lord_chosen_candidate", -1),
          (val_add, ":lords_who_have_voted", 1),
          (troop_get_slot, ":total_votes", ":lord_chosen_candidate", slot_troop_temp_slot),
          (val_add, ":total_votes", 1),
          (troop_set_slot, ":lord_chosen_candidate", slot_troop_temp_slot, ":total_votes"),
          (try_begin),
            (gt, ":popular_favorite", -1),
            (troop_get_slot, ":current_winner_votes", ":popular_favorite", slot_troop_temp_slot),
            (gt, ":total_votes", ":current_winner_votes"),
            (assign, ":popular_favorite", ":lord_chosen_candidate"),
          (else_try),
            (eq, ":popular_favorite", -1),
            (assign, ":popular_favorite", ":lord_chosen_candidate"),
          (try_end),
        (try_end),
        
        #Check to see if enough lords have voted
        (store_div, ":number_required_for_quorum", ":total_lords", 5),
        (val_mul, ":number_required_for_quorum", 4),
        
        
        #		(gt, ":lords_who_have_voted", ":number_required_for_quorum"),
        
        (store_current_hours, ":hours_on_agenda"),
        (faction_get_slot, ":hours_when_put_on_agenda", ":faction_no", slot_faction_political_issue_time), #Appointment of marshal
        (val_sub, ":hours_on_agenda", ":hours_when_put_on_agenda"),
        
        (this_or_next|gt, ":lords_who_have_voted", ":number_required_for_quorum"),
        (ge, ":hours_on_agenda", 120),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (assign, reg4, ":lords_who_have_voted"),
          (assign, reg5, ":number_required_for_quorum"),
          (assign, reg7, ":hours_on_agenda"),
          (str_store_faction_name, s4, ":faction_no"),
          (display_message, "@{!}DEBUG -- Issue resolution for {s4}: {reg4} votes for a quorum of {reg5}, {reg7} hours on agenda"),
        (try_end),
        
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "@{!}DEBUG -- Faction resolves political issue"),
        (try_end),
        
        
        #Resolve faction political issue
        (assign, ":winning_candidate", -1),
        (try_begin),
          (call_script, "script_troop_get_relation_with_troop", ":faction_leader", ":popular_favorite"),
          (this_or_next|ge, reg0, 10),
          (this_or_next|troop_slot_eq, ":faction_leader", slot_troop_stance_on_faction_issue, ":popular_favorite"),
          (troop_slot_eq, ":faction_leader", slot_troop_stance_on_faction_issue, -1),
          
          (assign, ":winning_candidate", ":popular_favorite"),
        (else_try),#Lord overrules lords' opinion
          (gt, ":faction_leader", -1), #not sure why this is necessary
          (troop_get_slot, ":liege_choice", ":faction_leader", slot_troop_stance_on_faction_issue),
          (ge, ":liege_choice", -1),
          
          (assign, ":winning_candidate", ":liege_choice"),
        (try_end),
        
        #Carry out faction decision
        (try_begin), #Nothing happens
          (eq, ":winning_candidate", -1),
          
        (else_try), #For player, create a menu to accept or refuse
          (eq, ":winning_candidate", "trp_player"),
          (eq, "$players_kingdom", ":faction_no"),
          (call_script, "script_add_notification_menu", "mnu_notification_player_faction_political_issue_resolved_for_player", 0, 0),
        (else_try),
          (eq, ":winning_candidate", "trp_player"),
          (neq, "$players_kingdom", ":faction_no"),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_faction_name, s4, ":faction_no"),
            (str_store_party_name, s5, ":winning_candidate"),
            (display_message, "@{!}DEBUG -- {s4} drops {s5} as winner, for having changed sides"),
          (try_end),
          
          (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
            (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
            (eq, ":active_npc_faction", ":faction_no"),
            (troop_set_slot, ":active_npc", slot_troop_stance_on_faction_issue, -1),
          (try_end),
          (try_begin),
            (eq, "$players_kingdom", ":faction_no"),
            (troop_set_slot, "trp_player", slot_troop_stance_on_faction_issue, -1),
          (try_end),
          
        (else_try),	#If candidate is not of winning faction, reset lrod votes
          (store_faction_of_troop, ":winning_candidate_faction", ":winning_candidate"),
          (neq, ":winning_candidate_faction", ":faction_no"),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_faction_name, s4, ":faction_no"),
            (str_store_party_name, s5, ":winning_candidate"),
            (display_message, "@{!}DEBUG -- {s4} drops {s5} as winner, for having changed sides"),
          (try_end),
          
          (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
            (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
            (eq, ":active_npc_faction", ":faction_no"),
            (troop_set_slot, ":active_npc", slot_troop_stance_on_faction_issue, -1),
          (try_end),
          (try_begin),
            (eq, "$players_kingdom", ":faction_no"),
            (troop_set_slot, "trp_player", slot_troop_stance_on_faction_issue, -1),
          (try_end),
          
        (else_try), #Honor awarded to another
          (faction_get_slot, ":issue_on_table", ":faction_no", slot_faction_political_issue),
          (try_begin), #A marshalship awarded to another
            (eq, ":issue_on_table", 1),
            (is_between, ":winning_candidate", active_npcs_begin, active_npcs_end),
            
            (this_or_next|is_between, ":winning_candidate", active_npcs_begin, active_npcs_end), #Prevents bug in which player given marshaldom of kingdom of which he/she is not a member
            (eq, "$players_kingdom", ":faction_no"),
            
            (assign, ":faction_marshal", ":winning_candidate"),
          (else_try), #A fief awarded to another
            (is_between, ":issue_on_table", centers_begin, centers_end),
            
            #If given to the player, resolved above
            (call_script, "script_give_center_to_lord", ":issue_on_table", ":winning_candidate", 0), #Zero means don't add garrison
            
            #If the player had requested a captured castle
            (try_begin),
              (eq, ":issue_on_table", "$g_castle_requested_by_player"),
              (party_slot_ge, ":issue_on_table", slot_town_lord, active_npcs_begin),
              (store_faction_of_party, ":faction_of_issue", ":issue_on_table"),
              (eq, ":faction_of_issue", "$players_kingdom"),
              (assign, "$g_center_to_give_to_player", ":issue_on_table"),
              (try_begin),
                (troop_get_slot, ":husband", "trp_player", slot_troop_spouse),
                (is_between, ":husband", active_npcs_begin, active_npcs_end),
                (eq, "$g_castle_requested_for_troop", ":husband"),
                (neq, ":winning_candidate", ":husband"),
                (jump_to_menu, "mnu_requested_castle_granted_to_another_female"),
              (else_try),
                (jump_to_menu, "mnu_requested_castle_granted_to_another"),
              (try_end),
            (try_end),
            
          (try_end),
          
          (try_begin),
            (eq, ":faction_no", "$players_kingdom"),
            (call_script, "script_add_notification_menu", "mnu_notification_player_faction_political_issue_resolved", ":issue_on_table", ":winning_candidate"),
          (try_end),
          
          #Reset political issue
          (faction_set_slot, ":faction_no", slot_faction_political_issue, 0),
          (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
            (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
            (eq, ":active_npc_faction", ":faction_no"),
            (troop_set_slot, ":active_npc", slot_troop_stance_on_faction_issue, -1),
          (try_end),
          (try_begin),
            (eq, "$players_kingdom", ":faction_no"),
            (troop_set_slot, "trp_player", slot_troop_stance_on_faction_issue, -1),
          (try_end),
        (try_end),
      (try_end),
      
      #Add fief to faction issues
      (try_begin),
        (neg|faction_slot_ge, ":faction_no", slot_faction_political_issue, 1),
        
        (assign, ":landless_lords", 0),
        (assign, ":unassigned_centers", 0),
        (assign, ":first_unassigned_center_found", 0),
        
        (troop_set_slot, "trp_player", slot_troop_temp_slot, 0),
        (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
          (troop_set_slot, ":active_npc", slot_troop_temp_slot, 0),
        (try_end),
        
        (try_for_range, ":center", centers_begin, centers_end),
          (store_faction_of_party, ":center_faction", ":center"),
          (eq, ":center_faction", ":faction_no"),
          
          (party_get_slot, ":town_lord", ":center", slot_town_lord),
          
          (try_begin),
            (lt, ":town_lord", 0),
            (val_add, ":unassigned_centers", 1),
            (try_begin),
              (eq, ":first_unassigned_center_found", 0),
              (assign, ":first_unassigned_center_found", ":center"),
            (try_end),
          (else_try),
            (troop_set_slot, ":town_lord", slot_troop_temp_slot, 1),
          (try_end),
        (try_end),
        
        (store_add, ":landless_lords_plus_unassigned_centers", ":landless_lords", ":unassigned_centers"),
        (ge, ":landless_lords_plus_unassigned_centers", 2),
        
        (faction_set_slot, ":faction_no", slot_faction_political_issue, ":first_unassigned_center_found"),
        (store_current_hours, ":hours"),
        (faction_set_slot, ":faction_no", slot_faction_political_issue_time, ":hours"), #Fief put on agenda
        
        (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
          (eq, ":active_npc_faction", ":faction_no"),
          (troop_set_slot, ":active_npc", slot_troop_stance_on_faction_issue, -1),
        (try_end),
        (try_begin),
          (eq, "$players_kingdom", ":faction_no"),
          (troop_set_slot, "trp_player", slot_troop_stance_on_faction_issue, -1),
        (try_end),
      (try_end),
      
      
      (try_begin), #If the marshal is changed
        (neg|faction_slot_eq, ":faction_no", slot_faction_marshal, ":faction_marshal"),
        #(assign, ":marshal_changed", 1),
        (eq, "$players_kingdom", ":faction_no"),
        (str_store_troop_name_link, s1, ":faction_marshal"),
        (str_store_faction_name_link, s2, ":faction_no"),
        (display_message, "@{s1} is the new marshal of the {s2}."),
        (call_script, "script_check_and_finish_active_army_quests_for_faction", ":faction_no"),
      (try_end),
      
      (try_begin), #If the marshal is changed
        (neg|faction_slot_eq, ":faction_no", slot_faction_marshal, ":faction_marshal"),
        (gt, ":faction_marshal", -1),
        (call_script, "script_appoint_faction_marshal", ":faction_no", ":faction_marshal"),
      (try_end),
      
      #DO FACTION AI HERE
      (try_begin),
        (eq, ":faction_no", "$players_kingdom"),
        (eq, ":faction_marshal", "trp_player"),
        (assign, ":faction_ai_decider", "trp_player"),
      (else_try),
        (is_between, ":faction_marshal", active_npcs_begin, active_npcs_end),
        (assign, ":faction_ai_decider", ":faction_marshal"),
      (else_try),
        (faction_get_slot, ":faction_ai_decider", ":faction_no", slot_faction_leader),
      (try_end),
      
      (call_script, "script_npc_decision_checklist_faction_ai_alt",  ":faction_ai_decider"),
      (assign, ":new_strategy", reg0),
      (assign, ":new_object", reg1),
      
      #new ozan
      (try_begin),
        (neq, ":new_strategy", ":old_faction_ai_state"),
        (eq, ":new_strategy", sfai_gathering_army),
        (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
        (ge, ":faction_marshal", 0),
        (troop_get_slot, ":marshal_party", ":faction_marshal", slot_troop_leaded_party),
        (party_is_active, ":marshal_party"),    #MOTO chief bug fix
        (party_set_slot, ":marshal_party", slot_party_ai_object, -1),
        (assign, "$g_gathering_new_started", 1),
        (call_script, "script_npc_decision_checklist_party_ai", ":faction_marshal"), #This handles AI for both marshal and other parties
        (call_script, "script_party_set_ai_state", ":marshal_party", reg0, reg1),
        (assign, "$g_gathering_new_started", 0),
      (else_try),
        #check if marshal arrived his target city during active gathering
        
        #for now i disabled below lines because after always/active gathering armies become very large.
        #in current style marshal makes active gathering only at first, it travels to a city and waits there.
        
        (eq, ":new_strategy", ":old_faction_ai_state"),
        (eq, ":new_strategy", sfai_gathering_army),
        (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
        (ge, ":faction_marshal", 0),
        (troop_get_slot, ":marshal_party", ":faction_marshal", slot_troop_leaded_party),
        (party_is_active, ":marshal_party"),    #MOTO chief bug fix
        (party_get_slot, ":party_ai_object", ":marshal_party", slot_party_ai_object),
        
        (ge, ":party_ai_object", 0),
        (ge, ":marshal_party", 0),
        (party_is_active, ":marshal_party"),
        (party_is_active, ":party_ai_object"),
        (store_distance_to_party_from_party, ":dist", ":marshal_party", ":party_ai_object"),
        (le, ":dist", 5),
        ##	   (party_get_current_terrain, ":terrain_type", ":marshal_party"),
        ##	   (neq,":terrain_type",0),	#no ambushes at sea
        ##	   (neq,":terrain_type",7),
        ##	   (neq,":terrain_type",8),
        (party_set_slot, ":marshal_party", slot_party_ai_object, -1),
      (try_end),
      #end ozan
      
      #The following logic is mostly transplanted to the new decision_checklist
      #Decision_checklist is used because I want to be able to reproduce the logic for strings
      #(call_script, "script_old_faction_ai"),
      #ozan - I collected all comment-out lines in here (faction ai script) and placed most bottom of scripts.py to avoid confusing.
      
      (faction_set_slot, ":faction_no", slot_faction_ai_state, ":new_strategy"),
      (faction_set_slot, ":faction_no", slot_faction_ai_object, ":new_object"),
      
      #Change of strategy
      (try_begin),
        (neq, ":new_strategy", ":old_faction_ai_state"),
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (str_store_faction_name, s5, ":faction_no"),
          (display_message, "str_s5_decides_s14"),
        (try_end),
        
        (store_current_hours, ":hours"),
        (faction_set_slot, ":faction_no", slot_faction_ai_current_state_started, ":hours"),
        (call_script, "script_update_report_to_army_quest_note", ":faction_no", ":new_strategy", ":old_faction_ai_state"),
        
        #Feast ends
        (try_begin),
          (eq, ":old_faction_ai_state", sfai_feast),
          (call_script, "script_faction_conclude_feast", ":faction_no", ":old_faction_ai_object"),
        (try_end),
        
        
        #Feast begins
        (try_begin),
          (eq, ":new_strategy", sfai_feast),
          
          (faction_get_slot, ":faction_object", ":faction_no", slot_faction_ai_object),
          (party_set_slot, ":faction_object", slot_town_has_tournament, 2),
          
          (try_begin),
            (eq, "$g_player_eligible_feast_center_no", ":faction_object"),
            (assign, "$g_player_eligible_feast_center_no", -1), #reset needed
          (try_end),
          
          ##         (str_store_faction_name, s1, ":faction_no"),
          ##         (str_store_party_name, s2, ":faction_object"),
          ##         (display_message, "str_lords_of_the_s1_gather_for_a_feast_at_s2"),
          
          (store_current_hours, ":hours"),
          (faction_set_slot, ":faction_no", slot_faction_last_feast_start_time, ":hours"), #new
          
          (party_get_slot, ":feast_host", ":faction_object", slot_town_lord),
          (try_begin),
            (check_quest_active, "qst_wed_betrothed"),
            (quest_slot_eq, "qst_wed_betrothed", slot_quest_giver_troop, ":feast_host"),
            (neg|quest_slot_ge, "qst_wed_betrothed", slot_quest_expiration_days, 362),
            (call_script, "script_add_notification_menu", "mnu_notification_player_wedding_day", ":feast_host", ":faction_object"),
          (else_try),
            (check_quest_active, "qst_wed_betrothed_female"),
            (quest_get_slot, ":player_betrothed", "qst_wed_betrothed", slot_quest_giver_troop),
            (store_faction_of_troop, ":player_betrothed_faction", ":player_betrothed"),
            (eq, ":player_betrothed_faction", ":faction_no"),
            (neg|quest_slot_ge, "qst_wed_betrothed", slot_quest_expiration_days, 362),
            (call_script, "script_add_notification_menu", "mnu_notification_player_kingdom_holds_feast", ":feast_host", ":faction_object"),
          (else_try),
            (eq, "$players_kingdom", ":faction_no"),
            (troop_slot_ge, "trp_player", slot_troop_renown, 150),
            #				(party_get_slot, ":feast_host", ":faction_object", slot_town_lord),
            (neq, ":feast_host", "trp_player"),
            (neg|check_quest_active, "qst_organize_feast"),
            (call_script, "script_add_notification_menu", "mnu_notification_player_kingdom_holds_feast", ":feast_host", ":faction_object"),
          (else_try),
            (check_quest_active, "qst_organize_feast"),
            (neq, ":feast_host", "trp_player"),
            (store_faction_of_troop, ":faction", ":feast_host"),
            (eq, ":faction", "$players_kingdom"),
            (call_script, "script_add_notification_menu", "mnu_notification_player_kingdom_holds_feast", "trp_player", ":faction_object"),
          (try_end),
        (try_end),
        
        
        #Offensive begins
        (try_begin),
          (eq, ":old_faction_ai_state", sfai_gathering_army),
          (is_between, ":new_strategy", sfai_attacking_center, sfai_feast),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_faction_name, s5, ":faction_no"),
            (display_message, "str_s5_begins_offensive"),
          (try_end),
          
          # #Appoint screening party MOTO chief move to script_npc_decision_checklist_party_ai so marshal will always have a screening party
          # (try_begin),
          # (assign, ":total_lords_participating", 0),
          # (assign, ":best_screening_party", -1),
          # (assign, ":score_to_beat", 30), #closest in size to 50
          # (troop_get_slot, ":faction_marshal_party", ":faction_marshal", slot_troop_leaded_party),
          # (party_is_active, ":faction_marshal_party"),
          
          # (try_for_range, ":screen_leader", active_npcs_begin, active_npcs_end),
          # (store_faction_of_troop, ":screen_leader_faction", ":screen_leader"),
          # (eq, ":screen_leader_faction", ":faction_no"),
          
          # (troop_get_slot, ":screening_party", ":screen_leader", slot_troop_leaded_party),
          # (party_is_active, ":screening_party"),
          # (party_slot_eq, ":screening_party", slot_party_ai_state, spai_accompanying_army),
          # (party_slot_eq, ":screening_party", slot_party_ai_object, ":faction_marshal_party"),
          # (val_add, ":total_lords_participating", 1),
          
          # (try_begin),
          # (ge, "$cheat_mode", 1),
          # (str_store_party_name, s4, ":screening_party"),
          # (display_message, "@{!}DEBUG -- {s4} participates in offensive"),
          # (try_end),
          
          
          # (store_party_size_wo_prisoners, ":screening_party_score", ":screening_party"),
          # (val_sub, ":screening_party_score", 50),
          # (val_abs, ":screening_party_score"),
          
          
          # (lt, ":screening_party_score", ":score_to_beat"),
          
          # #set party and score
          # (assign, ":best_screening_party", ":screening_party"),
          # (assign, ":score_to_beat", ":screening_party_score"),
          # (try_end),
          
          # (gt, ":total_lords_participating", 2),
          # (party_is_active, ":best_screening_party"),
          # (party_is_active, ":faction_marshal_party"),
          # (call_script, "script_party_set_ai_state", ":best_screening_party", spai_screening_army, ":faction_marshal_party"),
          # (try_begin),
          # (ge, "$cheat_mode", 1),
          # (str_store_party_name, s4, ":best_screening_party"),
          # (display_message, "@{!}DEBUG -- {s4} chosen as screen"),
          # (try_end),
          # #after this - dialogs on what doing, npc_decision_checklist
          # (try_end),
          
          #Offensive concludes
        (else_try),
          (store_current_hours, ":hours"),
          (this_or_next|eq, ":old_faction_ai_state", sfai_gathering_army),
          (this_or_next|eq, ":old_faction_ai_state", sfai_attacking_center),
          (this_or_next|eq, ":old_faction_ai_state", sfai_raiding_village),
          #(this_or_next|eq, ":old_faction_ai_state", sfai_attacking_enemies_around_center),
          (eq, ":old_faction_ai_state", sfai_attacking_enemy_army),
          
          (this_or_next|eq, ":new_strategy", sfai_default),
          (eq, ":new_strategy", sfai_feast),
          
          (call_script, "script_check_and_finish_active_army_quests_for_faction", ":faction_no"),
          (faction_set_slot, ":faction_no", slot_faction_last_offensive_concluded, ":hours"),
        (try_end),
      (try_end),
      
      (try_begin),
        (eq, "$players_kingdom", ":faction_no"),
        (neg|faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_center),
        (check_quest_active, "qst_join_siege_with_army"),
        (call_script, "script_abort_quest", "qst_join_siege_with_army", 0),
      (try_end),
      
      (try_begin),
        #old condition to rest, I changed below part - ozan, to rest (a faction's old strategy should be feast or default) and (a faction's new strategy should be feast or default)
        #(this_or_next|eq, ":new_strategy", sfai_default),
        #(this_or_next|eq, ":new_strategy", sfai_feast),
        #(this_or_next|eq, ":old_faction_ai_state", sfai_default),
        #(eq, ":old_faction_ai_state", sfai_feast),
        
        #new condition to rest, (a faction's new strategy should be feast or default) and (":hours_at_current_state" > 20)
        (this_or_next|eq, ":new_strategy", sfai_default),
        (eq, ":new_strategy", sfai_feast),
        
        (store_current_hours, ":hours_at_current_state"),
        (faction_get_slot, ":current_state_started", ":faction_no", slot_faction_ai_current_state_started),
        (val_sub, ":hours_at_current_state", ":current_state_started"),
        (ge, ":hours_at_current_state", 18), #Must have at least 18 hours to reset
        
        (store_current_hours, ":hours"),
        (faction_set_slot, ":faction_no", slot_faction_ai_last_rest_time, ":hours"),
      (try_end),
  ]),
  
  # script_check_and_finish_active_army_quests_for_faction
  # Input: faction_no
  # Output: none
  ("check_and_finish_active_army_quests_for_faction",
    [
      (store_script_param_1, ":faction_no"),
      (try_begin),
        (eq, "$players_kingdom", ":faction_no"),
        (try_begin),
          (check_quest_active, "qst_report_to_army"),
          (call_script, "script_cancel_quest", "qst_report_to_army"),
        (try_end),
        (assign, ":one_active", 0),
        (try_for_range, ":quest_no", army_quests_begin, army_quests_end),
          (check_quest_active, ":quest_no"),
          (call_script, "script_cancel_quest", ":quest_no"),
          (troop_get_slot, ":army_quest_giver_troop", ":quest_no", slot_quest_giver_troop),
          (assign, ":one_active", 1),
        (try_end),
        (try_begin),
          (check_quest_active, "qst_follow_army"),
          (assign, ":one_active", 1),
          (troop_get_slot, ":army_quest_giver_troop", "qst_follow_army", slot_quest_giver_troop),
          (call_script, "script_end_quest", "qst_follow_army"),
        (try_end),
        (eq, ":one_active", 1),
        (faction_get_slot, ":last_offensive_time", ":faction_no", slot_faction_last_offensive_concluded),
        (store_current_hours, ":cur_hours"),
        (store_sub, ":total_time_served", ":cur_hours", ":last_offensive_time"),
        (store_mul, ":xp_reward", ":total_time_served", 5),
        (val_div, ":xp_reward", 50),
        (val_mul, ":xp_reward", 50),
        (val_add, ":xp_reward", 50),
        (add_xp_as_reward, ":xp_reward"),
        (call_script, "script_troop_change_relation_with_troop", "trp_player", ":army_quest_giver_troop", 8),
      (try_end),
  ]),
  
  # script_troop_get_player_relation
  # Input: arg1 = troop_no
  # Output: reg0 = effective relation (modified by troop reputation, honor, etc.)
  # Modified by JuJu70
  ("troop_get_player_relation",
    [
      (store_script_param_1, ":troop_no"),
      (troop_get_slot, ":reputation", ":troop_no", slot_lord_reputation_type),
      (troop_get_slot, ":effective_relation", ":troop_no", slot_troop_player_relation),
      (troop_get_slot, ":tor", ":troop_no", slot_troop_tortured),
      (assign, ":torr", 0),
      (assign, ":honor_bonus", 0),
      (assign, ":ladyon", 0),
      (try_begin),
        (eq,  ":reputation", lrep_quarrelsome),
        (val_add, ":effective_relation", -3),
      (try_end),
      (try_begin),
        (gt, "$player_honor", 0),
        (assign, ":p_honor", "$player_honor"),
        (val_clamp, ":p_honor", -100, 101),
        (try_begin),
          (this_or_next|eq,  ":reputation", lrep_upstanding),
          (this_or_next|eq,  ":reputation", lrep_benefactor),
          (eq,  ":reputation", lrep_goodnatured),
          (store_div, ":honor_bonus", ":p_honor", 3),
        (try_end),
      (try_end),
      (try_begin),
        (lt, "$player_honor", 0),
        (assign, ":p_honor", "$player_honor"),
        (val_clamp, ":p_honor", -100, 101),
        (try_begin),
          (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_lady),
          (this_or_next|eq,  ":reputation", lrep_otherworldly),
          (this_or_next|eq,  ":reputation", lrep_conventional),
          (eq,  ":reputation", lrep_moralist),
          (store_div, ":honor_bonus", ":p_honor", 3),
        (try_end),
        (try_begin),
          (this_or_next|eq,  ":reputation", lrep_upstanding),
          (this_or_next|eq,  ":reputation", lrep_benefactor),
          (eq,  ":reputation", lrep_goodnatured),
          (store_div, ":honor_bonus", ":p_honor", 3),
        (else_try),
          (eq,  ":reputation", lrep_martial),
          (store_div, ":honor_bonus", ":p_honor", 5),
        (try_end),
      (try_end),
      (try_begin),
        (gt, ":tor", 0),
        (store_div, ":torr", ":tor", 3),
        (val_max, ":torr", 1),
        (val_mul, ":torr", -1),
      (try_end),
      (try_begin),
        (eq, "$special1", 2),
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_lady),
        (neg|troop_slot_eq, "trp_player", slot_troop_spouse, ":troop_no"),
        (neg|troop_slot_eq, ":troop_no", slot_troop_spouse, "trp_player"),
        (assign, ":ladyon", 3),
      (try_end),
      (val_add, ":effective_relation", ":honor_bonus"),
      (val_add,  ":effective_relation", ":torr"),
      (val_add, ":effective_relation", ":ladyon"),
      (val_clamp, ":effective_relation", -100, 101),
      (assign, reg0, ":effective_relation"),
  ]),
  
  # script_change_troop_renown
  # Input: arg1 = troop_no, arg2 = relation difference
  # Output: none
  ("change_troop_renown",
    [
      (store_script_param_1, ":troop_no"),
      (store_script_param_2, ":renown_change"),
      
      (troop_get_slot, ":old_renown", ":troop_no", slot_troop_renown),
      
      (try_begin),
        (gt, ":renown_change", 0),
        (assign, reg4, ":renown_change"),
        
        (store_div, ":subtraction", ":old_renown", 200),
        (val_sub, ":renown_change", ":subtraction"),
        (val_max, ":renown_change", 0),
        
        (eq, ":troop_no", "trp_player"),
        
        (assign, reg5, ":renown_change"),
        
        (eq, "$cheat_mode", 1),
        (display_message, "str_renown_change_of_reg4_reduced_to_reg5_because_of_high_existing_renown"),
      (try_end),
      
      (store_add, ":new_renown", ":old_renown", ":renown_change"),
      (val_max, ":new_renown", 0),
      (troop_set_slot, ":troop_no", slot_troop_renown, ":new_renown"),
      
      (try_begin),
        (eq, ":troop_no", "trp_player"),
        
        (try_begin),
          (ge, ":new_renown", 50),
          
          (try_begin),
            (troop_get_type, ":is_female", "trp_player"),
            (val_mod, ":is_female", 2),
            (eq, ":is_female", 1),
            (unlock_achievement, ACHIEVEMENT_TALK_OF_THE_TOWN),
          (try_end),
        (try_end),
        
        (str_store_troop_name, s1, ":troop_no"),
        (assign, reg12, ":renown_change"),
        (val_abs, reg12),
        (try_begin),
          (gt, ":renown_change", 0),
          (display_message, "@You gain {reg12} renown."),
        (else_try),
          (lt, ":renown_change", 0),
          (display_message, "@You lose {reg12} renown."),
        (try_end),
      (try_end),
      (call_script, "script_update_troop_notes", ":troop_no"),
  ]),
  
  
  # script_change_player_relation_with_troop
  # Input: arg1 = troop_no, arg2 = relation difference
  # Output: none
  ("change_player_relation_with_troop",
    [
      (store_script_param_1, ":troop_no"),
      (store_script_param_2, ":difference"),
      
      (try_begin),
        (neq, ":troop_no", "trp_player"),
        (neg|is_between, ":troop_no", soldiers_begin, soldiers_end),
        (neq, ":troop_no", -1),
        (neq, ":difference", 0),
        (call_script, "script_troop_get_player_relation", ":troop_no"),
        (assign, ":old_effective_relation", reg0),
        (troop_get_slot, ":player_relation", ":troop_no", slot_troop_player_relation),
        (val_add, ":player_relation", ":difference"),
        (val_clamp, ":player_relation", -100, 101),
        (try_begin),
          (troop_set_slot, ":troop_no", slot_troop_player_relation, ":player_relation"),
          
          (try_begin),
            (le, ":player_relation", -50),
            (unlock_achievement, ACHIEVEMENT_OLD_DIRTY_SCOUNDREL),
          (try_end),
          
          (str_store_troop_name_link, s1, ":troop_no"),
          (call_script, "script_troop_get_player_relation", ":troop_no"),
          (assign, ":new_effective_relation", reg0),
          (neq, ":old_effective_relation", ":new_effective_relation"),
          (assign, reg1, ":old_effective_relation"),
          (assign, reg2, ":new_effective_relation"),
          (try_begin),
            (gt, ":difference", 0),
            (display_message, "str_troop_relation_increased", color_good_news),
          (else_try),
            (lt, ":difference", 0),
            (display_message, "str_troop_relation_detoriated", color_bad_news),
          (try_end),
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            #phaiak begin
            (try_begin),
              (eq, "$campaign_type", camp_storyline),
              (eq, "$g_talk_troop", "trp_village_150_elder"),
              (try_begin),
                (ge, "$vc_debug_mode", 2),
                (display_message, "@{!} TEST in change_player_relation_with_troop"),
              (end_try),
            (else_try),
              (assign, "$g_talk_troop_relation", ":new_effective_relation"),
            (end_try),
            #phaiak end
            (call_script, "script_setup_talk_info"),
          (try_end),
          (call_script, "script_update_troop_notes", ":troop_no"),
        (try_end),
      (try_end),
  ]),
  
  # script_change_player_relation_with_center
  # Input: arg1 = party_no, arg2 = relation difference
  # Output: none
  ("change_player_relation_with_center",
    [
      (store_script_param_1, ":center_no"),
      (store_script_param_2, ":difference"),
      
      (try_begin),#new begin
        (gt, ":difference", 0),
        (options_get_campaign_ai, reg8), #0 = good, 1 = average, 2 = poor
        (eq, reg8, 2),
        (val_mul, ":difference", 125),
        (val_div, ":difference", 100),
      (try_end),#new end
      
      (party_get_slot, ":player_relation", ":center_no", slot_center_player_relation),
      (assign, reg1, ":player_relation"),
      (val_add, ":player_relation", ":difference"),
      (val_clamp, ":player_relation", -100, 100),
      (assign, reg2, ":player_relation"),
      (party_set_slot, ":center_no", slot_center_player_relation, ":player_relation"),
      
      (try_begin),
        (le, ":player_relation", -50),
        (unlock_achievement, ACHIEVEMENT_OLD_DIRTY_SCOUNDREL),
      (try_end),
      
      
      (str_store_party_name_link, s1, ":center_no"),
      (try_begin),
        (gt, ":difference", 0),
        (display_message, "@Your relation with {s1} has improved.", color_good_news),
      (else_try),
        (lt, ":difference", 0),
        (display_message, "@Your relation with {s1} has deteriorated.", color_bad_news),
      (try_end),
      (try_begin),
        (party_slot_eq, ":center_no", slot_party_type, spt_village),
        (call_script, "script_update_volunteer_troops_in_village", ":center_no"),
      (try_end),
      
      (try_begin),
        (this_or_next|is_between, "$g_talk_troop", village_elders_begin, village_elders_end),
        (this_or_next|is_between, "$g_talk_troop", "trp_castle_1_seneschal", "trp_town_1_arena_master"),
        (is_between, "$g_talk_troop", mayors_begin, mayors_end),
        # phaiak begin
        (neq, "$g_talk_troop", "trp_village_150_elder"),
        # phaiak end
        (assign, "$g_talk_troop_relation", ":player_relation"),
        (call_script, "script_setup_talk_info"),
      (try_end),
  ]),
  
  
  # script_change_player_relation_with_faction
  # Input: arg1 = faction_no, arg2 = relation difference
  # Output: reg1 previous relation, reg2 new relation
  ("change_player_relation_with_faction",
    [
      (store_script_param_1, ":faction_no"),
      (store_script_param_2, ":difference"),
      
      (store_relation, ":player_relation", ":faction_no", "fac_player_faction"),  #keeps track of "real" player relation
      (assign, ":old_relation", ":player_relation"),
      (val_add, ":player_relation", ":difference"),
      
      #instant war
      (try_begin),
        (is_between, ":faction_no", npc_kingdoms_begin, npc_kingdoms_end),
        (eq, "$players_kingdom", "fac_player_supporters_faction"),  #player kingdom?
        (ge, ":old_relation", 0),  #not already at war?
        (lt, ":player_relation", -9), #old boundary provocation/war (see script_diplomacy_start_war_between_kingdoms)
        (call_script, "script_diplomacy_start_war_between_kingdoms", "fac_player_supporters_faction", ":faction_no", logent_player_faction_declares_war),
        
      #log a provocation
      (else_try),
        (str_clear, s2),
        (is_between, ":faction_no", npc_kingdoms_begin, npc_kingdoms_end),
        (lt, ":player_relation", 0),
        (lt, ":difference", 0), #new provocation?
        
        (try_begin),
          (neq, "$players_kingdom", "fac_player_supporters_faction"),  #no player kingdom?
          (store_mul, ":num_days", ":difference", -1),
          (val_min, ":num_days", 30),
        (else_try),
          (assign, ":num_days", 30),  #log a provocation rather than starting a war immediately (see 'instant war' above)
        (try_end),
        
        (store_add, ":slot_provocation_days", "fac_player_supporters_faction", slot_faction_provocation_days_with_factions_begin),
        (val_sub, ":slot_provocation_days", kingdoms_begin),
        
        (neg|faction_slot_ge, ":faction_no", ":slot_provocation_days", ":num_days"),
        (faction_set_slot, ":faction_no", ":slot_provocation_days", ":num_days"),
        (str_store_string, s2, "@You have angered them."),
        (call_script, "script_set_player_relation_with_faction", ":faction_no", ":player_relation"),
        
      #wait for normal mechanism to end a war
      (else_try),
        (is_between, ":faction_no", npc_kingdoms_begin, npc_kingdoms_end),
        (eq, "$players_kingdom", "fac_player_supporters_faction"),  #player kingdom?
        (lt, ":old_relation", 0),
        (val_min, ":player_relation", -1),
        (call_script, "script_set_player_relation_with_faction", ":faction_no", ":player_relation"),
        
      #end a provocation
      (else_try),
        (is_between, ":faction_no", npc_kingdoms_begin, npc_kingdoms_end),
        (gt, ":player_relation", 9),
        
        (store_add, ":slot_provocation_days", "fac_player_supporters_faction", slot_faction_provocation_days_with_factions_begin),
        (val_sub, ":slot_provocation_days", kingdoms_begin),
        
        (faction_slot_ge, ":faction_no", ":slot_provocation_days", 1),
        (faction_set_slot, ":faction_no", ":slot_provocation_days", 0),
        (str_store_string, s2, "@You have appeased them."),
        (call_script, "script_set_player_relation_with_faction", ":faction_no", ":player_relation"),
        
      #default
      (else_try),
        (call_script, "script_set_player_relation_with_faction", ":faction_no", ":player_relation"),
      (try_end),
      
      (assign, reg1, ":old_relation"),
      (store_relation, reg2, ":faction_no", "fac_player_faction"),
      (str_store_faction_name_link, s1, ":faction_no"),
      (try_begin),
        (gt, ":difference", 0),
        (display_message, "str_faction_relation_increased",color_good_news),
      (else_try),
        (lt, ":difference", 0),
        (display_message, "str_faction_relation_detoriated",color_bad_news),
      (try_end),
  ]),
  
  # script_set_player_relation_with_faction
  # Input: arg1 = faction_no, arg2 = relation
  # Output: none
  ("set_player_relation_with_faction",
    [
      (store_script_param_1, ":faction_no"),
      (store_script_param_2, ":player_relation"),
      
      (try_begin),
        (le, ":player_relation", -50),
        (unlock_achievement, ACHIEVEMENT_OLD_DIRTY_SCOUNDREL),
      (try_end),
      
      (val_min, ":player_relation", 100),
      (val_max, ":player_relation", -100),
      
      (set_relation, ":faction_no", "fac_player_faction", ":player_relation"),
      
      #player has no kingdom
      (try_begin),
        (is_between, ":faction_no", kingdoms_begin, kingdoms_end),
        (neq, "$players_kingdom", "fac_player_supporters_faction"),
        (val_max, ":player_relation", 0), #can't be at war
      (try_end),
      
      (set_relation, ":faction_no", "fac_player_supporters_faction", ":player_relation"),
      (call_script, "script_update_all_notes"),
  ]),
  
  # script_change_player_relation_with_faction_ex
  # changes relations with other factions also (according to their relations between each other)
  # Input: arg1 = faction_no, arg2 = relation difference
  # Output: none
  # ("change_player_relation_with_faction_ex",
    # [
      # (store_script_param_1, ":faction_no"),
      # (store_script_param_2, ":difference"),
      
      # (call_script, "script_change_player_relation_with_faction", ":faction_no", ":difference"),
      
      # (try_for_range, ":other_faction", kingdoms_begin, kingdoms_end),
        # (faction_slot_eq, ":other_faction", slot_faction_state, sfs_active),
        # (neq, ":faction_no", ":other_faction"),
        # (store_relation, ":other_faction_relation", ":faction_no", ":other_faction"),
        # (store_relation, ":player_relation", ":other_faction", "fac_player_faction"),
        # (store_mul, ":relation_change", ":difference", ":other_faction_relation"),
        # (val_div, ":relation_change", 100),
        # (val_add, ":player_relation", ":relation_change"),
        # (neq, ":relation_change", 0),
        # (call_script, "script_change_player_relation_with_faction" ,":other_faction", ":relation_change"),
      # (try_end),
      # # (try_begin),  done in script_change_player_relation_with_faction
      # # (faction_slot_eq, "fac_player_supporters_faction", slot_faction_state, sfs_active),
      # # (try_for_range, ":kingdom_no", kingdoms_begin, kingdoms_end),
      # # (faction_slot_eq, ":kingdom_no", slot_faction_state, sfs_active),
      # # (call_script, "script_update_faction_notes", ":kingdom_no"),
      # # (try_end),
      # # (try_end),
  # ]),
  
  # script_cf_get_random_active_faction_except_player_faction_and_faction
  # Input: arg1 = except_faction_no
  # Output: reg0 = random_faction
  ("cf_get_random_active_faction_except_player_faction_and_faction",
    [
      (store_script_param_1, ":except_faction_no"),
      (assign, ":num_factions", 0),
      (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
        (neq, ":faction_no", "fac_player_supporters_faction"),
        (neq, ":faction_no", ":except_faction_no"),
        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        (val_add, ":num_factions", 1),
      (try_end),
      (gt, ":num_factions", 0),
      (assign, ":selected_faction", -1),
      (store_random_in_range, ":random_faction", 0, ":num_factions"),
      (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
        (ge, ":random_faction", 0),
        (neq, ":faction_no", "fac_player_supporters_faction"),
        (neq, ":faction_no", ":except_faction_no"),
        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        (val_sub, ":random_faction", 1),
        (lt, ":random_faction", 0),
        (assign, ":selected_faction", ":faction_no"),
      (try_end),
      (assign, reg0, ":selected_faction"),
  ]),
  
  # script_make_kingdom_hostile_to_player
  # Input: arg1 = faction_no, arg2 = relation difference
  # Output: none
  # Wipes out all positive relation
  ("make_kingdom_hostile_to_player",
    [
      (store_script_param_1, ":kingdom_no"),
      (store_script_param_2, ":difference"),
      
      (store_relation, ":player_relation", ":kingdom_no", "fac_player_faction"),
      
      (try_begin),
        (gt, ":player_relation", 0),
        (val_sub, ":difference", ":player_relation"),
      (try_end),
      
      (try_begin),
        (neq, ":difference", 0),
        (call_script, "script_change_player_relation_with_faction", ":kingdom_no", ":difference"),
      (try_end),
  ]),
  
  # script_change_player_honor
  # Input: arg1 = honor difference
  # Output: none
  ("change_player_honor",
    [
      (store_script_param_1, ":honor_dif"),
      (val_add, "$player_honor", ":honor_dif"),
      (try_begin),
        (gt, ":honor_dif", 0),
        (display_message, "@You gain reputation.", color_good_news),
      (else_try),
        (lt, ":honor_dif", 0),
        (display_message, "@You lose reputation.", color_bad_news),
      (try_end),
      (try_begin),
        (assign, reg1, "$player_honor"),
        (display_message, "@Now your reputation is {reg1}"),
      (try_end),
      
      ##      (val_mul, ":honor_dif", 1000),
      ##      (assign, ":temp_honor", 0),
      ##      (assign, ":num_nonlinear_steps", 10),
      ##      (try_begin),
      ##        (gt, "$player_honor", 0),
      ##        (lt, ":honor_dif", 0),
      ##        (assign, ":num_nonlinear_steps", 0),
      ##      (else_try),
      ##        (lt, "$player_honor", 0),
      ##        (gt, ":honor_dif", 0),
      ##        (assign, ":num_nonlinear_steps", 3),
      ##      (try_end),
      ##
      ##      (try_begin),
      ##        (ge, "$player_honor", 0),
      ##        (assign, ":temp_honor", "$player_honor"),
      ##      (else_try),
      ##        (val_sub, ":temp_honor", "$player_honor"),
      ##      (try_end),
      ##      (try_for_range, ":unused",0,":num_nonlinear_steps"),
      ##        (ge, ":temp_honor", 10000),
      ##        (val_div, ":temp_honor", 2),
      ##        (val_div, ":honor_dif", 2),
      ##      (try_end),
      ##      (val_add, "$player_honor", ":honor_dif"),
  ]),
  
  # script_change_player_party_morale
  # Input: arg1 = morale difference
  # Output: none
  ("change_player_party_morale",
    [
      (store_script_param_1, ":morale_dif"),
      (party_get_morale, ":cur_morale", "p_main_party"),
      (val_clamp, ":cur_morale", 0, 100),
      
      (store_add, ":new_morale", ":cur_morale", ":morale_dif"),
      (val_clamp, ":new_morale", 0, 100),
      
      (party_set_morale, "p_main_party", ":new_morale"),
      (try_begin),
        (lt, ":new_morale", ":cur_morale"),
        (store_sub, reg1, ":cur_morale", ":new_morale"),
        (display_message, "str_party_lost_morale", color_bad_news),
      (else_try),
        (gt, ":new_morale", ":cur_morale"),
        (store_sub, reg1, ":new_morale", ":cur_morale"),
        (display_message, "str_party_gained_morale", color_good_news),
      (try_end),
  ]),
  
  # script_cf_player_has_item_without_modifier
  # Input: arg1 = item_id, arg2 = modifier
  # Output: none (can_fail)
  ("cf_player_has_item_without_modifier",
    [
      (store_script_param, ":item_id", 1),
      (store_script_param, ":modifier", 2),
      (player_has_item, ":item_id"),
      #checking if any of the meat is not rotten
      (assign, ":has_without_modifier", 0),
      (troop_get_inventory_capacity, ":inv_size", "trp_player"),
      (try_for_range, ":i_slot", 0, ":inv_size"),
        (troop_get_inventory_slot, ":cur_item", "trp_player", ":i_slot"),
        (eq, ":cur_item", ":item_id"),
        (troop_get_inventory_slot_modifier, ":cur_modifier", "trp_player", ":i_slot"),
        (neq, ":cur_modifier", ":modifier"),
        (assign, ":has_without_modifier", 1),
        (assign, ":inv_size", 0), #break
      (try_end),
      (eq, ":has_without_modifier", 1),
  ]),
  
  # script_get_player_party_morale_values modified by motomataru moto chief
  # Output: reg0 = player_party_morale_target
  ("get_player_party_morale_values",
    [
      (assign, ":new_morale", 50),
      (assign, ":num_men", 0),
      (assign, "$g_player_party_morale_modifier_religion", 0),
      
      (party_get_num_companion_stacks, ":num_stacks","p_main_party"),
      (try_for_range, ":i_stack", 1, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop","p_main_party",":i_stack"),
        (try_begin),
          (troop_is_hero, ":stack_troop"),
          (val_add, ":num_men", 1), #it was 3 in "Mount&Blade", now it is 1 in Warband
          
          (try_begin),
            (call_script, "script_cf_troop_religion_matches_players", ":stack_troop"),
          (else_try),
            (val_add, "$g_player_party_morale_modifier_religion", 1),
          (try_end),
          
        (else_try),
          (party_stack_get_size, ":stack_size","p_main_party",":i_stack"),
          (val_add, ":num_men", ":stack_size"),
          
          (try_begin),
            (call_script, "script_cf_troop_religion_matches_players", ":stack_troop"),
          (else_try),
            (val_add, "$g_player_party_morale_modifier_religion", ":stack_size"),
          (try_end),
        (try_end),
      (try_end),
      
      (call_script, "script_game_get_party_companion_limit"),
      
      (try_begin),
        (party_slot_eq, "p_main_party", slot_party_on_water, 1),
        (assign, reg0, "$temp"), #(VC-3389)
      (end_try),
      
      (val_div, reg0, 2),    #this is how many the player can lead effectively (so average morale)
      (try_begin),
        (ge, reg0, ":num_men"),
        # (val_mul,":num_companions", 4),   #chief added for companions positive adjustment to morale
        (assign, "$g_player_party_morale_modifier_party_size", 0),
      (else_try),
        (store_sub, "$g_player_party_morale_modifier_party_size", ":num_men", reg0),
        (val_mul, "$g_player_party_morale_modifier_party_size", 100),
        (val_div, "$g_player_party_morale_modifier_party_size", reg0),
        (val_div, "$g_player_party_morale_modifier_party_size", 3),    #up to -33 effect
        (val_sub, ":new_morale", "$g_player_party_morale_modifier_party_size"),
      (try_end),
      
      (assign, "$g_player_party_morale_modifier_food", -15),    #troops expect basic bread + meat
      (try_for_range, ":cur_edible", food_begin, food_end),
        (call_script, "script_cf_player_has_item_without_modifier", ":cur_edible", imod_rotten),
        (item_get_slot, ":food_bonus", ":cur_edible", slot_item_food_bonus),
        (val_add, "$g_player_party_morale_modifier_food", ":food_bonus"),
      (try_end),
      (val_add, ":new_morale", "$g_player_party_morale_modifier_food"),
      
      (try_begin),
        (eq, "$g_player_party_morale_modifier_food", -15),
        (assign, "$g_player_party_morale_modifier_no_food", 15),
        (val_sub, ":new_morale", "$g_player_party_morale_modifier_no_food"),
      (else_try),
        (assign, "$g_player_party_morale_modifier_no_food", 0),
      (try_end),
      
      (assign, "$g_player_party_morale_modifier_debt", 0),
      (try_begin),
        (gt, "$g_player_debt_to_party_members", 0),
        (call_script, "script_calculate_player_faction_wage"),
        (gt, reg0, 0),    #if no more party, then no more morale penalty!
        (assign, ":new_wages", reg0),
        (assign, ":num_current_party_members", reg1),
        
        (store_mul, "$g_player_party_morale_modifier_debt", "$g_player_debt_to_party_members", 100),
        (try_begin),    #if party has grown, then morale impact of debt is diluted
          (gt, ":num_current_party_members", "$g_player_num_creditors"),
          (val_div, "$g_player_party_morale_modifier_debt", ":new_wages"),
        (else_try),
          (val_div, "$g_player_party_morale_modifier_debt", "$g_player_old_wages"),
        (try_end),
        (val_div, "$g_player_party_morale_modifier_debt", 3),    #target -33 effect for each week without ANY pay
        (val_sub, ":new_morale", "$g_player_party_morale_modifier_debt"),
      (try_end),
      
      ##religion
      #religion opuesta tropas chief
      (val_add, ":num_men", 1), #consider player for this
      (val_mul, "$g_player_party_morale_modifier_religion" ,10),  #max penalty 10
      (val_div, "$g_player_party_morale_modifier_religion" ,":num_men"),
      (val_sub, ":new_morale", "$g_player_party_morale_modifier_religion"),
      #religion chief acaba
      
      #####
      (party_get_slot, "$g_player_party_morale_modifier_weariness", "p_main_party", slot_party_unrested_morale_penalty),
      (val_sub, ":new_morale", "$g_player_party_morale_modifier_weariness"),
      
      (val_clamp, ":new_morale", 0, 101),
      (assign, reg0, ":new_morale"),
  ]),
  
  # script_diplomacy_start_war_between_kingdoms
  # MOTO chief rewrite to capture log entries
  # Input: arg1 = kingdom_1, arg2 = kingdom_2, arg3 =
  # 0 war starts after running script_npc_decision_checklist_peace_or_war - initializing game
  # 1 war starts after running script_npc_decision_checklist_peace_or_war
  # logent other reason war starts
  # Output: none
  ("diplomacy_start_war_between_kingdoms", #sets relations between two kingdoms and their vassals.
    [
      (store_script_param, ":kingdom_a", 1),
      (store_script_param, ":kingdom_b", 2),
      (store_script_param, ":initializing_war_peace_cond", 3), #1 = after start of game
      
      (try_begin),
        (gt, ":initializing_war_peace_cond", 1),	#war cause specified?
        (assign, ":war_event", ":initializing_war_peace_cond"),
        (assign, ":initializing_war_peace_cond", 1),	#must be after game initialized
      (else_try),
        (call_script, "script_npc_decision_checklist_peace_or_war", ":kingdom_a", ":kingdom_b", -1),
        (assign, ":explainer_string", reg1),
        
        #
        (try_begin),
          (eq, ":kingdom_a", "fac_player_supporters_faction"),
          (assign, ":war_event", logent_player_faction_declares_war),
        (else_try),
          (eq, ":explainer_string", "str_s12s15_declared_war_to_control_calradia"),
          (assign, ":war_event", logent_player_faction_declares_war), #for savegame compatibility, this event stands in for the attempt to declare war on all of calradia
        (else_try),
          (eq, ":explainer_string", "str_s12s15_considers_s16_to_be_dangerous_and_untrustworthy_and_shehe_wants_to_bring_s16_down"),
          (assign, ":war_event", logent_faction_declares_war_out_of_personal_enmity),
        (else_try),
          (eq, ":explainer_string", "str_s12s15_is_anxious_to_reclaim_old_lands_such_as_s18_now_held_by_s16"),
          (assign, ":war_event", logent_faction_declares_war_to_regain_territory),
        (else_try),
          (eq, ":explainer_string", "str_s12s15_faces_too_much_internal_discontent_to_feel_comfortable_ignoring_recent_provocations_by_s16s_subjects"),
          (assign, ":war_event", logent_faction_declares_war_to_respond_to_provocation),
        (else_try),
          (eq, ":explainer_string", "str_s12s15_is_alarmed_by_the_growing_power_of_s16"),
          (assign, ":war_event", logent_faction_declares_war_to_curb_power),
        (else_try),
          (eq, ":explainer_string", "str_s12s15_dominates_its_weaker_neighbor_s16"),
          (assign, ":war_event", logent_player_faction_declares_war), #for savegame compatibility, this event stands in for the attempt to declare war on all of calradia
        (else_try),
          (eq, ":explainer_string", "str_s12s15_acts_to_drive_the_people_of_s16_and_their_like_out_of_the_Isles"),
          (assign, ":war_event", logent_faction_declares_war_out_of_personal_enmity),
        (else_try),
          (assign, reg1, ":explainer_string"),
          (ge, "$cheat_mode", 1),#or this will be visible in game.
          (display_message, "@{!}script_diplomacy_start_war_between_kingdoms: unaccounted explainer string {reg1}"), #this will be visible in game.
        (try_end),
      (try_end),
      (call_script, "script_add_log_entry", ":war_event", ":kingdom_a", 0, 0, ":kingdom_b"),
      (call_script, "script_diplomacy_faction_get_diplomatic_status_with_faction", ":kingdom_a", ":kingdom_b"),
      (assign, ":current_diplomatic_status", reg0),
      
      (try_begin), #effects of policy only after the start of the game #chief add extra consequences
        (eq, ":war_event", logent_faction_declares_war_to_fulfil_alliance),
        (call_script, "script_faction_follows_controversial_policy", ":kingdom_a", logent_policy_ruler_declares_war_with_justification),
      (else_try),
        #MOTO chief end rewrite for log entries
        (eq, ":initializing_war_peace_cond", 1),
        (eq, ":current_diplomatic_status", -1), #normal war = no extra consequences
        (call_script, "script_faction_follows_controversial_policy", ":kingdom_a", logent_policy_ruler_declares_war_with_justification),
      (else_try),
        (eq, ":initializing_war_peace_cond", 1),
        (eq, ":current_diplomatic_status", 0), #attack neutral
        (call_script, "script_faction_follows_controversial_policy", ":kingdom_a", logent_policy_ruler_attacks_without_provocation),
        ###chief
        ##                            (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
        ##                                  (faction_get_slot, ":fac1_culture", ":kingdom_b", slot_faction_culture),
        ##                                (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        ##                                 (neq, "fac_player_supporters_faction", ":faction_no"), #should this affect to player too when other factions start war? Maybe no, player could lose control in game
        ##                                 (neq, ":kingdom_a", ":faction_no"), #should this affect to player too when other factions start war? Maybe no, player could lose control in game
        ##                                 (faction_get_slot, ":fac2_culture", ":faction_no", slot_faction_culture),
        ##                                 (eq, ":fac1_culture", ":fac2_culture"),
        ##                                      (call_script, "script_change_player_relation_with_faction", ":faction_no", -5),
        ##                              (try_end),
        #####
      (else_try),
        (eq, ":current_diplomatic_status", 1), #attack with truce
        (call_script, "script_faction_follows_controversial_policy", ":kingdom_a", logent_policy_ruler_breaks_truce),
        ###chief # too complicate factions relations remove
        ##                            (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
        ##                                  (faction_get_slot, ":fac1_culture", ":kingdom_b", slot_faction_culture),
        ##                                (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        ##                                 (neq, "fac_player_supporters_faction", ":faction_no"), #should this affect to player too when other factions start war? Maybe no, player could lose control in game
        ##                                 (neq, ":kingdom_a", ":faction_no"), #should this affect to player too when other factions start war? Maybe no, player could lose control in game
        ##                                 (faction_get_slot, ":fac2_culture", ":faction_no", slot_faction_culture),
        ##                                 (eq, ":fac1_culture", ":fac2_culture"),
        ##                                      (call_script, "script_change_player_relation_with_faction", ":faction_no", -10),
        ##                              (try_end),
        #####
        
      (try_end),
      
      (store_relation, ":relation", ":kingdom_a", ":kingdom_b"),
      (val_min, ":relation", -10),
      (val_add, ":relation", -30),
      (set_relation, ":kingdom_a", ":kingdom_b", ":relation"),
      
      (try_begin),
        (eq, "$players_kingdom", ":kingdom_a"),
        (call_script, "script_player_set_faction_relation_ceiling", ":kingdom_b", -10, 0), #VC-3960 new enemy factions can be just suspicious
      (else_try),
        (eq, "$players_kingdom", ":kingdom_b"),
        (call_script, "script_player_set_faction_relation_ceiling", ":kingdom_a", -10, 0),
      (try_end),
      
      (try_begin),
        (eq, ":initializing_war_peace_cond", 1),
        
        (str_store_faction_name_link, s1, ":kingdom_a"),
        (str_store_faction_name_link, s2, ":kingdom_b"),
        (display_log_message, "@The {s1} has declared war against the {s2}.", color_quest_and_faction_news),
        
        (store_current_hours, ":hours"),
        (faction_set_slot, ":kingdom_a", slot_faction_ai_last_decisive_event, ":hours"),
        (faction_set_slot, ":kingdom_b", slot_faction_ai_last_decisive_event, ":hours"),
        
        #set provocation and truce days
        (store_add, ":truce_slot", ":kingdom_b", slot_faction_truce_days_with_factions_begin),
        (store_add, ":provocation_slot", ":kingdom_b", slot_faction_provocation_days_with_factions_begin),
        (val_sub, ":truce_slot", kingdoms_begin),
        (val_sub, ":provocation_slot", kingdoms_begin),
        (faction_set_slot, ":kingdom_a", ":truce_slot", 0),
        (faction_set_slot, ":kingdom_a", ":provocation_slot", 0),
        
        (store_add, ":truce_slot", ":kingdom_a", slot_faction_truce_days_with_factions_begin),
        (store_add, ":provocation_slot", ":kingdom_a", slot_faction_provocation_days_with_factions_begin),
        (val_sub, ":truce_slot", kingdoms_begin),
        (val_sub, ":provocation_slot", kingdoms_begin),
        (faction_set_slot, ":kingdom_b", ":truce_slot", 0),
        (faction_set_slot, ":kingdom_b", ":provocation_slot", 0),
        
        (call_script, "script_add_notification_menu", "mnu_notification_war_declared", ":kingdom_a", ":kingdom_b"),
        
        (call_script, "script_update_faction_notes", ":kingdom_a"),
        (call_script, "script_update_faction_notes", ":kingdom_b"),
        (assign, "$g_recalculate_ais", 1),
      (try_end),
      
      (try_begin),
        (check_quest_active, "qst_cause_provocation"),
        (neg|check_quest_succeeded, "qst_cause_provocation"),
        (this_or_next|eq, "$players_kingdom", ":kingdom_a"),
        (eq, "$players_kingdom", ":kingdom_b"),
        (call_script, "script_abort_quest", "qst_cause_provocation", 0),
      (try_end),
      #moto diplomacy alliance
      (try_for_range, ":cur_kingdom", kingdoms_begin, kingdoms_end),
        (neq, ":cur_kingdom", ":kingdom_a"),
        (neq, ":cur_kingdom", ":kingdom_b"),
        
        (store_relation, ":cur_relation", ":cur_kingdom", ":kingdom_b"),
        (ge, ":cur_relation", 0), #AT PEACE
        
        (store_add, ":truce_slot", ":kingdom_a", slot_faction_truce_days_with_factions_begin),
        (val_sub, ":truce_slot", kingdoms_begin),
        (faction_get_slot, ":truce_days", ":cur_kingdom", ":truce_slot"),
        (gt, ":truce_days", truce_time), #truce time = final alliance
        #no truce o alliance
        (call_script, "script_diplomacy_faction_get_diplomatic_status_with_faction",":cur_kingdom", ":kingdom_b"),
        (ge, reg0, -1),
        (lt, reg0, 1), #but respect others
        
        (assign, "$g_last_acting_faction", ":cur_kingdom"),
        (assign, "$g_last_target_faction", ":kingdom_b"),
        (str_store_faction_name, s15, ":cur_kingdom"),
        (str_store_faction_name, s16, ":kingdom_b"),
        (str_store_faction_name, s17, ":kingdom_a"),
        (str_store_string, s64, "@The {s15} complies with its alliance with the {s17} by attacking the {s16}."),
        # MOTO build explanation string end
        (call_script, "script_diplomacy_start_war_between_kingdoms", ":cur_kingdom", ":kingdom_b", logent_faction_declares_war_to_fulfil_alliance),	#MOTO chief pass log entries
        #  		(call_script, "script_diplomacy_start_war_between_kingdoms", ":cur_kingdom", ":kingdom_b", 3),
      (try_end),
      ##moto diplomacy
  ]),
  
  
  ("diplomacy_party_attacks_neutral", #called from game_menus (plundering a village, raiding a village),  from dialogs: surprise attacking a neutral lord, any attack on caravan or villagers
    #Has no effect if factions are already at war
    [
      (store_script_param, ":attacker_party", 1),
      (store_script_param, ":defender_party", 2),
      
      (store_faction_of_party, ":attacker_faction", ":attacker_party"),
      (store_faction_of_party, ":defender_faction", ":defender_party"),
      
      (party_stack_get_troop_id, ":attacker_leader", ":attacker_party", 0),
      
      (try_begin),
        (eq, ":attacker_party", "p_main_party"),
        
        (assign, ":attacker_faction", "$players_kingdom"),
        (try_begin),
          (eq, ":attacker_faction", "fac_no_faction"),
          (assign, ":attacker_faction", "fac_player_supporters_faction"),
        (try_end),
        
        (store_relation, ":relation", ":attacker_faction", ":defender_faction"),
        (ge, ":relation", 0),
        (call_script, "script_change_player_honor", -2),
      (try_end),
      
      (try_begin),
        (check_quest_active, "qst_cause_provocation"),
        (quest_slot_eq, "qst_cause_provocation", slot_quest_target_faction, ":defender_faction"),
        (quest_get_slot, ":giver_troop", "qst_cause_provocation", slot_quest_giver_troop),
        (store_faction_of_troop, ":attacker_faction", ":giver_troop"),
        # (call_script, "script_succeed_quest", "qst_cause_provocation"),
      (try_end),
      
      (call_script, "script_diplomacy_faction_get_diplomatic_status_with_faction", ":attacker_faction", ":defender_faction"),
      (assign, ":diplomatic_status", reg0),
      
      (try_begin),
        (eq, ":attacker_faction", "fac_player_supporters_faction"),
        (neg|faction_slot_eq, "fac_player_supporters_faction", slot_faction_state, sfs_active),	#player faction inactive, no effect
      (else_try),
        (eq, ":diplomatic_status", -2),	#war, no effect
      (else_try),
        (eq, ":attacker_faction", "fac_player_supporters_faction"),
        (faction_slot_eq, ":attacker_faction", slot_faction_leader, "trp_player"),
        (call_script, "script_faction_follows_controversial_policy", "fac_player_supporters_faction",logent_policy_ruler_attacks_without_provocation),
      (else_try),
        (eq, ":diplomatic_status", 1),	#truce
        (party_stack_get_troop_id, ":defender_party_leader", ":defender_party", 0),
        (try_begin),
          (neg|is_between, ":defender_party_leader", active_npcs_begin, active_npcs_end),
          (store_faction_of_party, ":defender_party_faction", ":defender_party"),
          (faction_get_slot, ":defender_party_leader", ":defender_party_faction", slot_faction_leader),
        (try_end),
        
        (call_script, "script_add_log_entry", logent_border_incident_troop_breaks_truce, ":attacker_leader", -1, ":defender_party_leader", ":attacker_faction"),
      (else_try),
        (call_script, "script_add_log_entry", logent_border_incident_troop_attacks_neutral, ":attacker_leader", -1, ":defender_party_leader", ":attacker_faction"),
      (try_end),
      
      (try_begin),
        (is_between, ":defender_party", villages_begin, villages_end),
        (call_script, "script_add_log_entry", logent_village_raided, ":attacker_leader",  ":defender_party", -1, ":defender_faction"),
      (else_try),
        (party_get_template_id, ":template", ":defender_party"),
        (neq, ":template", "pt_kingdom_hero_party"),
        (try_begin),
          (ge, "$cheat_mode", 1),
          (str_store_faction_name, s5, ":defender_faction"),
          (display_message, "@{!}DEbug - {s5} caravan attacked"),
        (try_end),
        
        (call_script, "script_add_log_entry", logent_caravan_accosted, ":attacker_leader",  -1, -1, ":defender_faction"),
      (try_end),
      
      (store_add, ":slot_truce_days", ":attacker_faction", slot_faction_provocation_days_with_factions_begin),
      (val_sub, ":slot_truce_days", kingdoms_begin),
      (faction_set_slot, ":defender_faction", ":slot_truce_days", 0),
      
      (store_add, ":slot_provocation_days", ":attacker_faction", slot_faction_provocation_days_with_factions_begin),
      (val_sub, ":slot_provocation_days", kingdoms_begin),
      (try_begin),
        (neq, ":diplomatic_status", -2),
        (faction_slot_eq, ":defender_faction", ":slot_provocation_days", 0),
        (faction_set_slot, ":defender_faction", ":slot_provocation_days", 30),
      (try_end),
  ]),
  
  # script_party_calculate_and_set_nearby_friend_enemy_follower_strengths
  # Input: party_no
  # Output: none
  ("party_calculate_and_set_nearby_friend_enemy_follower_strengths",
    [
      (store_script_param, ":party_no", 1),
      (assign, ":follower_strength", 0),
      (assign, ":friend_strength", 0),
      (assign, ":enemy_strength", 0),
      (store_faction_of_party, ":party_faction", ":party_no"),
      
      (store_add, ":end_cond", active_npcs_end, 1),
      (try_for_range, ":iteration", active_npcs_begin, ":end_cond"),
        (try_begin),
          (eq, ":iteration", active_npcs_end),
          (assign, ":cur_troop", "trp_player"),
        (else_try),
          (assign, ":cur_troop", ":iteration"),
        (try_end),
        
        (troop_slot_eq, ":cur_troop", slot_troop_occupation, slto_kingdom_hero),
        (troop_get_slot, ":cur_troop_party", ":cur_troop", slot_troop_leaded_party),
        (ge, ":cur_troop_party", 0),
        (party_is_active, ":cur_troop_party"),
        
        
        #I moved these lines here from (*1) to faster process, ozan.
        (store_troop_faction, ":army_faction", ":cur_troop"),
        (store_relation, ":relation", ":army_faction", ":party_faction"),
        (this_or_next|neq, ":relation", 0),
        (eq, ":army_faction", ":party_faction"),
        #ozan end
        
        
        (neq, ":party_no", ":cur_troop_party"),
        (party_get_slot, ":str", ":cur_troop_party", slot_party_cached_strength),
        (try_begin),
          (neg|is_between, ":party_no", centers_begin, centers_end),
          (party_slot_eq, ":cur_troop_party", slot_party_ai_state, spai_accompanying_army),
          (party_get_slot, ":commander_party", ":cur_troop_party", slot_party_ai_object),
          (eq, ":commander_party", ":party_no"),
          (val_add, ":follower_strength", ":str"),
        (else_try),
          (store_distance_to_party_from_party, ":distance", ":cur_troop_party", ":party_no"),
          (lt, ":distance", 20),
          
          #(*1)
          
          (try_begin),
            (lt, ":distance", 5),
            (assign, ":str_divided", ":str"),
          (else_try),
            (lt, ":distance", 10),
            (store_div, ":str_divided", ":str", 2),
          (else_try),
            (lt, ":distance", 15),
            (store_div, ":str_divided", ":str", 4),
          (else_try),
            (store_div, ":str_divided", ":str", 8),
          (try_end),
          
          (try_begin),
            (this_or_next|eq, ":army_faction", ":party_faction"),
            (gt, ":relation", 0),
            (val_add, ":friend_strength", ":str_divided"),
          (else_try),
            (lt, ":relation", 0),
            (val_add, ":enemy_strength", ":str_divided"),
          (try_end),
        (try_end),
      (try_end),
      
      (party_set_slot, ":party_no", slot_party_follower_strength, ":follower_strength"),
      (party_set_slot, ":party_no", slot_party_nearby_friend_strength, ":friend_strength"),
      (party_set_slot, ":party_no", slot_party_nearby_enemy_strength, ":enemy_strength"),
  ]),
  
  # script_init_ai_calculation
  # Input: none
  # Output: none
  ("init_ai_calculation",
    [
      (try_for_range, ":cur_troop", active_npcs_begin, active_npcs_end),
        (troop_slot_eq, ":cur_troop", slot_troop_occupation, slto_kingdom_hero),
        (troop_get_slot, ":cur_party", ":cur_troop", slot_troop_leaded_party),
        (party_is_active, ":cur_party"),
        (call_script, "script_party_calculate_strength", ":cur_party", 0), #will update slot_party_cached_strength
      (try_end),
      (call_script, "script_party_calculate_strength", "p_main_party", 0), #will update slot_party_cached_strength
      (try_for_range, ":cur_center", walled_centers_begin, walled_centers_end),
        (call_script, "script_party_calculate_strength", ":cur_center", 0), #will update slot_party_cached_strength
      (try_end),
      
      (try_for_range, ":cur_center", centers_begin, centers_end),	#MOTO chief include villages
        (call_script, "script_party_calculate_and_set_nearby_friend_enemy_follower_strengths", ":cur_center"),
      (try_end),
      
      (try_for_range, ":cur_troop", active_npcs_begin, active_npcs_end),
        (troop_get_slot, ":cur_troop_party", ":cur_troop", slot_troop_leaded_party),
        (gt, ":cur_troop_party", 0),
        (party_is_active, ":cur_troop_party"),
        (call_script, "script_party_calculate_and_set_nearby_friend_enemy_follower_strengths", ":cur_troop_party"),
      (try_end),
      (call_script, "script_party_calculate_and_set_nearby_friend_enemy_follower_strengths", "p_main_party"),
  ]),
  
  
  # script_recalculate_ais	move to trigger
  # Input: none
  # Output: none
  
  #When a lord changes factions
  #When a center changes factions
  #When a center is captured
  #When a marshal is defeated
  #Every 23 hours
  # ("recalculate_ais",
  # [
  # (call_script, "script_init_ai_calculation"),
  
  # (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
  # (assign, reg8, ":faction_no"),
  # (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
  # #(neg|faction_slot_eq, ":faction_no",  slot_faction_marshal, "trp_player"),
  # (call_script, "script_decide_faction_ai", ":faction_no"),
  # (try_end),
  
  # (try_for_range, ":troop_no", active_npcs_begin, active_npcs_end),
  # (store_troop_faction, ":faction_no", ":troop_no"),
  # (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
  # (call_script, "script_calculate_troop_ai", ":troop_no"),
  # (try_end),
  # ]),
  
  # script_calculate_troop_ai
  # Input: troop_no
  # Output: none
  #Now called directly from scripts
  ("calculate_troop_ai",
    [
      (store_script_param, ":troop_no", 1),
      (try_begin),
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
        (neg|troop_slot_ge, ":troop_no", slot_troop_prisoner_of_party, 0),
        (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
        (gt, ":party_no", 0),
        (party_is_active, ":party_no"),
        (try_begin),
          (neg|troop_slot_ge, ":troop_no", slot_troop_current_mission, 1),
          (store_faction_of_troop, ":faction", ":troop_no"),
          (neq, ":faction", "fac_adventurers"),
          (call_script, "script_npc_decision_checklist_party_ai", ":troop_no"), #This handles AI for both marshal and other parties
          (call_script, "script_party_set_ai_state", ":party_no", reg0, reg1),
        (else_try),
          #			(str_store_troop_name, s13, ":troop_no"),
          (get_party_ai_object, ":ai_object", ":party_no"),
          (gt, ":ai_object", 0),
          (is_between, ":ai_object", centers_begin, centers_end),
          #			(str_store_party_name, s22, ":ai_object"),
          #			(display_message, "@{s22} is object for {s13}"),
          (this_or_next|troop_slot_eq, ":troop_no", slot_troop_mission_target, ":ai_object"),
          (troop_slot_eq, ":troop_no", slot_troop_mission_object,":ai_object"),
          (store_distance_to_party_from_party, ":cur_distance", ":ai_object", ":party_no"),
          (try_begin),
            (lt, ":cur_distance", 15),
            (get_party_ai_behavior, ":behavior", ":party_no"),
            (neq, ":behavior", ai_bhvr_patrol_party),
            (troop_get_slot, ":mission_object", ":troop_no", slot_troop_mission_target),
            (is_between, ":mission_object", centers_begin, centers_end),
            (party_get_attached_to, ":attached_to_party", ":party_no"),
            (try_begin),
              (is_between, ":attached_to_party", centers_begin, centers_end),
              (party_detach, ":party_no"),
            (try_end),
            (party_set_ai_object, ":party_no", ":ai_object"),
            (party_set_slot, ":party_no", slot_party_ai_object, ":ai_object"),
            (party_set_slot, ":party_no", slot_party_ai_state, spai_patrolling_around_center),
            (party_set_ai_behavior, ":party_no", ai_bhvr_patrol_party),
            (party_set_ai_patrol_radius, ":party_no", 15),
            (party_set_aggressiveness, ":party_no",8),
            (party_set_courage, ":party_no", 9),
            (party_set_ai_initiative, ":party_no", 80),
            #				(display_message, "@patroling"),
          (else_try),
            (gt, ":cur_distance", 15),
            (get_party_ai_behavior, ":behavior", ":party_no"),
            (neq, ":behavior", ai_bhvr_patrol_party),
            (party_set_ai_object, ":party_no", ":ai_object"),
            (party_set_slot, ":party_no", slot_party_ai_object, ":ai_object"),
            (party_set_slot, ":party_no", slot_party_ai_state, spai_retreating_to_center),
            (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
            #				(display_message, "@traveling"),
          (else_try),
            (get_party_ai_behavior, ":behavior", ":party_no"),
            (neq, ":behavior", ai_bhvr_patrol_party),
            (troop_get_slot, ":mission_object", ":troop_no", slot_troop_mission_target),
            (is_between, ":mission_object", centers_begin, centers_end),
            (party_is_in_town, ":party_no", ":mission_object"),
            (is_between, ":mission_object", centers_begin, centers_end),
            (party_set_ai_object, ":party_no", ":ai_object"),
            (party_set_slot, ":party_no", slot_party_ai_object, ":ai_object"),
            (party_set_slot, ":party_no", slot_party_ai_state, spai_holding_center),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  # script_diplomacy_start_peace_between_kingdoms
  # Input: arg1 = kingdom_1, arg2 = kingdom_2, arg3 = initializing_war_peace_cond
  # Output: none
  ("diplomacy_start_peace_between_kingdoms", #sets relations between two kingdoms
    [
      (store_script_param, ":kingdom_a", 1),
      (store_script_param, ":kingdom_b", 2),
      (store_script_param, ":initializing_war_peace_cond", 3), #set to 1 if not the start of the game
      
      (store_relation, ":relation", ":kingdom_a", ":kingdom_b"),
      (val_max, ":relation", 0),
      (set_relation, ":kingdom_a", ":kingdom_b", ":relation"),
      (call_script, "script_exchange_prisoners_between_factions", ":kingdom_a", ":kingdom_b"),
      
      (try_begin),
        (eq, "$players_kingdom", ":kingdom_a"),
        (store_relation, ":relation", "fac_player_faction", ":kingdom_b"),
        (val_add, ":relation", 10), #VC-3960 reduce reversals of player's personal relations 
        (try_begin),
          (faction_slot_eq, "$players_kingdom", slot_faction_leader, "trp_player"),
          (val_max, ":relation", 0), #VC-3971 ensure peace ensues
        (try_end),
        (call_script, "script_set_player_relation_with_faction", ":kingdom_b", ":relation"),
        (call_script, "script_event_kingdom_make_peace_with_kingdom", ":kingdom_b", "fac_player_supporters_faction"), #event cancels certain quests
      (else_try),
        (eq, "$players_kingdom", ":kingdom_b"),
        (store_relation, ":relation", "fac_player_faction", ":kingdom_a"),
        (val_add, ":relation", 10), #VC-3960 reduce reversals of player's personal relations 
        (try_begin),
          (faction_slot_eq, "$players_kingdom", slot_faction_leader, "trp_player"),
          (val_max, ":relation", 0), #VC-3971 ensure peace ensues
        (try_end),
        (call_script, "script_set_player_relation_with_faction", ":kingdom_a", ":relation"),
        (call_script, "script_event_kingdom_make_peace_with_kingdom", ":kingdom_a", "fac_player_supporters_faction"), #event cancels certain quests
      (try_end),
      
      (try_for_range, ":cur_center", centers_begin, centers_end),
        (store_faction_of_party, ":faction_no", ":cur_center"),
        (this_or_next|eq, ":faction_no", ":kingdom_a"),
        (eq, ":faction_no", ":kingdom_b"),
        (party_get_slot, ":besieger_party", ":cur_center", slot_center_is_besieged_by),
        (ge, ":besieger_party", 0), #town is under siege
        (party_is_active, ":besieger_party"),
        (store_faction_of_party, ":besieger_party_faction_no", ":besieger_party"),
        (this_or_next|eq, ":besieger_party_faction_no", ":kingdom_a"),
        (eq, ":besieger_party_faction_no", ":kingdom_b"),
        (call_script, "script_lift_siege", ":cur_center", 0),
      (try_end),
      
      (try_begin),
        (this_or_next|eq, "$players_kingdom", ":kingdom_a"),
        (eq, "$players_kingdom", ":kingdom_b"),
        
        (ge, "$g_player_besiege_town", 0),
        (party_is_active, "$g_player_besiege_town"),
        
        (store_faction_of_party, ":besieged_center_faction_no", "$g_player_besiege_town"),
        
        (this_or_next|eq, ":besieged_center_faction_no", ":kingdom_a"),
        (eq, ":besieged_center_faction_no", ":kingdom_b"),
        
        (call_script, "script_lift_siege", "$g_player_besiege_town", 0),
        (assign, "$g_player_besiege_town", -1),
      (try_end),
      
      (try_begin),
        (eq, ":initializing_war_peace_cond", 1),
        (str_store_faction_name_link, s1, ":kingdom_a"),
        (str_store_faction_name_link, s2, ":kingdom_b"),
        (display_log_message, "@The {s1} and the {s2} have made peace with each other.", color_quest_and_faction_news),
        (call_script, "script_add_notification_menu", "mnu_notification_peace_declared", ":kingdom_a", ":kingdom_b"), #stability penalty for early peace is in the menu
        (call_script, "script_event_kingdom_make_peace_with_kingdom", ":kingdom_a", ":kingdom_b"), #cancels quests
        (call_script, "script_event_kingdom_make_peace_with_kingdom", ":kingdom_b", ":kingdom_a"), #cancels quests
        (assign, "$g_recalculate_ais", 1),
      (try_end),
      
      (try_begin), #add truce
        (store_add, ":truce_slot", ":kingdom_a", slot_faction_truce_days_with_factions_begin),
        (val_sub, ":truce_slot", kingdoms_begin),
        (faction_set_slot, ":kingdom_b", ":truce_slot", truce_time),
        (store_add, ":truce_slot", ":kingdom_b", slot_faction_truce_days_with_factions_begin),
        (val_sub, ":truce_slot", kingdoms_begin),
        (faction_set_slot, ":kingdom_a", ":truce_slot", truce_time),
        (store_add, ":slot_war_damage_inflicted_on_b", ":kingdom_b", slot_faction_war_damage_inflicted_on_factions_begin),
        (val_sub, ":slot_war_damage_inflicted_on_b", kingdoms_begin),
        #(faction_get_slot, ":damage_inflicted_by_a", ":kingdom_a", ":slot_war_damage_inflicted_on_b"),
        (faction_set_slot, ":kingdom_a", ":slot_war_damage_inflicted_on_b", 0),
        (store_add, ":slot_war_damage_inflicted_on_a", ":kingdom_a", slot_faction_war_damage_inflicted_on_factions_begin),
        (val_sub, ":slot_war_damage_inflicted_on_a", kingdoms_begin),
        #(faction_get_slot, ":damage_inflicted_by_b", ":kingdom_b", ":slot_war_damage_inflicted_on_a"),
        (faction_set_slot, ":kingdom_b", ":slot_war_damage_inflicted_on_a", 0),
      (try_end),
  ]),
  
  
  
  ("event_kingdom_make_peace_with_kingdom",
    [
      (store_script_param_1, ":source_kingdom"),
      (store_script_param_2, ":target_kingdom"),
      (try_begin),
        (check_quest_active, "qst_capture_prisoners"),
        (try_begin),
          (eq, "$players_kingdom", ":source_kingdom"),
          (quest_slot_eq, "qst_capture_prisoners", slot_quest_target_faction, ":target_kingdom"),
          (call_script, "script_cancel_quest", "qst_capture_prisoners"),
        (else_try),
          (eq, "$players_kingdom", ":target_kingdom"),
          (quest_slot_eq, "qst_capture_prisoners", slot_quest_target_faction, ":source_kingdom"),
          (call_script, "script_cancel_quest", "qst_capture_prisoners"),
        (try_end),
      (try_end),
      
      (try_begin),
        (check_quest_active, "qst_capture_enemy_hero"),
        (try_begin),
          (eq, "$players_kingdom", ":source_kingdom"),
          (quest_slot_eq, "qst_capture_enemy_hero", slot_quest_target_faction, ":target_kingdom"),
          (call_script, "script_cancel_quest", "qst_capture_enemy_hero"),
        (else_try),
          (eq, "$players_kingdom", ":target_kingdom"),
          (quest_slot_eq, "qst_capture_enemy_hero", slot_quest_target_faction, ":source_kingdom"),
          (call_script, "script_cancel_quest", "qst_capture_enemy_hero"),
        (try_end),
      (try_end),
      
      
      
      (try_begin),
        (check_quest_active, "qst_persuade_lords_to_make_peace"),
        (quest_get_slot, ":lord_1", "qst_persuade_lords_to_make_peace", slot_quest_target_troop),
        (quest_get_slot, ":lord_2", "qst_persuade_lords_to_make_peace", slot_quest_object_troop),
        
        (try_begin),
          (lt, ":lord_1", 0),
          (val_mul, ":lord_1", -1),
        (try_end),
        (try_begin),
          (lt, ":lord_2", 0),
          (val_mul, ":lord_2", -1),
        (try_end),
        
        
        (store_faction_of_troop, ":lord_1_faction", ":lord_1"),
        (store_faction_of_troop, ":lord_2_faction", ":lord_2"),
        
        (this_or_next|eq, ":lord_1_faction", ":source_kingdom"),
        (eq, ":lord_2_faction", ":source_kingdom"),
        
        (this_or_next|eq, ":lord_1_faction", ":target_kingdom"),
        (eq, ":lord_2_faction", ":target_kingdom"),
        
        (call_script, "script_cancel_quest", "qst_persuade_lords_to_make_peace"),
        
      (try_end),
      
      #Rescue prisoners cancelled in simple_triggers
      
      (try_begin),
        (this_or_next|faction_slot_eq, ":target_kingdom", slot_faction_leader, "trp_player"),
        (faction_slot_eq, ":source_kingdom", slot_faction_leader, "trp_player"),
        
        (call_script, "script_change_player_right_to_rule", 3),
      (try_end),
      
  ]),
  
  
  # script_count_wars_and_pacts by Motomataru chief
  # Input: acting faction, faction considered
  # Output: reg0 = 0 + 2 x common wars - 2 x other wars + 1 x common pacts - 1 x other pacts
  #         reg1 = number agreements that would contradict a defensive pact or alliance
  
  ("count_wars_and_pacts", [
      (store_script_param_1, ":cur_kingdom"),
      (store_script_param_2, ":cur_kingdom_2"),
      
      (assign, reg0, 0),
      (assign, reg1, 0),
      (try_for_range, ":third_kingdom", kingdoms_begin, kingdoms_end),
        (neq, ":third_kingdom", ":cur_kingdom"),
        (neq, ":third_kingdom", ":cur_kingdom_2"),
        (faction_slot_eq, ":third_kingdom", slot_faction_state, sfs_active),
        
        (store_relation, ":cur_relation", ":cur_kingdom", ":third_kingdom"),
        (store_relation, ":cur_relation_2", ":cur_kingdom_2", ":third_kingdom"),
        (try_begin),
          (lt, ":cur_relation", 0),
          
          (try_begin),
            (lt, ":cur_relation_2", 0),	#common war?
            (val_add, reg0, 1),
          (else_try),
            (val_sub, reg0, 2),
          (try_end),
        (try_end),
        
        (try_begin),
          (lt, ":cur_relation_2", 0),
          
          (try_begin),
            (lt, ":cur_relation", 0),	#common war?
            (val_add, reg0, 1),
          (else_try),
            (val_sub, reg0, 2),
          (try_end),
        (try_end),
        
        (store_add, ":truce_slot", ":third_kingdom", slot_faction_truce_days_with_factions_begin),
        (val_sub, ":truce_slot", kingdoms_begin),
        (faction_get_slot, ":truce_days", ":cur_kingdom", ":truce_slot"),
        (faction_get_slot, ":truce_days_2", ":cur_kingdom_2", ":truce_slot"),
        (try_begin),
          (gt, ":truce_days", 0),
          
          (try_begin),
            (gt, ":truce_days_2", truce_time),	#common pact?
            (val_add, reg0, 1),
          (else_try),
            (val_sub, reg0, 1),
          (try_end),
        (try_end),
        
        (try_begin),
          (gt, ":truce_days_2", 0),
          
          (try_begin),
            (gt, ":truce_days", truce_time),	#common pact?
            # (val_add, reg0, 1),	#already done in last section
          (else_try),
            (val_sub, reg0, 1),
          (try_end),
        (try_end),
        
        ###alliances
        (faction_get_slot, ":alliance_days", ":cur_kingdom", ":truce_slot"),
        (faction_get_slot, ":alliance_days_2", ":cur_kingdom_2", ":truce_slot"),
        (try_begin),
          (gt, ":alliance_days", 0),
          
          (try_begin),
            (gt, ":alliance_days_2", alliance_time),	#
            (val_add, reg0, 1),
          (else_try),
            (val_sub, reg0, 1),
          (try_end),
        (try_end),
        
        (try_begin),
          (gt, ":alliance_days_2", 0),
          
          (try_begin),
            (gt, ":alliance_days", alliance_time),	#
            # (val_add, reg0, 1),	#already done in last section
          (else_try),
            (val_sub, reg0, 1),
          (try_end),
        (try_end),
        ####
        
        (try_begin),
          (gt, ":truce_days", 0),
          (lt, ":cur_relation_2", 0),
          (val_add, reg1, 1),
        (try_end),
        
        (try_begin),
          (gt, ":truce_days_2", 0),
          (lt, ":cur_relation", 0),
          (val_add, reg1, 1),
        (try_end),
        
      (try_end),
      #		(str_store_faction_name, s33, ":cur_kingdom"),
      #	(str_store_faction_name, s34, ":cur_kingdom_2"),
      #	(display_message, "@{s33} and {s34} have {reg0} and {reg1}"),
  ]),
  
  
  # script_find_neighbors by Motomataru moto chief
  # Input: none
  # Output: none
  
  ("find_neighbors", [
      (store_sub, reg0, kingdoms_end, kingdoms_begin),
      (try_for_range, ":faction_1", kingdoms_begin, kingdoms_end),
        (try_for_range, ":faction_2", 0, reg0),
          (store_add, ":slot", slot_faction_neighbors_begin, ":faction_2"),
          (faction_set_slot, ":faction_1", ":slot", 0),
        (try_end),
      (try_end),
      (try_for_range, ":center_1", centers_begin, centers_end),
        (store_faction_of_party, ":faction_1", ":center_1"),
        (store_add, ":already_done", ":center_1", 1),
        (try_for_range, ":center_2", ":already_done", centers_end),
          (store_faction_of_party, ":faction_2", ":center_2"),
          (store_add, ":slot", slot_faction_neighbors_begin, ":faction_2"),
          (val_sub, ":slot", kingdoms_begin),
          (faction_slot_eq, ":faction_1", ":slot", 0),
          (store_distance_to_party_from_party, ":distance", ":center_2", ":center_1"),
          (lt, ":distance", min_dist_neighb_of_neighb),
          (faction_set_slot, ":faction_1", ":slot", 1),
          
          #set for OTHER neighbor
          (store_add, ":slot", slot_faction_neighbors_begin, ":faction_1"),
          (val_sub, ":slot", kingdoms_begin),
          (faction_set_slot, ":faction_2", ":slot", 1),
        (try_end),
      (try_end),
  ]),
  
  # script_randomly_start_war_peace moto chief rewrite
  # Input: arg1 = initializing_war_peace_cond (1 = true, 0 = false)
  # Output: none
  
  #Aims to introduce a slightly simpler system in which the AI kings' reasoning could be made more  transparent to the player. At the start of the game, this may lead to less variation in outcomes, though
  ("randomly_start_war_peace_new",
    [
      (store_script_param_1, ":initializing_war_peace_cond"),
      
      (assign, ":players_kingdom_at_peace", 0), #if the player kingdom is at peace, then create an enmity
      (try_begin),
        (is_between, "$players_kingdom", npc_kingdoms_begin, npc_kingdoms_end),
        (assign, ":players_kingdom_at_peace", 1),
      (try_end),
      
      #Introduce some minor variation by changing the order in which factions consider things.
      (store_random_in_range, ":random_offset_1", npc_kingdoms_begin, npc_kingdoms_end),
      (val_sub, ":random_offset_1", npc_kingdoms_begin),
      
      (try_for_range, ":cur_kingdom", npc_kingdoms_begin, npc_kingdoms_end),
        (val_add, ":cur_kingdom", ":random_offset_1"),
        (try_begin),
          (ge, ":cur_kingdom", npc_kingdoms_end),
          (val_sub, ":cur_kingdom", npc_kingdoms_end),
          (val_add, ":cur_kingdom", npc_kingdoms_begin),
        (try_end),
        (faction_slot_eq, ":cur_kingdom", slot_faction_state, sfs_active),
        
        #MOTO restrict to one truce per faction per function call
        # (try_for_range, ":cur_kingdom_2", kingdoms_begin, kingdoms_end),
        # (neq, ":cur_kingdom", ":cur_kingdom_2"),
        # (faction_slot_eq, ":cur_kingdom_2", slot_faction_state, sfs_active),
        
        #MOTO ...but avoid slowing down late game diplomacy with duds
        (assign, ":num_tries", 50),
        
        (try_for_range, reg0, 0, ":num_tries"),
          (store_random_in_range, ":cur_kingdom_2", kingdoms_begin, kingdoms_end),
          (neq, ":cur_kingdom", ":cur_kingdom_2"),
          (faction_slot_eq, ":cur_kingdom_2", slot_faction_state, sfs_active),
          (assign, ":num_tries", reg0),
        (try_end),
        
        (try_begin),    #MOTO restrict to one truce per faction per function call
          (call_script, "script_npc_decision_checklist_peace_or_war", ":cur_kingdom", ":cur_kingdom_2", -1),
          (assign, ":kingdom_1_to_kingdom_2", reg0),
          (str_store_string_reg, s57, s14),	#MOTO save diplomatic explanation string
          
          #MOTO chief neighbors
          (store_add, ":slot", slot_faction_neighbors_begin, ":cur_kingdom_2"),
          (val_sub, ":slot", kingdoms_begin),
          (faction_get_slot, ":neighbors", ":cur_kingdom", ":slot"),
          #MOTO chief neighbors end
          
          (store_relation, ":cur_relation", ":cur_kingdom", ":cur_kingdom_2"),
          (try_begin),
            (lt, ":cur_relation", 0), #AT WAR
            
            (try_begin),
              (eq, ":cur_kingdom", "$players_kingdom"),
              (assign, ":players_kingdom_at_peace", 0),
            (try_end),
            
            (this_or_next|eq, ":initializing_war_peace_cond", 0),	#MOTO randomize game start
            (ge, ":kingdom_1_to_kingdom_2", 1),
            
            (try_begin),
              (eq, ":cur_kingdom_2", "fac_player_supporters_faction"),
              
              (store_mul, ":goodwill_level", ":kingdom_1_to_kingdom_2", 2),
              (store_random_in_range, ":random", 0, 20),
              (try_begin),
                (lt, ":random", ":goodwill_level"),
                (call_script, "script_add_notification_menu", "mnu_question_peace_offer", ":cur_kingdom", 0),
              (try_end),
            (else_try),
              (call_script, "script_npc_decision_checklist_peace_or_war", ":cur_kingdom_2", ":cur_kingdom", -1),
              (assign, ":kingdom_2_to_kingdom_1", reg0),
              (ge, ":kingdom_2_to_kingdom_1", 1),
              
              (store_mul, ":goodwill_level", ":kingdom_1_to_kingdom_2", ":kingdom_2_to_kingdom_1"),
              
              #MOTO randomize game start
              (try_begin),
                (eq, ":initializing_war_peace_cond", 0),
                (store_add, reg1, ":kingdom_1_to_kingdom_2", 3),
                (store_add, reg2, ":kingdom_2_to_kingdom_1", 3),
                (store_mul, ":goodwill_level", reg1, reg2),
                (val_mul, ":goodwill_level", 3),
                (val_div, ":goodwill_level", 4),	#MOTO fine tuning; results in about 4 peace made start of game w 18 calls TODO redo for 21 factions, 38 calls
              (try_end),
              #MOTO randomize game start end
              # (store_random_in_range, ":random", 0, 20),
              (store_random_in_range, ":random", -1, 19),	#MOTO increase chance of peace (goodwill_level max at 9 -- figure 1/4 chance is once per 120 days)
              (lt, ":random", ":goodwill_level"),
              
              #MOTO salvage explanation (when multiple explanations needed, only last one valid)
              # (try_begin),
              # (eq, "$g_include_diplo_explanation", 0),
              # (assign, "$g_include_diplo_explanation", ":cur_kingdom"),
              # # (str_store_string, s57, "str_s14"),	MOTO not correct string (see correction above)
              # (try_end),
              (assign, "$g_last_acting_faction", ":cur_kingdom"),
              (assign, "$g_last_target_faction", ":cur_kingdom_2"),
              (str_store_string_reg, s64, s57),	#MOTO save diplomatic explanation string
              #MOTO salvage explanation end
              
              (call_script, "script_diplomacy_start_peace_between_kingdoms", ":cur_kingdom", ":cur_kingdom_2", ":initializing_war_peace_cond"),
            (try_end),
          (else_try),
            (ge, ":cur_relation", 0), #AT PEACE
            
            #MOTO rewrite TODO rework for 21 factions over 38 days
            (call_script, "script_diplomacy_faction_get_diplomatic_status_with_faction", ":cur_kingdom", ":cur_kingdom_2"),
            (le, reg0, 0), #no truce
            
            (assign, ":hostility", ":kingdom_1_to_kingdom_2"),
            (try_begin),
              (eq, ":initializing_war_peace_cond", 0),	#start of game
              (val_sub, ":hostility", ":neighbors"),	#simulate border conflicts
            (try_end),
            (lt, ":hostility", 0),	#non-start range -1 to -3
            
            # (store_mul, ":hostility_squared", ":hostility", ":hostility"),
            (store_mul, ":hostility_squared", ":hostility", -3),
            (try_begin),
              # (eq, ":initializing_war_peace_cond", 0),
              # (store_random_in_range, ":random", 0, 17),	#MOTO trim to get 5 factions at peace into war
              # (else_try),
              # (store_random_in_range, ":random", 0, 50),
              (store_random_in_range, ":random", 0, 9),	#MOTO increase chance of war (means unprovoked attack once in 600 days, 30 days for script_npc_decision_checklist_peace_or_war results = -3)
            (try_end),
            #MOTO rewrite end
            ###pact
            (try_for_range, ":third_kingdom", kingdoms_begin, kingdoms_end),
              (faction_slot_eq, ":third_kingdom", slot_faction_state, sfs_active),
              (neq, ":third_kingdom", ":cur_kingdom"),
              (neq, ":third_kingdom", ":cur_kingdom_2"),
              (store_relation, ":cur_relation", ":cur_kingdom_2", ":third_kingdom"),
              (ge, ":cur_relation", 0), #AT PEACE
              (store_add, ":truce_slot", ":third_kingdom", slot_faction_truce_days_with_factions_begin),
              (val_sub, ":truce_slot", kingdoms_begin),
              (faction_get_slot, ":truce_days", ":cur_kingdom_2", ":truce_slot"),
              (gt, ":truce_days", truce_time),
              (store_div, ":hostility_change", ":truce_days", 20),
              (val_sub, ":hostility_change", 1),	#MOTO adjust to 1-2 points (since random less)
              (val_sub, ":hostility_squared", ":hostility_change"),
            (try_end),
            
            (lt, ":random", ":hostility_squared"),
            
            #MOTO salvage explanation (when multiple explanations needed, only last one valid)
            # (try_begin),
            # (eq, "$g_include_diplo_explanation", 0),
            # (assign, "$g_include_diplo_explanation", ":cur_kingdom"),
            # (str_store_string, s57, "str_s14"),
            # (try_end),
            (assign, "$g_last_acting_faction", ":cur_kingdom"),
            (assign, "$g_last_target_faction", ":cur_kingdom_2"),
            (str_store_string_reg, s64, s57),	#MOTO save diplomatic explanation string
            #MOTO salvage explanation end
            
            (call_script, "script_diplomacy_start_war_between_kingdoms", ":cur_kingdom", ":cur_kingdom_2", ":initializing_war_peace_cond"),
            
            (try_begin), #do some war damage for
              (eq, ":initializing_war_peace_cond", 0),
              (store_random_in_range, ":war_damage_inflicted", 10, 120),
              (store_add, ":slot_war_damage_inflicted", ":cur_kingdom", slot_faction_war_damage_inflicted_on_factions_begin),
              (val_sub, ":slot_war_damage_inflicted", kingdoms_begin),
              (faction_set_slot, ":cur_kingdom_2",  ":slot_war_damage_inflicted", ":war_damage_inflicted"),
              
              (store_add, ":slot_war_damage_inflicted", ":cur_kingdom_2", slot_faction_war_damage_inflicted_on_factions_begin),
              (val_sub, ":slot_war_damage_inflicted", kingdoms_begin),
              (faction_set_slot, ":cur_kingdom", ":slot_war_damage_inflicted", ":war_damage_inflicted"),
            (try_end),
          (else_try), #start wars and pacts with nearby factions
            (ge, ":cur_relation", 0), #AT PEACE
            (ge, ":kingdom_1_to_kingdom_2", 1),
            
            #MOTO chief consider just nearby factions
            (assign, ":neighbors_of_neighbors", 0),
            (assign, ":end_loop", kingdoms_end),
            (try_for_range, ":third_kingdom", kingdoms_begin, ":end_loop"),
              (neq, ":third_kingdom", ":cur_kingdom"),
              (neq, ":third_kingdom", ":cur_kingdom_2"),
              (neq, ":third_kingdom", "fac_kingdom_3"),	#avoid using Laithlind as bridge across the entire map
              (assign, ":neighbor_of_1", 0),
              (assign, ":neighbor_of_2", 0),
              (assign, ":end_loop_2", kingdoms_end),
              (try_for_range, ":test_faction", kingdoms_begin, ":end_loop_2"),
                (store_add, ":slot", slot_faction_neighbors_begin, ":test_faction"),
                (val_sub, ":slot", kingdoms_begin),
                (faction_slot_eq, ":third_kingdom", ":slot", 1),
                
                (try_begin),
                  (eq, ":test_faction", ":cur_kingdom"),
                  (assign, ":neighbor_of_1", 1),
                (else_try),
                  (eq, ":test_faction", ":cur_kingdom_2"),
                  (assign, ":neighbor_of_2", 1),
                (try_end),
                
                (eq, ":neighbor_of_1", 1),
                (eq, ":neighbor_of_2", 1),
                (assign, ":neighbors_of_neighbors", 1),
                (assign, ":end_loop_2", ":test_faction"),
              (try_end),
              (eq, ":neighbors_of_neighbors", 1),
              (assign, ":end_loop", ":third_kingdom"),
            (try_end),
            
            (this_or_next|eq, ":cur_kingdom", "fac_kingdom_1"),	#Norse homelands consider anyone on the map
            (this_or_next|eq, ":cur_kingdom", "fac_kingdom_2"),
            (this_or_next|eq, ":neighbors", 1),
            (eq, ":neighbors_of_neighbors", 1),
            #MOTO chief consider just nearby factions end
            
            #MOTO chief randomize like other sections
            (try_begin),
              (eq, ":cur_kingdom_2", "fac_player_supporters_faction"),
              (assign, ":kingdom_2_to_kingdom_1", 2),
              (assign, ":shared_history", 50),
            (else_try),
              (call_script, "script_npc_decision_checklist_peace_or_war", ":cur_kingdom_2", ":cur_kingdom", -1),
              (assign, ":kingdom_2_to_kingdom_1", reg0),
              (store_relation, ":shared_history", ":cur_kingdom_2", ":cur_kingdom"),
            (try_end),
            
            (ge, ":kingdom_2_to_kingdom_1", 1),
            
            (store_add, ":goodwill_level", ":kingdom_1_to_kingdom_2", ":kingdom_2_to_kingdom_1"),	#range 2 to 6
            (val_mul, ":goodwill_level", 3),
            (val_sub, ":goodwill_level", 5),	#range 1 to 13
            
            (val_add, ":shared_history", ":cur_relation"),
            (val_div, ":shared_history", 20),	#range 0 to 9
            (val_add, ":goodwill_level", ":shared_history"),
            
            (ge, ":goodwill_level", 1),	#both kingdoms like each other, range 1-22
            (store_random_in_range, ":random", 0, 7),	#if they either really like each other or want each other, this will auto succeed
            (lt, ":random", ":goodwill_level"),
            #MOTO chief randomize like other sections end
            
            (store_add, ":slot_truce_days", ":cur_kingdom", slot_faction_truce_days_with_factions_begin),
            (val_sub, ":slot_truce_days", kingdoms_begin),
            (faction_get_slot, ":truce_days", ":cur_kingdom_2", ":slot_truce_days"),
            
            
            (try_begin),
              (eq, ":cur_kingdom_2", "fac_player_supporters_faction"),
              
              (try_begin),
                (ge, ":cur_relation", 30), #good relation = possible alliance
                (faction_slot_eq, ":cur_kingdom", slot_faction_recognized_player, 1), #recognized us
                #MOTO avoid "diplomacy" wars
                (call_script, "script_count_wars_and_pacts", ":cur_kingdom", ":cur_kingdom_2"),
                (ge, reg0, 0),
                (eq, reg1, 0),	#no countervailing pacts OR
                #MOTO avoid "diplomacy" wars end
                (call_script, "script_add_notification_menu", "mnu_notification_alliance_offer", ":cur_kingdom", 0),
              (try_end),
            (else_try),
              
              (try_begin),
                # MOTO chief use shared history
                # (ge, ":cur_relation", 30),
                (ge, ":shared_history", 3),
                # MOTO chief use shared history end
                #MOTO avoid "diplomacy" wars
                (call_script, "script_count_wars_and_pacts", ":cur_kingdom", ":cur_kingdom_2"),
                (ge, reg0, 0),
                (eq, reg1, 0),	#no countervailing pacts OR
                #MOTO avoid "diplomacy" wars end
                #						(assign, reg44, ":truce_days"),
                #						(display_message, "@{reg44} is truce days"),
                #						(is_between, ":truce_days", truce_time,alliance_time),
                (eq, ":truce_days", 0),
                (call_script, "script_diplomacy_start_alliance_between_kingdoms", ":cur_kingdom", ":cur_kingdom_2", ":initializing_war_peace_cond"),
              (try_end),
            (try_end),
            ##Moto diplomacy end
          (try_end),
        (try_end),
      (try_end),
      
      (try_begin),
        (eq, ":players_kingdom_at_peace", 1),
        (val_add, "$players_kingdom_days_at_peace", 1),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (assign, reg3, "$players_kingdom_days_at_peace"),
          (display_message, "@{!}DEBUG -- Player's kingdom has had {reg3} days of peace"),
        (try_end),
      (else_try),
        (assign, "$players_kingdom_days_at_peace", 0),
      (try_end),
      
  ]),
  
  # script_randomly_start_war_peace
  # Input: arg1 = initializing_war_peace_cond (1 = true, 0 = false)
  # Output: none
  #  ("randomly_start_war_peace",
  #    [
  #      (store_script_param_1, ":initializing_war_peace_cond"),
  #      (assign, ":total_resources", 0),
  #      (assign, ":total_active_kingdoms", 0),
  #      (try_for_range, ":cur_kingdom", kingdoms_begin, kingdoms_end),
  #        (faction_slot_eq, ":cur_kingdom", slot_faction_state, sfs_active),
  #        (val_add, ":total_active_kingdoms", 1),
  #        (faction_get_slot, ":num_towns", ":cur_kingdom", slot_faction_num_towns),
  #        (store_mul, ":kingdom_resources_value", ":num_towns", 2),
  #        (faction_get_slot, ":num_castles", ":cur_kingdom", slot_faction_num_castles),
  #        (val_add, ":kingdom_resources_value", ":num_castles"),
  #        (val_mul, ":kingdom_resources_value", 10),
  #        (val_max, ":kingdom_resources_value", 1),
  #        (val_mul, ":kingdom_resources_value", 1000),
  #        (faction_get_slot, ":num_armies", ":cur_kingdom", slot_faction_num_armies),
  #        (val_max, ":num_armies", 1),
  #        (val_div, ":kingdom_resources_value", ":num_armies"),
  #        (val_add, ":total_resources", ":kingdom_resources_value"),
  #      (try_end),
  #      (val_max, ":total_active_kingdoms", 1),
  #      (store_div, ":average_resources", ":total_resources", ":total_active_kingdoms"),
  
  #      (try_for_range, ":cur_kingdom", kingdoms_begin, kingdoms_end),
  ##       (neq, ":cur_kingdom", "fac_player_supporters_faction"),
  #        (faction_slot_eq, ":cur_kingdom", slot_faction_state, sfs_active),
  #        (assign, ":num_ongoing_wars", 0),
  #        (try_for_range, ":other_kingdom", kingdoms_begin, kingdoms_end),
  #          (faction_slot_eq, ":other_kingdom", slot_faction_state, sfs_active),
  #          (store_relation, ":other_relation", ":cur_kingdom", ":other_kingdom"),
  #          (lt, ":other_relation", 0),
  #          (val_add, ":num_ongoing_wars", 1),
  #        (try_end),
  
  #        (faction_get_slot, ":num_towns", ":cur_kingdom", slot_faction_num_towns),
  #        (store_mul, ":kingdom_1_resources_value", ":num_towns", 2),
  #        (faction_get_slot, ":num_castles", ":cur_kingdom", slot_faction_num_castles),
  #        (val_add, ":kingdom_1_resources_value", ":num_castles"),
  #        (val_mul, ":kingdom_1_resources_value", 10),
  #        (val_max, ":kingdom_1_resources_value", 1),
  #        (val_mul, ":kingdom_1_resources_value", 1000),
  #        (faction_get_slot, ":num_armies", ":cur_kingdom", slot_faction_num_armies),
  #        (val_max, ":num_armies", 1),
  #        (val_div, ":kingdom_1_resources_value", ":num_armies"),
  
  #        (store_add, ":start_cond", ":cur_kingdom", 1),
  #        (try_for_range, ":cur_kingdom_2", ":start_cond", kingdoms_end),
  ##         (neq, ":cur_kingdom", "fac_player_supporters_faction"),
  #          (faction_slot_eq, ":cur_kingdom_2", slot_faction_state, sfs_active),
  
  #          (assign, ":num_ongoing_wars_2", 0),
  #          (try_for_range, ":other_kingdom", kingdoms_begin, kingdoms_end),
  #            (faction_slot_eq, ":other_kingdom", slot_faction_state, sfs_active),
  #            (store_relation, ":other_relation", ":cur_kingdom_2", ":other_kingdom"),
  #            (lt, ":other_relation", 0),
  #            (val_add, ":num_ongoing_wars_2", 1),
  #          (try_end),
  
  #          (store_add, ":total_ongoing_wars", ":num_ongoing_wars", ":num_ongoing_wars_2"),
  
  #          (faction_get_slot, ":num_towns", ":cur_kingdom_2", slot_faction_num_towns),
  #          (store_mul, ":kingdom_2_resources_value", ":num_towns", 2),
  #          (faction_get_slot, ":num_castles", ":cur_kingdom_2", slot_faction_num_castles),
  #          (val_add, ":kingdom_2_resources_value", ":num_castles"),
  #          (val_mul, ":kingdom_2_resources_value", 10),
  #          (val_max, ":kingdom_2_resources_value", 1),
  #          (val_mul, ":kingdom_2_resources_value", 1000),
  #          (faction_get_slot, ":num_armies", ":cur_kingdom_2", slot_faction_num_armies),
  #          (val_max, ":num_armies", 1),
  #          (val_div, ":kingdom_2_resources_value", ":num_armies"),
  
  #          (assign, ":max_resources_value", ":kingdom_1_resources_value"),
  #          (val_max, ":max_resources_value", ":kingdom_2_resources_value"),
  #          (val_mul, ":max_resources_value", 100),
  #          (val_div, ":max_resources_value", ":average_resources"),
  
  #          (assign, ":cur_king", -1),
  #          (try_begin),
  #            (eq, ":cur_kingdom", "fac_player_supporters_faction"),
  #            (faction_get_slot, ":cur_king", ":cur_kingdom_2", slot_faction_leader),
  #            (assign, ":cur_relation", reg0),
  #            (store_sub, ":relation_effect", 200, ":cur_relation"),
  #            (val_mul, ":kingdom_1_resources_value", ":relation_effect"),
  #            (val_div, ":kingdom_1_resources_value", 200),
  #          (else_try),
  #            (eq, ":cur_kingdom_2", "fac_player_supporters_faction"),
  #            (faction_get_slot, ":cur_king", ":cur_kingdom", slot_faction_leader),
  #          (try_end),
  
  #          (try_begin),
  #            (ge, ":cur_king", 0),
  #            (call_script, "script_troop_get_player_relation", ":cur_king"),
  #            (assign, ":cur_relation", reg0),
  #            (store_sub, ":relation_effect", 200, ":cur_relation"),
  #            (val_mul, ":max_resources_value", ":relation_effect"),
  #            (val_div, ":max_resources_value", 200),
  #          (try_end),
  
  #max_resources_value is the obtained value that gives us how tempting the kingdom's values are
  #average is 100
  #         (val_clamp, ":max_resources_value", 20, 500),
  #not letting more than 5 times higher chance of declaring war or peace
  
  #        (store_random_in_range, ":random_no", 0, 10000),
  #         (store_relation, ":cur_relation", ":cur_kingdom", ":cur_kingdom_2"),
  #         (try_begin),
  #           (lt, ":cur_relation", 0), #AT WAR
  #           (store_mul, ":chance_to_make_peace", ":total_ongoing_wars", 50),
  #           (val_mul, ":chance_to_make_peace", 100),
  #           (val_div, ":chance_to_make_peace", ":max_resources_value"),
  #           (try_begin),
  #disable random peace for special conditions
  #             (this_or_next|eq, ":cur_kingdom", "fac_player_supporters_faction"),
  #             (eq, ":cur_kingdom_2", "fac_player_supporters_faction"),
  #             (assign, ":continue", 0),
  #    (try_begin),
  #     (gt, "$supported_pretender", 0),
  #    (this_or_next|eq, ":cur_kingdom", "$supported_pretender_old_faction"),
  #     (eq, ":cur_kingdom_2", "$supported_pretender_old_faction"),
  #      (assign, ":continue", 1),
  #     (else_try),
  #         (is_between, "$players_oath_renounced_against_kingdom", kingdoms_begin, kingdoms_end),
  #          (this_or_next|eq, ":cur_kingdom", "$players_oath_renounced_against_kingdom"),
  #           (eq, ":cur_kingdom_2", "$players_oath_renounced_against_kingdom"),
  #            (assign, ":continue", 1),
  #           (try_end),
  #     (eq, ":continue", 1),
  #      (assign, ":chance_to_make_peace", 0),
  #     (try_end),
  #      (try_begin),
  #         (lt, ":random_no", ":chance_to_make_peace"),
  #          (assign, ":continue", 1),
  #           (try_begin),
  #              (check_quest_active, "qst_persuade_lords_to_make_peace"),
  #  (quest_get_slot, ":quest_target_faction", "qst_persuade_lords_to_make_peace", slot_quest_target_faction),
  #   (quest_get_slot, ":quest_object_faction", "qst_persuade_lords_to_make_peace", slot_quest_object_faction),
  #    (this_or_next|eq, ":cur_kingdom", ":quest_target_faction"),
  #     (eq, ":cur_kingdom", ":quest_object_faction"),
  #      (this_or_next|eq, ":cur_kingdom_2", ":quest_target_faction"),
  #       (eq, ":cur_kingdom_2", ":quest_object_faction"),
  #        (assign, ":continue", 0), #Do not declare war if the quest is active for the specific kingdoms
  #       (try_end),
  #        (eq, ":continue", 1),
  #         (try_begin),
  #            (eq, ":cur_kingdom", "fac_player_supporters_faction"),
  #             (call_script, "script_add_notification_menu", "mnu_question_peace_offer", ":cur_kingdom_2", 0),
  #            (else_try),
  #    (eq, ":cur_kingdom_2", "fac_player_supporters_faction"),
  #     (call_script, "script_add_notification_menu", "mnu_question_peace_offer", ":cur_kingdom", 0),
  #    (else_try),
  #       (call_script, "script_diplomacy_start_peace_between_kingdoms", ":cur_kingdom", ":cur_kingdom_2", ":initializing_war_peace_cond"),
  #      (try_end),
  #     (try_end),
  #    (else_try), # AT PEACE
  #       (assign, ":chance_to_declare_war", 6),
  #        (val_sub, ":chance_to_declare_war", ":total_ongoing_wars"),
  #         (val_mul, ":chance_to_declare_war", 50),
  #  (val_mul, ":chance_to_declare_war", ":max_resources_value"),
  #   (val_div, ":chance_to_declare_war", 100),
  #    (try_begin),
  #       (lt, ":random_no", ":chance_to_declare_war"),
  #        (assign, ":continue", 1),
  #         (try_begin),
  #            (check_quest_active, "qst_raid_caravan_to_start_war"),
  # (quest_get_slot, ":quest_target_faction", "qst_raid_caravan_to_start_war", slot_quest_target_faction),
  #  (quest_get_slot, ":quest_object_faction", "qst_raid_caravan_to_start_war", slot_quest_object_faction),
  #   (this_or_next|eq, ":cur_kingdom", ":quest_target_faction"),
  #    (eq, ":cur_kingdom", ":quest_object_faction"),
  #     (this_or_next|eq, ":cur_kingdom_2", ":quest_target_faction"),
  #      (eq, ":cur_kingdom_2", ":quest_object_faction"),
  #       (assign, ":continue", 0), #Do not declare war if the quest is active for the specific kingdoms
  #      (try_end),
  #       (eq, ":continue", 1),
  #        (call_script, "script_diplomacy_start_war_between_kingdoms", ":cur_kingdom", ":cur_kingdom_2", ":initializing_war_peace_cond"),
  #       (try_end),
  #      (try_end),
  #     (try_end),
  #    (try_end),
  #    ]),
  
  
  
  # script_exchange_prisoners_between_factions
  # Input: arg1 = faction_no_1, arg2 = faction_no_2
  ("exchange_prisoners_between_factions",
    [
      (store_script_param_1, ":faction_no_1"),
      (store_script_param_2, ":faction_no_2"),
      (assign, ":faction_no_3", -1),
      (assign, ":faction_no_4", -1),
      (assign, ":free_companions_too", 0),
      (try_begin),
        (this_or_next|eq, "$players_kingdom", ":faction_no_1"),
        (eq, "$players_kingdom", ":faction_no_2"),
        (assign, ":faction_no_3", "fac_player_faction"),
        (assign, ":faction_no_4", "fac_player_supporters_faction"),
        (assign, ":free_companions_too", 1),
      (try_end),
      
      (try_for_parties, ":party_no"),
        (store_faction_of_party, ":party_faction", ":party_no"),
        (this_or_next|eq, ":party_faction", ":faction_no_1"),
        (this_or_next|eq, ":party_faction", ":faction_no_2"),
        (this_or_next|eq, ":party_faction", ":faction_no_3"),
        (eq, ":party_faction", ":faction_no_4"),
        (party_get_num_prisoner_stacks, ":num_stacks", ":party_no"),
        (try_for_range_backwards, ":troop_iterator", 0, ":num_stacks"),
          (party_prisoner_stack_get_troop_id, ":cur_troop_id", ":party_no", ":troop_iterator"),
          
          (assign, ":continue", 0),
          (try_begin),
            (is_between, ":cur_troop_id", companions_begin, companions_end),
            (eq, ":free_companions_too", 1),
            (assign, ":continue", 1),
          (else_try),
            (neg|is_between, ":cur_troop_id", companions_begin, companions_end),
            (store_troop_faction, ":cur_faction", ":cur_troop_id"),
            (this_or_next|eq, ":cur_faction", ":faction_no_1"),
            (this_or_next|eq, ":cur_faction", ":faction_no_2"),
            (this_or_next|eq, ":cur_faction", ":faction_no_3"),
            (eq, ":cur_faction", ":faction_no_4"),
            (assign, ":continue", 1),
          (try_end),
          (eq, ":continue", 1),
          
          (try_begin),
            (troop_is_hero, ":cur_troop_id"),
            (call_script, "script_remove_troop_from_prison", ":cur_troop_id"),
          (try_end),
          (party_prisoner_stack_get_size, ":stack_size", ":party_no", ":troop_iterator"),
          (party_remove_prisoners, ":party_no", ":cur_troop_id", ":stack_size"),
        (try_end),
      (try_end),
      
  ]),
  
  # script_add_notification_menu
  # Input: arg1 = menu_no, arg2 = menu_var_1, arg3 = menu_var_2
  # Output: none
  ("add_notification_menu",
    [
      (try_begin),
        (eq, "$g_infinite_camping", 0),
        (store_script_param, ":menu_no", 1),
        (store_script_param, ":menu_var_1", 2),
        (store_script_param, ":menu_var_2", 3),
        (assign, ":end_cond", 1),
        (try_for_range, ":cur_slot", 0, ":end_cond"),
          (try_begin),
            (troop_slot_ge, "trp_notification_menu_types", ":cur_slot", 1),
            (val_add, ":end_cond", 1),
          (else_try),
            (troop_set_slot, "trp_notification_menu_types", ":cur_slot", ":menu_no"),
            (troop_set_slot, "trp_notification_menu_var1", ":cur_slot", ":menu_var_1"),
            (troop_set_slot, "trp_notification_menu_var2", ":cur_slot", ":menu_var_2"),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  # script_finish_quest
  # Input: arg1 = quest_no, arg2 = finish_percentage
  # Output: none
  ("finish_quest",
    [
      (store_script_param_1, ":quest_no"),
      (store_script_param_2, ":finish_percentage"),
      
      (quest_get_slot, ":quest_giver", ":quest_no", slot_quest_giver_troop),
      (quest_get_slot, ":quest_importance", ":quest_no", slot_quest_importance),
      (quest_get_slot, ":quest_xp_reward", ":quest_no", slot_quest_xp_reward),
      (quest_get_slot, ":quest_gold_reward", ":quest_no", slot_quest_gold_reward),
      
      (try_begin),
        (lt, ":finish_percentage", 100),
        (val_mul, ":quest_xp_reward", ":finish_percentage"),
        (val_div, ":quest_xp_reward", 100),
        (val_mul, ":quest_gold_reward", ":finish_percentage"),
        (val_div, ":quest_gold_reward", 100),
        #Changing the relation factor. Negative relation if less than 75% of the quest is finished.
        #Positive relation if more than 75% of the quest is finished.
        (store_sub, ":importance_multiplier", ":finish_percentage", 75),
        (val_mul, ":quest_importance", ":importance_multiplier"),
        (val_div, ":quest_importance", 100),
      (else_try),
       (call_script, "script_change_player_relation_with_troop", ":quest_giver", ":quest_importance"),
      (try_end),
      
      (add_xp_as_reward, ":quest_xp_reward"),
      (call_script, "script_troop_add_gold", "trp_player", ":quest_gold_reward"),
      (call_script, "script_end_quest", ":quest_no"),
  ]),
  
  
  # script_get_information_about_troops_position
  # Input: arg1 = troop_no, arg2 = time (0 if present tense, 1 if past tense)
  # Output: s1 = String, reg0 = knows-or-not
  ("get_information_about_troops_position",
    [
      (store_script_param_1, ":troop_no"),
      (store_script_param_2, reg3),
      (troop_get_type, reg4, ":troop_no"),
      (val_mod, reg4, 2),
      (str_store_troop_name, s2, ":troop_no"),
      
      (assign, ":found", 0),
      (troop_get_slot, ":center_no", ":troop_no", slot_troop_cur_center),
      (try_begin),
        (gt, ":center_no", 0),
        (is_between, ":center_no", centers_begin, centers_end),
        (str_store_party_name_link, s3, ":center_no"),
        (str_store_string, s1, "@{s2} {reg3?was:is currently} at {s3}."),
        (assign, ":found", 1),
      (else_try),
        (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
        (gt, ":party_no", 0),
        (call_script, "script_get_troop_attached_party", ":troop_no"),
        (assign, ":center_no", reg0),
        (try_begin),
          (is_between, ":center_no", centers_begin, centers_end),
          (str_store_party_name_link, s3, ":center_no"),
          (str_store_string, s1, "@{s2} {reg3?was:is currently} at {s3}."),
          (assign, ":found", 1),
        (else_try),
          (get_party_ai_behavior, ":ai_behavior", ":party_no"),
          (eq, ":ai_behavior", ai_bhvr_travel_to_party),
          (get_party_ai_object, ":ai_object", ":party_no"),
          (is_between, ":ai_object", centers_begin, centers_end),
          (call_script, "script_get_closest_center", ":party_no"),
          (str_store_party_name_link, s4, reg0),
          (str_store_party_name_link, s3, ":ai_object"),
          (str_store_string, s1, "@{s2} {reg3?was:is} travelling to {s3}, and {reg4?she:he} {reg3?was:should be} close to {s4}{reg3?: at the moment}."),
          (assign, ":found", 1),
        (else_try),
          (call_script, "script_get_closest_center", ":party_no"),
          (str_store_party_name_link, s3, reg0),
          (str_store_string, s1, "@{s2} {reg3?was:is} in the field, and {reg4?she:he} {reg3?was:should be} close to {s3}{reg3?: at the moment}."),
          (assign, ":found", 1),
        (try_end),
      (else_try),
        #(troop_slot_ge, ":troop_no", slot_troop_is_prisoner, 1),
        (troop_slot_ge, ":troop_no", slot_troop_prisoner_of_party, 0),
        (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
          (party_count_prisoners_of_type, ":num_prisoners", ":center_no", ":troop_no"),
          (gt, ":num_prisoners", 0),
          (assign, ":found", 1),
          (str_store_party_name_link, s3, ":center_no"),
          (str_store_string, s1, "@{s2} {reg3?was:is} being held captive at {s3}."),
        (try_end),
        (try_begin),
          (eq, ":found", 0),
          (str_store_string, s1, "@{s2} {reg3?was:has been} taken captive by {reg4?her:his} enemies."),
          (assign, ":found", 1),
        (try_end),
      (try_end),
      (try_begin),
        (eq, ":found", 0),
        (str_store_string, s1, "@I don't know where {s2} {reg3?was:is}."),
      (try_end),
      (assign, reg0, ":found"),
      
      # changes for VC Story mode (VC-1240):
      (try_begin),
        (eq, "$campaign_type", camp_storyline),
        (eq, "$g_lord_hide", 2),
        (try_begin),
          (this_or_next|eq, ":troop_no", "trp_kingdom_8_lord"),
          (this_or_next|eq, ":troop_no", "trp_knight_8_4"),
          (this_or_next|eq, ":troop_no", "trp_knight_8_7"),
          (is_between, ":troop_no", "trp_knight_8_11", "trp_knight_9_1"),
          (str_store_string, s1, "@{s2} is attacking the Saxons."),
        (else_try),
          (this_or_next|eq, ":troop_no", "trp_kingdom_5_lord"),
          (is_between, ":troop_no", "trp_knight_5_1", "trp_knight_5_6"),
          (str_store_string, s1, "@{s2} is defending the kingdom of Ragnar's sons."),
        (end_try),
      (end_try),
      # VC changes end
  ]),
  
  # script_recruit_troop_as_companion
  # Input: arg1 = troop_no,
  # Output: none
  ("recruit_troop_as_companion",
    [
      (store_script_param_1, ":troop_no"),
      (troop_set_slot, ":troop_no", slot_troop_occupation, slto_player_companion),
      (troop_set_slot, ":troop_no", slot_troop_cur_center, -1),
      (troop_set_auto_equip, ":troop_no", 0),
      (party_add_members, "p_main_party", ":troop_no", 1),
      (str_store_troop_name, s6, ":troop_no"),
      (display_message, "@{s6} has joined your party.", color_hero_news),
      (troop_set_note_available, ":troop_no", 1),
      
      ###Initial level 13 to companions.
      ##               (set_show_messages, 0),#29999 xp is the max value for add_xp_to_troop
      ##               (add_xp_to_troop,5900,":troop_no"), #changed to level 3-4 for advoid overpower companions.
      ###level 13
      ####               (add_xp_to_troop,29900,":troop_no"), #29999 xp is the max value for add_xp_to_troop
      ####               (add_xp_to_troop,17999,":troop_no"), #29999 xp is the max value for add_xp_to_troop
      ##               (set_show_messages, 1),
      #####
      (try_begin),
        (is_between, ":troop_no", companions_begin, companions_end),
        (store_sub, ":companion_number", ":troop_no", companions_begin),
        
        (set_achievement_stat, ACHIEVEMENT_KNIGHTS_OF_THE_ROUND, ":companion_number", 1),
        
        (assign, ":number_of_companions_hired", 0),
        (try_for_range, ":cur_companion", 0, 16),
          (get_achievement_stat, ":is_hired", ACHIEVEMENT_KNIGHTS_OF_THE_ROUND, ":cur_companion"),
          (eq, ":is_hired", 1),
          (val_add, ":number_of_companions_hired", 1),
        (try_end),
        
        (try_begin),
          (ge, ":number_of_companions_hired", 6),
          (unlock_achievement, ACHIEVEMENT_KNIGHTS_OF_THE_ROUND),
        (try_end),
      (try_end),
  ]),
  
  
  # script_setup_random_scene
  # Input: arg1 = center_no, arg2 = mission_template_no
  # Output: none
  ("setup_random_scene",
    [
      (party_get_current_terrain, ":terrain_type", "p_main_party"),
      (assign, ":scene_to_use", "scn_random_scene"),
      (try_begin),
        (eq, ":terrain_type", rt_steppe),
        (assign, ":scene_to_use", "scn_random_scene_steppe"),
      (else_try),
        (eq, ":terrain_type", rt_plain),
        (assign, ":scene_to_use", "scn_random_scene_plain"),
      (else_try),
        (eq, ":terrain_type", rt_snow),
        (assign, ":scene_to_use", "scn_random_scene_snow"),
      (else_try),
        (eq, ":terrain_type", rt_desert),
        (assign, ":scene_to_use", "scn_random_scene_desert"),
      (else_try),
        (eq, ":terrain_type", rt_steppe_forest),
        (assign, ":scene_to_use", "scn_random_scene_steppe_forest"),
      (else_try),
        (eq, ":terrain_type", rt_forest),
        (assign, ":scene_to_use", "scn_random_scene_plain_forest"),
      (else_try),
        (eq, ":terrain_type", rt_snow_forest),
        (assign, ":scene_to_use", "scn_random_scene_snow_forest"),
      (else_try),
        (eq, ":terrain_type", rt_desert_forest),
        (assign, ":scene_to_use", "scn_random_scene_desert_forest"),
      (else_try),
        (eq, ":terrain_type", rt_water),
        (assign, ":scene_to_use", "scn_water"),
      (else_try),
        (eq, ":terrain_type", rt_bridge),
        (assign, ":scene_to_use", "scn_random_scene_plain"),
      (try_end),
      (jump_to_scene,":scene_to_use"),
  ]),
  
  # script_enter_dungeon
  # Input: arg1 = center_no, arg2 = mission_template_no
  # Output: none
  ("enter_dungeon",
    [
      (store_script_param_1, ":center_no"),
      (store_script_param_2, ":mission_template_no"),
      
      (set_jump_mission,":mission_template_no"),
      #new added...
      (mission_tpl_entry_set_override_flags, ":mission_template_no", 0, af_override_horse),
      (try_begin),
        (eq, "$sneaked_into_town", 1),
        (mission_tpl_entry_set_override_flags, ":mission_template_no", 0, af_override_all),
        
        (mission_tpl_entry_clear_override_items, ":mission_template_no", 0),
        # (mission_tpl_entry_add_override_item, ":mission_template_no", 0, "itm_pilgrim_hood"),
        (mission_tpl_entry_add_override_item, ":mission_template_no", 0, "itm_pilgrim_disguise"),
        (mission_tpl_entry_add_override_item, ":mission_template_no", 0, "itm_practice_staff"),
        (mission_tpl_entry_add_override_item, ":mission_template_no", 0, "itm_javelin"),
      (try_end),
      #new added end
      
      (party_get_slot, ":dungeon_scene", ":center_no", slot_town_prison),
      
      (modify_visitors_at_site,":dungeon_scene"),
      (reset_visitors),
      (assign, ":cur_pos", 16),
      # Torturer
      (assign, ":ch", 0),
      (try_begin),
        (party_slot_eq, ":center_no", slot_town_lord, "trp_player"),
        (party_slot_eq, ":center_no", slot_center_has_prisoner_tower, 1),
        (try_begin),
          (eq, ":center_no", "p_town_11"),
          (set_visitor, 16, "trp_doccinga_torturador"),
          (assign, ":ch", 1),
        (else_try),
          (this_or_next|is_between, ":center_no", "p_castle_45", "p_castle_48"),
          (this_or_next|eq,":center_no","p_castle_34"),
          (this_or_next|eq,":center_no","p_castle_30"),
          (this_or_next|eq,":center_no","p_castle_25"),
          (this_or_next|eq,":center_no","p_castle_6"),
          (this_or_next|eq,":center_no","p_castle_4"),
          (eq,":center_no","p_castle_7"),
          (set_visitor, 17, "trp_doccinga_torturador"),
          (assign, ":ch", 3),
        (else_try),
          (eq,":center_no","p_town_14"),
          (set_visitor, 18, "trp_doccinga_torturador"),
          (assign, ":ch", 4),
        (else_try),
          (this_or_next|eq,":center_no","p_castle_31"),
          (eq,":center_no","p_town_10"),
          (set_visitor, 31, "trp_doccinga_torturador"),
          (assign, ":ch", 5),
        (else_try),
          (set_visitor, 25, "trp_doccinga_torturador"),
          (assign, ":ch", 2),
        (try_end),
      (try_end),
      (try_begin),
        (eq, ":ch", 1),
        (assign, ":cur_pos", 17),
      (try_end),
      
      # Prisoners
      (call_script, "script_get_heroes_attached_to_center_as_prisoner", ":center_no", "p_temp_party"),
      (party_get_num_companion_stacks, ":num_stacks","p_temp_party"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop","p_temp_party",":i_stack"),
        
        (assign, ":prisoner_offered_parole", 0),
        (try_begin),
          (party_slot_eq, ":center_no", slot_town_lord, "trp_player"),
        (else_try),
          (call_script, "script_cf_prisoner_offered_parole", ":stack_troop"),
          (assign, ":prisoner_offered_parole", 1),
        (else_try),
          (assign, ":prisoner_offered_parole", 0),
        (try_end),
        (eq, ":prisoner_offered_parole", 0),
        (assign, ":cont", 0),
        (try_begin),
          (neq, "$talk_context", tc_prison_break),
          (troop_slot_ge, ":stack_troop", slot_troop_tortured, 1),
          (store_troop_health, ":health", ":stack_troop", 0),
          (le, ":health", 60),
          (assign, ":cont", 1),
        (try_end),
        (eq, ":cont", 0),
        (assign, ":end_pos", 32),
        (try_begin),
          (eq, ":ch",5),
          (assign, ":end_pos", 31),
        (try_end),
        (lt, ":cur_pos", ":end_pos"), # spawn up to entry point 32
        (try_begin),
          (eq, ":ch", 2),
          (eq, ":cur_pos", 25),
          (val_add,":cur_pos", 1),
        (else_try),
          (eq, ":ch", 3),
          (eq, ":cur_pos", 17),
          (val_add,":cur_pos", 1),
        (else_try),
          (eq, ":ch", 4),
          (eq, ":cur_pos", 18),
          (val_add,":cur_pos", 1),
        (try_end),
        (set_visitor, ":cur_pos", ":stack_troop"),
        (val_add,":cur_pos", 1),
      (try_end),
      
      #	  (set_visitor, ":cur_pos", "trp_npc3"),
      #	  (troop_set_slot, "trp_npc3", slot_troop_prisoner_of_party, "$g_encountered_party"),
      
      (set_jump_entry, 0),
      (jump_to_scene,":dungeon_scene"),
      (scene_set_slot, ":dungeon_scene", slot_scene_visited, 1),
      (change_screen_mission),
  ]),
  
  # script_enter_court
  # Input: arg1 = center_no
  # Output: none
  #other search term: setup_court
  ("enter_court",
    [
      (store_script_param_1, ":center_no"),
      
      (assign, "$talk_context", tc_court_talk),
      
      (set_jump_mission,"mt_visit_town_castle"),
      
      (mission_tpl_entry_clear_override_items, "mt_visit_town_castle", 0),
      #(mission_tpl_entry_set_override_flags, "mt_visit_town_castle", 0, af_override_all),
      
      (party_get_slot, ":castle_scene", ":center_no", slot_town_castle),
      (modify_visitors_at_site,":castle_scene"),
      (reset_visitors),
      #Adding guards
      (store_faction_of_party, ":center_faction", ":center_no"),
      (faction_get_slot, ":guard_troop", ":center_faction", slot_faction_guard_troop),
      (try_begin),
        (le, ":guard_troop", 0),
        (assign, ":guard_troop", "trp_briton_level1_companion"),
      (try_end),
      (set_visitor, 6, ":guard_troop"),
      (set_visitor, 7, ":guard_troop"),
      
      ### Add-ons chroniclers
      (try_begin),
        (eq, "$game_started_with_content_update", 1),
        (try_begin),
          (eq, ":center_no", "p_town_5"), #norse
          (set_visitor, 14, "trp_town_1_seneschal"),
        (try_end),
        (try_begin),
          (eq, ":center_no", "p_town_13"), #britons
          (set_visitor, 14, "trp_town_2_seneschal"),
        (try_end),
        (try_begin),
          (eq, ":center_no", "p_town_29"), #irish
          (set_visitor, 14, "trp_town_3_seneschal"),
        (try_end),
        (try_begin),
          (eq, ":center_no", "p_town_12"), #saxons
          (set_visitor, 14, "trp_town_4_seneschal"),
        (try_end),
      (try_end),
      
      ###Add-ons add Mael Bresail to Aileach court
      (try_begin), #first meeting.
        (eq, ":center_no", "p_town_25"),
        (check_quest_active, "qst_blank_quest_25"),
        (neg|check_quest_succeeded, "qst_blank_quest_25"),
        (quest_get_slot, ":quest_current_state", "qst_blank_quest_25", slot_quest_current_state),
        (ge, ":quest_current_state", 2),
        (set_visitor, 14, "trp_town_25_seneschal"),
      (try_end),
      (try_begin), #second meeting
        (eq, ":center_no", "p_town_25"),
        (check_quest_active, "qst_blank_quest_26"),
        (neg|check_quest_succeeded, "qst_blank_quest_26"),
        (quest_get_slot, ":quest_current_state", "qst_blank_quest_26", slot_quest_current_state),
        (eq, ":quest_current_state", 0),
        (set_visitor, 14, "trp_town_25_seneschal"),
      (try_end),
      #####
      #Adding priest
      (try_begin),
##        (faction_slot_eq, ":center_faction", slot_faction_leader, "trp_player"),
##        (try_begin),
##          (eq, "$g_player_faith", 2),
##          (set_visitor, 15, "trp_paganop_1"),
##        (else_try),
##          (set_visitor, 15, "trp_sacerdote_1"),
##        (try_end),
##      (else_try),
        (party_slot_eq, ":center_no", slot_town_lord, "trp_player"),
        (try_begin),
          (eq, "$g_player_faith", 2),
          (set_visitor, 15, "trp_paganop_1"),
        (else_try),
          (set_visitor, 15, "trp_sacerdote_1"),
        (try_end),
        
      (else_try),
            (party_get_slot, ":center_lord", ":center_no", slot_town_lord),
            (ge, ":center_lord", 0),
            (troop_get_slot, ":religion", ":center_lord", slot_troop_religion),
		(try_begin),
		  (eq, ":religion", 1),
                   (set_visitor, 15, "trp_sacerdote_1"),
		(else_try),
                   (set_visitor, 15, "trp_paganop_1"),
		(try_end),
      (else_try),
        (is_between, ":center_faction", "fac_kingdom_1", "fac_kingdom_5"),
        (set_visitor, 15, "trp_paganop_1"),
      (else_try),
        (is_between, ":center_faction", "fac_kingdom_5", "fac_kingdoms_end"),
        (set_visitor, 15, "trp_sacerdote_1"),
      (else_try),
        (set_visitor, 15, "trp_sacerdote_1"),
      (try_end),
      
      
      (assign, ":cur_pos", 16),
      
      (try_begin),
        (troop_get_slot, ":player_spouse", "trp_player", slot_troop_spouse),
        (gt, ":player_spouse", 0),
        (troop_slot_eq, ":player_spouse", slot_troop_cur_center, ":center_no"),
        (set_visitor, ":cur_pos", ":player_spouse"),
        (val_add,":cur_pos", 1),
      (else_try),
        (troop_get_slot, ":player_betrothed", "trp_player", slot_troop_betrothed),
        (gt, ":player_betrothed", 0),
        (troop_slot_eq, ":player_betrothed", slot_troop_cur_center, ":center_no"),
        (set_visitor, ":cur_pos", ":player_betrothed"),
        (val_add,":cur_pos", 1),
      (try_end),
      
      (try_begin),
        (eq, "$g_player_court", ":center_no"),
        (gt, "$g_player_minister", 0),
        (neg|troop_slot_eq, "trp_player", slot_troop_spouse, "$g_player_minister"),
        (set_visitor, ":cur_pos", "$g_player_minister"),
        (val_add,":cur_pos", 1),
      (try_end),
      
      #Lords wishing to pledge allegiance - inactive, but part of player faction
      (try_begin),
        (eq, "$g_player_court", ":center_no"),
        (faction_slot_eq, ":center_faction", slot_faction_leader, "trp_player"),
        
        #VC-3909 keep end game crowds out
        (store_sub, ":end_cond", active_npcs_end, active_npcs_begin),
        (val_mul, ":end_cond", 3),  #ensure a selection
        (try_for_range, ":i_stack", 1, ":end_cond"),
          (store_random_in_range, ":active_npc", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
          (eq, ":active_npc_faction", "fac_player_supporters_faction"),
          (troop_slot_eq, ":active_npc", slot_troop_occupation, slto_inactive),
          (neg|troop_slot_ge, ":active_npc", slot_troop_prisoner_of_party, 0),  #if he/she is not prisoner in any center.
          (troop_slot_eq, ":active_npc", slot_troop_change_to_faction, 0),  #hasn't already been refused by player (see lord recruit dialog)
          (neq, ":active_npc", "$g_player_minister"),
          (set_visitor, ":cur_pos", ":active_npc"),
          (val_add,":cur_pos", 1),
          (assign, ":end_cond", 0), #break
        (try_end),
      (try_end),
      
      (assign,":num_lords",0),
      (call_script, "script_get_heroes_attached_to_center", ":center_no", "p_temp_party"),
      (party_get_num_companion_stacks, ":num_stacks","p_temp_party"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop","p_temp_party",":i_stack"),
        (gt, ":stack_troop", 0), ####BUGFIX LINE moto chief
        # (lt, ":cur_pos", 32), # spawn up to entry point 32 - is it possible to add another 10 spots?
        # (set_visitor, ":cur_pos", ":stack_troop"),
        # (val_add,":cur_pos", 1),
        (val_add,":num_lords", 1),
        (troop_set_slot,"trp_temp_array_a",":num_lords",":stack_troop"),
      (try_end),
      (troop_set_slot,"trp_temp_array_a",0,":num_lords"),
      
      (assign,":num_ladies",0),
      (try_for_range, ":cur_troop", kingdom_ladies_begin, kingdom_ladies_end),
        (neq, ":cur_troop", "trp_knight_1_1_wife"), #The one who should not appear in game
        #(troop_slot_eq, ":cur_troop", slot_troop_occupation, slto_kingdom_lady),
        (troop_slot_eq, ":cur_troop", slot_troop_cur_center, ":center_no"),
        
        (assign, ":lady_meets_visitors", 0),
        (try_begin),
          (this_or_next|troop_slot_eq, "trp_player", slot_troop_spouse, ":cur_troop"), #player spouse goes in position of honor
          (this_or_next|troop_slot_eq, "trp_player", slot_troop_betrothed, ":cur_troop"), #player spouse goes in position of honor
          (this_or_next|troop_slot_eq, ":cur_troop", slot_troop_spouse, "trp_player"), #player spouse goes in position of honor
          (troop_slot_eq, ":cur_troop", slot_troop_betrothed, "trp_player"),
          
          (assign, ":lady_meets_visitors", 0), #She is already in the place of honor
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, s4, ":cur_troop"),
            (display_message, "str_s4_is_present_at_the_center_and_in_place_of_honor"),
          (try_end),
          
        (else_try), #lady is troop
          (store_faction_of_troop, ":lady_faction", ":cur_troop"),
          (neq, ":lady_faction", ":center_faction"),
          
          (assign, ":lady_meets_visitors", 1),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, s4, ":cur_troop"),
            (display_message, "str_s4_is_present_at_the_center_as_a_refugee"),
          (try_end),
          
        (else_try), #VC-3963 add case to weed out defecting or otherwise inactive lords' ladies
          (call_script, "script_get_kingdom_lady_social_determinants", ":cur_troop"),
          (assign, ":active_npc", reg0),
          (troop_slot_eq, ":active_npc", slot_troop_occupation, slto_inactive),
          (assign, ":lady_meets_visitors", 0),
          
        (else_try),
          (troop_slot_ge, ":cur_troop", slot_troop_spouse, 1),
          
          (try_begin),
            #married ladies at a feast will not mingle - this is ahistorical, as married women and widows probably had much more freedom than unmarried ones, at least in the West, but the game needs to leave slots for them to show off their unmarried daughters
            (faction_slot_eq, ":center_faction", slot_faction_ai_state, sfai_feast),
            (faction_slot_eq, ":center_faction", slot_faction_ai_object, ":center_no"),
            (assign, ":lady_meets_visitors", 0),
            
            (try_begin),
              (eq, "$cheat_mode", 1),
              (str_store_troop_name, s4, ":cur_troop"),
              (display_message, "str_s4_is_present_at_the_center_and_not_attending_the_feast"),
            (try_end),
            
          (else_try),
            (assign, ":lady_meets_visitors", 1),
            
            (try_begin),
              (eq, "$cheat_mode", 1),
              (str_store_troop_name, s4, ":cur_troop"),
              (display_message, "str_s4_is_present_at_the_center_and_is_married"),
            (try_end),
          (try_end),
          
        (else_try), #feast is in progress
          (faction_slot_eq, ":center_faction", slot_faction_ai_state, sfai_feast),
          (faction_slot_eq, ":center_faction", slot_faction_ai_object, ":center_no"),
          (assign, ":lady_meets_visitors", 1),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, s4, ":cur_troop"),
            (display_message, "@{!}DEBUG -- {s4} is present at the center and is attending the feast"),
          (try_end),
          
        (else_try), #already met - awaits in private
          (troop_slot_ge, ":cur_troop", slot_troop_met, 2),
          (assign, ":lady_meets_visitors", 0),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, s4, ":cur_troop"),
            (display_message, "@{!}DEBUG -- {s4} is present at the center and is awaiting the player in private"),
          (try_end),
          
        (else_try),
          (call_script, "script_npc_decision_checklist_male_guardian_assess_suitor", ":active_npc", "trp_player"),
          (gt, reg0, 0),
          (assign, ":lady_meets_visitors", 1),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, s4, ":cur_troop"),
            (display_message, "@{!}DEBUG -- {s4} is_present_at_the_center_and_is_allowed_to_meet_the_player"),
          (try_end),
          
        (else_try),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, s4, ":cur_troop"),
            (display_message, "@{!}DEBUG -- {s4}is_present_at_the_center_and_is_not_allowed_to_meet_the_player"),
          (try_end),
          
        (try_end),
        
        (eq, ":lady_meets_visitors", 1),
        
        # (lt, ":cur_pos", 32), # spawn up to entry point 32
        # (set_visitor, ":cur_pos", ":cur_troop"),
        # (val_add,":cur_pos", 1),
        (val_add,":num_ladies", 1),
        (troop_set_slot,"trp_temp_array_b",":num_ladies",":cur_troop"),
      (try_end),
      (troop_set_slot,"trp_temp_array_b",0,":num_ladies"),
      
      (try_begin),#guarantee at least 1 lady
        (gt,":num_ladies",0),
        (val_sub,":num_ladies",1),
        (call_script,"script_cf_get_value_from_array","trp_temp_array_b"),
        (set_visitor,":cur_pos",reg0),
        (val_add,":cur_pos",1),
      (try_end),
      
      (try_for_range,":spawn",":cur_pos",33),
        (store_add,":total_guests",":num_ladies",":num_lords"),
        (store_random_in_range,":r",0,":total_guests"),
        (try_begin),
          (lt,":r",":num_ladies"),#ladies
          (val_sub,":num_ladies",1),
          (call_script,"script_cf_get_value_from_array","trp_temp_array_b"),
          (set_visitor,":spawn",reg0),
        (else_try),#lords
          (val_sub,":num_lords",1),
          (call_script,"script_cf_get_value_from_array","trp_temp_array_a"),
          (set_visitor,":spawn",reg0),
        (try_end),
      (try_end),
      
      #VC-2404
      (try_begin),
        (call_script, "script_cf_player_use_second_outfit"),#is using second outfit?
        (call_script, "script_init_second_outfit", "mt_visit_town_castle", 0),
        (mission_tpl_entry_set_override_flags, "mt_visit_town_castle", 0, af_override_outfit_1|af_override_horse),
      (try_end),
      
      (set_jump_entry, 0),
      
      (jump_to_scene,":castle_scene"),
      (scene_set_slot, ":castle_scene", slot_scene_visited, 1),
      (change_screen_mission),
  ]),
  
  
  ("setup_meet_lady",
    [
      (store_script_param_1, ":lady_no"),
      (store_script_param_2, ":center_no"),
      
      #(mission_tpl_entry_set_override_flags, "mt_visit_town_castle", 0, af_override_horse),
      (troop_set_slot, ":lady_no", slot_lady_last_suitor, "trp_player"),
      
      (set_jump_mission,"mt_visit_town_castle"),
      (party_get_slot, ":castle_scene", ":center_no", slot_town_castle),
      (modify_visitors_at_site,":castle_scene"),
      (reset_visitors),
      
      (troop_set_age, "trp_nurse_for_lady", 100),
      (set_visitor, 7, "trp_nurse_for_lady"),
      
      (assign, ":cur_pos", 16),
      (set_visitor, ":cur_pos", ":lady_no"),
      
      (assign, "$talk_context", tc_garden),
      
      (jump_to_scene,":castle_scene"),
      (scene_set_slot, ":castle_scene", slot_scene_visited, 1),
      (change_screen_mission),
  ]),
  
  # script_find_high_ground_around_pos1
  # Input: pos1 should hold center_position_no
  #        arg1: team_no
  #        arg2: search_radius (in meters)
  # Output: pos52 contains highest ground within <search_radius> meters of team leader
  # Destroys position registers: pos10, pos11, pos15
  ("find_high_ground_around_pos1",
    [
      (store_script_param, ":team_no", 1),
      (store_script_param, ":search_radius", 2),
      (val_mul, ":search_radius", 100),
      (get_scene_boundaries, pos10,pos11),
      (team_get_leader, ":ai_leader", ":team_no"),
      (agent_get_position, pos1, ":ai_leader"),
      (set_fixed_point_multiplier, 100),
      (position_get_x, ":o_x", pos1),
      (position_get_y, ":o_y", pos1),
      (store_sub, ":min_x", ":o_x", ":search_radius"),
      (store_sub, ":min_y", ":o_y", ":search_radius"),
      (store_add, ":max_x", ":o_x", ":search_radius"),
      (store_add, ":max_y", ":o_y", ":search_radius"),
      (position_get_x, ":scene_min_x", pos10),
      (position_get_x, ":scene_max_x", pos11),
      (position_get_y, ":scene_min_y", pos10),
      (position_get_y, ":scene_max_y", pos11),
      #do not find positions close to borders (20 m)
      (val_add, ":scene_min_x", 2000),
      (val_sub, ":scene_max_x", 2000),
      (val_add, ":scene_min_y", 2000),
      (val_sub, ":scene_max_y", 2000),
      (val_max, ":min_x", ":scene_min_x"),
      (val_max, ":min_y", ":scene_min_y"),
      (val_min, ":max_x", ":scene_max_x"),
      (val_min, ":max_y", ":scene_max_y"),
      
      (store_div, ":min_x_meters", ":min_x", 100),
      (store_div, ":min_y_meters", ":min_y", 100),
      (store_div, ":max_x_meters", ":max_x", 100),
      (store_div, ":max_y_meters", ":max_y", 100),
      
      (assign, ":highest_pos_z", -10000),
      (copy_position, pos52, pos1),
      (init_position, pos15),
      
      (try_for_range, ":i_x", ":min_x_meters", ":max_x_meters"),
        (store_mul, ":i_x_cm", ":i_x", 100),
        (try_for_range, ":i_y", ":min_y_meters", ":max_y_meters"),
          (store_mul, ":i_y_cm", ":i_y", 100),
          (position_set_x, pos15, ":i_x_cm"),
          (position_set_y, pos15, ":i_y_cm"),
          (position_set_z, pos15, 10000),
          (position_set_z_to_ground_level, pos15),
          (position_get_z, ":cur_pos_z", pos15),
          (try_begin),
            (gt, ":cur_pos_z", ":highest_pos_z"),
            (copy_position, pos52, pos15),
            (assign, ":highest_pos_z", ":cur_pos_z"),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  # script_select_battle_tactic
  # Input: none
  # Output: none
  # ("select_battle_tactic",
    # [
      # (assign, "$ai_team_1_battle_tactic", 0),
      # (get_player_agent_no, ":player_agent"),
      # (agent_get_team, ":player_team", ":player_agent"),
      # (try_begin),
        # (num_active_teams_le, 2),
        # (try_begin),
          # (eq, ":player_team", 0),
          # (assign, "$ai_team_1", 1),
        # (else_try),
          # (assign, "$ai_team_1", 0),
        # (try_end),
        # (assign, "$ai_team_2", -1),
      # (else_try),
        # (try_begin),
          # (eq, ":player_team", 0),
          # (assign, "$ai_team_1", 1),
        # (else_try),
          # (assign, "$ai_team_1", 0),
        # (try_end),
        # (store_add, "$ai_team_2", ":player_team", 2),
      # (try_end),
      # (call_script, "script_select_battle_tactic_aux", "$ai_team_1", 0),
      # (assign, "$ai_team_1_battle_tactic", reg0),
      # (try_begin),
        # (ge, "$ai_team_2", 0),
        # (assign, ":defense_not_an_option", 0),
        # (try_begin),
          # (eq, "$ai_team_1_battle_tactic", btactic_hold),
          # (assign, ":defense_not_an_option", 1), #don't let two AI defend at the same time
        # (try_end),
        # (call_script, "script_select_battle_tactic_aux", "$ai_team_2", ":defense_not_an_option"),
        # (assign, "$ai_team_2_battle_tactic", reg0),
      # (try_end),
  # ]),
  
  # script_select_battle_tactic_aux
  # Input: team_no
  # Output: battle_tactic
  # ("select_battle_tactic_aux",
    # [
      # (store_script_param, ":team_no", 1),
      # (store_script_param, ":defense_not_an_option", 2),
      # (assign, ":battle_tactic", 0),
      # (get_player_agent_no, ":player_agent"),
      # (agent_get_team, ":player_team", ":player_agent"),
      # (try_begin),
        # (eq, "$cant_leave_encounter", 1),
        # (teams_are_enemies, ":team_no", ":player_team"),
        # (assign, ":defense_not_an_option", 1),
      # (try_end),
      # (call_script, "script_team_get_class_percentages", ":team_no", 0),
      # #      (assign, ":ai_perc_infantry", reg0),
      # (assign, ":ai_perc_archers",  reg1),
      # (assign, ":ai_perc_cavalry",  reg2),
      # (call_script, "script_team_get_class_percentages", ":team_no", 1),#enemies of the ai_team
      # #      (assign, ":enemy_perc_infantry", reg0),
      # #      (assign, ":enemy_perc_archers",  reg1),
      # #      (assign, ":enemy_perc_cavalry",  reg2),
      
      # (store_random_in_range, ":rand", 0, 100),
      # (try_begin),
        # (assign, ":continue", 0),
        # (try_begin),
          # (teams_are_enemies, ":team_no", ":player_team"),
          # (party_slot_eq, "$g_enemy_party", slot_party_type, spt_kingdom_hero_party),
          # (assign, ":continue", 1),
        # (else_try),
          # (neg|teams_are_enemies, ":team_no", ":player_team"),
          # (gt, "$g_ally_party", 0),
          # (party_slot_eq, "$g_ally_party", slot_party_type, spt_kingdom_hero_party),
          # (assign, ":continue", 1),
        # (try_end),
        # #(this_or_next|lt, ":rand", 20),
        # (eq, ":continue", 1),
        # #(store_faction_of_party, ":enemy_faction_no", "$g_enemy_party"),
        # #(neq, ":enemy_faction_no", "fac_kingdom_3"), #don't let khergits use battle tactics
        # (try_begin),
          # (eq, ":defense_not_an_option", 0),
          # (gt, ":ai_perc_archers", 50),
          # (lt, ":ai_perc_cavalry", 35),
          # (assign, ":battle_tactic", btactic_hold),
        # (else_try),
          # (lt, ":rand", 80),
          # (assign, ":battle_tactic", btactic_follow_leader),
        # (try_end),
      # (try_end),
      # (assign, reg0, ":battle_tactic"),
  # ]),
  
  # script_battle_calculate_initial_powers
  # Input: none
  # Output: none
  #("battle_calculate_initial_powers",
  #  [
  #    (try_for_agents, ":agent_no"),
  #      (agent_is_human, ":agent_no"),
  #
  #      (call_script, "script_calculate_team_powers", ":agent_no"),
  #      (assign, ":ally_power", reg0),
  #      (assign, ":enemy_power", reg1),
  #
  #      (agent_set_slot, ":agent_no", slot_agent_initial_ally_power, ":ally_power"),
  #      (agent_set_slot, ":agent_no", slot_agent_initial_enemy_power, ":enemy_power"),
  #    (try_end),
  #]),
  
  # script_battle_tactic_init
  # Input: none
  # Output: none
  # ("battle_tactic_init",
    # [
      # (call_script, "script_battle_tactic_init_aux", "$ai_team_1", "$ai_team_1_battle_tactic"),
      # (try_begin),
        # (ge, "$ai_team_2", 0),
        # (call_script, "script_battle_tactic_init_aux", "$ai_team_2", "$ai_team_2_battle_tactic"),
      # (try_end),
      
      # (try_for_agents, ":cur_agent"),
        # (agent_set_slot, ":cur_agent",  slot_agent_is_running_away, 0), #initially nobody is running away.
      # (try_end),
  # ]),
  
  # script_battle_tactic_init_aux
  # Input: team_no, battle_tactic
  # Output: none
  # ("battle_tactic_init_aux",
    # [
      # (store_script_param, ":team_no", 1),
      # (store_script_param, ":battle_tactic", 2),
      # (team_get_leader, ":ai_leader", ":team_no"),
      # (try_begin),
        # (eq, ":battle_tactic", btactic_hold),
        # (agent_get_position, pos1, ":ai_leader"),
        # (call_script, "script_find_high_ground_around_pos1", ":team_no", 30),
        # (copy_position, pos1, pos52),
        # (call_script, "script_find_high_ground_around_pos1", ":team_no", 30), # call again just in case we are not at peak point.
        # (copy_position, pos1, pos52),
        # (call_script, "script_find_high_ground_around_pos1", ":team_no", 30), # call again just in case we are not at peak point.
        # (team_give_order, ":team_no", grc_everyone, mordr_hold),
        # (team_set_order_position, ":team_no", grc_everyone, pos52),
        # (team_give_order, ":team_no", grc_archers, mordr_advance),
        # (team_give_order, ":team_no", grc_archers, mordr_advance),
      # (else_try),
        # (eq, ":battle_tactic", btactic_follow_leader),
        # (team_get_leader, ":ai_leader", ":team_no"),
        # (ge, ":ai_leader", 0),
        # (agent_set_speed_limit, ":ai_leader", 8),
        # (agent_get_position, pos60, ":ai_leader"),
        # (team_give_order, ":team_no", grc_everyone, mordr_hold),
        # (team_set_order_position, ":team_no", grc_everyone, pos60),
      # (try_end),
  # ]),
  
  # script_calculate_team_powers
  # Input: none
  # Output: ally_power, enemy_power
  # ("calculate_team_powers",
    # [
      # (store_script_param, ":agent_no", 1),
      
      # (try_begin),
        # (assign, ":agent_side", 0),
        # (agent_is_ally, ":agent_no"),
        # (assign, ":agent_side", 1),
      # (try_end),
      
      # (assign, ":ally_power", 0),
      # (assign, ":enemy_power", 0),
      
      # (try_for_agents, ":cur_agent"),
        # (agent_is_human, ":cur_agent"),
        # (agent_is_alive, ":cur_agent"),
        
        # (try_begin),
          # (assign, ":agent_side_cur", 0),
          # (agent_is_ally, ":cur_agent"),
          # (assign, ":agent_side_cur", 1),
        # (try_end),
        
        # (try_begin),
          # (agent_get_horse, ":agent_horse_id", ":cur_agent"),
          # (neq, ":agent_horse_id", -1),
          # (assign, ":agent_power", 2), #if this agent is horseman then his power effect is 2
        # (else_try),
          # (assign, ":agent_power", 1), #if this agent is walker then his power effect is 1
        # (try_end),
        
        # (try_begin),
          # (eq, ":agent_side", ":agent_side_cur"),
          # (val_add, ":ally_power", ":agent_power"),
        # (else_try),
          # (val_add, ":enemy_power", ":agent_power"),
        # (try_end),
      # (try_end),
      
      # (assign, reg0, ":ally_power"),
      # (assign, reg1, ":enemy_power"),
  # ]), #ozan
  ####
  # script_change_courage_around_agent
  # Input: none
  # Output: none
  ("change_courage_around_agent",
    [
      (store_script_param, ":delta_courage", 1),
      (store_script_param, ":main_agent", 2),
      (get_player_agent_no, ":player_agent"),
      (set_fixed_point_multiplier, 100),
      
      (try_begin),
        (try_begin),
          (agent_is_ally, ":main_agent"),
          (assign, ":main_agent_is_ally", 1),
        (else_try),
          (assign, ":main_agent_is_ally", 0),
        (try_end),
        (agent_get_position, pos1, ":main_agent"),
        
        (try_for_agents, ":agent"),
          (agent_is_human, ":agent"),
          (agent_is_alive, ":agent"),
          (neq, ":agent", ":player_agent"),
          (try_begin),
            (agent_is_ally, ":agent"),
            (assign, ":agent_is_ally", 1),
          (else_try),
            (assign, ":agent_is_ally", 0),
          (try_end),
          (eq, ":main_agent_is_ally", ":agent_is_ally"),
          (agent_get_position, pos2, ":agent"),
          (get_distance_between_positions, ":dist", pos1, pos2),
          (assign, ":agent_delta_courage_score", ":delta_courage"),
          #(agent_get_slot, reg7, ":agent", slot_agent_courage_score),
          
          (try_begin),
            (lt, ":dist", 2000), #0-20 meter
            (agent_get_slot, ":agent_courage_score", ":agent", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 50),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 4000), #21-40 meter
            (agent_get_slot, ":agent_courage_score", ":agent", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 40),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 7000), #41-70 meter
            (agent_get_slot, ":agent_courage_score", ":agent", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 30),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 11000), #71-110 meter
            (agent_get_slot, ":agent_courage_score", ":agent", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 20),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 16000), # 111-160 meter
            (agent_get_slot, ":agent_courage_score", ":agent", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 10),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent", slot_agent_courage_score, ":agent_courage_score"),
          (try_end),
          
          # (agent_get_slot, reg8, ":agent", slot_agent_courage_score),
          # (display_message, "@courage from {reg7} to {reg8}"),
        (try_end),
      (try_end),
  ]),
  ####
  # script_apply_effect_of_other_people_on_courage_scores
  # Input: none
  # Output: none
  ("apply_effect_of_other_people_on_courage_scores",
    [
      (get_player_agent_no, ":player_agent"),
      (store_random_in_range, ":turn", 0, 75),
      
      (try_for_agents, ":centered_agent_no"),
        (store_mod, reg0, ":centered_agent_no", 75),
        (eq, reg0, ":turn"),
        (agent_is_human, ":centered_agent_no"),
        (agent_is_alive, ":centered_agent_no"),
        (neq, ":centered_agent_no", ":player_agent"),
        (agent_get_position, pos0, ":centered_agent_no"),
        (try_begin),
          (agent_is_ally, ":centered_agent_no"),
          (assign, ":is_centered_agent_ally", 1),
        (else_try),
          (assign, ":is_centered_agent_ally", 0),
        (try_end),
        
        (try_for_agents, ":agent_no"),
          (agent_is_human, ":agent_no"),
          (agent_is_alive, ":agent_no"),
          (neq, ":centered_agent_no", ":agent_no"),
          
          (try_begin),
            (agent_is_ally, ":agent_no"),
            (assign, ":is_agent_ally", 1),
          (else_try),
            (assign, ":is_agent_ally", 0),
          (try_end),
          
          (eq, ":is_centered_agent_ally", ":is_agent_ally"), #if centered agent and other agent is at same team then continue.
          (agent_get_slot, ":agent_is_running_away_or_not", ":agent_no", slot_agent_is_running_away),
          
          (try_begin),
            (eq, ":agent_no", ":player_agent"),
            (assign, ":agent_delta_courage_score", 6),
          (else_try),
            (agent_get_troop_id, ":troop_id", ":agent_no"),
            (troop_is_hero, ":troop_id"),
            
            #Hero Agent : if near agent (hero, agent_no) is not running away his positive effect on centered agent (centered_agent_no) fighting at his side is effected by his hit points.
            (try_begin),
              (neq, ":agent_is_running_away_or_not", 1), #if agent is not running away
              (store_agent_hit_points, ":agent_hit_points", ":agent_no"),
              (try_begin),
                (eq, ":agent_hit_points", 100),
                (assign, ":agent_delta_courage_score", 6),
              (else_try),
                (ge, ":agent_hit_points", 75),
                (assign, ":agent_delta_courage_score", 5),
              (else_try),
                (ge, ":agent_hit_points", 60),
                (assign, ":agent_delta_courage_score", 4),
              (else_try),
                (ge, ":agent_hit_points", 45),
                (assign, ":agent_delta_courage_score", 3),
              (else_try),
                (ge, ":agent_hit_points", 30),
                (assign, ":agent_delta_courage_score", 2),
              (else_try),
                (ge, ":agent_hit_points", 15),
                (assign, ":agent_delta_courage_score", 1),
              (end_try),
            (else_try),
              (assign, ":agent_delta_courage_score", 4),
            (end_try),
          (else_try),
            #Normal Agent : if near agent (agent_no) is not running away his positive effect on centered agent (centered_agent_no) fighting at his side is effected by his hit points.
            (try_begin),
              (neq, ":agent_is_running_away_or_not", 1), # if agent is not running away
              (store_agent_hit_points, ":agent_hit_points", ":agent_no"),
              (try_begin),
                (eq, ":agent_hit_points", 100),
                (assign, ":agent_delta_courage_score", 4),
              (else_try),
                (ge, ":agent_hit_points", 75),
                (assign, ":agent_delta_courage_score", 3),
              (else_try),
                (ge, ":agent_hit_points", 50),
                (assign, ":agent_delta_courage_score", 2),
              (else_try),
                (ge, ":agent_hit_points", 25),
                (assign, ":agent_delta_courage_score", 1),
              (try_end),
              (try_begin), # to make our warrior run away easier we decrease one, because they have player_agent (+6) advantage.
                (agent_is_ally, ":agent_no"),
                (val_sub, ":agent_delta_courage_score", 1),
              (try_end),
            (else_try),
              (assign, ":agent_delta_courage_score", 2),
            (end_try),
          (try_end),
          
          (try_begin),
            (neq, ":agent_is_running_away_or_not", 1),
            (val_mul, ":agent_delta_courage_score", 1),
            (try_begin), # centered agent not running away cannot take positive courage score from one another agent not running away.
              (agent_get_slot, ":agent_is_running_away_or_not", ":centered_agent_no", slot_agent_is_running_away),
              (eq, ":agent_is_running_away_or_not", 0),
              (val_mul, ":agent_delta_courage_score", 0),
            (try_end),
          (else_try),
            (try_begin),
              (agent_get_slot, ":agent_is_running_away_or_not", ":agent_no", slot_agent_is_running_away),
              (eq, ":agent_is_running_away_or_not", 0),
              (val_mul, ":agent_delta_courage_score", -2), # running away agent fears not running away agent more.
            (else_try),
              (val_mul, ":agent_delta_courage_score", -1),
            (try_end),
          (try_end),
          
          (neq, ":agent_delta_courage_score", 0),
          
          (agent_get_position, pos1, ":agent_no"),
          (get_distance_between_positions, ":dist", pos0, pos1),
          
          (try_begin),
            (ge, ":agent_delta_courage_score", 0),
            (try_begin),
              (lt, ":dist", 2000), #0-20 meter
              (agent_get_slot, ":agent_courage_score", ":centered_agent_no", slot_agent_courage_score),
              (val_mul, ":agent_delta_courage_score", 50),
              (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
              (agent_set_slot, ":centered_agent_no", slot_agent_courage_score, ":agent_courage_score"),
            (else_try),
              (lt, ":dist", 4000), #21-40 meter
              (agent_get_slot, ":agent_courage_score", ":centered_agent_no", slot_agent_courage_score),
              (val_mul, ":agent_delta_courage_score", 40),
              (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
              (agent_set_slot, ":centered_agent_no", slot_agent_courage_score, ":agent_courage_score"),
            (else_try),
              (lt, ":dist", 7000), #41-70 meter
              (agent_get_slot, ":agent_courage_score", ":centered_agent_no", slot_agent_courage_score),
              (val_mul, ":agent_delta_courage_score", 30),
              (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
              (agent_set_slot, ":centered_agent_no", slot_agent_courage_score, ":agent_courage_score"),
            (else_try),
              (lt, ":dist", 11000), #71-110 meter
              (agent_get_slot, ":agent_courage_score", ":centered_agent_no", slot_agent_courage_score),
              (val_mul, ":agent_delta_courage_score", 20),
              (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
              (agent_set_slot, ":centered_agent_no", slot_agent_courage_score, ":agent_courage_score"),
            (else_try),
              (lt, ":dist", 16000), # 111-160 meter, assumed that eye can see agents friendly at most 160 meters far while fighting.
              # this is more than below limit (108 meters) because we hear that allies come from further.
              (agent_get_slot, ":agent_courage_score", ":centered_agent_no", slot_agent_courage_score),
              (val_mul, ":agent_delta_courage_score", 10),
              (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
              (agent_set_slot, ":centered_agent_no", slot_agent_courage_score, ":agent_courage_score"),
            (try_end),
          (else_try),                                               # negative effect of running agent on other ally agents are lower then positive effects above, to avoid starting
            (try_begin),                                            # run away of all agents at a moment. I want to see agents running away one by one during battle, not all together.
              (lt, ":dist", 200), #1-2 meter,                       # this would create better game play.
              (agent_get_slot, ":agent_courage_score", ":centered_agent_no", slot_agent_courage_score),
              (val_mul, ":agent_delta_courage_score", 15),
              (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
              (agent_set_slot, ":centered_agent_no", slot_agent_courage_score, ":agent_courage_score"),
            (else_try),
              (lt, ":dist", 400), #3-4 meter,
              (agent_get_slot, ":agent_courage_score", ":centered_agent_no", slot_agent_courage_score),
              (val_mul, ":agent_delta_courage_score", 13),
              (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
              (agent_set_slot, ":centered_agent_no", slot_agent_courage_score, ":agent_courage_score"),
            (else_try),
              (lt, ":dist", 600), #5-6 meter
              (agent_get_slot, ":agent_courage_score", ":centered_agent_no", slot_agent_courage_score),
              (val_mul, ":agent_delta_courage_score", 11),
              (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
              (agent_set_slot, ":centered_agent_no", slot_agent_courage_score, ":agent_courage_score"),
            (else_try),
              (lt, ":dist", 800), #7-8 meter
              (agent_get_slot, ":agent_courage_score", ":centered_agent_no", slot_agent_courage_score),
              (val_mul, ":agent_delta_courage_score", 9),
              (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
              (agent_set_slot, ":centered_agent_no", slot_agent_courage_score, ":agent_courage_score"),
            (else_try),
              (lt, ":dist", 1200), #9-12 meters
              (agent_get_slot, ":agent_courage_score", ":centered_agent_no", slot_agent_courage_score),
              (val_mul, ":agent_delta_courage_score", 7),
              (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
              (agent_set_slot, ":centered_agent_no", slot_agent_courage_score, ":agent_courage_score"),
            (else_try),
              (lt, ":dist", 2400), #13-24 meters
              (agent_get_slot, ":agent_courage_score", ":centered_agent_no", slot_agent_courage_score),
              (val_mul, ":agent_delta_courage_score", 5),
              (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
              (agent_set_slot, ":centered_agent_no", slot_agent_courage_score, ":agent_courage_score"),
            (else_try),
              (lt, ":dist", 4800), #25-48 meters
              (agent_get_slot, ":agent_courage_score", ":centered_agent_no", slot_agent_courage_score),
              (val_mul, ":agent_delta_courage_score", 3),
              (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
              (agent_set_slot, ":centered_agent_no", slot_agent_courage_score, ":agent_courage_score"),
            (else_try),
              (lt, ":dist", 9600), #49-98 meters, assumed that eye can see agents running away at most 98 meters far while fighting.
              (agent_get_slot, ":agent_courage_score", ":centered_agent_no", slot_agent_courage_score),
              (val_mul, ":agent_delta_courage_score", 1),
              (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
              (agent_set_slot, ":centered_agent_no", slot_agent_courage_score, ":agent_courage_score"),
            (try_end),
          (try_end),
        (try_end),
      (try_end),
  ]), #ozan
  
  
  # script_apply_death_effect_on_courage_scores
  # Input: dead agent id, killer agent id
  # Output: none
  ("apply_death_effect_on_courage_scores",
    [
      (store_script_param, ":dead_agent_no", 1),
      (store_script_param, ":killer_agent_no", 2),
      
      (try_begin),
        (agent_is_human, ":dead_agent_no"),
        (set_fixed_point_multiplier, 100),
        (try_begin),
          (agent_is_ally, ":dead_agent_no"),
          (assign, ":is_dead_agent_ally", 1),
        (else_try),
          (assign, ":is_dead_agent_ally", 0),
        (try_end),
        
        (agent_get_position, pos2, ":dead_agent_no"),
        (assign, ":number_of_near_allies_to_dead_agent", 0),
        
        (try_for_agents, ":agent_no", pos2, 1600),
          (agent_is_human, ":agent_no"),
          (agent_is_alive, ":agent_no"),
          
          (try_begin),
            (agent_is_ally, ":agent_no"),
            (assign, ":is_agent_ally", 1),
          (else_try),
            (assign, ":is_agent_ally", 0),
          (try_end),
          
          (try_begin),
            (eq, ":is_dead_agent_ally", ":is_agent_ally"),
            (val_add, ":number_of_near_allies_to_dead_agent", 1), # (number_of_near_allies_to_dead_agent) is counted because if there are
          (try_end),                                              # many allies of dead agent around him, negative courage effect become less.
        (try_end),
        
        (try_for_agents, ":agent_no"),
          (agent_is_human, ":agent_no"),
          (agent_is_alive, ":agent_no"),
          
          (try_begin),
            (agent_is_ally, ":agent_no"),
            (assign, ":is_agent_ally", 1),
          (else_try),
            (assign, ":is_agent_ally", 0),
          (try_end),
          
          (try_begin), # each agent is effected by a killed agent positively if he is rival or negatively if he is ally.
            (neq, ":is_dead_agent_ally", ":is_agent_ally"),
            (assign, ":agent_delta_courage_score", 10),  # if killed agent is agent of rival side, add points to fear score
          (else_try),
            #(assign, ":agent_delta_courage_score", -15), # if killed agent is agent of our side, decrease points from fear score
            (assign, ":agent_delta_courage_score", -20),	#phaiak vc_courage
            (val_add, ":agent_delta_courage_score", ":number_of_near_allies_to_dead_agent"), # ":number_of_near_allies_to_dead_agent" is added because if there are many
            (try_begin),                                                                     # allies of dead agent around him, negative courage effect become less.
              (gt, ":agent_delta_courage_score", -5),
              (assign, ":agent_delta_courage_score", -5),
            (try_end),
            
            (agent_get_slot, ":dead_agent_was_running_away_or_not", ":dead_agent_no",  slot_agent_is_running_away), #look dead agent was running away or not.
            (try_begin),
              (eq, ":dead_agent_was_running_away_or_not", 1),
              (val_div, ":agent_delta_courage_score", 3),  # if killed agent was running away his negative effect on ally courage scores become very less. This added because
            (try_end),                                     # running away agents are easily killed and courage scores become very in a running away group after a time, and
          (try_end),                                       # they do not stop running away althought they pass near a new powerfull ally party.
          (agent_get_position, pos1, ":agent_no"),
          (get_distance_between_positions, ":dist", pos2, pos1),
          
          (try_begin),
            (eq, ":killer_agent_no", ":agent_no"),
            (agent_get_slot, ":agent_courage_score", ":agent_no", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 20),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent_no", slot_agent_courage_score, ":agent_courage_score"),
          (try_end),
          
          (try_begin),
            (lt, ":dist", 100), #0-1 meters
            (agent_get_slot, ":agent_courage_score", ":agent_no", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 150),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent_no", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 200), #2 meters
            (agent_get_slot, ":agent_courage_score", ":agent_no", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 120),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent_no", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 300), #3 meter
            (agent_get_slot, ":agent_courage_score", ":agent_no", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 100),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent_no", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 400), #4 meters
            (agent_get_slot, ":agent_courage_score", ":agent_no", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 90),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent_no", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 600), #5-6 meters
            (agent_get_slot, ":agent_courage_score", ":agent_no", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 80),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent_no", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 800), #7-8 meters
            (agent_get_slot, ":agent_courage_score", ":agent_no", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 70),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent_no", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 1000), #9-10 meters
            (agent_get_slot, ":agent_courage_score", ":agent_no", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 60),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent_no", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 1500), #11-15 meter
            (agent_get_slot, ":agent_courage_score", ":agent_no", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 50),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent_no", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 2500), #16-25 meters
            (agent_get_slot, ":agent_courage_score", ":agent_no", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 40),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent_no", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 4000), #26-40 meters
            (agent_get_slot, ":agent_courage_score", ":agent_no", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 30),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent_no", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 6500), #41-65 meters
            (agent_get_slot, ":agent_courage_score", ":agent_no", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 20),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent_no", slot_agent_courage_score, ":agent_courage_score"),
          (else_try),
            (lt, ":dist", 10000), #61-100 meters
            (agent_get_slot, ":agent_courage_score", ":agent_no", slot_agent_courage_score),
            (val_mul, ":agent_delta_courage_score", 10),
            (val_add, ":agent_courage_score", ":agent_delta_courage_score"),
            (agent_set_slot, ":agent_no", slot_agent_courage_score, ":agent_courage_score"),
          (try_end),
        (try_end),
      (try_end),
  ]), #ozan
  
  # script_decide_run_away_or_not moto chief
  # Input: agent, mission timer
  # Output: none
  # Note: starting morale is 7200 +/- 1500 (see mt_lead_charge)
  ("decide_run_away_or_not",
    [
      (store_script_param, ":cur_agent", 1),
      (store_script_param, ":mission_time", 2),
      
      (assign, ":force_retreat", 0),
      (agent_get_team, ":agent_team", ":cur_agent"),
      (agent_get_division, ":agent_division", ":cur_agent"),
      (try_begin),
        (lt, ":agent_division", 9), #static classes
        (team_get_movement_order, ":agent_movement_order", ":agent_team", ":agent_division"),
        (eq, ":agent_movement_order", mordr_retreat),
        (assign, ":force_retreat", 1),
      (try_end),
      
      (agent_get_slot, ":is_cur_agent_running_away", ":cur_agent", slot_agent_is_running_away),
      (try_begin),
        (eq, ":is_cur_agent_running_away", 0),
        (try_begin),
          (eq, ":force_retreat", 1),
          (agent_clear_scripted_mode, ":cur_agent"),	#MOTO handle scripted mode troops
          (agent_start_running_away, ":cur_agent"),
          (agent_set_slot, ":cur_agent",  slot_agent_is_running_away, 1),
        (else_try),
          #phaiak changes begin
          #(ge, ":mission_time", 180), #first 45 seconds anyone does not run away whatever happens. Chief cambia a 180 segundos >>> Phaiak @ Idibil: Well, I think thats why agents dont run away for you... lol
          (ge, ":mission_time", 45), # native = 45, brytenwalda = 180, vc = ?
          #phaiak changes end
          (agent_get_slot, ":agent_courage_score", ":cur_agent",  slot_agent_courage_score),
          (store_agent_hit_points, ":agent_hit_points", ":cur_agent"),
          (val_mul, ":agent_hit_points", 4),
          (try_begin),
            (agent_is_ally, ":cur_agent"),
            (val_sub, ":agent_hit_points", 100), #ally agents will be more tend to run away, to make game more funnier/harder
          (try_end),
          (val_mul, ":agent_hit_points", 10),
          
          (store_sub, ":start_running_away_courage_score_limit", 3500, ":agent_hit_points"),
          (lt, ":agent_courage_score", ":start_running_away_courage_score_limit"), #if (courage score < 3500 - (agent hit points * 40)) and (agent is not running away) then start running away, average hit points : 50, average running away limit = 1500
          
          (agent_get_troop_id, ":troop_id", ":cur_agent"), #for now do not let heroes to run away from battle
          (neg|troop_is_hero, ":troop_id"),
          
          (agent_clear_scripted_mode, ":cur_agent"),	#handle scripted mode troops - motomataru chief
          (agent_start_running_away, ":cur_agent"),
          (agent_set_slot, ":cur_agent",  slot_agent_is_running_away, 1),
          #phaiak begin
          (try_begin),
            (ge, "$vc_debug_mode", 2),
            (agent_is_ally, ":cur_agent"),
            (display_message, "@{!}TEST: A ally agent just started to flee!"),
          (else_try),
            (ge, "$vc_debug_mode", 2),
            (display_message, "@{!}TEST: A enemy agent just started to flee!"),
          (end_try),
          #phaiak end
        (try_end),
      (else_try),
        (neq, ":force_retreat", 1),
        (agent_get_slot, ":agent_courage_score", ":cur_agent",  slot_agent_courage_score),
        (store_agent_hit_points, ":agent_hit_points", ":cur_agent"),
        (val_mul, ":agent_hit_points", 4),
        (try_begin),
          (agent_is_ally, ":cur_agent"),
          (val_sub, ":agent_hit_points", 100), #ally agents will be more tend to run away, to make game more funnier/harder
        (try_end),
        (val_mul, ":agent_hit_points", 10),
        (store_sub, ":stop_running_away_courage_score_limit", 3700, ":agent_hit_points"),
        (ge, ":agent_courage_score", ":stop_running_away_courage_score_limit"), #if (courage score > 3700 - agent hit points) and (agent is running away) then stop running away, average hit points : 50, average running away limit = 1700
        (agent_stop_running_away, ":cur_agent"),
        (agent_set_slot, ":cur_agent",  slot_agent_is_running_away, 0),
      (try_end),
  ]), #ozan
  
  # script_battle_tactic_apply
  # Input: none
  # Output: none
  # ("battle_tactic_apply",
    # [
      # (call_script, "script_battle_tactic_apply_aux", "$ai_team_1", "$ai_team_1_battle_tactic"),
      # (assign, "$ai_team_1_battle_tactic", reg0),
      # (try_begin),
        # (ge, "$ai_team_2", 0),
        # (call_script, "script_battle_tactic_apply_aux", "$ai_team_2", "$ai_team_2_battle_tactic"),
        # (assign, "$ai_team_2_battle_tactic", reg0),
      # (try_end),
  # ]),
  
  # script_battle_tactic_apply_aux
  # Input: team_no, battle_tactic
  # Output: battle_tactic
  # ("battle_tactic_apply_aux",
    # [
      # (store_script_param, ":team_no", 1),
      # (store_script_param, ":battle_tactic", 2),
      # (store_mission_timer_a, ":mission_time"),
      # (try_begin),
        # (eq, ":battle_tactic", btactic_hold),
        # (copy_position, pos1, pos52),
        # (call_script, "script_get_closest3_distance_of_enemies_at_pos1", ":team_no", 1),
        # (assign, ":avg_dist", reg0),
        # (assign, ":min_dist", reg1),
        # (try_begin),
          # (this_or_next|lt, ":min_dist", 1000),
          # (lt, ":avg_dist", 4000),
          # (assign, ":battle_tactic", 0),
          # (team_give_order, ":team_no", grc_everyone, mordr_charge),
        # (try_end),
      # (else_try),
        # (eq, ":battle_tactic", btactic_follow_leader),
        # (team_get_leader, ":ai_leader", ":team_no"),
        # (try_begin),
          # (ge, ":ai_leader", 0),
          # (agent_is_alive, ":ai_leader"),
          # (agent_set_speed_limit, ":ai_leader", 9),
          # (call_script, "script_team_get_average_position_of_enemies", ":team_no"),
          # (copy_position, pos60, pos0),
          # (agent_get_position, pos61, ":ai_leader"),
          # (position_transform_position_to_local, pos62, pos61, pos60), #pos62 = vector to enemy w.r.t leader
          # (position_normalize_origin, ":distance_to_enemy", pos62),
          # (convert_from_fixed_point, ":distance_to_enemy"),
          # (assign, reg17, ":distance_to_enemy"),
          # (position_get_x, ":dir_x", pos62),
          # (position_get_y, ":dir_y", pos62),
          # (val_mul, ":dir_x", 23),
          # (val_mul, ":dir_y", 23), #move 23 meters
          # (position_set_x, pos62, ":dir_x"),
          # (position_set_y, pos62, ":dir_y"),
          
          # (position_transform_position_to_parent, pos63, pos61, pos62), #pos63 is 23m away from leader in the direction of the enemy.
          # (position_set_z_to_ground_level, pos63),
          
          # (team_give_order, ":team_no", grc_everyone, mordr_hold),
          # (team_set_order_position, ":team_no", grc_everyone, pos63),
          # (agent_get_position, pos1, ":ai_leader"),
          # (try_begin),
            # (lt, ":distance_to_enemy", 50),
            # (ge, ":mission_time", 30),
            # (assign, ":battle_tactic", 0),
            # (team_give_order, ":team_no", grc_everyone, mordr_charge),
            # (agent_set_speed_limit, ":ai_leader", 60),
          # (try_end),
        # (else_try),
          # (assign, ":battle_tactic", 0),
          # (team_give_order, ":team_no", grc_everyone, mordr_charge),
        # (try_end),
      # (try_end),
      
      # (try_begin), # charge everyone after a while
        # (neq, ":battle_tactic", 0),
        # (ge, ":mission_time", 300),
        # (assign, ":battle_tactic", 0),
        # (team_give_order, ":team_no", grc_everyone, mordr_charge),
        # (team_get_leader, ":ai_leader", ":team_no"),
        # (agent_set_speed_limit, ":ai_leader", 60),
      # (try_end),
      # (assign, reg0, ":battle_tactic"),
  # ]),
  
  
  ##  # script_siege_defender_tactic_apply
  ##  # Input: none
  ##  # Output: none
  ##  ("siege_defender_tactic_apply",
  ##    [
  ##      (try_begin),
  ##        (eq, "$defender_team", 1),
  ##        (ge, "$belfry_positioned", 2),
  ##
  ##        (assign, ":enemy_too_weak", 0),
  ##        (try_begin),
  ##          (ge, "$attacker_reinforcement_stage", 2),
  ##          (call_script, "script_calculate_team_strength", "$defender_team"),
  ##          (assign, ":defender_strength", reg0),
  ##          (call_script, "script_calculate_team_strength", "$attacker_team"),
  ##          (assign, ":attacker_strength", reg0),
  ##          (store_mul, ":attacker_strength_multiplied", ":attacker_strength", 2),
  ##          (ge, ":defender_strength", ":attacker_strength_multiplied"),
  ##          (assign, ":enemy_too_weak", 1),
  ##        (try_end),
  ##
  ##        (try_begin),
  ##          (eq, ":enemy_too_weak", 1),
  ##          (neq, "$ai_battle_tactic", btactic_charge),
  ##          (assign, "$ai_battle_tactic", btactic_charge),
  ##          (team_give_order, "$defender_team", grc_infantry, mordr_charge),
  ##        (else_try),
  ##          (neq, "$ai_battle_tactic", btactic_charge),
  ##          (neq, "$ai_battle_tactic", btactic_hold),
  ##          (assign, "$ai_battle_tactic", btactic_hold),
  ##          (team_give_order, "$defender_team", grc_infantry, mordr_hold),
  ##          (team_give_order, "$defender_team", grc_heroes, mordr_hold),
  ##          (entry_point_get_position,pos1,10),
  ##          (team_set_order_position, "$defender_team", grc_infantry, pos1),
  ##          (team_set_order_position, "$defender_team", grc_heroes, pos1),
  ##        (try_end),
  ##      (try_end),
  ##  ]),
  
  
  # script_team_get_class_percentages
  # Input: arg1: team_no, arg2: try for team's enemies
  # Output: reg0: percentage infantry, reg1: percentage archers, reg2: percentage cavalry
  ("team_get_class_percentages",
    [
      (assign, ":num_infantry", 0),
      (assign, ":num_archers", 0),
      (assign, ":num_cavalry", 0),
      (assign, ":num_total", 0),
      (store_script_param, ":team_no", 1),
      (store_script_param, ":negate", 2),
      (try_for_agents,":cur_agent"),
        (agent_is_alive, ":cur_agent"),
        (agent_is_human, ":cur_agent"),
        (agent_get_team, ":agent_team", ":cur_agent"),
        (assign, ":continue", 0),
        (try_begin),
          (eq, ":negate", 1),
          (teams_are_enemies, ":agent_team", ":team_no"),
          (assign, ":continue", 1),
        (else_try),
          (eq, ":agent_team", ":team_no"),
          (assign, ":continue", 1),
        (try_end),
        (eq, ":continue", 1),
        (val_add, ":num_total", 1),
        (agent_get_class, ":agent_class", ":cur_agent"),
        (try_begin),
          (eq, ":agent_class", grc_infantry),
          (val_add,  ":num_infantry", 1),
        (else_try),
          (eq, ":agent_class", grc_archers),
          (val_add,  ":num_archers", 1),
        (else_try),
          (eq, ":agent_class", grc_cavalry),
          (val_add,  ":num_cavalry", 1),
        (try_end),
      (try_end),
      (try_begin),
        (eq,  ":num_total", 0),
        (assign,  ":num_total", 1),
      (try_end),
      (store_mul, ":perc_infantry",":num_infantry",100),
      (val_div, ":perc_infantry",":num_total"),
      (store_mul, ":perc_archers",":num_archers",100),
      (val_div, ":perc_archers",":num_total"),
      (store_mul, ":perc_cavalry",":num_cavalry",100),
      (val_div, ":perc_cavalry",":num_total"),
      (assign, reg0, ":perc_infantry"),
      (assign, reg1, ":perc_archers"),
      (assign, reg2, ":perc_cavalry"),
  ]),
  
  # script_get_closest3_distance_of_enemies_at_pos1
  # Input: arg1: team_no, pos1
  # Output: reg0: distance in cms.
  ("get_closest3_distance_of_enemies_at_pos1",
    [
      (assign, ":min_distance_1", 100000),
      (assign, ":min_distance_2", 100000),
      (assign, ":min_distance_3", 100000),
      
      (store_script_param, ":team_no", 1),
      (try_for_agents,":cur_agent"),
        (agent_is_alive, ":cur_agent"),
        (agent_is_human, ":cur_agent"),
        (agent_get_team, ":agent_team", ":cur_agent"),
        (teams_are_enemies, ":agent_team", ":team_no"),
        
        (agent_get_position, pos2, ":cur_agent"),
        (get_distance_between_positions,":cur_dist",pos2,pos1),
        (try_begin),
          (lt, ":cur_dist", ":min_distance_1"),
          (assign, ":min_distance_3", ":min_distance_2"),
          (assign, ":min_distance_2", ":min_distance_1"),
          (assign, ":min_distance_1", ":cur_dist"),
        (else_try),
          (lt, ":cur_dist", ":min_distance_2"),
          (assign, ":min_distance_3", ":min_distance_2"),
          (assign, ":min_distance_2", ":cur_dist"),
        (else_try),
          (lt, ":cur_dist", ":min_distance_3"),
          (assign, ":min_distance_3", ":cur_dist"),
        (try_end),
      (try_end),
      
      (assign, ":total_distance", 0),
      (assign, ":total_count", 0),
      (try_begin),
        (lt, ":min_distance_1", 100000),
        (val_add, ":total_distance", ":min_distance_1"),
        (val_add, ":total_count", 1),
      (try_end),
      (try_begin),
        (lt, ":min_distance_2", 100000),
        (val_add, ":total_distance", ":min_distance_2"),
        (val_add, ":total_count", 1),
      (try_end),
      (try_begin),
        (lt, ":min_distance_3", 100000),
        (val_add, ":total_distance", ":min_distance_3"),
        (val_add, ":total_count", 1),
      (try_end),
      (assign, ":average_distance", 100000),
      (try_begin),
        (gt, ":total_count", 0),
        (store_div, ":average_distance", ":total_distance", ":total_count"),
      (try_end),
      (assign, reg0, ":average_distance"),
      (assign, reg1, ":min_distance_1"),
      (assign, reg2, ":min_distance_2"),
      (assign, reg3, ":min_distance_3"),
  ]),
  
  # script_team_get_average_position_of_enemies
  # Input: arg1: team_no,
  # Output: pos0: average position.
  ("team_get_average_position_of_enemies",
    [
      (store_script_param_1, ":team_no"),
      (init_position, pos0),
      (assign, ":num_enemies", 0),
      (assign, ":accum_x", 0),
      (assign, ":accum_y", 0),
      (assign, ":accum_z", 0),
      (try_for_agents,":enemy_agent"),
        (agent_is_alive, ":enemy_agent"),
        (agent_is_human, ":enemy_agent"),
        (agent_get_team, ":enemy_team", ":enemy_agent"),
        (teams_are_enemies, ":team_no", ":enemy_team"),
        
        (agent_get_position, pos62, ":enemy_agent"),
        
        (position_get_x, ":x", pos62),
        (position_get_y, ":y", pos62),
        (position_get_z, ":z", pos62),
        
        (val_add, ":accum_x", ":x"),
        (val_add, ":accum_y", ":y"),
        (val_add, ":accum_z", ":z"),
        (val_add, ":num_enemies", 1),
      (try_end),
      
      (try_begin), #to avoid division by zeros at below division part.
        (le, ":num_enemies", 0),
        (assign, ":num_enemies", 1),
      (try_end),
      
      (store_div, ":average_x", ":accum_x", ":num_enemies"),
      (store_div, ":average_y", ":accum_y", ":num_enemies"),
      (store_div, ":average_z", ":accum_z", ":num_enemies"),
      
      (position_set_x, pos0, ":average_x"),
      (position_set_y, pos0, ":average_y"),
      (position_set_z, pos0, ":average_z"),
      
      (assign, reg0, ":num_enemies"),
  ]),
  
  
  # script_search_troop_prisoner_of_party
  # Input: arg1 = troop_no
  # Output: reg0 = party_no (-1 if troop is not a prisoner.)
  ("search_troop_prisoner_of_party",
    [
      (store_script_param_1, ":troop_no"),
      (assign, ":prisoner_of", -1),
      (try_for_parties, ":party_no"),
        (eq,  ":prisoner_of", -1),
        (this_or_next|eq, ":party_no", "p_main_party"),
        (ge, ":party_no", centers_begin),
        (party_count_prisoners_of_type, ":troop_found", ":party_no", ":troop_no"),
        (gt, ":troop_found", 0),
        (assign, ":prisoner_of", ":party_no"),
      (try_end),
      (assign, reg0, ":prisoner_of"),
  ]),
  
  
  ##  # script_clear_last_quest
  ##  # Input: arg1 = troop_no
  ##  # Output: none
  ##  ("clear_last_quest",
  ##    [
  ##      (store_script_param_1, ":troop_no"),
  ##
  ##      (troop_set_slot, ":troop_no",slot_troop_last_quest, 0),
  ##      (troop_set_slot, ":troop_no",slot_troop_last_quest_betrayed, 0)
  ##  ]),
  
  
  
  # script_change_debt_to_troop
  # Input: arg1 = troop_no, arg2 = new debt amount
  # Output: none
  ("change_debt_to_troop",
    [
      (store_script_param_1, ":troop_no"),
      (store_script_param_2, ":new_debt"),
      
      (troop_get_slot, ":cur_debt", ":troop_no", slot_troop_player_debt),
      (assign, reg1, ":cur_debt"),
      (val_add, ":cur_debt", ":new_debt"),
      (assign, reg2, ":cur_debt"),
      (troop_set_slot, ":troop_no", slot_troop_player_debt, ":cur_debt"),
      (str_store_troop_name_link, s1, ":troop_no"),
      (display_message, "@You now owe {reg2} peningas to {s1}."),
  ]),
  
  
  
  
  # script_abort_quest
  # Input: arg1 = quest_no, arg2 = apply relation penalty
  # Output: none
  ("abort_quest",
    [
      (store_script_param_1, ":quest_no"),
      (store_script_param_2, ":abort_type"), #0=aborted by event, 1=abort by talking 2=abort by expire
      
      (assign, ":quest_return_penalty", -1),
      (assign, ":quest_expire_penalty", -2),
      #Campaign quest no possible to abort
      (neg|is_between, ":quest_no","qst_collect_men", "qst_save_relative_of_merchant"),
      (neg|is_between, ":quest_no", "qst_kennemer_jarl", "qst_blank_quest_2"),
      ########
      
      #      (quest_get_slot, ":quest_object_troop", ":quest_no", slot_quest_object_troop),
      (try_begin),
        (this_or_next|eq, ":quest_no", "qst_deliver_message"),
        (eq, ":quest_no", "qst_deliver_message_to_enemy_lord"),
        (assign, ":quest_return_penalty", -2),
        (assign, ":quest_expire_penalty", -3),
      (else_try),
        (eq, ":quest_no", "qst_kidnapped_girl"),
        (party_remove_members, "p_main_party", "trp_kidnapped_girl", 1),
        (quest_get_slot, ":quest_target_party", "qst_kidnapped_girl", slot_quest_target_party),
        (try_begin),
          (party_is_active, ":quest_target_party"),
          (remove_party, ":quest_target_party"),
        (try_end),
      (else_try),
        (eq, ":quest_no", "qst_escort_lady"),
        (quest_get_slot, ":quest_object_troop", "qst_escort_lady", slot_quest_object_troop),
        (party_remove_members, "p_main_party", ":quest_object_troop", 1),
        (assign, ":quest_return_penalty", -2),
        (assign, ":quest_expire_penalty", -3),
        ##      (else_try),
        ##        (eq, ":quest_no", "qst_rescue_lady_under_siege"),
        ##        (party_remove_members, "p_main_party", ":quest_object_troop", 1),
        ##      (else_try),
        ##        (eq, ":quest_no", "qst_deliver_message_to_lover"),
        ##      (else_try),
        ##        (eq, ":quest_no", "qst_bring_prisoners_to_enemy"),
        ##        (try_begin),
        ##          (check_quest_succeeded, ":quest_no"),
        ##          (quest_get_slot, ":quest_target_amount", ":quest_no", slot_quest_target_amount),
        ##          (quest_get_slot, ":quest_object_troop", ":quest_no", slot_quest_object_troop),
        ##          (quest_get_slot, ":quest_giver_troop", ":quest_no", slot_quest_giver_troop),
        ##          (call_script, "script_game_get_join_cost", ":quest_object_troop"),
        ##          (assign, ":reward", reg0),
        ##          (val_mul, ":reward", ":quest_target_amount"),
        ##          (val_div, ":reward", 2),
        ##        (else_try),
        ##          (quest_get_slot, ":reward", ":quest_no", slot_quest_target_amount),
        ##        (try_end),
        ##        (call_script, "script_change_debt_to_troop", ":quest_giver_troop", ":reward"),
        ##      (else_try),
        ##        (eq, ":quest_no", "qst_bring_reinforcements_to_siege"),
        ##        (quest_get_slot, ":quest_target_amount", ":quest_no", slot_quest_target_amount),
        ##        (quest_get_slot, ":quest_object_troop", ":quest_no", slot_quest_object_troop),
        ##        (quest_get_slot, ":quest_giver_troop", ":quest_no", slot_quest_giver_troop),
        ##        (call_script, "script_game_get_join_cost", ":quest_object_troop"),
        ##        (assign, ":reward", reg0),
        ##        (val_mul, ":reward", ":quest_target_amount"),
        ##        (val_mul, ":reward", 2),
        ##        (call_script, "script_change_debt_to_troop", ":quest_giver_troop", ":reward"),
        ##      (else_try),
        ##        (eq, ":quest_no", "qst_deliver_supply_to_center_under_siege"),
        ##        (quest_get_slot, ":quest_target_amount", ":quest_no", slot_quest_target_amount),
        ##        (quest_get_slot, ":quest_giver_troop", ":quest_no", slot_quest_giver_troop),
        ##        (store_item_value, ":reward", "itm_siege_supply"),
        ##        (val_mul, ":reward", ":quest_target_amount"),
        ##        (call_script, "script_change_debt_to_troop", ":quest_giver_troop", ":reward"),
      (else_try),
        (eq, ":quest_no", "qst_raise_troops"),
        (quest_get_slot, ":quest_giver_troop", ":quest_no", slot_quest_giver_troop),
        (call_script, "script_change_debt_to_troop", ":quest_giver_troop", 100),
        (assign, ":quest_return_penalty", -4),
        (assign, ":quest_expire_penalty", -5),
      (else_try),
        (eq, ":quest_no", "qst_deal_with_looters"),
        (try_for_parties, ":cur_party_no"),
          (party_get_template_id, ":cur_party_template", ":cur_party_no"),
          (eq, ":cur_party_template", "pt_looters"),
          (party_set_flags, ":cur_party_no", pf_quest_party, 0),
        (try_end),
        (assign, ":quest_return_penalty", -4),
        (assign, ":quest_expire_penalty", -5),
      (else_try),
        (eq, ":quest_no", "qst_deal_with_bandits_at_lords_village"),
        (quest_get_slot, ":quest_giver_troop", ":quest_no", slot_quest_giver_troop),
        (call_script, "script_change_debt_to_troop", ":quest_giver_troop", 200),
        (assign, ":quest_return_penalty", -5),
        (assign, ":quest_expire_penalty", -6),
      (else_try),
        (eq, ":quest_no", "qst_collect_taxes"),
        (quest_get_slot, ":gold_reward", ":quest_no", slot_quest_gold_reward),
        (quest_set_slot, ":quest_no", slot_quest_gold_reward, 0),
        (quest_get_slot, ":quest_giver_troop", ":quest_no", slot_quest_giver_troop),
        (call_script, "script_change_debt_to_troop", ":quest_giver_troop", ":gold_reward"),
        (assign, ":quest_return_penalty", -4),
        (assign, ":quest_expire_penalty", -6),
        ##      (else_try),
        ##        (eq, ":quest_no", "qst_capture_messenger"),
        ##      (else_try),
        ##        (eq, ":quest_no", "qst_bring_back_deserters"),
      (else_try),
        (eq, ":quest_no", "qst_hunt_down_fugitive"),
        (assign, ":quest_return_penalty", -3),
        (assign, ":quest_expire_penalty", -4),
      (else_try),
        (eq, ":quest_no", "qst_kill_local_merchant"),
      (else_try),
        (eq, ":quest_no", "qst_bring_back_runaway_serfs"),
        (assign, ":quest_return_penalty", -1),
        (assign, ":quest_expire_penalty", -1),
      (else_try),
        (eq, ":quest_no", "qst_collect_debt"),
        (try_begin),
          (quest_slot_eq, "qst_collect_debt", slot_quest_current_state, 1), #debt collected but not delivered
          (quest_get_slot, ":debt", "qst_collect_debt", slot_quest_target_amount),
          (quest_get_slot, ":quest_giver", "qst_collect_debt", slot_quest_giver_troop),
          (call_script, "script_change_debt_to_troop", ":quest_giver", ":debt"),
          (assign, ":quest_return_penalty", -3),
          (assign, ":quest_expire_penalty", -6),
        (else_try),
          (assign, ":quest_return_penalty", -3),
          (assign, ":quest_expire_penalty", -4),
        (try_end),
      (else_try),
        (eq, ":quest_no", "qst_deal_with_bandits_at_lords_village"),
        (assign, ":quest_return_penalty", -6),
        (assign, ":quest_expire_penalty", -6),
      (else_try),
        (eq, ":quest_no", "qst_cause_provocation"),
        (assign, ":quest_return_penalty", -10),
        (assign, ":quest_expire_penalty", -13),
      (else_try),
        (eq, ":quest_no", "qst_persuade_lords_to_make_peace"),
        (assign, ":quest_return_penalty", -10),
        (assign, ":quest_expire_penalty", -13),
      (else_try),
        (eq, ":quest_no", "qst_deal_with_night_bandits"),
        (assign, ":quest_return_penalty", -1),
        (assign, ":quest_expire_penalty", -1),
        
      (else_try),
        (eq, ":quest_no", "qst_follow_spy"),
        (assign, ":quest_return_penalty", -2),
        (assign, ":quest_expire_penalty", -3),
        (try_begin),
          (party_is_active, "$qst_follow_spy_spy_party"),
          (remove_party, "$qst_follow_spy_spy_party"),
        (try_end),
        (try_begin),
          (party_is_active, "$qst_follow_spy_spy_partners_party"),
          (remove_party, "$qst_follow_spy_spy_partners_party"),
        (try_end),
      (else_try),
        (eq, ":quest_no", "qst_capture_enemy_hero"),
        (assign, ":quest_return_penalty", -3),
        (assign, ":quest_expire_penalty", -4),
      (else_try),
        (eq, ":quest_no", "qst_lend_companion"),
        (quest_get_slot, ":quest_target_troop", "qst_lend_companion", slot_quest_target_troop),
        (troop_set_slot, ":quest_target_troop", slot_troop_current_mission, npc_mission_rejoin_when_possible),
        (troop_set_slot, ":quest_target_troop", slot_troop_days_on_mission, 0),
      (else_try),
        (eq, ":quest_no", "qst_lend_surgeon"),
        (quest_get_slot, ":quest_target_troop", "qst_lend_surgeon", slot_quest_target_troop),
        (troop_set_slot, ":quest_target_troop", slot_troop_current_mission, npc_mission_rejoin_when_possible),
        (troop_set_slot, ":quest_target_troop", slot_troop_days_on_mission, 0),
        ##        (party_add_members, "p_main_party", ":quest_target_troop", 1),
        ##      (else_try),
        ##        (eq, ":quest_no", "qst_capture_conspirators"),
        ##      (else_try),
        ##        (eq, ":quest_no", "qst_defend_nobles_against_peasants"),
      (else_try),
        (eq, ":quest_no", "qst_incriminate_loyal_commander"),
        (assign, ":quest_return_penalty", -5),
        (assign, ":quest_expire_penalty", -6),
        ##      (else_try),
        ##        (eq, ":quest_no", "qst_hunt_down_raiders"),
        ##      (else_try),
        ##        (eq, ":quest_no", "qst_capture_prisoners"),
        ##        #Enemy lord quests
      (else_try),
        (eq, ":quest_no", "qst_lend_surgeon"),
        
        #Kingdom lady quests
      (else_try),
        (eq, ":quest_no", "qst_rescue_lord_by_replace"),
        (assign, ":quest_return_penalty", -1),
        (assign, ":quest_expire_penalty", -1),
      (else_try),
        (eq, ":quest_no", "qst_deliver_message_to_prisoner_lord"),
        (assign, ":quest_return_penalty", 0),
        (assign, ":quest_expire_penalty", -1),
      (else_try),
        (eq, ":quest_no", "qst_duel_for_lady"),
        (assign, ":quest_return_penalty", -1),
        (assign, ":quest_expire_penalty", -1),
        
        #Kingdom Army quests
      (else_try),
        (eq, ":quest_no", "qst_follow_army"),
        (assign, ":quest_return_penalty", 0), #was -4
        (assign, ":quest_expire_penalty", 0), #was -5
      (else_try),
        (eq, ":quest_no", "qst_deliver_cattle_to_army"),
        (assign, ":quest_return_penalty", 0),
        (assign, ":quest_expire_penalty", 0),
      (else_try),
        (eq, ":quest_no", "qst_join_siege_with_army"),
        (assign, ":quest_return_penalty", -1),
        (assign, ":quest_expire_penalty", -2),
      (else_try),
        (eq, ":quest_no", "qst_scout_waypoints"),
        (assign, ":quest_return_penalty", 0),
        (assign, ":quest_expire_penalty", 0),
        
        #Village Elder quests
      (else_try),
        (eq, ":quest_no", "qst_deliver_grain"),
        (assign, ":quest_return_penalty", -10),
        (assign, ":quest_expire_penalty", -15),
      (else_try),
        (eq, ":quest_no", "qst_deliver_cattle"),
        (assign, ":quest_return_penalty", -10),
        (assign, ":quest_expire_penalty", -15),
      (else_try),
        (eq, ":quest_no", "qst_train_peasants_against_bandits"),
        (assign, ":quest_return_penalty", -20),
        (assign, ":quest_expire_penalty", -20),
        
        #Mayor quests
      (else_try),
        (eq, ":quest_no", "qst_deliver_wine"),
        (assign, ":quest_return_penalty", -1),
        (assign, ":quest_expire_penalty", -3),
        (val_add, "$debt_to_merchants_guild", "$qst_deliver_wine_debt"),
      (else_try),
        (eq, ":quest_no", "qst_move_cattle_herd"),
        (assign, ":quest_return_penalty", -10),
        (assign, ":quest_expire_penalty", -15),
      (else_try),
        (eq, ":quest_no", "qst_escort_merchant_caravan"),
        (assign, ":quest_return_penalty", -5),
        (assign, ":quest_expire_penalty", -10),
      (else_try),
        (eq, ":quest_no", "qst_troublesome_bandits"),
        (assign, ":quest_return_penalty", -5),
        (assign, ":quest_expire_penalty", -10),
        #Other quests
      (else_try),
        (eq, ":quest_no", "qst_join_faction"),
        (assign, ":quest_return_penalty", -3),
        (assign, ":quest_expire_penalty", -3),
        (try_begin),
          (call_script, "script_get_number_of_hero_centers", "trp_player"),
          (gt, reg0, 0),
          (call_script, "script_change_player_relation_with_faction", "$g_invite_faction", -10),
        (try_end),
        
        
        (try_begin), #if the vassalage is part of a surrender option, then the faction returns to a state of war
          (quest_slot_eq, "qst_join_faction", slot_quest_failure_consequence, 1),
          (call_script, "script_diplomacy_start_war_between_kingdoms", "fac_player_supporters_faction", "$g_invite_faction", logent_player_faction_declares_war),	#MOTO chief pass log entries
          (call_script, "script_change_player_honor", -5),
          (quest_set_slot, "qst_join_faction", slot_quest_failure_consequence, 0),
        (try_end),
        
        
        (assign, "$g_invite_faction", 0),
        (assign, "$g_invite_faction_lord", 0),
        (assign, "$g_invite_offered_center", 0),
      (else_try),
        (eq, ":quest_no", "qst_eliminate_bandits_infesting_village"),
        (assign, ":quest_return_penalty", -10),
        (assign, ":quest_expire_penalty", -15),
      # (else_try),
        # (ge, ":quest_no", "qst_resolve_dispute"),
        # (assign, ":authority_loss", -2),
        # (assign, ":quest_return_penalty", 0),
        # (assign, ":quest_expire_penalty", 0),
      (else_try),
        # (ge, ":quest_no", "qst_consult_with_minister"),
        (is_between, ":quest_no", player_realm_quests_begin, player_realm_quests_end), #political quests
        (assign, ":authority_loss", -2),
        # (assign, ":quest_return_penalty", 0), authority loss just affects a player's vassals, so allow this to go through
        # (assign, ":quest_expire_penalty", 0),
      (else_try),
        (eq, ":quest_no", "qst_blank_quest_2"),
        (quest_get_slot, ":quest_object_troop", "qst_blank_quest_2", slot_quest_object_troop),
        (party_remove_members, "p_main_party", ":quest_object_troop", 1),
        (assign, ":quest_return_penalty", -2),
        (assign, ":quest_expire_penalty", -3),
      (else_try),
        (eq, ":quest_no", "qst_blank_quest_3"),
        (assign, ":quest_return_penalty", -3),
        (assign, ":quest_expire_penalty", -4),
      (else_try),
        (eq, ":quest_no", "qst_blank_quest_4"),
        (try_for_parties, ":cur_party_no"),
          (quest_get_slot, ":target_party", "qst_blank_quest_4", slot_quest_target_party),
          (eq, ":cur_party_no", ":target_party"),
          (party_set_flags, ":cur_party_no", pf_quest_party, 0),
        (try_end),
        (assign, ":quest_return_penalty", -4),
        (assign, ":quest_expire_penalty", -5),
      (else_try),
        (eq, ":quest_no", "qst_blank_quest_6"),
        (assign, ":quest_return_penalty", -5),
        (assign, ":quest_expire_penalty", -15),
      (else_try),
        (eq, ":quest_no", "qst_blank_quest_9"),
        (assign, ":quest_return_penalty", -3),
        (assign, ":quest_expire_penalty", -4),
      (else_try),
        (eq, ":quest_no", "qst_blank_quest_10"),
        (assign, ":quest_return_penalty", -10),
        (assign, ":quest_expire_penalty", -15),
      (else_try),
        (eq, ":quest_no", "qst_blank_quest_11"),
        (assign, ":quest_return_penalty", -3),
        (assign, ":quest_expire_penalty", -3),
      (else_try),
        (eq, ":quest_no", "qst_blank_quest_12"),
        (assign, ":quest_return_penalty", -4),
        (assign, ":quest_expire_penalty", -5),
      (else_try),
        (eq, ":quest_no", "qst_blank_quest_13"),
        (assign, ":quest_return_penalty", -6),
        (assign, ":quest_expire_penalty", -8),
      (else_try),
        (eq, ":quest_no", "qst_blank_quest_16"),
        (assign, ":quest_return_penalty", -3),
        (assign, ":quest_expire_penalty", -3),
      (else_try),
        (eq, ":quest_no", "qst_blank_quest_17"),
        (assign, ":quest_return_penalty", -3),
        (assign, ":quest_expire_penalty", -3),
      (else_try),
        (eq, ":quest_no", "qst_blank_quest_18"),
        (assign, ":quest_return_penalty", -3),
        (assign, ":quest_expire_penalty", -2),
      (else_try),
        (eq, ":quest_no", "qst_blank_quest_7"),
        (quest_get_slot,":party","qst_blank_quest_7",slot_quest_target_party),
        (assign, ":quest_return_penalty", -3),
        (assign, ":quest_expire_penalty", -4),
        (remove_party, ":party"),
      (try_end),
      
      (try_begin),
        (gt, ":abort_type", 0),
        (neq, ":quest_expire_penalty", 0),  #":quest_return_penalty" is always 0 when ":quest_expire_penalty" is 0
        # (this_or_next|lt, ":quest_no", "qst_resolve_dispute"),
        # (this_or_next|eq, ":quest_no", "qst_cause_provocation"),
        # (is_between, ":quest_no", "qst_blank_quest_2","qst_collect_men"),
        
        (quest_get_slot, ":quest_giver", ":quest_no", slot_quest_giver_troop),
        (assign, ":relation_penalty", ":quest_return_penalty"),
        (try_begin),
          (eq, ":abort_type", 2),
          (assign, ":relation_penalty", ":quest_expire_penalty"),
        (try_end),
        (try_begin),
          (this_or_next|is_between, ":quest_giver", village_elders_begin, village_elders_end),
          (this_or_next|is_between, ":quest_giver", mayors_begin, mayors_end),
          (is_between, ":quest_giver", "trp_castle_1_seneschal","trp_town_1_arena_master"),
          (quest_get_slot, ":quest_giver_center", ":quest_no", slot_quest_giver_center),
          (call_script, "script_change_player_relation_with_center", ":quest_giver_center", ":relation_penalty"),
        (else_try),
          (eq, ":quest_no", "qst_blank_quest_12"),
          (call_script, "script_change_player_relation_with_faction", "fac_christians", ":relation_penalty"),
        (else_try),
          (eq, ":quest_no", "qst_blank_quest_9"),
          (call_script, "script_change_player_relation_with_faction", "fac_pagans", ":relation_penalty"),
        (else_try),
          (call_script, "script_change_player_relation_with_troop", ":quest_giver", ":relation_penalty"),
        (try_end),
      (try_end),
      
      (fail_quest, ":quest_no"),
      
      #NPC companion changes begin
      (try_begin),
        (gt, ":abort_type", 0),
        (neq, ":quest_no", "qst_consult_with_minister"),
        (neq, ":quest_no", "qst_resolve_dispute"),
        (neq, ":quest_no", "qst_visit_lady"),
        (neq, ":quest_no", "qst_formal_marriage_proposal"),
        (neq, ":quest_no", "qst_duel_courtship_rival"),
        (neq, ":quest_no", "qst_follow_army"),
        (neq, ":quest_no", "qst_denounce_lord"),
        (neq, ":quest_no", "qst_intrigue_against_lord"),
        (neq, ":quest_no", "qst_offer_gift"),
        (neq, ":quest_no", "qst_organize_feast"),
        (neq, ":quest_no", "qst_blank_quest_3"),
        (neq, ":quest_no", "qst_blank_quest_14"),
        (neq, ":quest_no", "qst_blank_quest_15"),
        
        (call_script, "script_objectionable_action", tmt_honest, "str_fail_quest"),
      (try_end),
      #NPC companion changes end
      
      (try_begin),
        (is_between, ":quest_no", player_realm_quests_begin, player_realm_quests_end), #see where ":authority_loss" is defined above
        (call_script, "script_change_player_right_to_rule", ":authority_loss"),
        
        (faction_slot_eq, "$players_kingdom", slot_faction_leader, "trp_player"),
        (try_for_range, ":lord", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":lord_faction", ":lord"),
          (eq, ":lord_faction", "fac_player_supporters_faction"),
          (call_script, "script_troop_change_relation_with_troop", ":lord", "trp_player", ":authority_loss"),
        (try_end),
      (try_end),
      
      
      (try_begin),
        (eq, ":quest_no", "qst_organize_feast"),
        (call_script, "script_add_notification_menu", "mnu_notification_feast_quest_expired", 0, 0),
      (try_end),
      
      
      (call_script, "script_end_quest", ":quest_no"),
  ]),
  
  
  ##  # script_event_center_captured
  ##  # Input: arg1 = center_no, arg2 = old_faction_no
  ##  # Output: none
  ##  ("event_center_captured",
  ##    [
  ##      #      (store_script_param_1, ":center_no"),
  ##      #       (store_script_param_2, ":old_faction_no"),
  ##      #       (store_faction_of_party, ":faction_no"),
  ##
  ##      (try_begin),
  ##        (check_quest_active, "qst_deliver_message"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_escort_lady"),
  ##        (quest_slot_eq, "qst_escort_lady", slot_quest_target_center, ":center_no"),
  ##        (call_script, "script_abort_quest", "qst_escort_lady"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_rescue_lady_under_siege"),
  ##        (quest_slot_eq, "qst_rescue_lady_under_siege", slot_quest_target_center, ":center_no"),
  ##        (quest_slot_eq, "qst_rescue_lady_under_siege", slot_quest_current_state, 0),
  ##        (call_script, "script_abort_quest", "qst_rescue_lady_under_siege", 1),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_deliver_message_to_lover"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_deliver_message_to_enemy_lord"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_bring_prisoners_to_enemy"),
  ##        (quest_slot_eq, "qst_bring_prisoners_to_enemy", slot_quest_target_center, ":center_no"),
  ##        (neg|check_quest_succeeded, "qst_bring_prisoners_to_enemy"),
  ##        (call_script, "script_abort_quest", "qst_bring_prisoners_to_enemy"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_bring_reinforcements_to_siege"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_deliver_supply_to_center_under_siege"),
  ##        (quest_slot_eq, "qst_deliver_supply_to_center_under_siege", slot_quest_target_center, ":center_no"),
  ##        (call_script, "script_abort_quest", "qst_deliver_supply_to_center_under_siege", 1),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_raise_troops"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_capture_messenger"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_bring_back_deserters"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_kill_local_merchant"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_bring_back_runaway_serfs"),
  ##        (quest_slot_eq, "qst_bring_back_runaway_serfs", slot_quest_object_center, ":center_no"),
  ##        (neg|check_quest_succeeded, "qst_bring_back_runaway_serfs"),
  ##        (neg|check_quest_failed, "qst_bring_back_runaway_serfs"),
  ##        (call_script, "script_abort_quest", "qst_bring_back_runaway_serfs"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_follow_spy"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_capture_enemy_hero"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_lend_companion"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_capture_conspirators"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_defend_nobles_against_peasants"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_incriminate_loyal_commander"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_hunt_down_raiders"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_capture_prisoners"),
  ##      (try_end),
  ##      #Enemy lord quests
  ##      (try_begin),
  ##        (check_quest_active, "qst_lend_surgeon"),
  ##      (try_end),
  ##      #Kingdom lady quests
  ##      (try_begin),
  ##        (check_quest_active, "qst_rescue_lord_by_replace"),
  ##        (quest_get_slot, ":quest_target_troop", "qst_rescue_lord_by_replace", slot_quest_target_troop),
  ##        (troop_slot_eq, ":quest_target_troop", slot_troop_is_prisoner, 0),
  ##        (neg|check_quest_succeeded, "qst_rescue_lord_by_replace"),
  ##        (call_script, "script_abort_quest", "qst_rescue_lord_by_replace"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_deliver_message_to_prisoner_lord"),
  ##      (try_end),
  ##      (try_begin),
  ##        (check_quest_active, "qst_duel_for_lady"),
  ##      (try_end),
  ##  ]),
  
  # script_cf_is_quest_troop
  # Input: arg1 = troop_no
  # Output: none (can fail)
  ("cf_is_quest_troop",
    [
      (store_script_param_1, ":troop_no"),
      (assign, ":is_quest_troop", 0),
      (try_for_range, ":cur_quest", all_quests_begin, all_quests_end),
        (check_quest_active, ":cur_quest"),
        (quest_get_slot, ":quest_troop_1", ":cur_quest", slot_quest_target_troop),
        (quest_get_slot, ":quest_troop_2", ":cur_quest", slot_quest_object_troop),
        (quest_get_slot, ":quest_troop_3", ":cur_quest", slot_quest_giver_troop),
        (this_or_next|eq, ":quest_troop_1", ":troop_no"),
        (this_or_next|eq, ":quest_troop_2", ":troop_no"),
        (eq, ":quest_troop_3", ":troop_no"),
        (assign, ":is_quest_troop", 1),
      (try_end),
      (eq, ":is_quest_troop", 1),
  ]),
  
  
  ##  # script_calculate_team_strength
  ##  # Input: arg1 = team_no
  ##  # Output: strength
  ##  ("calculate_team_strength",
  ##    [
  ##      (store_script_param_1, ":team_no"),
  ##      (assign, ":total_strength", 0),
  ##      (try_for_agents, ":cur_agent"),
  ##        (agent_get_team, ":agent_team", ":cur_agent"),
  ##        (eq, ":team_no", ":agent_team"),
  ##        (agent_is_human, ":cur_agent"),
  ##        (agent_is_alive, ":cur_agent"),
  ##
  ##        (agent_get_troop_id, ":cur_troop", ":cur_agent"),
  ##        (store_character_level, ":cur_level", ":cur_troop"),
  ##        (val_add, ":cur_level", 5),
  ##        (try_begin),
  ##          (troop_is_hero, ":cur_troop"),
  ##          (val_add, ":cur_level", 5),
  ##        (try_end),
  ##        (val_add, ":total_strength", ":cur_level"),
  ##      (try_end),
  ##      (assign, reg0, ":total_strength"),
  ##  ]),
  
  # script_check_friendly_kills
  # Input: none
  # Output: none (changes the morale of the player's party)
  ("check_friendly_kills",
    [(get_player_agent_own_troop_kill_count, ":count"),
      (try_begin),
        (neq, "$g_player_current_own_troop_kills", ":count"),
        (val_sub, ":count", "$g_player_current_own_troop_kills"),
        (val_add, "$g_player_current_own_troop_kills", ":count"),
        (val_mul, ":count", -1),
        (call_script, "script_change_player_party_morale", ":count"),
      (try_end),
  ]),
  
  # script_simulate_retreat
  # Input: arg1 = players_side_damage, arg2 = enemy_side_damage, arg3 = continue_battle s5 = title_string
  # Output: none
  ("simulate_retreat",
    [
      (call_script, "script_music_set_situation_with_culture", mtf_sit_killed),
      (set_show_messages, 0),
      (store_script_param, ":players_side_damage", 1),
      (store_script_param, ":enemy_side_damage", 2),
      (store_script_param, ":continue_battle", 3),
      
      (assign, ":players_side_strength", 0),
      (assign, ":enemy_side_strength", 0),
      
      (assign, ":do_calculate", 1),
      (try_begin),
        (try_for_agents, ":cur_agent"),
          (agent_is_human, ":cur_agent"),
          (agent_is_alive, ":cur_agent"),
          (agent_set_slot, ":cur_agent", slot_agent_is_alive_before_retreat, 1),#needed for simulation
          
          (agent_get_troop_id, ":cur_troop", ":cur_agent"),
          (store_character_level, ":cur_level", ":cur_troop"),
          (val_add, ":cur_level", 5),
          (try_begin),
            (troop_is_hero, ":cur_troop"),
            (val_add, ":cur_level", 5),
          (try_end),
          (try_begin),
            (agent_is_ally, ":cur_agent"),
            (val_add, ":players_side_strength", ":cur_level"),
          (else_try),
            (val_add, ":enemy_side_strength", ":cur_level"),
          (try_end),
        (try_end),
        (eq, "$pin_player_fallen", 0),
        (lt, ":enemy_side_strength", ":players_side_strength"),
        (eq, ":continue_battle", 1),
        (assign, ":do_calculate", 0),
      (try_end),
      
      (try_begin),
        (eq, ":do_calculate", 1),
        
        (assign, "$g_last_mission_player_damage", 0),
        (party_clear, "p_temp_party"),
        (party_clear, "p_temp_party_2"),
        (call_script, "script_simulate_battle_with_agents_aux", 0, ":players_side_damage"),
        (call_script, "script_simulate_battle_with_agents_aux", 1, ":enemy_side_damage"),
        
        (assign, ":display_casualties", 0),
        
        (try_begin),
          (gt, "$g_last_mission_player_damage", 0),
          (assign, ":display_casualties", 1),
          (assign, reg1, "$g_last_mission_player_damage"),
          (str_store_string, s12, "str_casualty_display_hp"),
        (else_try),
          (str_clear, s12),
        (try_end),
        
        (call_script, "script_print_casualties_to_s0", "p_temp_party", 1),
        (try_begin),
          (party_get_num_companion_stacks, ":num_stacks", "p_temp_party"),
          (gt, ":num_stacks", 0),
          (assign, ":display_casualties", 1),
        (try_end),
        (str_store_string_reg, s10, s0),
        
        (call_script, "script_print_casualties_to_s0", "p_temp_party_2", 1),
        (try_begin),
          (party_get_num_companion_stacks, ":num_stacks", "p_temp_party_2"),
          (gt, ":num_stacks", 0),
          (assign, ":display_casualties", 1),
        (try_end),
        (str_store_string_reg, s11, s0),
        (try_begin),
          (eq, ":display_casualties", 1),
          (dialog_box,"str_casualty_display", s5),
        (try_end),
      (try_end),
      (set_show_messages, 1),
      
      #Calculating morale penalty (can be between 0-30)
      (assign, ":ally_casualties", 0),
      (assign, ":enemy_casualties", 0),
      (assign, ":total_allies", 0),
      
      (try_for_agents, ":cur_agent"),
        (agent_is_human, ":cur_agent"),
        (try_begin),
          (agent_is_ally, ":cur_agent"),
          (val_add, ":total_allies", 1),
          (try_begin),
            (neg|agent_is_alive, ":cur_agent"),
            (val_add, ":ally_casualties", 1),
          (try_end),
        (else_try),
          (neg|agent_is_alive, ":cur_agent"),
          (val_add, ":enemy_casualties", 1),
        (try_end),
      (try_end),
      (store_add, ":total_casualties", ":ally_casualties", ":enemy_casualties"),
      (try_begin),
        (gt, ":total_casualties", 0),
        (store_mul, ":morale_adder", ":ally_casualties", 100),
        (val_div, ":morale_adder", ":total_casualties"),
        (val_mul, ":morale_adder", ":ally_casualties"),
        (val_div, ":morale_adder", ":total_allies"),
        (val_mul, ":morale_adder", -30),
        (val_div, ":morale_adder", 100),
        (call_script, "script_change_player_party_morale", ":morale_adder"),
      (try_end),
  ]),
  
  
  
  # script_simulate_battle_with_agents_aux
  # For internal use only
  # Input: arg1 = attacker_side (0 = ally, 1 = enemy), arg2 = damage amount
  # Output: none
  ("simulate_battle_with_agents_aux",
    [
      (store_script_param_1, ":attacker_side"),
      (store_script_param_2, ":damage"),
      
      (get_player_agent_no, ":player_agent"),
      (try_for_agents, ":cur_agent"),
        (neq, ":player_agent", ":cur_agent"),
        (agent_is_human, ":cur_agent"),
        #do not check agent_is_alive, check slot_agent_is_alive_before_retreat instead, so that dead agents can still hit enemies
        (agent_slot_eq, ":cur_agent", slot_agent_is_alive_before_retreat, 1),
        (try_begin),
          (agent_is_ally, ":cur_agent"),
          (assign, ":cur_agents_side", 0),
        (else_try),
          (assign, ":cur_agents_side", 1),
        (try_end),
        (eq, ":cur_agents_side", ":attacker_side"),
        (agent_get_position, pos2, ":cur_agent"),
        (assign, ":closest_agent", -1),
        (assign, ":min_distance", 100000),
        (try_for_agents, ":cur_agent_2"),
          (agent_is_human, ":cur_agent_2"),
          (agent_is_alive, ":cur_agent_2"),
          (try_begin),
            (agent_is_ally, ":cur_agent_2"),
            (assign, ":cur_agents_side_2", 0),
          (else_try),
            (assign, ":cur_agents_side_2", 1),
          (try_end),
          (this_or_next|neq, ":cur_agent_2", ":player_agent"),
          (eq, "$pin_player_fallen", 0),
          (neq, ":attacker_side", ":cur_agents_side_2"),
          (agent_get_position, pos3, ":cur_agent_2"),
          (get_distance_between_positions, ":cur_distance", pos2, pos3),
          (lt, ":cur_distance", ":min_distance"),
          (assign, ":min_distance", ":cur_distance"),
          (assign, ":closest_agent", ":cur_agent_2"),
        (try_end),
        (ge, ":closest_agent", 0),
        #Fight
        (agent_get_class, ":agent_class", ":cur_agent"),
        (assign, ":agents_speed", 1),
        (assign, ":agents_additional_hit", 0),
        (try_begin),
          (eq, ":agent_class", grc_archers),
          (assign, ":agents_additional_hit", 2),
        (else_try),
          (eq, ":agent_class", grc_cavalry),
          (assign, ":agents_speed", 2),
        (try_end),
        (agent_get_class, ":agent_class", ":closest_agent"),
        (assign, ":agents_speed_2", 1),
        (try_begin),
          (eq, ":agent_class", grc_cavalry),
          (assign, ":agents_speed_2", 2),
        (try_end),
        (assign, ":agents_hit", 18000),
        (val_add, ":min_distance", 3000),
        (val_div, ":agents_hit", ":min_distance"),
        (val_mul, ":agents_hit", 2),# max 10, min 2 hits within 150 meters
        
        (val_mul, ":agents_hit", ":agents_speed"),
        (val_div, ":agents_hit", ":agents_speed_2"),
        (val_add, ":agents_hit", ":agents_additional_hit"),
        
        (assign, ":cur_damage", ":damage"),
        (agent_get_troop_id, ":closest_troop", ":closest_agent"),
        (agent_get_troop_id, ":cur_troop", ":cur_agent"),
        (store_character_level, ":closest_level", ":closest_troop"),
        (store_character_level, ":cur_level", ":cur_troop"),
        (store_sub, ":level_dif", ":cur_level", ":closest_level"),
        (val_div, ":level_dif", 5),
        (val_add, ":cur_damage", ":level_dif"),
        
        (try_begin),
          (eq, ":closest_agent", ":player_agent"),
          (val_div, ":cur_damage", 2),
          (store_agent_hit_points, ":init_player_hit_points", ":player_agent", 1),
        (try_end),
        
        (try_for_range, ":unused", 0, ":agents_hit"),
          (store_random_in_range, ":random_damage", 0, 100),
          (lt, ":random_damage", ":cur_damage"),
          (agent_deliver_damage_to_agent, ":cur_agent", ":closest_agent"),
        (try_end),
        
        (try_begin),
          (eq, ":closest_agent", ":player_agent"),
          (store_agent_hit_points, ":final_player_hit_points", ":player_agent", 1),
          (store_sub, ":hit_points_difference", ":init_player_hit_points", ":final_player_hit_points"),
          (val_add, "$g_last_mission_player_damage", ":hit_points_difference"),
        (try_end),
        
        (neg|agent_is_alive, ":closest_agent"),
        (try_begin),
          (eq, ":attacker_side", 1),
          (party_add_members, "p_temp_party", ":closest_troop", 1),
          (try_begin),
            (agent_is_wounded, ":closest_agent"),
            (party_wound_members, "p_temp_party", ":closest_troop", 1),
          (try_end),
        (else_try),
          (party_add_members, "p_temp_party_2", ":closest_troop", 1),
          (try_begin),
            (agent_is_wounded, ":closest_agent"),
            (party_wound_members, "p_temp_party_2", ":closest_troop", 1),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  
  # script_map_get_random_position_around_position_within_range
  # Input: arg1 = minimum_distance in km, arg2 = maximum_distance in km, pos1 = origin position
  # Output: pos2 = result position
  ("map_get_random_position_around_position_within_range",
    [
      (store_script_param_1, ":min_distance"),
      (store_script_param_2, ":max_distance"),
      (val_mul, ":min_distance", 100),
      (assign, ":continue", 1),
      (try_for_range, ":unused", 0, 20),
        (eq, ":continue", 1),
        (map_get_random_position_around_position, pos2, pos1, ":max_distance"),
        (get_distance_between_positions, ":distance", pos2, pos1),
        (ge, ":distance", ":min_distance"),
        (assign, ":continue", 0),
      (try_end),
  ]),
  
  
  # script_get_number_of_unclaimed_centers_by_player
  # Input: none
  # Output: reg0 = number of unclaimed centers, reg1 = last unclaimed center_no
  # ("get_number_of_unclaimed_centers_by_player",
    # [
      # (assign, ":unclaimed_centers", 0),
      # (assign, reg1, -1),
      # (try_for_range, ":center_no", centers_begin, centers_end),
        # (store_faction_of_party, ":faction_no", ":center_no"),
        # (eq, ":faction_no", "fac_player_supporters_faction"),
        # (party_slot_eq, ":center_no", slot_town_claimed_by_player, 0),
        # (party_get_num_companion_stacks, ":num_stacks", ":center_no"),
        # (ge, ":num_stacks", 1), #castle is garrisoned
        # (assign, reg1, ":center_no"),
        # (val_add, ":unclaimed_centers", 1),
      # (try_end),
      # (assign, reg0, ":unclaimed_centers"),
  # ]),
  
  # script_troop_count_number_of_enemy_troops
  # Input: arg1 = troop_no
  # Output: reg0 = number_of_enemy_troops
  #  ("troop_count_number_of_enemy_troops",
  #    [
  #      (store_script_param_1, ":troop_no"),
  #      (assign, ":enemy_count", 0),
  #      (try_for_range, ":i_enemy_slot", slot_troop_enemies_begin, slot_troop_enemies_end),
  #        (troop_slot_ge, ":troop_no", ":i_enemy_slot", 1),
  #        (val_add, ":enemy_count", 1),
  #      (try_end),
  #      (assign, reg0, ":enemy_count"),
  #  ]),
  
  
  # script_cf_troop_check_troop_is_enemy
  # Input: arg1 = troop_no, arg2 = checked_troop_no
  # Output: none (Can fail)
  # ("cf_troop_check_troop_is_enemy",
    # [
      # (store_script_param_1, ":troop_no"),
      # (store_script_param_2, ":checked_troop_no"),
      # (call_script, "script_troop_get_relation_with_troop", ":troop_no", ":checked_troop_no"),
      # (lt, reg0, -10),
  # ]),
  
  
  # script_troop_get_leaded_center_with_index
  # Input: arg1 = troop_no, arg2 = center index within range between zero and the number of centers that troop owns
  # Output: reg0 = center_no
  ("troop_get_leaded_center_with_index",
    [
      (store_script_param_1, ":troop_no"),
      (store_script_param_2, ":random_center"),
      (assign, ":result", -1),
      (assign, ":center_count", 0),
      (try_for_range, ":center_no", centers_begin, centers_end),
        (eq, ":result", -1),
        (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
        (val_add, ":center_count", 1),
        (gt, ":center_count", ":random_center"),
        (assign, ":result", ":center_no"),
      (try_end),
      (assign, reg0, ":result"),
  ]),
  
  # script_cf_troop_get_random_leaded_walled_center_with_less_strength_priority
  # Input: arg1 = troop_no, arg2 = preferred_center_no
  # Output: reg0 = center_no (Can fail)
  # ("cf_troop_get_random_leaded_walled_center_with_less_strength_priority",
    # [
      # (store_script_param, ":troop_no", 1),
      # (store_script_param, ":preferred_center_no", 2),
      
      # (assign, ":num_centers", 0),
      # (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
        # (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
        # (party_slot_eq, ":center_no", slot_center_is_besieged_by, -1),
        # (val_add, ":num_centers", 1),
        # (try_begin),
          # (eq, ":center_no", ":preferred_center_no"),
          # (val_add, ":num_centers", 99),
        # (try_end),
        # ##        (call_script, "script_party_calculate_regular_strength", ":center_no"),
        # ##        (assign, ":strength", reg0),
        # ##        (lt, ":strength", 80),
        # ##        (store_sub, ":strength", 100, ":strength"),
        # ##        (val_div, ":strength", 20),
        # ##        (val_add, ":num_centers", ":strength"),
      # (try_end),
      # (gt, ":num_centers", 0),
      # (store_random_in_range, ":random_center", 0, ":num_centers"),
      # (assign, ":result", -1),
      # (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
        # (eq, ":result", -1),
        # (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
        # (party_slot_eq, ":center_no", slot_center_is_besieged_by, -1),
        # (val_sub, ":random_center", 1),
        # (try_begin),
          # (eq, ":center_no", ":preferred_center_no"),
          # (val_sub, ":random_center", 99),
        # (try_end),
        # ##        (try_begin),
        # ##          (call_script, "script_party_calculate_regular_strength", ":center_no"),
        # ##          (assign, ":strength", reg0),
        # ##          (lt, ":strength", 80),
        # ##          (store_sub, ":strength", 100, ":strength"),
        # ##          (val_div, ":strength", 20),
        # ##          (val_sub, ":random_center", ":strength"),
        # ##        (try_end),
        # (lt, ":random_center", 0),
        # (assign, ":result", ":center_no"),
      # (try_end),
      # (assign, reg0, ":result"),
  # ]),
  
  # script_cf_troop_get_random_leaded_town_or_village_except_center
  # Input: arg1 = troop_no, arg2 = except_center_no
  # Output: reg0 = center_no (Can fail)
  ("cf_troop_get_random_leaded_town_or_village_except_center",
    [
      (store_script_param_1, ":troop_no"),
      (store_script_param_2, ":except_center_no"),
      
      (assign, ":num_centers", 0),
      (try_for_range, ":center_no", centers_begin, centers_end),
        (neg|party_slot_eq, ":center_no", slot_party_type, spt_castle),
        (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
        (neq, ":center_no", ":except_center_no"),
        (val_add, ":num_centers", 1),
      (try_end),
      
      (gt, ":num_centers", 0),
      (store_random_in_range, ":random_center", 0, ":num_centers"),
      (assign, ":end_cond", centers_end),
      (try_for_range, ":center_no", centers_begin, ":end_cond"),
        (neg|party_slot_eq, ":center_no", slot_party_type, spt_castle),
        (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
        (neq, ":center_no", ":except_center_no"),
        (val_sub, ":random_center", 1),
        (lt, ":random_center", 0),
        (assign, ":target_center", ":center_no"),
        (assign, ":end_cond", 0),
      (try_end),
      (assign, reg0, ":target_center"),
  ]),
  
  # script_troop_write_owned_centers_to_s2
  # Input: arg1 = troop_no
  # Output: none
  ("troop_write_owned_centers_to_s2",
    [
      (store_script_param_1, ":troop_no"),
      
      (call_script, "script_get_number_of_hero_centers", ":troop_no"),
      (assign, ":no_centers", reg0),
      
      (str_store_troop_name, s5, ":troop_no"),
      
      (try_begin),
        (gt, ":no_centers", 1),
        (try_for_range, ":i_center", 1, ":no_centers"),
          (call_script, "script_troop_get_leaded_center_with_index", ":troop_no", ":i_center"),
          (str_store_party_name_link, s50, reg0),
          (try_begin),
            (eq, ":i_center", 1),
            (call_script, "script_troop_get_leaded_center_with_index", ":troop_no", 0),
            (str_store_party_name_link, s51, reg0),
            (str_store_string, s51, "str_s50_and_s51"),
          (else_try),
            (str_store_string, s51, "str_s50_comma_s51"),
          (try_end),
        (try_end),
        (str_store_string, s2, "str_s5_is_the_ruler_of_s51"),
      (else_try),
        (eq, ":no_centers", 1),
        (call_script, "script_troop_get_leaded_center_with_index", ":troop_no", 0),
        (str_store_party_name_link, s51, reg0),
        (str_store_string, s2, "str_s5_is_the_ruler_of_s51"),
      (else_try),
        (store_troop_faction, ":faction_no", ":troop_no"),
        (str_store_faction_name_link, s6, ":faction_no"),
        (str_store_string, s2, "str_s5_is_a_nobleman_of_s6"),
      (try_end),
  ]),
  
  # script_troop_write_family_relations_to_s1
  ("troop_write_family_relations_to_s1",
    [
      (str_clear, s1),
      #redo, possibly using base from update_troop_notes
      
  ]),
  
  # script_write_family_relation_as_s3s_s2_to_s4
  
  # Inputs: arg1 = troop_no, arg2 = family_no (valid slot no after slot_troop_family_begin)
  # Outputs: s11 = what troop_1 is to troop_2, reg0 = strength of relationship. reg4 gender. Normally, "$g_talk_troop" should be troop_2
  
  ("troop_get_family_relation_to_troop",
    [
      (store_script_param_1, ":troop_1"),
      (store_script_param_2, ":troop_2"),
      
      (troop_get_type, ":gender_1", ":troop_1"),
      (val_mod, ":gender_1", 2),
      (assign, ":relation_strength", 0),
      
      (troop_get_slot, ":spouse_of_1", ":troop_1", slot_troop_spouse),
      (troop_get_slot, ":spouse_of_2", ":troop_2", slot_troop_spouse),
      
      (try_begin),
        (gt, ":spouse_of_1", -1),
        (troop_get_slot, ":father_of_spouse_of_1", ":spouse_of_1", slot_troop_father),
      (else_try),
        (assign, ":father_of_spouse_of_1", -1),
      (try_end),
      
      (try_begin),
        (gt, ":spouse_of_2", -1),
        (troop_get_slot, ":father_of_spouse_of_2", ":spouse_of_2", slot_troop_father),
      (else_try),
        (assign, ":father_of_spouse_of_2", -1),
      (try_end),
      
      (try_begin),
        (gt, ":spouse_of_2", -1),
        (troop_get_slot, ":mother_of_spouse_of_2", ":spouse_of_2", slot_troop_mother),
      (else_try),
        (assign, ":mother_of_spouse_of_2", -1),
      (try_end),
      
      (troop_get_slot, ":father_of_1", ":troop_1", slot_troop_father),
      (troop_get_slot, ":father_of_2", ":troop_2", slot_troop_father),
      
      #For the sake of simplicity, we can assume that all male aristocrats in prior generations either married commoners or procured their brides from the Old Country, thus discounting intermarriage
      (troop_get_slot, ":mother_of_1", ":troop_1", slot_troop_mother),
      (troop_get_slot, ":mother_of_2", ":troop_2", slot_troop_mother),
      
      (try_begin),
        (is_between, ":father_of_1", companions_begin, kingdom_ladies_end),
        (troop_get_slot, ":paternal_grandfather_of_1", ":father_of_1", slot_troop_father),
      (else_try),
        (assign, ":paternal_grandfather_of_1", -1),
      (try_end),
      
      (try_begin),
        (is_between, ":father_of_2", companions_begin, kingdom_ladies_end),
        (troop_get_slot, ":paternal_grandfather_of_2", ":father_of_2", slot_troop_father),
      (else_try),
        (assign, ":paternal_grandfather_of_2", -1),
      (try_end),
      
      (troop_get_slot, ":guardian_of_1", ":troop_1", slot_troop_guardian),
      (troop_get_slot, ":guardian_of_2", ":troop_2", slot_troop_guardian),
      
      (str_store_string, s11, "str_no_relation"),
      
      (try_begin),
        (eq, ":troop_1", ":troop_2"),
        #self
      (else_try),
        (eq, ":spouse_of_1", ":troop_2"),
        (assign, ":relation_strength", 20),
        (try_begin),
          (eq, ":gender_1", 1),
          (str_store_string, s11, "str_wife"),
        (else_try),
          (str_store_string, s11, "str_husband"),
        (try_end),
      (else_try),
        (eq, ":father_of_2", ":troop_1"),
        (assign, ":relation_strength", 15),
        (str_store_string, s11, "str_father"),
      (else_try),
        (eq, ":mother_of_2", ":troop_1"),
        (assign, ":relation_strength", 15),
        (str_store_string, s11, "str_mother"),
      (else_try),
        (this_or_next|eq, ":father_of_1", ":troop_2"),
        (eq, ":mother_of_1", ":troop_2"),
        (assign, ":relation_strength", 15),
        (try_begin),
          (eq, ":gender_1", 1),
          (str_store_string, s11, "str_daughter"),
        (else_try),
          (str_store_string, s11, "str_son"),
        (try_end),
      (else_try),
        (gt, ":father_of_1", -1), #necessary, as some lords do not have the father registered
        (eq, ":father_of_1", ":father_of_2"),
        (assign, ":relation_strength", 10),
        (try_begin),
          (eq, ":gender_1", 1),
          (str_store_string, s11, "str_sister"),
        (else_try),
          (str_store_string, s11, "str_brother"),
        (try_end),
      (else_try),
        (eq, ":guardian_of_2", ":troop_1"),
        (assign, ":relation_strength", 10),
        (try_begin),
          (eq, ":gender_1", 1),
          (str_store_string, s11, "str_sister"),
        (else_try),
          (str_store_string, s11, "str_brother"),
        (try_end),
      (else_try),
        (eq, ":guardian_of_1", ":troop_2"),
        (assign, ":relation_strength", 10),
        (try_begin),
          (eq, ":gender_1", 1),
          (str_store_string, s11, "str_sister"),
        (else_try),
          (str_store_string, s11, "str_brother"),
        (try_end),
      (else_try),
        (gt, ":paternal_grandfather_of_1", -1),
        (eq, ":paternal_grandfather_of_1", ":father_of_2"),
        (assign, ":relation_strength", 4),
        (try_begin),
          (eq, ":gender_1", 1),
          (str_store_string, s11, "str_niece"),
        (else_try),
          (str_store_string, s11, "str_nephew"),
        (try_end),
      (else_try), #specifically aunt and uncle by blood -- i assume that in a medieval society with lots of internal family conflicts, they would not include aunts and uncles by marriage
        (gt, ":paternal_grandfather_of_2", -1),
        (eq, ":paternal_grandfather_of_2", ":father_of_1"),
        (assign, ":relation_strength", 4),
        (try_begin),
          (eq, ":gender_1", 1),
          (str_store_string, s11, "str_aunt"),
        (else_try),
          (str_store_string, s11, "str_uncle"),
        (try_end),
      (else_try),
        (gt, ":paternal_grandfather_of_1", 0),
        (eq, ":paternal_grandfather_of_2", ":paternal_grandfather_of_1"),
        (assign, ":relation_strength", 2),
        (str_store_string, s11, "str_cousin"),
      (else_try),
        (eq, ":father_of_spouse_of_1", ":troop_2"),
        (assign, ":relation_strength", 5),
        (try_begin),
          (eq, ":gender_1", 1),
          (str_store_string, s11, "str_daughterinlaw"),
        (else_try),
          (str_store_string, s11, "str_soninlaw"),
        (try_end),
      (else_try),
        (eq, ":father_of_spouse_of_2", ":troop_1"),
        (assign, ":relation_strength", 5),
        (str_store_string, s11, "str_fatherinlaw"),
      (else_try),
        (eq, ":mother_of_spouse_of_2", ":troop_1"),
        (neq, ":mother_of_spouse_of_2", "trp_player"), #May be necessary if mother for troops not set to -1
        (assign, ":relation_strength", 5),
        (str_store_string, s11, "str_motherinlaw"),
        
      (else_try),
        (gt, ":father_of_spouse_of_1", -1), #necessary
        (eq, ":father_of_spouse_of_1", ":father_of_2"),
        (assign, ":relation_strength", 5),
        (try_begin),
          (eq, ":gender_1", 1),
          (str_store_string, s11, "str_sisterinlaw"),
        (else_try),
          (str_store_string, s11, "str_brotherinlaw"),
        (try_end),
      (else_try),
        (gt, ":father_of_spouse_of_2", -1), #necessary
        (eq, ":father_of_spouse_of_2", ":father_of_1"),
        (assign, ":relation_strength", 5),
        (try_begin),
          (eq, ":gender_1", 1),
          (str_store_string, s11, "str_sisterinlaw"),
        (else_try),
          (str_store_string, s11, "str_brotherinlaw"),
        (try_end),
      (else_try),
        (gt, ":spouse_of_2", -1), #necessary to avoid bug
        (troop_slot_eq, ":spouse_of_2", slot_troop_guardian, ":troop_1"),
        (assign, ":relation_strength", 5),
        (try_begin),
          (eq, ":gender_1", 1),
          (str_store_string, s11, "str_sisterinlaw"),
        (else_try),
          (str_store_string, s11, "str_brotherinlaw"),
        (try_end),
      (else_try),
        (gt, ":spouse_of_1", -1), #necessary to avoid bug
        (troop_slot_eq, ":spouse_of_1", slot_troop_guardian, ":troop_2"),
        (assign, ":relation_strength", 5),
        (try_begin),
          (eq, ":gender_1", 1),
          (str_store_string, s11, "str_sisterinlaw"),
        (else_try),
          (str_store_string, s11, "str_brotherinlaw"),
        (try_end),
      (else_try),
        (troop_slot_eq, ":troop_1", slot_troop_guardian, ":troop_2"),
        (store_faction_of_troop, ":faction", ":troop_2"),
        (faction_slot_eq, ":faction", slot_faction_leader, ":troop_2"),
        (str_store_string, s11, "@Royal guardian"),
      (try_end),
      
      (assign, reg4, ":gender_1"),
      (assign, reg0, ":relation_strength"),
  ]),
  
  
  # script_complete_family_relations
  # Inputs: none
  # Outputs: none
  
  #complete family relations removed
  
  # script_collect_friendly_parties
  # Fills the party p_collective_friends with the members of parties attached to main_party and ally_party_no
  ("collect_friendly_parties",
    [
      (party_collect_attachments_to_party, "p_main_party", "p_collective_friends"),
      (try_begin),
        (gt, "$g_ally_party", 0),
        (party_collect_attachments_to_party, "$g_ally_party", "p_temp_party"),
        (assign, "$g_move_heroes", 1),
        (call_script, "script_party_add_party", "p_collective_friends", "p_temp_party"),
      (try_end),
  ]),
  
  # script_encounter_calculate_fit
  # Input: arg1 = troop_no
  # Output: none
  ("encounter_calculate_fit",
    [
      #(assign, "$g_enemy_fit_for_battle_old",  "$g_enemy_fit_for_battle"),
      #(assign, "$g_friend_fit_for_battle_old", "$g_friend_fit_for_battle"),
      #(assign, "$g_main_party_fit_for_battle_old", "$g_main_party_fit_for_battle"),
      # (call_script, "script_party_count_fit_for_battle", "p_main_party"),
      #(assign, "$g_main_party_fit_for_battle", reg(0)),
      (call_script, "script_collect_friendly_parties"),
      (call_script, "script_party_count_fit_for_battle", "p_collective_friends"),
      (assign, "$g_friend_fit_for_battle", reg(0)),
      
      (party_clear, "p_collective_ally"),
      (try_begin),
        (gt, "$g_ally_party", 0),
        (party_is_active, "$g_ally_party"),
        (party_collect_attachments_to_party, "$g_ally_party", "p_collective_ally"),
        #(call_script, "script_party_count_fit_for_battle", "p_collective_ally"),
        #(val_add, "$g_friend_fit_for_battle", reg(0)),
      (try_end),
      
      (party_clear, "p_collective_enemy"),
      (try_begin),
        (party_is_active, "$g_enemy_party"),
        #(display_message, "@VC Test: party is active"),
        (party_collect_attachments_to_party, "$g_enemy_party", "p_collective_enemy"),
      (try_end),
      (call_script, "script_party_count_fit_for_battle", "p_collective_enemy"),
      (assign, "$g_enemy_fit_for_battle", reg(0)),
      (assign, reg11, "$g_enemy_fit_for_battle"),
      (assign, reg10, "$g_friend_fit_for_battle"),
      #(display_message, "@VC Test: g_enemy_fit_for_battle = {reg11}, g_friend_fit_for_battle = {reg10}"),
  ]),
  
  # script_encounter_init_variables
  # Input: arg1 = troop_no
  # Output: none
  ("encounter_init_variables",
    [
      (assign, "$capture_screen_shown", 0),
      (assign, "$loot_options_shown", 0),	#vc
      (assign, "$ship_capture_shown", 0),	#vc
      (assign, "$loot_screen_shown", 0),
      (assign, "$thanked_by_ally_leader", 0),
      (assign, "$g_battle_result", 0),
      (assign, "$cant_leave_encounter", 0),
      (assign, "$cant_talk_to_enemy", 0),
      (assign, "$last_defeated_hero", 0),
      (assign, "$last_freed_hero", 0),
      
      # (call_script, "script_encounter_calculate_fit"),  always called before this is
      (call_script, "script_party_copy", "p_main_party_backup", "p_main_party"),
      (call_script, "script_party_calculate_strength", "p_main_party", 0),
      (assign, "$g_starting_strength_main_party", reg0),
      (call_script, "script_party_copy", "p_encountered_party_backup", "p_collective_enemy"),
      (call_script, "script_party_calculate_strength", "p_collective_enemy", 0),
      (assign, "$g_starting_strength_enemy_party", reg0),
      (assign, "$g_strength_contribution_of_player", 100),
      
      (call_script, "script_party_copy", "p_collective_friends_backup", "p_collective_friends"),
      (call_script, "script_party_calculate_strength", "p_collective_friends", 0),
      (assign, "$g_starting_strength_friends", reg0),
      
      (store_mul, "$g_strength_contribution_of_player","$g_starting_strength_main_party", 100), # reduce contribution if we are helping someone.
      
      (try_begin),
        (gt, "$g_starting_strength_friends", 0), #this new to prevent occasional div by zero error
        (val_div, "$g_strength_contribution_of_player","$g_starting_strength_friends"),
      (else_try),
        (assign, "$g_strength_contribution_of_player", 100), #Or zero, maybe
      (try_end),
      
      (party_clear, "p_routed_enemies"), #new
      (assign, "$num_routed_us", 0),#newtoday
      (assign, "$num_routed_allies", 0),#newtoday
      (assign, "$num_routed_enemies", 0),#newtoday
      (party_get_num_companion_stacks, ":num_stacks", "p_main_party"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop_id", "p_main_party", ":i_stack"),
        (try_begin),
          (troop_set_slot, ":stack_troop_id", slot_troop_player_routed_agents, 0),
          #(troop_set_slot, ":stack_troop_id", slot_troop_enemy_routed_agents, 0),
          #(troop_set_slot, ":stack_troop_id", slot_troop_ally_routed_agents, 0),
        (try_end),
      (try_end),
      
      (party_get_num_companion_stacks, ":num_stacks", "p_collective_friends"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop_id", "p_collective_friends", ":i_stack"),
        (try_begin),
          #(troop_set_slot, ":stack_troop_id", slot_troop_player_routed_agents, 0),
          #(troop_set_slot, ":stack_troop_id", slot_troop_enemy_routed_agents, 0),
          (troop_set_slot, ":stack_troop_id", slot_troop_ally_routed_agents, 0),
        (try_end),
      (try_end),
      
      (party_get_num_companion_stacks, ":num_stacks", "p_collective_enemy"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop_id", "p_collective_enemy", ":i_stack"),
        (try_begin),
          #(troop_set_slot, ":stack_troop_id", slot_troop_player_routed_agents, 0),
          (troop_set_slot, ":stack_troop_id", slot_troop_enemy_routed_agents, 0),
          #(troop_set_slot, ":stack_troop_id", slot_troop_ally_routed_agents, 0),
        (try_end),
      (try_end),
      
      (try_for_range, ":cur_faction", fac_kingdom_1, fac_kingdoms_end),
        (faction_set_slot, ":cur_faction", slot_faction_num_routed_agents, 0),
      (try_end),
      
      (assign, "$routed_party_added", 0), #new
      (party_clear, "p_total_enemy_casualties"), #new
      
      #      (try_begin),
      #        (gt, "$g_ally_party", 0),
      #        (call_script, "script_party_copy", "p_ally_party_backup", "p_collective_ally"),
      #        (call_script, "script_party_calculate_strength", "p_collective_ally"),
      #        (assign, "$g_starting_strength_ally_party", reg0),
      #        (store_add, ":starting_strength_factor_combined","$g_starting_strength_ally_party","$g_starting_strength_main_party"),
      #         (store_mul, "$g_strength_contribution_of_player","$g_starting_strength_main_party", 80), #reduce contribution if we are helping someone.
      #        (val_div, "$g_strength_contribution_of_player",":starting_strength_factor_combined"),
      #      (try_end),
  ]),
  
  # script_calculate_renown_value
  # Input: arg1 = troop_no
  # Output: fills $battle_renown_value
  ("calculate_renown_value",
    [
      (call_script, "script_party_calculate_strength", "p_main_party", 0),
      (assign, ":main_party_strength", reg0),
      (call_script, "script_party_calculate_strength", "p_collective_enemy", 0),
      (assign, ":enemy_strength", reg0),
      (call_script, "script_party_calculate_strength", "p_collective_friends", 0),
      (assign, ":friends_strength", reg0),
      
      (val_add, ":friends_strength", 1),
      (store_mul, ":enemy_strength_ratio", ":enemy_strength", 100),
      (val_div, ":enemy_strength_ratio", ":friends_strength"),
      
      (assign, ":renown_val", ":enemy_strength"),
      (val_mul, ":renown_val", ":enemy_strength_ratio"),
      (val_div, ":renown_val", 100),
      
      (val_mul, ":renown_val", ":main_party_strength"),
      (val_div, ":renown_val",":friends_strength"),
      
      (store_div, "$battle_renown_value", ":renown_val", 5),
      (val_min, "$battle_renown_value", 2500),
      (convert_to_fixed_point, "$battle_renown_value"),
      (store_sqrt, "$battle_renown_value", "$battle_renown_value"),
      (convert_from_fixed_point, "$battle_renown_value"),
      (assign, reg8, "$battle_renown_value"),
      (display_message, "@You stand to gain {reg8} renown if you win this battle.",0xFFFFFFFF),
  ]),
  
  
  ##  # script_calculate_weekly_wage_for_player
  ##  # Input: none
  ##  # Output: none
  ##  ("calculate_weekly_wage_for_player",
  ##    [
  ##        (call_script, "script_calculate_weekly_party_wage", "p_main_party"),
  ##        (assign, ":result", reg0),
  ##        (try_for_parties, ":party_no"),
  ##          (store_faction_of_party, ":party_faction", ":party_no"),
  ##          (eq, ":party_faction", "fac_player_supporters_faction"),
  ##          (call_script, "script_calculate_weekly_party_wage", ":party_no"),
  ##          (val_add, ":result", reg0),
  ##        (try_end),
  ##        (assign, reg0, ":result"),
  ##  ]),
  
  
  # script_get_first_agent_with_troop_id
  # Input: arg1 = troop_no
  # Output: agent_id
  ("cf_get_first_agent_with_troop_id",
    [
      (store_script_param_1, ":troop_no"),
      #      (store_script_param_2, ":agent_no_to_begin_searching_after"),
      (assign, ":result", -1),
      (try_for_agents, ":cur_agent"),
        (eq, ":result", -1),
        ##        (try_begin),
        ##          (eq, ":cur_agent", ":agent_no_to_begin_searching_after"),
        ##          (assign, ":agent_no_to_begin_searching_after", -1),
        ##        (try_end),
        ##        (eq, ":agent_no_to_begin_searching_after", -1),
        (agent_get_troop_id, ":cur_troop_no", ":cur_agent"),
        (eq, ":cur_troop_no", ":troop_no"),
        (assign, ":result", ":cur_agent"),
      (try_end),
      (assign, reg0, ":result"),
      (neq, reg0, -1),
  ]),
  
  # script_cf_team_get_average_position_of_agents_with_type_to_pos1
  # Input: arg1 = team_no, arg2 = class_no (grc_everyone, grc_infantry, grc_cavalry, grc_archers, grc_heroes)
  # Output: none, pos1 = average_position (0,0,0 if there are no matching agents)
  # ("cf_team_get_average_position_of_agents_with_type_to_pos1",  #MOTO see division data slot_team_d0_avg_x, slot_team_d0_avg_y
    # [
      # (store_script_param_1, ":team_no"),
      # (store_script_param_2, ":division_no"),
      # (assign, ":total_pos_x", 0),
      # (assign, ":total_pos_y", 0),
      # (assign, ":total_pos_z", 0),
      # (assign, ":num_agents", 0),
      # (set_fixed_point_multiplier, 100),
      # (try_for_agents, ":cur_agent"),
        # (agent_is_alive, ":cur_agent"),
        # (agent_is_human, ":cur_agent"),
        # (agent_get_team, ":cur_team_no", ":cur_agent"),
        # (eq, ":cur_team_no", ":team_no"),
        # (agent_get_division, ":cur_agent_division", ":cur_agent"),
        # (this_or_next|eq, ":division_no", grc_everyone),
        # (eq, ":division_no", ":cur_agent_division"),
        # (agent_get_position, pos1, ":cur_agent"),
        # (position_get_x, ":cur_pos_x", pos1),
        # (val_add, ":total_pos_x", ":cur_pos_x"),
        # (position_get_y, ":cur_pos_y", pos1),
        # (val_add, ":total_pos_y", ":cur_pos_y"),
        # (position_get_z, ":cur_pos_z", pos1),
        # (val_add, ":total_pos_z", ":cur_pos_z"),
        # (val_add, ":num_agents", 1),
      # (try_end),
      # (gt, ":num_agents", 1),
      # (val_div, ":total_pos_x", ":num_agents"),
      # (val_div, ":total_pos_y", ":num_agents"),
      # (val_div, ":total_pos_z", ":num_agents"),
      # (init_position, pos1),
      # (position_move_x, pos1, ":total_pos_x"),
      # (position_move_y, pos1, ":total_pos_y"),
      # (position_move_z, pos1, ":total_pos_z"),
  # ]),
  
  # script_cf_turn_windmill_fans
  # Input: arg1 = instance_no (none = 0)
  # Output: none
  ("cf_turn_windmill_fans",
    [(store_script_param_1, ":instance_no"),
      (scene_prop_get_instance, ":windmill_fan_object", "spr_windmill_fan_turning", ":instance_no"),
      (ge, ":windmill_fan_object", 0),
      (prop_instance_get_position, pos1, ":windmill_fan_object"),
      (position_rotate_y, pos1, 10),
      (prop_instance_animate_to_position, ":windmill_fan_object", pos1, 100),
      (val_add, ":instance_no", 1),
      (call_script, "script_cf_turn_windmill_fans", ":instance_no"),
  ]),
  
  # script_print_party_members
  # Input: arg1 = party_no
  # Output: s51 = output string. "noone" if the party is empty
  ("print_party_members",
    [
      (store_script_param_1, ":party_no"),
      (party_get_num_companion_stacks, ":num_stacks", ":party_no"),
      (assign, reg10, ":num_stacks"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop",":party_no",":i_stack"),
        (troop_is_hero, ":stack_troop"),
        (try_begin),
          (eq, ":i_stack", 0),
          (str_store_troop_name, s51, ":stack_troop"),
        (try_end),
        (str_store_troop_name, s52, ":stack_troop"),
        (try_begin),
          (eq, ":i_stack", 1),
          (str_store_string, s51, "str_s52_and_s51"),
        (else_try),
          (gt, ":i_stack", 1),
          (str_store_string, s51, "str_s52_comma_s51"),
        (try_end),
      (try_end),
      (try_begin),
        (eq, ":num_stacks", 0),
        (str_store_string, s51, "str_noone"),
      (try_end),
  ]),
  
  # script_round_value
  # Input: arg1 = value
  # Output: reg0 = rounded_value
  ("round_value",
    [
      (store_script_param_1, ":value"),
      (try_begin),
        (lt, ":value", 100),
        (neq, ":value", 0),
        (val_add, ":value", 5),
        (val_div, ":value", 10),
        (val_mul, ":value", 10),
        (try_begin),
          (eq, ":value", 0),
          (assign, ":value", 5),
        (try_end),
      (else_try),
        (lt, ":value", 300),
        (val_add, ":value", 25),
        (val_div, ":value", 50),
        (val_mul, ":value", 50),
      (else_try),
        (val_add, ":value", 50),
        (val_div, ":value", 100),
        (val_mul, ":value", 100),
      (try_end),
      (assign, reg0, ":value"),
  ]),
  
  
  # script_change_banners_and_chest
  # Input: none
  # Output: none
  ("change_banners_and_chest",
    [(party_get_slot, ":cur_leader", "$g_encountered_party", slot_town_lord),
      (try_begin),
        (ge, ":cur_leader", 0),
        #normal_banner_begin
        (troop_get_slot, ":troop_banner_object", ":cur_leader", slot_troop_banner_scene_prop),
        (gt, ":troop_banner_object", 0),
        (replace_scene_props, banner_scene_props_begin, ":troop_banner_object"),
        (replace_scene_props, "spr_banner_a", ":troop_banner_object"),#fix for old scenes
      (else_try),
        (replace_scene_props, banner_scene_props_begin, "spr_empty"),
        (replace_scene_props, "spr_banner_a", "spr_empty"),#fix for old scenes
      (try_end),
      (try_begin),
        (neq, ":cur_leader", "trp_player"),
        (replace_scene_props, "spr_player_chest", "spr_locked_player_chest"),
        (replace_scene_props, "spr_chest_a", "spr_locked_chest_a"), #VC-3474
      (try_end),
  ]),
  
  
  # script_remove_siege_objects
  # Input: none
  # Output: none
  ("remove_siege_objects",
    [
      (replace_scene_props, "spr_battlement_a_destroyed", "spr_battlement_a"),
      (replace_scene_props, "spr_snowy_castle_battlement_a_destroyed", "spr_snowy_castle_battlement_a"),
      (replace_scene_props, "spr_castle_e_battlement_a_destroyed", "spr_castle_e_battlement_a"),
      (replace_scene_props, "spr_castle_battlement_a_destroyed", "spr_castle_battlement_a"),
      (replace_scene_props, "spr_castle_battlement_b_destroyed", "spr_castle_battlement_b"),
      (replace_scene_props, "spr_earth_wall_a2", "spr_earth_wall_a"),
      (replace_scene_props, "spr_earth_wall_b2", "spr_earth_wall_b"),
      (replace_scene_props, "spr_belfry_platform_b", "spr_empty"),
      (replace_scene_props, "spr_belfry_platform_a", "spr_empty"),
      (replace_scene_props, "spr_belfry_a", "spr_empty"),
      (replace_scene_props, "spr_belfry_wheel", "spr_empty"),
      (replace_scene_props, "spr_siege_ladder_move_6m", "spr_empty"),
      (replace_scene_props, "spr_siege_ladder_move_8m", "spr_empty"),
      (replace_scene_props, "spr_siege_ladder_move_10m", "spr_empty"),
      (replace_scene_props, "spr_siege_ladder_move_12m", "spr_empty"),
      (replace_scene_props, "spr_siege_ladder_move_14m", "spr_empty"),
      (replace_scene_props, "spr_siege_ladder_12m", "spr_empty"),
      (replace_scene_props, "spr_siege_ladder_14m", "spr_empty"),
      (replace_scene_props, "spr_mangonel", "spr_empty"),
      (replace_scene_props, "spr_trebuchet_old", "spr_empty"),
      (replace_scene_props, "spr_trebuchet_new", "spr_empty"),
      (replace_scene_props, "spr_stone_ball", "spr_empty"),
      (replace_scene_props, "spr_Village_fire_big", "spr_empty"),
      (replace_scene_props, "spr_ado_nordic_wall_defence_destroy", "spr_ado_nordic_wall_defence"),
      (replace_scene_props, "spr_ado_saxon_wall_defence_wood_destroy", "spr_ado_saxon_wall_defence_wood"),
      (replace_scene_props, "spr_ado_saxon_palisade_wall_short_destroy", "spr_ado_saxon_palisade_wall_short"),
  ]),
  
  # script_describe_relation_to_s63
  # Input: arg1 = relation (-100 .. 100)
  # Output: none
  ("describe_relation_to_s63",
    [(store_script_param_1, ":relation"),
      (store_add, ":normalized_relation", ":relation", 100),
      (val_add, ":normalized_relation", 5),
      (store_div, ":str_offset", ":normalized_relation", 10),
      (val_clamp, ":str_offset", 0, 20),
      (store_add, ":str_id", "str_relation_mnus_100",  ":str_offset"),
      (str_store_string, s63, ":str_id"),
  ]),
  
  # script_describe_center_relation_to_s3
  # Input: arg1 = relation (-100 .. 100)
  # Output: none
  ("describe_center_relation_to_s3",
    [(store_script_param_1, ":relation"),
      (store_add, ":normalized_relation", ":relation", 100),
      (val_add, ":normalized_relation", 5),
      (store_div, ":str_offset", ":normalized_relation", 10),
      (val_clamp, ":str_offset", 0, 20),
      (store_add, ":str_id", "str_center_relation_mnus_100",  ":str_offset"),
      (str_store_string, s3, ":str_id"),
  ]),
  
  # script_center_set_walker_to_type
  # Input: arg1 = center_no, arg2 = walker_no, arg3 = walker_type,
  # Output: none
  ("center_set_walker_to_type",
    [
      (store_script_param, ":center_no", 1),
      (store_script_param, ":walker_no", 2),
      (store_script_param, ":walker_type", 3),
      (store_add, ":type_slot", slot_center_walker_0_type, ":walker_no"),
      (party_set_slot, ":center_no", ":type_slot", ":walker_type"),
      (party_get_slot, ":center_faction", ":center_no", slot_center_original_faction),
      (faction_get_slot, ":center_culture", ":center_faction", slot_faction_culture),
      (store_random_in_range, ":walker_troop_slot", 0, 2),
      (try_begin),
        (party_slot_eq, ":center_no", slot_party_type, spt_village),
        (val_add, ":walker_troop_slot", slot_faction_village_walker_male_troop),
      (else_try), #anadido chief castle walkers
        (party_slot_eq, ":center_no", slot_party_type, spt_castle),
        (val_add, ":walker_troop_slot", slot_faction_village_walker_male_troop),
      (else_try),
        (val_add, ":walker_troop_slot", slot_faction_town_walker_male_troop),
      (try_end),
      (try_begin),
        (eq,":walker_type", walkert_spy),
        (assign,":original_walker_slot",":walker_troop_slot"),
        (val_add,":walker_troop_slot",4), # select spy troop id slot
      (try_end),
      (faction_get_slot, ":walker_troop_id", ":center_culture", ":walker_troop_slot"),
      (try_begin),
        (eq,":walker_type", walkert_spy),
        (faction_get_slot, ":original_walker", ":center_culture", ":original_walker_slot"),
        # restore spy inventory
        (try_for_range,":item_no","itm_horse_meat","itm_wooden_stick"),
          (store_item_kind_count,":num_items",":item_no",":original_walker"),
          (ge,":num_items",1),
          (store_item_kind_count,":num_items",":item_no",":walker_troop_id"),
          (lt,":num_items",1),
          (troop_add_items,":walker_troop_id",":item_no",1),
        (try_end),
        # determine spy recognition item
        (store_random_in_range,":spy_item_type",itp_type_head_armor,itp_type_hand_armor),
        (assign,":num",0),
        (try_for_range,":item_no","itm_horse_meat","itm_wooden_stick"),
          (store_item_kind_count,":num_items",":item_no",":walker_troop_id"),
          (ge,":num_items",1),
          (item_get_type, ":itp", ":item_no"),
          (eq,":itp",":spy_item_type"),
          (val_add,":num",1),
          (troop_remove_items,":walker_troop_id",":item_no",":num_items"),
        (try_end),
        (store_random_in_range,":random_item",0,":num"),
        (assign,":num",-1),
        (try_for_range,":item_no","itm_horse_meat","itm_wooden_stick"),
          (store_item_kind_count,":num_items",":item_no",":original_walker"),
          (ge,":num_items",1),
          (item_get_type, ":itp", ":item_no"),
          (eq,":itp",":spy_item_type"),
          (val_add,":num",1),
          (eq,":num",":random_item"),
          (troop_add_items,":walker_troop_id",":item_no",1),
          (assign,":spy_item",":item_no"),
        (try_end),
        (assign,"$spy_item_worn",":spy_item"),
        (assign,"$spy_quest_troop",":walker_troop_id"),
        (troop_equip_items,":walker_troop_id"),
      (try_end),
      (store_add, ":troop_slot", slot_center_walker_0_troop, ":walker_no"),
      (party_set_slot, ":center_no", ":troop_slot", ":walker_troop_id"),
      (store_random_in_range, ":walker_dna", 0, 1000000),
      (store_add, ":dna_slot", slot_center_walker_0_dna, ":walker_no"),
      (party_set_slot, ":center_no", ":dna_slot", ":walker_dna"),
  ]),
  
  
  # script_cf_center_get_free_walker
  # Input: arg1 = center_no
  # Output: reg0 = walker no (can fail)
  ("cf_center_get_free_walker",
    [
      (store_script_param, ":center_no", 1),
      (assign, ":num_free_walkers", 0),
      (try_for_range, ":walker_no", 0, num_town_walkers),
        (store_add, ":type_slot", slot_center_walker_0_type, ":walker_no"),
        (party_slot_eq, ":center_no", ":type_slot", walkert_default),
        (val_add, ":num_free_walkers", 1),
      (try_end),
      (gt, ":num_free_walkers", 0),
      (assign, reg0, -1),
      (store_random_in_range, ":random_rank", 0, ":num_free_walkers"),
      (try_for_range, ":walker_no", 0, num_town_walkers),
        (store_add, ":type_slot", slot_center_walker_0_type, ":walker_no"),
        (party_slot_eq, ":center_no", ":type_slot", walkert_default),
        (val_sub, ":num_free_walkers", 1),
        (eq, ":num_free_walkers", ":random_rank"),
        (assign, reg0, ":walker_no"),
      (try_end),
  ]),
  
  # script_center_remove_walker_type_from_walkers
  # Input: arg1 = center_no, arg2 = walker_type,
  # Output: reg0 = 1 if comment found, 0 otherwise; s61 will contain comment string if found
  ("center_remove_walker_type_from_walkers",
    [
      (store_script_param, ":center_no", 1),
      (store_script_param, ":walker_type", 2),
      (try_for_range, ":walker_no", 0, num_town_walkers),
        (store_add, ":type_slot", slot_center_walker_0_type, ":walker_no"),
        (party_slot_eq, ":center_no", ":type_slot", ":walker_type"),
        (call_script, "script_center_set_walker_to_type", ":center_no", ":walker_no", walkert_default),
      (try_end),
  ]),
  
  
  # script_init_town_walkers
  # Input: none
  # Output: none
  ("init_town_walkers",
    [
      (try_begin),
        (eq, "$town_nighttime", 0),
        (assign, ":have_girls", 0),
        (assign, ":have_boys", 0),
        (try_for_range, ":walker_no", 0, num_town_walkers),
          (store_add, ":troop_slot", slot_center_walker_0_troop, ":walker_no"),
          (party_get_slot, ":walker_troop_id", "$current_town", ":troop_slot"),
          (gt, ":walker_troop_id", 0),
          (store_add, ":entry_no", town_walker_entries_start, ":walker_no"),
          #        (set_visitor, ":entry_no", ":walker_troop_id"),
          #		   (set_visitors,":entry_no", ":walker_troop_id",2), #chief anade mas ninos, childs
          
          (try_begin), #monk walkers in monastery chief
            (is_between, "$g_encountered_party", "p_monasterio1", "p_yourlair"),
            (set_visitors,":entry_no", "trp_monk_walker",1),	#phaiak fixing VC-824
            # (store_random_in_range, ":is_monk", 1, 5),
            # (try_begin),
            # (eq, ":is_monk", 1),
            # (set_visitors,":entry_no", ":walker_troop_id",1), #chief anade mas ninos, childs
            # (else_try),
            # (set_visitors,":entry_no", "trp_monk_walker",1),
            # (try_end),
            
          (else_try),
            (store_random_in_range, ":is_child", 1, 5),
            (try_begin),
              (eq, ":is_child", 1),
              (try_begin),
                (eq, ":have_boys", 0),
                (set_visitors,":entry_no", "trp_nino_varon",2),
                (assign, ":have_boys", 1),
              (else_try),
                (eq, ":have_girls", 0),
                (set_visitors,":entry_no", "trp_nina_chica",2),
                (assign, ":have_girls", 1),
              (else_try),
                (store_random_in_range, ":childs_type", 1, 3),
                (eq, ":childs_type", 1),
                (set_visitors,":entry_no", "trp_nina_chica",2),
              (else_try),
                (set_visitors,":entry_no", "trp_nino_varon",2),
              (try_end),
            (else_try),
              (store_random_in_range, ":walkers_num", 1, 3),
              (set_visitors,":entry_no", ":walker_troop_id",":walkers_num"), #chief anade mas ninos, childs
            (try_end),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  
  # script_cf_enter_center_location_bandit_check
  # Input: none
  # Output: none
  ("cf_enter_center_location_bandit_check",
    [
      (neq, "$town_nighttime", 0),
      (try_begin),
        (party_slot_eq, "$current_town", slot_center_has_bandits, 0),
        (party_get_slot, ":center_relation", "$current_town", slot_center_player_relation),
        (lt, ":center_relation", -40),
        (store_random_in_range, ":rand", 0, 100),
        (lt, ":rand", 35),
        (store_random_in_range, ":bandit", "trp_bandit", "trp_sea_raider"),
        (party_set_slot, "$current_town", slot_center_has_bandits, ":bandit"),
      (try_end),
      (party_get_slot, ":bandit_troop", "$current_town", slot_center_has_bandits),
      (is_between, ":bandit_troop", "trp_looter", "trp_sea_raider"),
      #     (party_slot_ge, "$current_town", slot_center_has_bandits, 1),
      (eq, "$g_defending_against_siege", 0),#Skip if the center is under siege (because of resting)
      (eq, "$sneaked_into_town", 0),#Skip if sneaked
      (try_begin),
        (party_slot_eq, "$current_town", slot_party_type, spt_village),
        (party_get_slot, ":cur_scene", "$current_town", slot_castle_exterior),
      (else_try),
        (party_get_slot, ":cur_scene", "$current_town", slot_town_center),
      (try_end),
      (modify_visitors_at_site, ":cur_scene"),
      (reset_visitors),
      #     (party_get_slot, ":bandit_troop", "$current_town", slot_center_has_bandits),
      (store_character_level, ":level", "trp_player"),
      
      (set_jump_mission, "mt_bandits_at_night"),
      (try_begin),
        (party_slot_eq, "$current_town", slot_party_type, spt_village),
        (assign, ":spawn_amount", 2),
        (store_div, ":level_fac",  ":level", 10),
        (val_add, ":spawn_amount", ":level_fac"),
        (try_for_range, ":unused", 0, 4),
          (gt, ":level", 10),
          (store_random_in_range, ":random_no", 0, 100),
          (lt, ":random_no", ":level"),
          (val_add, ":spawn_amount", 3),
        (try_end),
        (set_visitors, 4, ":bandit_troop", ":spawn_amount"),
        (assign, "$num_center_bandits", ":spawn_amount"),
        (set_jump_entry, 2),
      (else_try),
        (assign, ":spawn_amount", 3),
        (assign, "$num_center_bandits", 0),
        (store_div, ":level_fac",  ":level", 10),
        (val_add, ":spawn_amount", ":level_fac"),
        (try_begin),
          (gt, ":level", 15),
          (store_random_in_range, ":random_no", 0, 100),
          (lt, ":random_no", ":level"),
          (val_add, ":spawn_amount", 3),
        (try_end),
        (val_add, "$num_center_bandits",  ":spawn_amount"),
        (set_visitors, 11, ":bandit_troop", ":spawn_amount"),
        (assign, ":spawn_amount", 2),
        (try_begin),
          (gt, ":level", 20),
          (store_random_in_range, ":random_no", 0, 100),
          (lt, ":random_no", ":level"),
          (val_add, ":spawn_amount", 4),
        (try_end),
        (set_visitors, 27, ":bandit_troop", ":spawn_amount"),
        (val_add, "$num_center_bandits",  ":spawn_amount"),
        (try_begin),
          (gt, ":level", 9),
          (assign, ":spawn_amount", 2),
          (try_begin),
            (gt, ":level", 25),
            (store_random_in_range, ":random_no", 0, 100),
            (lt, ":random_no", ":level"),
            (assign, ":spawn_amount", 6),
          (try_end),
          (set_visitors, 28, ":bandit_troop", ":spawn_amount"),
          (val_add, "$num_center_bandits",  ":spawn_amount"),
        (try_end),
        (assign, "$town_entered", 1),
        (assign, "$all_doors_locked", 1),
      (try_end),
      (display_message, "@You have run into a trap!", 0xFFFF2222),
      (display_message, "@You are attacked by a group of bandits!", 0xFFFF2222),
      (jump_to_scene, ":cur_scene"),
      (change_screen_mission),
  ]),
  
  # script_init_town_agent
  # Input: none
  # Output: none
  ("init_town_agent",
    [
      (store_script_param, ":agent_no", 1),
      (agent_get_troop_id, ":troop_no", ":agent_no"),
      (set_fixed_point_multiplier, 100),
      (assign, ":stand_animation", -1),
      (try_begin),
        (this_or_next|is_between, ":troop_no", armor_merchants_begin, armor_merchants_end),
        (is_between, ":troop_no", weapon_merchants_begin, weapon_merchants_end),
        (try_begin),
          (troop_get_type, ":cur_troop_gender", ":troop_no"),
          (val_mod, ":cur_troop_gender", 2),
          (eq, ":cur_troop_gender", 0),
          (agent_set_animation, ":agent_no", "anim_stand_townguard"),
        (else_try),
          (agent_set_animation, ":agent_no", "anim_stand_townguard"),
        (end_try),
      (else_try),
        (is_between, ":troop_no", kingdom_ladies_begin, kingdom_ladies_end),
        (assign, ":stand_animation", "anim_stand_lady"),
      (else_try),
        (is_between, ":troop_no", active_npcs_begin, active_npcs_end),
        (assign, ":stand_animation", "anim_stand_lord"),
      (else_try),
        (is_between, ":troop_no", soldiers_begin, soldiers_end),
        (assign, ":stand_animation", "anim_stand_townguard"),
      (try_end),
      (try_begin),
        (ge, ":stand_animation", 0),
        (agent_set_stand_animation, ":agent_no", ":stand_animation"),
        (agent_set_animation, ":agent_no", ":stand_animation"),
        (store_random_in_range, ":random_no", 0, 100),
        (agent_set_animation_progress, ":agent_no", ":random_no"),
      (try_end),
  ]),
  
  # script_init_town_walker_agents
  # Input: none
  # Output: none
  ("init_town_walker_agents",
    [(assign, ":num_walkers", 0),
      (try_for_agents, ":cur_agent"),
        (neg|agent_slot_ge, ":cur_agent", slot_agent_is_blocked, 1),
        (agent_get_troop_id, ":cur_troop", ":cur_agent"),
        (is_between, ":cur_troop", walkers_begin, walkers_end),
        (val_add, ":num_walkers", 1),
        # phaiak begin (VC-1664)
        #(agent_set_speed_limit, ":cur_agent", 1),
        (store_random_in_range, ":speed", 0, 8),
        (val_mul, ":speed", 5),
        (val_add, ":speed", 50),	#means: 50, 55, 60 , ..., 85),
        (call_script, "script_advanced_agent_set_speed_modifier",":cur_agent", ":speed"),
        # phaiak end
        (agent_get_position, pos1, ":cur_agent"),
        (try_for_range, ":i_e_p", 9, 40),#Entry points
          (entry_point_get_position, pos2, ":i_e_p"),
          (get_distance_between_positions, ":distance", pos1, pos2),
          (lt, ":distance", 200),
          (agent_set_slot, ":cur_agent", 0, ":i_e_p"),
        (try_end),
        (call_script, "script_set_town_walker_destination", ":cur_agent"),
      (try_end),
  ]),
  
  # script_agent_get_town_walker_details
  # This script assumes this is one of town walkers.
  # Input: agent_id
  # Output: reg0: town_walker_type, reg1: town_walker_dna
  ("agent_get_town_walker_details",
    [(store_script_param, ":agent_no", 1),
      (agent_get_entry_no, ":entry_no", ":agent_no"),
      (store_sub, ":walker_no", ":entry_no", town_walker_entries_start),
      
      (store_add, ":type_slot", slot_center_walker_0_type, ":walker_no"),
      (party_get_slot, ":walker_type", "$current_town", ":type_slot"),
      (store_add, ":dna_slot", slot_center_walker_0_dna,  ":walker_no"),
      (party_get_slot, ":walker_dna", "$current_town", ":dna_slot"),
      (assign, reg0, ":walker_type"),
      (assign, reg1, ":walker_dna"),
      (assign, reg2, ":walker_no"),
  ]),
  
  
  ("town_walker_occupation_string_to_s14",
    [
      (store_script_param, ":agent_no", 1),
      
      #Cairo, approx 1799:
      #adult males = 114,000
      #military, 10,400
      #civil, including religious 5,000
      #commerce 3,500
      #merchants 4,500
      #coffee shops, 1,500 (maybe broaden to inns and taverns)
      #artisans 21,800
      #workmen 4,300
      #itinerants 8,600
      #servants (inc water carriers) 26,400
      (assign, ":check_for_good_price", 0),
      (str_store_string, s14, "str_i_take_what_work_i_can_sirmadame_i_carry_water_or_help_the_merchants_with_their_loads_or_help_build_things_if_theres_things_to_be_built"),
      
      (call_script, "script_agent_get_town_walker_details", ":agent_no"),
      (assign, ":type", reg0),
      (assign, ":walker_dna", reg1),
      
      (assign, ":item", -1),
      (assign, ":total_item_production", 0),
      (try_for_range, ":trade_good", trade_goods_begin, trade_goods_end),
        (call_script, "script_center_get_production", "$g_encountered_party", ":trade_good"),
        (val_add, ":total_item_production", reg0),
      (try_end),
      
      (val_max, ":total_item_production", 1),
      
      (store_mod, ":semi_random_number", ":walker_dna", ":total_item_production"),
      
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (assign, reg4, ":walker_dna"),
        (assign, reg5, ":total_item_production"),
        (assign, reg7, ":semi_random_number"),
        (display_message, "str_dna_reg4_total_production_reg5_modula_reg7"),
      (try_end),
      
      (try_for_range, ":trade_good", trade_goods_begin, trade_goods_end),
        (gt, ":semi_random_number", -1),
        (call_script, "script_center_get_production", "$g_encountered_party", ":trade_good"),
        (val_sub, ":semi_random_number", reg0),
        (lt, ":semi_random_number", 0),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (str_store_item_name, s9, ":trade_good"),
          (display_message, "str_agent_produces_s9"),
        (try_end),
        (assign, ":item", ":trade_good"),
      (try_end),
      
      
      (try_begin),
        (eq, ":type", walkert_needs_money),
        (is_between, "$g_encountered_party", towns_begin, towns_end),
        (str_store_string, s14, "str_im_not_doing_anything_sirmadame_theres_no_work_to_be_had_around_here_these_days"),
      (else_try),
        (eq, ":type", walkert_needs_money),
        (str_store_string, s14, "str_im_not_doing_anything_sirmadame_i_have_no_land_of_my_own_and_theres_no_work_to_be_had_around_here_these_days"),
      (else_try),
        (eq, ":type", walkert_needs_money_helped),
        (str_store_string, s14, "str_why_im_still_living_off_of_your_kindness_and_goodness_sirmadame_hopefully_there_will_be_work_shortly"),
      (else_try),
        (eq, ":item", "itm_grain"),
        (is_between, "$g_encountered_party", towns_begin, towns_end),
        (str_store_string, s14, "str_i_work_in_the_fields_just_outside_the_walls_where_they_grow_grain_we_dont_quite_grow_enough_to_meet_our_needs_though_and_have_to_import_grain_from_the_surrounding_countryside"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_grain"),
        (str_store_string, s14, "str_i_work_mostly_in_the_fields_growing_grain_in_the_town_they_grind_it_to_make_bread_or_ale_and_we_can_also_boil_it_as_a_porridge"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_ale"),
        (str_store_string, s14, "str_i_work_in_the_breweries_making_ale_the_poor_folk_drink_a_lot_of_it_as_its_cheaper_than_wine_we_make_it_with_grain_brought_in_from_the_countryside"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_bread"),
        (str_store_string, s14, "str_i_work_in_a_mill_grinding_flour_to_make_bread_bread_is_cheap_keeps_well_and_fills_the_stomach"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_dried_meat"),
        (str_store_string, s14, "str_i_tend_cattle_we_dry_and_salt_meat_to_preserve_it_and_make_cheese_from_the_milk"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_cheese"),
        (str_store_string, s14, "str_i_tend_cattle_we_dry_and_salt_meat_to_preserve_it_and_make_cheese_from_the_milk_so_it_doesnt_spoil"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_butter"),
        (str_store_string, s14, "str_i_tend_cattle_we_dry_and_salt_meat_to_preserve_it_and_make_cheese_from_the_milk_so_it_doesnt_spoil"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_wool"),
        (str_store_string, s14, "str_i_tend_sheep_we_send_the_wool_to_the_cities_to_be_woven_into_cloth_and_make_mutton_sausage_when_we_cull_the_herds"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_sausages"),
        (str_store_string, s14, "str_i_tend_sheep_we_send_the_wool_to_the_cities_to_be_woven_into_cloth_and_make_mutton_sausage_when_we_cull_the_herds"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_vc_wool_cloth"),
        (str_store_string, s14, "str_i_work_at_a_loom_spinning_cloth_from_wool_wool_is_some_of_the_cheapest_cloth_you_can_buy_but_it_will_still_keep_you_warm"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_smoked_fish"),
        (str_store_string, s14, "str_i_crew_a_fishing_boat_we_salt_and_smoke_the_flesh_to_sell_it_far_inland"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_salt"),
        (str_store_string, s14, "str_i_sift_salt_from_a_nearby_flat_they_need_salt_everywhere_to_preserve_meat_and_fish"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_iron"),
        (str_store_string, s14, "str_i_mine_iron_from_a_vein_in_a_nearby_cliffside_they_use_it_to_make_tools_arms_and_other_goods"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_jewelry"),
        (str_store_string, s14, "str_i_make_pottery_which_people_use_to_store_grain_and_carry_water"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_tools"),
        (str_store_string, s14, "str_trade_explanation_tools"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_tar"),
        (str_store_string, s14, "str_trade_explanation_tar"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_linen"),
        (str_store_string, s14, "str_trade_explanation_linen"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_silver"),
        (str_store_string, s14, "str_trade_explanation_silver"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_mead"),
        (str_store_string, s14, "str_trade_explanation_mead"),
        (assign, ":check_for_good_price", 1),
        
      (else_try),
        (eq, ":item", "itm_apples"),
        (str_store_string, s14, "str_trade_explanation_apples"),
        (assign, ":check_for_good_price", 1),
        
        #new in VC:
      (else_try),
        (eq, ":item", "itm_ivory"),
        (str_store_string, s14, "str_trade_explanation_ivory"),
        (assign, ":check_for_good_price", 1),
      (else_try),
        (eq, ":item", "itm_barley"),
        (str_store_string, s14, "str_trade_explanation_barley"),
        (assign, ":check_for_good_price", 1),
      (else_try),
        (eq, ":item", "itm_soapstone"),
        (str_store_string, s14, "str_trade_explanation_soapstone"),
        (assign, ":check_for_good_price", 1),
      (else_try),
        (eq, ":item", "itm_venison"),
        (str_store_string, s14, "str_trade_explanation_venison"),
        (assign, ":check_for_good_price", 1),
        
      (try_end),
      
      
      (try_begin),
        (eq, ":check_for_good_price", 1),
        
        (assign, ":trade_destination", -1),
        (store_skill_level, ":trade_skill", "skl_trade", "trp_player"),
        
        (try_begin),
          (is_between, "$g_encountered_party", villages_begin, villages_end),
          (party_get_slot, ":trade_town", "$g_encountered_party", slot_village_market_town),
        (else_try),
          (assign, ":trade_town", "$g_encountered_party"),
        (try_end),
        
        (store_sub, ":item_to_price_slot", slot_town_trade_good_prices_begin, trade_goods_begin),
        (store_add, ":cur_good_price_slot", ":item", ":item_to_price_slot"),
        (party_get_slot, ":score_to_beat", ":trade_town", ":cur_good_price_slot"),
        (val_add, ":score_to_beat", 400),
        (store_mul, ":deduction_for_trade_skill", ":trade_skill", 35),
        (try_begin),
          (is_between, "$g_encountered_party", villages_begin, villages_end),
          (val_add, ":score_to_beat", 200),
        (try_end),
        (val_sub, ":score_to_beat", ":deduction_for_trade_skill"),
        
        (try_for_range, ":trade_route_slot", slot_town_trade_routes_begin, slot_town_trade_routes_end),
          (party_get_slot, ":other_town", ":trade_town", ":trade_route_slot"),
          (party_get_slot, ":price_in_other_town", ":other_town", ":cur_good_price_slot"),
          
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (assign, reg4, ":price_in_other_town"),
            (assign, reg5, ":score_to_beat"),
            (str_store_party_name, s10, ":other_town"),
            (display_message, "str_s10_has_reg4_needs_reg5"),
          (try_end),
          
          (gt, ":price_in_other_town", ":score_to_beat"),
          
          (assign, ":trade_destination", ":other_town"),
          (assign, ":score_to_beat", ":price_in_other_town"),
        (try_end),
        
        (is_between, ":trade_destination", centers_begin, centers_end),
        
        (str_store_party_name, s15, ":trade_destination"),
        (str_store_string, s14, "str_s14_i_hear_that_you_can_find_a_good_price_for_it_in_s15"),
        
        #Reasons -- raw material
        #Reason -- road cut
        #Reason -- villages looted
        
      (try_end),
      
      
  ]),
  
  
  
  
  
  
  
  # script_tick_town_walkers
  # Input: none
  # Output: none
  ("tick_town_walkers",
    [(try_for_agents, ":cur_agent"),
        (neg|agent_slot_ge, ":cur_agent", slot_agent_is_blocked, 1),
        (agent_get_troop_id, ":cur_troop", ":cur_agent"),
        (is_between, ":cur_troop", walkers_begin, walkers_end),
        (agent_get_slot, ":target_entry_point", ":cur_agent", 0),
        (entry_point_get_position, pos1, ":target_entry_point"),
        (try_begin),
          (lt, ":target_entry_point", 32),
          (init_position, pos2),
          (position_set_y, pos2, 250),
          (position_transform_position_to_parent, pos1, pos1, pos2),
        (try_end),
        (agent_get_position, pos2, ":cur_agent"),
        (get_distance_between_positions, ":distance", pos1, pos2),
        (lt, ":distance", 400),
        (assign, ":random_no", 0),
        (try_begin),
          (lt, ":target_entry_point", 32),
          (store_random_in_range, ":random_no", 0, 100),
        (try_end),
        (lt, ":random_no", 20),
        (call_script, "script_set_town_walker_destination", ":cur_agent"),
      (try_end),
  ]),
  
  ####chief cambia townwalrkers routines
  # script_set_town_walker_destination
  # Input: arg1 = agent_no
  # Output: none
  ("set_town_walker_destination",
    [(store_script_param_1, ":agent_no"),
      (store_random_in_range, ":rand_dest", 1, 12),
      
      (try_begin),
        (eq, ":rand_dest", 1),
        (assign, ":target_entry_point", 9),
      (else_try),
        (eq, ":rand_dest", 2),
        (assign, ":target_entry_point", 32),
      (else_try),
        (eq, ":rand_dest", 3),
        (assign, ":target_entry_point", 12),
      (else_try),
        (eq, ":rand_dest", 4),
        (assign, ":target_entry_point", 32),
      (else_try),
        (eq, ":rand_dest", 2),
        (assign, ":target_entry_point", 33),
      (else_try),
        (eq, ":rand_dest", 5),
        (assign, ":target_entry_point", 34),
      (else_try),
        (eq, ":rand_dest", 6),
        (assign, ":target_entry_point", 35),
      (else_try),
        (eq, ":rand_dest", 7),
        (assign, ":target_entry_point", 36),
      (else_try),
        (eq, ":rand_dest", 8),
        (assign, ":target_entry_point", 37),
      (else_try),
        (eq, ":rand_dest", 9),
        (assign, ":target_entry_point", 38),
      (else_try),
        (eq, ":rand_dest", 10),
        (assign, ":target_entry_point", 39),
      (else_try),
        (assign, ":target_entry_point", 12),
      (try_end),
      
      (agent_set_slot, ":agent_no", 0, ":target_entry_point"),
      (entry_point_get_position, pos1, ":target_entry_point"),
      (try_begin),
        (init_position, pos2),
        (position_set_y, pos2, 250),
        (position_transform_position_to_parent, pos1, pos1, pos2),
      (try_end),
      (agent_set_scripted_destination, ":agent_no", pos1, 0),
      (agent_get_troop_id, reg0, ":agent_no"),
      (troop_get_type, ":troop_type", reg0),
      (val_and, ":troop_type", tf_child),
      (try_begin),
        (eq, ":troop_type", tf_child),
        (agent_set_speed_limit, ":agent_no", 10),
        (call_script, "script_advanced_agent_set_speed_modifier",":agent_no", 80),	#VC-1664
      (else_try),
        (agent_set_speed_limit, ":agent_no", 1),
      (try_end),
  ]),
  #chief acaba modificando townwalkers routes
  
  # script_town_init_doors
  # Input: door_state (-1 = closed, 1 = open, 0 = use $town_nighttime)
  # Output: none (required for siege mission templates)
  ("town_init_doors",
    [(store_script_param, ":door_state", 1),
      (try_begin),
        (assign, ":continue", 0),
        (try_begin),
          (eq, ":door_state", 1),
          (assign, ":continue", 1),
        (else_try),
          (eq, ":door_state", 0),
          (eq, "$town_nighttime", 0),
          (assign, ":continue", 1),
        (try_end),
        (eq, ":continue", 1),# open doors
        (assign, ":end_cond", 1),
        (try_for_range, ":i_instance", 0, ":end_cond"),
          (scene_prop_get_instance, ":object", "spr_towngate_door_left", ":i_instance"),
          (ge, ":object", 0),
          (val_add, ":end_cond", 1),
          (prop_instance_get_position, pos1, ":object"),
          (position_rotate_z, pos1, -100),
          (prop_instance_animate_to_position, ":object", pos1, 1),
        (try_end),
        (assign, ":end_cond", 1),
        (try_for_range, ":i_instance", 0, ":end_cond"),
          (scene_prop_get_instance, ":object", "spr_towngate_rectangle_door_left", ":i_instance"),
          (ge, ":object", 0),
          (val_add, ":end_cond", 1),
          (prop_instance_get_position, pos1, ":object"),
          (position_rotate_z, pos1, -80),
          (prop_instance_animate_to_position, ":object", pos1, 1),
        (try_end),
        (assign, ":end_cond", 1),
        (try_for_range, ":i_instance", 0, ":end_cond"),
          (scene_prop_get_instance, ":object", "spr_towngate_door_right", ":i_instance"),
          (ge, ":object", 0),
          (val_add, ":end_cond", 1),
          (prop_instance_get_position, pos1, ":object"),
          (position_rotate_z, pos1, 100),
          (prop_instance_animate_to_position, ":object", pos1, 1),
        (try_end),
        (assign, ":end_cond", 1),
        (try_for_range, ":i_instance", 0, ":end_cond"),
          (scene_prop_get_instance, ":object", "spr_towngate_rectangle_door_right", ":i_instance"),
          (ge, ":object", 0),
          (val_add, ":end_cond", 1),
          (prop_instance_get_position, pos1, ":object"),
          (position_rotate_z, pos1, 80),
          (prop_instance_animate_to_position, ":object", pos1, 1),
        (try_end),
      (try_end),
  ]),
  
  # script_siege_init_ai_and_belfry
  # Input: none
  # Output: none (required for siege mission templates)
  ("siege_init_ai_and_belfry",
    [(assign, "$cur_belfry_pos", 50),
      (assign, ":cur_belfry_object_pos", slot_scene_belfry_props_begin),
      (store_current_scene, ":cur_scene"),
      #Collecting belfry objects
      (try_for_range, ":i_belfry_instance", 0, 3),
        (scene_prop_get_instance, ":belfry_object", "spr_belfry_a", ":i_belfry_instance"),
        (ge, ":belfry_object", 0),
        (scene_set_slot, ":cur_scene", ":cur_belfry_object_pos", ":belfry_object"),
        (val_add, ":cur_belfry_object_pos", 1),
      (try_end),
      (try_for_range, ":i_belfry_instance", 0, 3),
        (scene_prop_get_instance, ":belfry_object", "spr_belfry_platform_a", ":i_belfry_instance"),
        (ge, ":belfry_object", 0),
        (scene_set_slot, ":cur_scene", ":cur_belfry_object_pos", ":belfry_object"),
        (val_add, ":cur_belfry_object_pos", 1),
      (try_end),
      (try_for_range, ":i_belfry_instance", 0, 3),
        (scene_prop_get_instance, ":belfry_object", "spr_belfry_platform_b", ":i_belfry_instance"),
        (ge, ":belfry_object", 0),
        (scene_set_slot, ":cur_scene", ":cur_belfry_object_pos", ":belfry_object"),
        (val_add, ":cur_belfry_object_pos", 1),
      (try_end),
      (assign, "$belfry_rotating_objects_begin", ":cur_belfry_object_pos"),
      (try_for_range, ":i_belfry_instance", 0, 5),
        (scene_prop_get_instance, ":belfry_object", "spr_belfry_wheel", ":i_belfry_instance"),
        (ge, ":belfry_object", 0),
        (scene_set_slot, ":cur_scene", ":cur_belfry_object_pos", ":belfry_object"),
        (val_add, ":cur_belfry_object_pos", 1),
      (try_end),
      (assign, "$last_belfry_object_pos", ":cur_belfry_object_pos"),
      
      #Lifting up the platform  at the beginning
      (try_begin),
        (scene_prop_get_instance, ":belfry_object_to_rotate", "spr_belfry_platform_a", 0),
      (try_end),
      
      #Moving the belfry objects to their starting position
      (entry_point_get_position,pos1,55),
      (entry_point_get_position,pos3,50),
      (try_for_range, ":i_belfry_object_pos", slot_scene_belfry_props_begin, "$last_belfry_object_pos"),
        (assign, ":pos_no", pos_belfry_begin),
        (val_add, ":pos_no", ":i_belfry_object_pos"),
        (val_sub, ":pos_no", slot_scene_belfry_props_begin),
        (scene_get_slot, ":cur_belfry_object", ":cur_scene", ":i_belfry_object_pos"),
        (prop_instance_get_position, pos2, ":cur_belfry_object"),
        (try_begin),
          (eq, ":cur_belfry_object", ":belfry_object_to_rotate"),
          (position_rotate_x, pos2, 90),
        (try_end),
        (position_transform_position_to_local, ":pos_no", pos1, pos2),
        (position_transform_position_to_parent, pos4, pos3, ":pos_no"),
        (prop_instance_animate_to_position, ":cur_belfry_object", pos4, 1),
      (try_end),
      (assign, "$belfry_positioned", 0),
      (assign, "$belfry_num_slots_positioned", 0),
      (assign, "$belfry_num_men_pushing", 0),
      
      (set_show_messages, 0),
      (team_give_order, "$attacker_team", grc_everyone, mordr_stand_ground),
      (team_give_order, "$attacker_team_2", grc_everyone, mordr_stand_ground),
      (set_show_messages, 1),
  ]),
  
  # script_cf_siege_move_belfry
  # Input: none
  # Output: none (required for siege mission templates)
  ("cf_siege_move_belfry",
    [(neq, "$last_belfry_object_pos", slot_scene_belfry_props_begin),
      (entry_point_get_position,pos1,50),
      (entry_point_get_position,pos4,55),
      (get_distance_between_positions, ":total_distance", pos4, pos1),
      (store_current_scene, ":cur_scene"),
      (scene_get_slot, ":first_belfry_object", ":cur_scene", slot_scene_belfry_props_begin),
      (prop_instance_get_position, pos2, ":first_belfry_object"),
      (entry_point_get_position,pos1,"$cur_belfry_pos"),
      (position_transform_position_to_parent, pos3, pos1, pos_belfry_begin),
      (position_transform_position_to_parent, pos5, pos4, pos_belfry_begin),
      (get_distance_between_positions, ":cur_distance", pos2, pos3),
      (get_distance_between_positions, ":distance_left", pos2, pos5),
      (try_begin),
        (le, ":cur_distance", 10),
        (val_add, "$cur_belfry_pos", 1),
        (entry_point_get_position,pos1,"$cur_belfry_pos"),
        (position_transform_position_to_parent, pos3, pos1, pos_belfry_begin),
        (get_distance_between_positions, ":cur_distance", pos2, pos3),
      (try_end),
      (neq, "$cur_belfry_pos", 50),
      
      (assign, ":base_speed", 20),
      (store_div, ":slow_range", ":total_distance", 60),
      (store_sub, ":distance_moved", ":total_distance", ":distance_left"),
      
      (try_begin),
        (lt, ":distance_moved", ":slow_range"),
        (store_mul, ":base_speed", ":distance_moved", -60),
        (val_div, ":base_speed", ":slow_range"),
        (val_add, ":base_speed", 80),
      (else_try),
        (lt, ":distance_left", ":slow_range"),
        (store_mul, ":base_speed", ":distance_left", -60),
        (val_div, ":base_speed", ":slow_range"),
        (val_add, ":base_speed", 80),
      (try_end),
      (store_mul, ":belfry_speed", ":cur_distance", ":base_speed"),
      (try_begin),
        (eq, "$belfry_num_men_pushing", 0),
        (assign, ":belfry_speed", 1000000),
      (else_try),
        (val_div, ":belfry_speed", "$belfry_num_men_pushing"),
      (try_end),
      
      (try_begin),
        (le, "$cur_belfry_pos", 55),
        (init_position, pos3),
        (position_rotate_x, pos3, ":distance_moved"),
        (scene_get_slot, ":base_belfry_object", ":cur_scene", slot_scene_belfry_props_begin),
        (prop_instance_get_position, pos4, ":base_belfry_object"),
        (entry_point_get_position,pos1,"$cur_belfry_pos"),
        (try_for_range, ":i_belfry_object_pos", slot_scene_belfry_props_begin, "$last_belfry_object_pos"),
          (scene_get_slot, ":cur_belfry_object", ":cur_scene", ":i_belfry_object_pos"),
          (try_begin),
            (ge, ":i_belfry_object_pos", "$belfry_rotating_objects_begin"),
            (prop_instance_get_starting_position, pos5, ":base_belfry_object"),
            (prop_instance_get_starting_position, pos6, ":cur_belfry_object"),
            (position_transform_position_to_local, pos7, pos5, pos6),
            (position_transform_position_to_parent, pos5, pos4, pos7),
            (position_transform_position_to_parent, pos6, pos5, pos3),
            (prop_instance_set_position, ":cur_belfry_object", pos6),
          (else_try),
            (assign, ":pos_no", pos_belfry_begin),
            (val_add, ":pos_no", ":i_belfry_object_pos"),
            (val_sub, ":pos_no", slot_scene_belfry_props_begin),
            (position_transform_position_to_parent, pos2, pos1, ":pos_no"),
            (prop_instance_animate_to_position, ":cur_belfry_object", pos2, ":belfry_speed"),
          (try_end),
        (try_end),
      (try_end),
      (gt, "$cur_belfry_pos", 55),
      (assign, "$belfry_positioned", 1),
  ]),
  
  # script_cf_siege_rotate_belfry_platform
  # Input: none
  # Output: none (required for siege mission templates)
  ("cf_siege_rotate_belfry_platform",
    [(eq, "$belfry_positioned", 1),
      (scene_prop_get_instance, ":belfry_object", "spr_belfry_platform_a", 0),
      (prop_instance_get_position, pos1, ":belfry_object"),
      (position_rotate_x, pos1, -90),
      (prop_instance_animate_to_position, ":belfry_object", pos1, 400),
      (assign, "$belfry_positioned", 2),
  ]),
  
  # script_cf_siege_assign_men_to_belfry
  # Input: none
  # Output: none (required for siege mission templates)
  ("cf_siege_assign_men_to_belfry",
    [
      ##    (store_mission_timer_a, ":cur_seconds"),
      (neq, "$last_belfry_object_pos", slot_scene_belfry_props_begin),
      (assign, ":end_trigger", 0),
      (try_begin),
        (lt, "$belfry_positioned", 3),
        (get_player_agent_no, ":player_agent"),
        (store_current_scene, ":cur_scene"),
        (scene_get_slot, ":first_belfry_object", ":cur_scene", slot_scene_belfry_props_begin),
        (prop_instance_get_position, pos2, ":first_belfry_object"),
        (assign, ":slot_1_positioned", 0),
        (assign, ":slot_2_positioned", 0),
        (assign, ":slot_3_positioned", 0),
        (assign, ":slot_4_positioned", 0),
        (assign, ":slot_5_positioned", 0),
        (assign, ":slot_6_positioned", 0),
        (assign, "$belfry_num_slots_positioned", 0),
        (assign, "$belfry_num_men_pushing", 0),
        (try_for_agents, ":cur_agent"),
          (agent_is_alive, ":cur_agent"),
          (agent_is_human, ":cur_agent"),
          (try_begin),
            (agent_get_slot, ":x_pos", ":cur_agent", slot_agent_target_x_pos),
            (neq, ":x_pos", 0),
            (agent_get_slot, ":y_pos", ":cur_agent", slot_agent_target_y_pos),
            (try_begin),
              (eq, ":x_pos", -600),
              (try_begin),
                (eq, ":y_pos", 0),
                (assign, ":slot_1_positioned", 1),
              (else_try),
                (eq, ":y_pos", -200),
                (assign, ":slot_2_positioned", 1),
              (else_try),
                (assign, ":slot_3_positioned", 1),
              (try_end),
            (else_try),
              (try_begin),
                (eq, ":y_pos", 0),
                (assign, ":slot_4_positioned", 1),
              (else_try),
                (eq, ":y_pos", -200),
                (assign, ":slot_5_positioned", 1),
              (else_try),
                (assign, ":slot_6_positioned", 1),
              (try_end),
            (try_end),
            (val_add, "$belfry_num_slots_positioned", 1),
            (init_position, pos1),
            (position_move_x, pos1, ":x_pos"),
            (position_move_y, pos1, ":y_pos"),
            (init_position, pos3),
            (position_move_x, pos3, ":x_pos"),
            (position_move_y, pos3, -1000),
            (position_transform_position_to_parent, pos4, pos2, pos1),
            (position_transform_position_to_parent, pos5, pos2, pos3),
            (agent_get_position, pos6, ":cur_agent"),
            (get_distance_between_positions, ":target_distance", pos6, pos4),
            (get_distance_between_positions, ":waypoint_distance", pos6, pos5),
            (try_begin),
              (this_or_next|lt, ":target_distance", ":waypoint_distance"),
              (lt, ":waypoint_distance", 600),
              (agent_set_scripted_destination, ":cur_agent", pos4, 1),
            (else_try),
              (agent_set_scripted_destination, ":cur_agent", pos5, 1),
            (try_end),
            (try_begin),
              (le, ":target_distance", 300),
              (val_add, "$belfry_num_men_pushing", 1),
            (try_end),
            ##        (else_try),
            ##          (agent_get_team, ":cur_agent_team", ":cur_agent"),
            ##          (this_or_next|eq, "$attacker_team", ":cur_agent_team"),
            ##          (             eq, "$attacker_team_2", ":cur_agent_team"),
            ##          (try_begin),
            ##            (gt, ":cur_seconds", 20),
            ##            (agent_get_position, pos1, ":cur_agent"),
            ##            (agent_set_scripted_destination, ":cur_agent", pos1, 0),
            ##          (else_try),
            ##            (try_begin),
            ##              (team_get_movement_order, ":order1", "$attacker_team", grc_infantry),
            ##              (team_get_movement_order, ":order2", "$attacker_team", grc_cavalry),
            ##              (team_get_movement_order, ":order3", "$attacker_team", grc_archers),
            ##              (this_or_next|neq, ":order1", mordr_stand_ground),
            ##              (this_or_next|neq, ":order2", mordr_stand_ground),
            ##              (neq, ":order3", mordr_stand_ground),
            ##              (set_show_messages, 0),
            ##              (team_give_order, "$attacker_team", grc_everyone, mordr_stand_ground),
            ##              (set_show_messages, 1),
            ##            (try_end),
            ##          (try_end),
          (try_end),
        (try_end),
        (try_begin),
          (lt, "$belfry_num_slots_positioned", 6),
          (try_for_agents, ":cur_agent"),
            (agent_is_alive, ":cur_agent"),
            (agent_get_team, ":cur_agent_team", ":cur_agent"),
            (this_or_next|eq, "$attacker_team", ":cur_agent_team"),
            (eq, "$attacker_team_2", ":cur_agent_team"),
            (neq, ":player_agent", ":cur_agent"),
            (agent_get_class, ":agent_class", ":cur_agent"),
            (this_or_next|eq, ":agent_class", grc_infantry),
            (eq, ":agent_class", grc_cavalry),
            (agent_get_slot, ":x_pos", ":cur_agent", 1),
            (eq, ":x_pos", 0),
            (assign, ":y_pos", 0),
            (try_begin),
              (eq, ":slot_1_positioned", 0),
              (assign, ":x_pos", -600),
              (assign, ":y_pos", 0),
              (val_add, ":slot_1_positioned", 1),
            (else_try),
              (eq, ":slot_2_positioned", 0),
              (assign, ":x_pos", -600),
              (assign, ":y_pos", -200),
              (val_add, ":slot_2_positioned", 1),
            (else_try),
              (eq, ":slot_3_positioned", 0),
              (assign, ":x_pos", -600),
              (assign, ":y_pos", -400),
              (val_add, ":slot_3_positioned", 1),
            (else_try),
              (eq, ":slot_4_positioned", 0),
              (assign, ":x_pos", 600),
              (assign, ":y_pos", 0),
              (val_add, ":slot_4_positioned", 1),
            (else_try),
              (eq, ":slot_5_positioned", 0),
              (assign, ":x_pos", 600),
              (assign, ":y_pos", -200),
              (val_add, ":slot_5_positioned", 1),
            (else_try),
              (eq, ":slot_6_positioned", 0),
              (assign, ":x_pos", 600),
              (assign, ":y_pos", -400),
              (val_add, ":slot_6_positioned", 1),
            (try_end),
            (val_add, "$belfry_num_slots_positioned", 1),
            (agent_set_slot, ":cur_agent", 1, ":x_pos"),
            (agent_set_slot, ":cur_agent", 2, ":y_pos"),
          (try_end),
        (try_end),
        (try_begin),
          (store_mission_timer_a, ":cur_timer"),
          (gt, ":cur_timer", 20),
          (lt, "$belfry_num_slots_positioned", 6),
          (try_for_agents, ":cur_agent"),
            (agent_is_alive, ":cur_agent"),
            (agent_get_team, ":cur_agent_team", ":cur_agent"),
            (this_or_next|eq, "$attacker_team", ":cur_agent_team"),
            (             eq, "$attacker_team_2", ":cur_agent_team"),
            (neq, ":player_agent", ":cur_agent"),
            (agent_get_slot, ":x_pos", ":cur_agent", 1),
            (eq, ":x_pos", 0),
            (assign, ":y_pos", 0),
            (try_begin),
              (eq, ":slot_1_positioned", 0),
              (assign, ":x_pos", -600),
              (assign, ":y_pos", 0),
              (val_add, ":slot_1_positioned", 1),
            (else_try),
              (eq, ":slot_2_positioned", 0),
              (assign, ":x_pos", -600),
              (assign, ":y_pos", -200),
              (val_add, ":slot_2_positioned", 1),
            (else_try),
              (eq, ":slot_3_positioned", 0),
              (assign, ":x_pos", -600),
              (assign, ":y_pos", -400),
              (val_add, ":slot_3_positioned", 1),
            (else_try),
              (eq, ":slot_4_positioned", 0),
              (assign, ":x_pos", 600),
              (assign, ":y_pos", 0),
              (val_add, ":slot_4_positioned", 1),
            (else_try),
              (eq, ":slot_5_positioned", 0),
              (assign, ":x_pos", 600),
              (assign, ":y_pos", -200),
              (val_add, ":slot_5_positioned", 1),
            (else_try),
              (eq, ":slot_6_positioned", 0),
              (assign, ":x_pos", 600),
              (assign, ":y_pos", -400),
              (val_add, ":slot_6_positioned", 1),
            (try_end),
            (val_add, "$belfry_num_slots_positioned", 1),
            (agent_set_slot, ":cur_agent", 1, ":x_pos"),
            (agent_set_slot, ":cur_agent", 2, ":y_pos"),
          (try_end),
        (try_end),
      (else_try),
        (assign, ":end_trigger", 1),
        (try_for_agents, ":cur_agent"),
          (agent_clear_scripted_mode, ":cur_agent"),
        (try_end),
        (set_show_messages, 0),
        (team_give_order, "$attacker_team", grc_everyone, mordr_charge),
        (set_show_messages, 1),
      (try_end),
      (eq, ":end_trigger", 1),
  ]),
  
  # script_siege_move_archers_to_archer_positions
  # Input: none
  # Output: none
  ("siege_move_archers_to_archer_positions",
    [
      (try_for_agents, ":agent_no"),
        (agent_is_alive, ":agent_no"),
        (agent_slot_eq, ":agent_no", slot_agent_is_not_reinforcement, 0),
        (agent_is_defender, ":agent_no"),
        (agent_get_class, ":agent_class", ":agent_no"),
        #       (agent_get_troop_id, ":agent_troop", ":agent_no"),
        (eq, ":agent_class", grc_archers),
        (try_begin),
          (agent_slot_eq, ":agent_no", slot_agent_target_entry_point, 0),
          (store_random_in_range, ":random_entry_point", 40, 44),
          (agent_set_slot, ":agent_no", slot_agent_target_entry_point, ":random_entry_point"),
        (try_end),
        (try_begin),
          (agent_get_position, pos0, ":agent_no"),
          (entry_point_get_position, pos1, ":random_entry_point"),
          (get_distance_between_positions, ":dist", pos0, pos1),
          (lt, ":dist", 300),
          (agent_clear_scripted_mode, ":agent_no"),
          (agent_set_slot, ":agent_no", slot_agent_is_in_scripted_mode, 0),
          (agent_set_slot, ":agent_no", slot_agent_is_not_reinforcement, 1),
          #         (str_store_troop_name, s1, ":agent_troop"),
          #         (assign, reg0, ":agent_no"),
          #         (display_message, "@{s1} ({reg0}) reached pos"),
        (else_try),
          (agent_get_simple_behavior, ":agent_sb", ":agent_no"),
          (agent_get_combat_state, ":agent_cs", ":agent_no"),
          (this_or_next|eq, ":agent_sb", aisb_ranged),
          (eq, ":agent_sb", aisb_go_to_pos),#scripted mode
          (eq, ":agent_cs", 7), # 7 = no visible targets (state for ranged units)
          (try_begin),
            (agent_slot_eq, ":agent_no", slot_agent_is_in_scripted_mode, 0),
            (agent_set_scripted_destination, ":agent_no", pos1, 0),
            (agent_set_slot, ":agent_no", slot_agent_is_in_scripted_mode, 1),
            #           (str_store_troop_name, s1, ":agent_troop"),
            #           (assign, reg0, ":agent_no"),
            #           (display_message, "@{s1} ({reg0}) moving to pos"),
          (try_end),
        (else_try),
          (agent_slot_eq, ":agent_no", slot_agent_is_in_scripted_mode, 1),
          (agent_clear_scripted_mode, ":agent_no"),
          (agent_set_slot, ":agent_no", slot_agent_is_in_scripted_mode, 0),
          #        (str_store_troop_name, s1, ":agent_troop"),
          #        (assign, reg0, ":agent_no"),
          #        (display_message, "@{s1} ({reg0}) seeing target or changed mode"),
        (try_end),
      (try_end),
  ]),
  
  
  # script_store_movement_order_name_to_s1
  # Input: arg1 = team_no, arg2 = class_no
  # Output: s1 = order_name
  ("store_movement_order_name_to_s1",
    [(store_script_param_1, ":team_no"),
      (store_script_param_2, ":class_no"),
      (team_get_movement_order, ":cur_order", ":team_no", ":class_no"),
      (try_begin),
        (store_add, ":slot", slot_team_d0_move_order, ":class_no"),
        (team_slot_eq, ":team_no", ":slot", mordr_follow),  #system uses repeated holds to have divisions follow at an offset
        (str_store_string, s1, "@Following"),
      (else_try),
        (eq, ":cur_order", mordr_hold),
        (str_store_string, s1, "@Holding"),
      (else_try),
        (eq, ":cur_order", mordr_follow),
        (str_store_string, s1, "@Following"),
      (else_try),
        (eq, ":cur_order", mordr_charge),
        (str_store_string, s1, "@Charging"),
      (else_try),
        (eq, ":cur_order", mordr_advance),
        (str_store_string, s1, "@Advancing"),
      (else_try),
        (eq, ":cur_order", mordr_fall_back),
        (str_store_string, s1, "@Falling Back"),
      (else_try),
        (eq, ":cur_order", mordr_stand_closer),
        (str_store_string, s1, "@Standing Closer"),
      (else_try),
        (eq, ":cur_order", mordr_spread_out),
        (str_store_string, s1, "@Spreading Out"),
      (else_try),
        (eq, ":cur_order", mordr_stand_ground),
        (str_store_string, s1, "@Standing"),
      (else_try),
        (eq, ":cur_order", mordr_retreat),
        (str_store_string, s1, "@Retreating"),
      (else_try),
        (str_store_string, s1, "@N/A"),
      (try_end),
  ]),
  
  # script_store_riding_order_name_to_s1
  # Input: arg1 = team_no, arg2 = class_no
  # Output: s1 = order_name
  ("store_riding_order_name_to_s1",
    [(store_script_param_1, ":team_no"),
      (store_script_param_2, ":class_no"),
      (team_get_riding_order, ":cur_order", ":team_no", ":class_no"),
      (try_begin),
        (eq, ":cur_order", rordr_free),
        (str_store_string, s1, "@Free"),
      (else_try),
        (eq, ":cur_order", rordr_mount),
        (str_store_string, s1, "@Mount"),
      (else_try),
        (eq, ":cur_order", rordr_dismount),
        (str_store_string, s1, "@Dismount"),
      (else_try),
        (str_store_string, s1, "@N/A"),
      (try_end),
  ]),
  
  # script_store_weapon_usage_order_name_to_s1
  # Input: arg1 = team_no, arg2 = class_no
  # Output: s1 = order_name
  ("store_weapon_usage_order_name_to_s1",
    [(store_script_param_1, ":team_no"),
      (store_script_param_2, ":class_no"),
      (team_get_weapon_usage_order, ":cur_order", ":team_no", ":class_no"),
      (team_get_hold_fire_order, ":cur_hold_fire", ":team_no", ":class_no"),
      (try_begin),
        (eq, ":cur_order", wordr_use_any_weapon),
        (eq, ":cur_hold_fire", aordr_fire_at_will),
        (str_store_string, s1, "@Any Weapon"),
      (else_try),
        (eq, ":cur_order", wordr_use_blunt_weapons),
        (eq, ":cur_hold_fire", aordr_fire_at_will),
        (str_store_string, s1, "@Blunt Weapons"),
      (else_try),
        (eq, ":cur_order", wordr_use_any_weapon),
        (eq, ":cur_hold_fire", aordr_hold_your_fire),
        (str_store_string, s1, "str_hold_fire"),
      (else_try),
        (eq, ":cur_order", wordr_use_blunt_weapons),
        (eq, ":cur_hold_fire", aordr_hold_your_fire),
        (str_store_string, s1, "str_blunt_hold_fire"),
      (else_try),
        (str_store_string, s1, "@N/A"),
      (try_end),
  ]),
  
  # script_team_give_order_from_order_panel
  # Input: arg1 = leader_agent_no, arg2 = class_no
  # Output: none
  ("team_give_order_from_order_panel",
    [(store_script_param_1, ":leader_agent_no"),
      (store_script_param_2, ":order"),
      (agent_get_team, ":team_no", ":leader_agent_no"),
      (set_show_messages, 0),
      (try_begin),
        (eq, "$g_formation_group0_selected", 1),
        (team_give_order, ":team_no", 0, ":order"),
      (try_end),
      (try_begin),
        (eq, "$g_formation_group1_selected", 1),
        (team_give_order, ":team_no", 1, ":order"),
      (try_end),
      (try_begin),
        (eq, "$g_formation_group2_selected", 1),
        (team_give_order, ":team_no", 2, ":order"),
      (try_end),
      (try_begin),
        (eq, "$g_formation_group3_selected", 1),
        (team_give_order, ":team_no", 3, ":order"),
      (try_end),
      (try_begin),
        (eq, "$g_formation_group4_selected", 1),
        (team_give_order, ":team_no", 4, ":order"),
      (try_end),
      (try_begin),
        (eq, "$g_formation_group5_selected", 1),
        (team_give_order, ":team_no", 5, ":order"),
      (try_end),
      (try_begin),
        (eq, "$g_formation_group6_selected", 1),
        (team_give_order, ":team_no", 6, ":order"),
      (try_end),
      (try_begin),
        (eq, "$g_formation_group7_selected", 1),
        (team_give_order, ":team_no", 7, ":order"),
      (try_end),
      (try_begin),
        (eq, "$g_formation_group8_selected", 1),
        (team_give_order, ":team_no", 8, ":order"),
      (try_end),
      
      (try_begin),
        (eq, ":order", mordr_hold),
        (agent_get_position, pos1, ":leader_agent_no"),
        (try_begin),
          (eq, "$g_formation_group0_selected", 1),
          (team_set_order_position, ":team_no", 0, pos1),
        (try_end),
        (try_begin),
          (eq, "$g_formation_group1_selected", 1),
          (team_set_order_position, ":team_no", 1, pos1),
        (try_end),
        (try_begin),
          (eq, "$g_formation_group2_selected", 1),
          (team_set_order_position, ":team_no", 2, pos1),
        (try_end),
        (try_begin),
          (eq, "$g_formation_group3_selected", 1),
          (team_set_order_position, ":team_no", 3, pos1),
        (try_end),
        (try_begin),
          (eq, "$g_formation_group4_selected", 1),
          (team_set_order_position, ":team_no", 4, pos1),
        (try_end),
        (try_begin),
          (eq, "$g_formation_group5_selected", 1),
          (team_set_order_position, ":team_no", 5, pos1),
        (try_end),
        (try_begin),
          (eq, "$g_formation_group6_selected", 1),
          (team_set_order_position, ":team_no", 6, pos1),
        (try_end),
        (try_begin),
          (eq, "$g_formation_group7_selected", 1),
          (team_set_order_position, ":team_no", 7, pos1),
        (try_end),
        (try_begin),
          (eq, "$g_formation_group8_selected", 1),
          (team_set_order_position, ":team_no", 8, pos1),
        (try_end),
      (try_end),
      (call_script, "script_player_order_formations", ":order"),	#for formations motomataru chief
      (set_show_messages, 1),
  ]),
  
  
  # script_update_order_panel
  # Input: arg1 = team_no
  # Output: none
  ("update_order_panel",
    [(store_script_param_1, ":team_no"),
      (set_fixed_point_multiplier, 1000),
      
      #ozan added
      (try_begin),
        (eq, "$group0_has_troops", 1),
        (call_script, "script_store_movement_order_name_to_s1", ":team_no", 0),
        (overlay_set_text, "$g_presentation_but0_movement", s1),
        (call_script, "script_store_riding_order_name_to_s1", ":team_no", 0),
        (overlay_set_text, "$g_presentation_but0_riding", s1),
        (call_script, "script_store_weapon_usage_order_name_to_s1", ":team_no", 0),
        (overlay_set_text, "$g_presentation_but0_weapon_usage", s1),
      (try_end),
      (try_begin),
        (eq, "$group1_has_troops", 1),
        (call_script, "script_store_movement_order_name_to_s1", ":team_no", 1),
        (overlay_set_text, "$g_presentation_but1_movement", s1),
        (call_script, "script_store_riding_order_name_to_s1", ":team_no", 1),
        (overlay_set_text, "$g_presentation_but1_riding", s1),
        (call_script, "script_store_weapon_usage_order_name_to_s1", ":team_no", 1),
        (overlay_set_text, "$g_presentation_but1_weapon_usage", s1),
      (try_end),
      (try_begin),
        (eq, "$group2_has_troops", 1),
        (call_script, "script_store_movement_order_name_to_s1", ":team_no", 2),
        (overlay_set_text, "$g_presentation_but2_movement", s1),
        (call_script, "script_store_riding_order_name_to_s1", ":team_no", 2),
        (overlay_set_text, "$g_presentation_but2_riding", s1),
        (call_script, "script_store_weapon_usage_order_name_to_s1", ":team_no", 2),
        (overlay_set_text, "$g_presentation_but2_weapon_usage", s1),
      (try_end),
      (try_begin),
        (eq, "$group3_has_troops", 1),
        (call_script, "script_store_movement_order_name_to_s1", ":team_no", 3),
        (overlay_set_text, "$g_presentation_but3_movement", s1),
        (call_script, "script_store_riding_order_name_to_s1", ":team_no", 3),
        (overlay_set_text, "$g_presentation_but3_riding", s1),
        (call_script, "script_store_weapon_usage_order_name_to_s1", ":team_no", 3),
        (overlay_set_text, "$g_presentation_but3_weapon_usage", s1),
      (try_end),
      (try_begin),
        (eq, "$group4_has_troops", 1),
        (call_script, "script_store_movement_order_name_to_s1", ":team_no", 4),
        (overlay_set_text, "$g_presentation_but4_movement", s1),
        (call_script, "script_store_riding_order_name_to_s1", ":team_no", 4),
        (overlay_set_text, "$g_presentation_but4_riding", s1),
        (call_script, "script_store_weapon_usage_order_name_to_s1", ":team_no", 4),
        (overlay_set_text, "$g_presentation_but4_weapon_usage", s1),
      (try_end),
      (try_begin),
        (eq, "$group5_has_troops", 1),
        (call_script, "script_store_movement_order_name_to_s1", ":team_no", 5),
        (overlay_set_text, "$g_presentation_but5_movement", s1),
        (call_script, "script_store_riding_order_name_to_s1", ":team_no", 5),
        (overlay_set_text, "$g_presentation_but5_riding", s1),
        (call_script, "script_store_weapon_usage_order_name_to_s1", ":team_no", 5),
        (overlay_set_text, "$g_presentation_but5_weapon_usage", s1),
      (try_end),
      (try_begin),
        (eq, "$group6_has_troops", 1),
        (call_script, "script_store_movement_order_name_to_s1", ":team_no", 6),
        (overlay_set_text, "$g_presentation_but6_movement", s1),
        (call_script, "script_store_riding_order_name_to_s1", ":team_no", 6),
        (overlay_set_text, "$g_presentation_but6_riding", s1),
        (call_script, "script_store_weapon_usage_order_name_to_s1", ":team_no", 6),
        (overlay_set_text, "$g_presentation_but6_weapon_usage", s1),
      (try_end),
      (try_begin),
        (eq, "$group7_has_troops", 1),
        (call_script, "script_store_movement_order_name_to_s1", ":team_no", 7),
        (overlay_set_text, "$g_presentation_but7_movement", s1),
        (call_script, "script_store_riding_order_name_to_s1", ":team_no", 7),
        (overlay_set_text, "$g_presentation_but7_riding", s1),
        (call_script, "script_store_weapon_usage_order_name_to_s1", ":team_no", 7),
        (overlay_set_text, "$g_presentation_but7_weapon_usage", s1),
      (try_end),
      (try_begin),
        (eq, "$group8_has_troops", 1),
        (call_script, "script_store_movement_order_name_to_s1", ":team_no", 8),
        (overlay_set_text, "$g_presentation_but8_movement", s1),
        (call_script, "script_store_riding_order_name_to_s1", ":team_no", 8),
        (overlay_set_text, "$g_presentation_but8_riding", s1),
        (call_script, "script_store_weapon_usage_order_name_to_s1", ":team_no", 8),
        (overlay_set_text, "$g_presentation_but8_weapon_usage", s1),
      (try_end),
      
      #ozan added
      (assign, ":stat_position_y", 660),
      (try_begin),
        (position_set_y, pos1, ":stat_position_y"),
        (eq, "$group0_has_troops", 1),
        (position_set_x, pos1, 270),
        (overlay_set_position, "$g_presentation_but0_movement", pos1),
        (position_set_x, pos1, 410),
        (overlay_set_position, "$g_presentation_but0_riding", pos1),
        (position_set_x, pos1, 550),
        (overlay_set_position, "$g_presentation_but0_weapon_usage", pos1),
        (val_add, ":stat_position_y", -40),
      (try_end),
      (try_begin),
        (position_set_y, pos1, ":stat_position_y"),
        (eq, "$group1_has_troops", 1),
        (position_set_x, pos1, 270),
        (overlay_set_position, "$g_presentation_but1_movement", pos1),
        (position_set_x, pos1, 410),
        (overlay_set_position, "$g_presentation_but1_riding", pos1),
        (position_set_x, pos1, 550),
        (overlay_set_position, "$g_presentation_but1_weapon_usage", pos1),
        (val_add, ":stat_position_y", -40),
      (try_end),
      (try_begin),
        (position_set_y, pos1, ":stat_position_y"),
        (eq, "$group2_has_troops", 1),
        (position_set_x, pos1, 270),
        (overlay_set_position, "$g_presentation_but2_movement", pos1),
        (position_set_x, pos1, 410),
        (overlay_set_position, "$g_presentation_but2_riding", pos1),
        (position_set_x, pos1, 550),
        (overlay_set_position, "$g_presentation_but2_weapon_usage", pos1),
        (val_add, ":stat_position_y", -40),
      (try_end),
      (try_begin),
        (position_set_y, pos1, ":stat_position_y"),
        (eq, "$group3_has_troops", 1),
        (position_set_x, pos1, 270),
        (overlay_set_position, "$g_presentation_but3_movement", pos1),
        (position_set_x, pos1, 410),
        (overlay_set_position, "$g_presentation_but3_riding", pos1),
        (position_set_x, pos1, 550),
        (overlay_set_position, "$g_presentation_but3_weapon_usage", pos1),
        (val_add, ":stat_position_y", -40),
      (try_end),
      (try_begin),
        (position_set_y, pos1, ":stat_position_y"),
        (eq, "$group4_has_troops", 1),
        (position_set_x, pos1, 270),
        (overlay_set_position, "$g_presentation_but4_movement", pos1),
        (position_set_x, pos1, 410),
        (overlay_set_position, "$g_presentation_but4_riding", pos1),
        (position_set_x, pos1, 550),
        (overlay_set_position, "$g_presentation_but4_weapon_usage", pos1),
        (val_add, ":stat_position_y", -40),
      (try_end),
      (try_begin),
        (position_set_y, pos1, ":stat_position_y"),
        (eq, "$group5_has_troops", 1),
        (position_set_x, pos1, 270),
        (overlay_set_position, "$g_presentation_but5_movement", pos1),
        (position_set_x, pos1, 410),
        (overlay_set_position, "$g_presentation_but5_riding", pos1),
        (position_set_x, pos1, 550),
        (overlay_set_position, "$g_presentation_but5_weapon_usage", pos1),
        (val_add, ":stat_position_y", -40),
      (try_end),
      (try_begin),
        (position_set_y, pos1, ":stat_position_y"),
        (eq, "$group6_has_troops", 1),
        (position_set_x, pos1, 270),
        (overlay_set_position, "$g_presentation_but6_movement", pos1),
        (position_set_x, pos1, 410),
        (overlay_set_position, "$g_presentation_but6_riding", pos1),
        (position_set_x, pos1, 550),
        (overlay_set_position, "$g_presentation_but6_weapon_usage", pos1),
        (val_add, ":stat_position_y", -40),
      (try_end),
      (try_begin),
        (position_set_y, pos1, ":stat_position_y"),
        (eq, "$group7_has_troops", 1),
        (position_set_x, pos1, 270),
        (overlay_set_position, "$g_presentation_but7_movement", pos1),
        (position_set_x, pos1, 410),
        (overlay_set_position, "$g_presentation_but7_riding", pos1),
        (position_set_x, pos1, 550),
        (overlay_set_position, "$g_presentation_but7_weapon_usage", pos1),
        (val_add, ":stat_position_y", -40),
      (try_end),
      (try_begin),
        (position_set_y, pos1, ":stat_position_y"),
        (eq, "$group8_has_troops", 1),
        (position_set_x, pos1, 270),
        (overlay_set_position, "$g_presentation_but8_movement", pos1),
        (position_set_x, pos1, 410),
        (overlay_set_position, "$g_presentation_but8_riding", pos1),
        (position_set_x, pos1, 550),
        (overlay_set_position, "$g_presentation_but8_weapon_usage", pos1),
        (val_add, ":stat_position_y", -40),
      (try_end),
  ]),
  
  # script_update_agent_position_on_map
  # Input: arg1 = agent_no, pos2 = map_size_pos
  # Output: none
  ("update_agent_position_on_map",
    [(store_script_param_1, ":agent_no"),
      (agent_get_slot, ":agent_overlay", ":agent_no", slot_agent_map_overlay_id),
      
      (get_player_agent_no, ":player_agent"),
      (agent_get_team, ":player_team", ":player_agent"),
      (try_begin),
        (le, ":agent_overlay", 0),
        (set_fixed_point_multiplier, 1000),
        (try_begin),
          (eq, ":agent_no", ":player_agent"),
          (create_mesh_overlay, reg1, "mesh_player_dot"),
          (position_set_x, pos1, 400),
          (position_set_y, pos1, 400),
          (overlay_set_size, reg1, pos1),
          (overlay_set_additional_render_height, reg1, 10),
        (else_try),
          (create_mesh_overlay, reg1, "mesh_white_dot"),
          (position_set_x, pos1, 200),
          (position_set_y, pos1, 200),
          (overlay_set_size, reg1, pos1),
        (try_end),
        (overlay_set_alpha, reg1, 0x88),
        (agent_set_slot, ":agent_no", slot_agent_map_overlay_id, reg1),
        (assign, ":agent_overlay", reg1),
      (try_end),
      
      (try_begin),
        (neq, ":agent_no", ":player_agent"),
        # (agent_get_party_id, ":agent_party", ":agent_no"),
        (agent_get_team, ":agent_team", ":agent_no"),
        (try_begin),
          # (eq, ":agent_party", "p_main_party"),
          (eq, ":agent_team", ":player_team"),
          
          (agent_get_division, ":agent_division", ":agent_no"),
          (try_begin),
            (eq, ":agent_division", 0),
            (overlay_set_color, ":agent_overlay", 0x8d5220),
          (else_try),
            (eq, ":agent_division", 1),
            (overlay_set_color, ":agent_overlay", 0x34c6e4),
          (else_try),
            (eq, ":agent_division", 2),
            (overlay_set_color, ":agent_overlay", 0x569619),
          (else_try),
            (eq, ":agent_division", 3),
            (overlay_set_color, ":agent_overlay", 0xFFE500),
          (else_try),
            (eq, ":agent_division", 4),
            (overlay_set_color, ":agent_overlay", 0x990099),
          (else_try),
            (eq, ":agent_division", 5),
            (overlay_set_color, ":agent_overlay", 0x99FE80),
          (else_try),
            (eq, ":agent_division", 6),
            (overlay_set_color, ":agent_overlay", 0x9DEFFE),
          (else_try),
            (eq, ":agent_division", 7),
            (overlay_set_color, ":agent_overlay", 0xFECB9D),
          (else_try),
            (eq, ":agent_division", 8),
            (overlay_set_color, ":agent_overlay", 0xB19C9C),
          (try_end),
        (else_try),
          (agent_is_ally, ":agent_no"),
          (overlay_set_color, ":agent_overlay", 0x5555FF),
        (else_try),
          (overlay_set_color, ":agent_overlay", 0xFF0000),
        (try_end),
      (try_end),
      
      (try_begin),
        (eq, ":agent_no", ":player_agent"),
        (agent_get_look_position, pos1, ":agent_no"),
        (position_get_rotation_around_z, ":rot", pos1),
        (init_position, pos10),
        (position_rotate_z, pos10, ":rot"),
        (overlay_set_mesh_rotation, ":agent_overlay", pos10),
        (call_script, "script_convert_3d_pos_to_map_pos"),
      (else_try),
        (agent_get_position, pos1, ":agent_no"),
        (call_script, "script_convert_3d_pos_to_map_pos"),
      (try_end),
      (overlay_set_position, ":agent_overlay", pos0),
  ]),
  
  # script_convert_3d_pos_to_map_pos
  # Input: pos1 = 3d_pos, pos2 = map_size_pos
  # Output: pos0 = map_pos
  ("convert_3d_pos_to_map_pos",
    [(set_fixed_point_multiplier, 1000),
      (position_transform_position_to_local, pos3, pos2, pos1),
      (position_get_x, ":agent_x_pos", pos3),
      (position_get_y, ":agent_y_pos", pos3),
      (val_div, ":agent_x_pos", "$g_battle_map_scale"),
      (val_div, ":agent_y_pos", "$g_battle_map_scale"),
      (set_fixed_point_multiplier, 1000),
      (store_sub, ":map_x", 980, "$g_battle_map_width"),
      (store_sub, ":map_y", 730, "$g_battle_map_height"),
      (val_add, ":agent_x_pos", ":map_x"),
      (val_add, ":agent_y_pos", ":map_y"),
      (position_set_x, pos0, ":agent_x_pos"),
      (position_set_y, pos0, ":agent_y_pos"),
  ]),
  
  # script_update_order_flags_on_map
  # Input: none
  # Output: none
  ("update_order_flags_on_map",
    [(set_fixed_point_multiplier, 1000),
      (get_player_agent_no, ":player_agent"),
      (agent_get_team, ":player_team", ":player_agent"),
      
      (get_scene_boundaries, pos2, pos3),
      
      (team_get_movement_order, ":cur_order", ":player_team", grc_infantry),
      (try_begin),
        (eq, ":cur_order", mordr_hold),
        (eq, "$group0_has_troops", 1),
        (team_get_order_position, pos1, ":player_team", grc_infantry),
        (call_script, "script_convert_3d_pos_to_map_pos"),
        (overlay_set_alpha, "$g_battle_map_infantry_order_flag", 0xFF),
        (overlay_set_position, "$g_battle_map_infantry_order_flag", pos0),
      (else_try),
        (overlay_set_alpha, "$g_battle_map_infantry_order_flag", 0),
      (try_end),
      (team_get_movement_order, ":cur_order", ":player_team", grc_archers),
      (try_begin),
        (eq, ":cur_order", mordr_hold),
        (eq, "$group1_has_troops", 1),
        (team_get_order_position, pos1, ":player_team", grc_archers),
        (call_script, "script_convert_3d_pos_to_map_pos"),
        (overlay_set_alpha, "$g_battle_map_archers_order_flag", 0xFF),
        (overlay_set_position, "$g_battle_map_archers_order_flag", pos0),
      (else_try),
        (overlay_set_alpha, "$g_battle_map_archers_order_flag", 0),
      (try_end),
      (team_get_movement_order, ":cur_order", ":player_team", grc_cavalry),
      (try_begin),
        (eq, ":cur_order", mordr_hold),
        (eq, "$group2_has_troops", 1),
        (team_get_order_position, pos1, ":player_team", grc_cavalry),
        (call_script, "script_convert_3d_pos_to_map_pos"),
        (overlay_set_alpha, "$g_battle_map_cavalry_order_flag", 0xFF),
        (overlay_set_position, "$g_battle_map_cavalry_order_flag", pos0),
      (else_try),
        (overlay_set_alpha, "$g_battle_map_cavalry_order_flag", 0),
      (try_end),
      (team_get_movement_order, ":cur_order", ":player_team", 3),
      (try_begin),
        (eq, ":cur_order", mordr_hold),
        (eq, "$group3_has_troops", 1),
        (team_get_order_position, pos1, ":player_team", 3),
        (call_script, "script_convert_3d_pos_to_map_pos"),
        (overlay_set_alpha, "$g_battle_map_spears_order_flag", 0xFF),
        (overlay_set_position, "$g_battle_map_spears_order_flag", pos0),
      (else_try),
        (overlay_set_alpha, "$g_battle_map_spears_order_flag", 0),
      (try_end),
      (team_get_movement_order, ":cur_order", ":player_team", 4),
      (try_begin),
        (eq, ":cur_order", mordr_hold),
        (eq, "$group4_has_troops", 1),
        (team_get_order_position, pos1, ":player_team", 4),
        (call_script, "script_convert_3d_pos_to_map_pos"),
        (overlay_set_alpha, "$g_battle_map_skirmishers_order_flag", 0xFF),
        (overlay_set_position, "$g_battle_map_skirmishers_order_flag", pos0),
      (else_try),
        (overlay_set_alpha, "$g_battle_map_skirmishers_order_flag", 0),
      (try_end),
      (team_get_movement_order, ":cur_order", ":player_team", 5),
      (try_begin),
        (eq, ":cur_order", mordr_hold),
        (eq, "$group5_has_troops", 1),
        (team_get_order_position, pos1, ":player_team", 5),
        (call_script, "script_convert_3d_pos_to_map_pos"),
        (overlay_set_alpha, "$g_battle_map_horsemen_order_flag", 0xFF),
        (overlay_set_position, "$g_battle_map_horsemen_order_flag", pos0),
      (else_try),
        (overlay_set_alpha, "$g_battle_map_horsemen_order_flag", 0),
      (try_end),
      (team_get_movement_order, ":cur_order", ":player_team", 6),
      (try_begin),
        (eq, ":cur_order", mordr_hold),
        (eq, "$group6_has_troops", 1),
        (team_get_order_position, pos1, ":player_team", 6),
        (call_script, "script_convert_3d_pos_to_map_pos"),
        (overlay_set_alpha, "$g_battle_map_d7_order_flag", 0xFF),
        (overlay_set_position, "$g_battle_map_d7_order_flag", pos0),
      (else_try),
        (overlay_set_alpha, "$g_battle_map_d7_order_flag", 0),
      (try_end),
      (team_get_movement_order, ":cur_order", ":player_team", 7),
      (try_begin),
        (eq, ":cur_order", mordr_hold),
        (eq, "$group7_has_troops", 1),
        (team_get_order_position, pos1, ":player_team", 7),
        (call_script, "script_convert_3d_pos_to_map_pos"),
        (overlay_set_alpha, "$g_battle_map_d8_order_flag", 0xFF),
        (overlay_set_position, "$g_battle_map_d8_order_flag", pos0),
      (else_try),
        (overlay_set_alpha, "$g_battle_map_d8_order_flag", 0),
      (try_end),
      (team_get_movement_order, ":cur_order", ":player_team", 8),
      (try_begin),
        (eq, ":cur_order", mordr_hold),
        (eq, "$group8_has_troops", 1),
        (team_get_order_position, pos1, ":player_team", 8),
        (call_script, "script_convert_3d_pos_to_map_pos"),
        (overlay_set_alpha, "$g_battle_map_d9_order_flag", 0xFF),
        (overlay_set_position, "$g_battle_map_d9_order_flag", pos0),
      (else_try),
        (overlay_set_alpha, "$g_battle_map_d9_order_flag", 0),
      (try_end),
  ]),
  
  # script_update_order_panel_checked_classes
  # Input: none
  # Output: none
  ("update_order_panel_checked_classes",
    [(get_player_agent_no, ":player_agent"),
      (agent_get_team, ":player_team", ":player_agent"),
      
      (try_begin),
        (eq, "$group0_has_troops", 1),
        (class_is_listening_order, ":player_team", 0),
        (overlay_set_val, "$g_presentation_obj_battle_check0", 1),
        (assign, "$g_formation_group0_selected", 1),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but0", 250, 0x44),
      (else_try),
        (eq, "$group0_has_troops", 1),
        (overlay_set_val, "$g_presentation_obj_battle_check0", 0),
        (assign, "$g_formation_group0_selected", 0),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but0", 250, 0),
      (try_end),
      (try_begin),
        (eq, "$group1_has_troops", 1),
        (class_is_listening_order, ":player_team", 1),
        (overlay_set_val, "$g_presentation_obj_battle_check1", 1),
        (assign, "$g_formation_group1_selected", 1),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but1", 250, 0x44),
      (else_try),
        (eq, "$group1_has_troops", 1),
        (overlay_set_val, "$g_presentation_obj_battle_check1", 0),
        (assign, "$g_formation_group1_selected", 0),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but1", 250, 0),
      (try_end),
      (try_begin),
        (eq, "$group2_has_troops", 1),
        (class_is_listening_order, ":player_team", 2),
        (overlay_set_val, "$g_presentation_obj_battle_check2", 1),
        (assign, "$g_formation_group2_selected", 1),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but2", 250, 0x44),
      (else_try),
        (eq, "$group2_has_troops", 1),
        (overlay_set_val, "$g_presentation_obj_battle_check2", 0),
        (assign, "$g_formation_group2_selected", 0),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but2", 250, 0),
      (try_end),
      (try_begin),
        (eq, "$group3_has_troops", 1),
        (class_is_listening_order, ":player_team", 3),
        (overlay_set_val, "$g_presentation_obj_battle_check3", 1),
        (assign, "$g_formation_group3_selected", 1),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but3", 250, 0x44),
      (else_try),
        (eq, "$group3_has_troops", 1),
        (overlay_set_val, "$g_presentation_obj_battle_check3", 0),
        (assign, "$g_formation_group3_selected", 0),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but3", 250, 0),
      (try_end),
      (try_begin),
        (eq, "$group4_has_troops", 1),
        (class_is_listening_order, ":player_team", 4),
        (overlay_set_val, "$g_presentation_obj_battle_check4", 1),
        (assign, "$g_formation_group4_selected", 1),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but4", 250, 0x44),
      (else_try),
        (eq, "$group4_has_troops", 1),
        (overlay_set_val, "$g_presentation_obj_battle_check4", 0),
        (assign, "$g_formation_group4_selected", 0),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but4", 250, 0),
      (try_end),
      (try_begin),
        (eq, "$group5_has_troops", 1),
        (class_is_listening_order, ":player_team", 5),
        (overlay_set_val, "$g_presentation_obj_battle_check5", 1),
        (assign, "$g_formation_group5_selected", 1),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but5", 250, 0x44),
      (else_try),
        (eq, "$group5_has_troops", 1),
        (overlay_set_val, "$g_presentation_obj_battle_check5", 0),
        (assign, "$g_formation_group5_selected", 0),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but5", 250, 0),
      (try_end),
      (try_begin),
        (eq, "$group6_has_troops", 1),
        (class_is_listening_order, ":player_team", 6),
        (overlay_set_val, "$g_presentation_obj_battle_check6", 1),
        (assign, "$g_formation_group6_selected", 1),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but6", 250, 0x44),
      (else_try),
        (eq, "$group6_has_troops", 1),
        (overlay_set_val, "$g_presentation_obj_battle_check6", 0),
        (assign, "$g_formation_group6_selected", 0),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but6", 250, 0),
      (try_end),
      (try_begin),
        (eq, "$group7_has_troops", 1),
        (class_is_listening_order, ":player_team", 7),
        (overlay_set_val, "$g_presentation_obj_battle_check7", 1),
        (assign, "$g_formation_group7_selected", 1),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but7", 250, 0x44),
      (else_try),
        (eq, "$group7_has_troops", 1),
        (overlay_set_val, "$g_presentation_obj_battle_check7", 0),
        (assign, "$g_formation_group7_selected", 0),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but7", 250, 0),
      (try_end),
      (try_begin),
        (eq, "$group8_has_troops", 1),
        (class_is_listening_order, ":player_team", 8),
        (overlay_set_val, "$g_presentation_obj_battle_check8", 1),
        (assign, "$g_formation_group8_selected", 1),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but8", 250, 0x44),
      (else_try),
        (eq, "$group8_has_troops", 1),
        (overlay_set_val, "$g_presentation_obj_battle_check8", 0),
        (assign, "$g_formation_group8_selected", 0),
        (overlay_animate_to_alpha, "$g_presentation_obj_battle_but8", 250, 0),
      (try_end),
  ]),
  
  # script_update_order_panel_statistics_and_map
  # Input: none
  # Output: none
  ("update_order_panel_statistics_and_map", #TODO: Call this in every battle mission template, once per second
    [(set_fixed_point_multiplier, 1000),
      
      (assign, ":num_us_ready_group0", 0),
      (assign, ":num_us_ready_group1", 0),
      (assign, ":num_us_ready_group2", 0),
      (assign, ":num_us_ready_group3", 0),
      (assign, ":num_us_ready_group4", 0),
      (assign, ":num_us_ready_group5", 0),
      (assign, ":num_us_ready_group6", 0),
      (assign, ":num_us_ready_group7", 0),
      (assign, ":num_us_ready_group8", 0),
      
      (assign, ":num_us_ready_men", 0),
      (assign, ":num_us_wounded_men", 0),
      (assign, ":num_us_routed_men", 0),
      (assign, ":num_us_dead_men", 0),
      (assign, ":num_allies_ready_men", 0),
      (assign, ":num_allies_wounded_men", 0),
      (assign, ":num_allies_routed_men", 0),
      (assign, ":num_allies_dead_men", 0),
      (assign, ":num_enemies_ready_men", 0),
      (assign, ":num_enemies_wounded_men", 0),
      (assign, ":num_enemies_routed_men", 0),
      (assign, ":num_enemies_dead_men", 0),
      
      (get_player_agent_no, ":player_agent"),
      (agent_get_team, ":player_team", ":player_agent"),
      (get_scene_boundaries, pos2, pos3),
      
      (try_for_agents,":cur_agent"),
        (agent_is_human, ":cur_agent"),
        (agent_get_division, ":agent_division", ":cur_agent"),
        (agent_get_team, ":agent_team", ":cur_agent"),
        (agent_get_slot, ":agent_overlay", ":cur_agent", slot_agent_map_overlay_id),
        #
        (this_or_next|neq, "$dog_companion", 1),#VC-2111
        (neq, ":cur_agent", "$player_dog_agent_no"),#VC-2111
        #
        (try_begin),
          (eq, ":agent_team", ":player_team"),
          (try_begin),
            (agent_is_alive, ":cur_agent"),
            (call_script, "script_update_agent_position_on_map", ":cur_agent"),
            (try_begin),
              (eq, ":agent_division", 0),
              (val_add, ":num_us_ready_group0", 1),
              (eq, "$group0_has_troops", 1), #added to solve problem. test this.
            (else_try),
              (eq, ":agent_division", 1),
              (val_add, ":num_us_ready_group1", 1),
              (eq, "$group1_has_troops", 1), #added to solve problem.
            (else_try),
              (eq, ":agent_division", 2),
              (val_add, ":num_us_ready_group2", 1),
              (eq, "$group2_has_troops", 1), #added to solve problem.
            (else_try),
              (eq, ":agent_division", 3),
              (val_add, ":num_us_ready_group3", 1),
              (eq, "$group3_has_troops", 1), #added to solve problem.
            (else_try),
              (eq, ":agent_division", 4),
              (val_add, ":num_us_ready_group4", 1),
              (eq, "$group4_has_troops", 1), #added to solve problem.
            (else_try),
              (eq, ":agent_division", 5),
              (val_add, ":num_us_ready_group5", 1),
              (eq, "$group5_has_troops", 1), #added to solve problem.
            (else_try),
              (eq, ":agent_division", 6),
              (val_add, ":num_us_ready_group6", 1),
              (eq, "$group6_has_troops", 1), #added to solve problem.
            (else_try),
              (eq, ":agent_division", 7),
              (val_add, ":num_us_ready_group7", 1),
              (eq, "$group7_has_troops", 1), #added to solve problem.
            (else_try),
              (eq, ":agent_division", 8),
              (val_add, ":num_us_ready_group8", 1),
              (eq, "$group8_has_troops", 1), #added to solve problem.
            (try_end),
            (val_add, ":num_us_ready_men", 1),
          (else_try),
            (try_begin),
              (gt, ":agent_overlay", 0),
              (overlay_set_alpha, ":agent_overlay", 0),
            (try_end),
            (agent_is_wounded, ":cur_agent"),
            (val_add, ":num_us_wounded_men", 1),
          (else_try),
            (agent_is_routed, ":cur_agent"),
            (val_add, ":num_us_routed_men", 1),
          (else_try),
            (val_add, ":num_us_dead_men", 1),
          (try_end),
        (else_try),
          (agent_is_ally, ":cur_agent"),
          (try_begin),
            (agent_is_alive, ":cur_agent"),
            (call_script, "script_update_agent_position_on_map", ":cur_agent"),
            (val_add, ":num_allies_ready_men", 1),
          (else_try),
            (try_begin),
              (gt, ":agent_overlay", 0),
              (overlay_set_alpha, ":agent_overlay", 0),
            (try_end),
            (agent_is_wounded, ":cur_agent"),
            (val_add, ":num_allies_wounded_men", 1),
          (else_try),
            (agent_is_routed, ":cur_agent"),
            (val_add, ":num_allies_routed_men", 1),
          (else_try),
            (val_add, ":num_allies_dead_men", 1),
          (try_end),
        (else_try),
          (try_begin),
            (agent_is_alive, ":cur_agent"),
            (call_script, "script_update_agent_position_on_map", ":cur_agent"),
            (val_add, ":num_enemies_ready_men", 1),
          (else_try),
            (try_begin),
              (gt, ":agent_overlay", 0),
              (overlay_set_alpha, ":agent_overlay", 0),
            (try_end),
            (agent_is_wounded, ":cur_agent"),
            (val_add, ":num_enemies_wounded_men", 1),
          (else_try),
            (agent_is_routed, ":cur_agent"),
            (val_add, ":num_enemies_routed_men", 1),
          (else_try),
            (val_add, ":num_enemies_dead_men", 1),
          (try_end),
        (try_end),
      (try_end),
      
      (assign, reg1, ":num_us_ready_group0"),
      (assign, reg2, ":num_us_ready_group1"),
      (assign, reg3, ":num_us_ready_group2"),
      (assign, reg4, ":num_us_ready_group3"),
      (assign, reg5, ":num_us_ready_group4"),
      (assign, reg6, ":num_us_ready_group5"),
      (assign, reg7, ":num_us_ready_group6"),
      (assign, reg8, ":num_us_ready_group7"),
      (assign, reg9, ":num_us_ready_group8"),
      (assign, reg10, ":num_us_ready_men"),
      (assign, reg11, ":num_us_wounded_men"),
      (assign, reg12, ":num_us_routed_men"),
      (assign, reg13, ":num_us_dead_men"),
      (assign, reg14, ":num_allies_ready_men"),
      (assign, reg15, ":num_allies_wounded_men"),
      (assign, reg16, ":num_allies_routed_men"),
      (assign, reg17, ":num_allies_dead_men"),
      (assign, reg18, ":num_enemies_ready_men"),
      (assign, reg19, ":num_enemies_wounded_men"),
      (assign, reg20, ":num_enemies_routed_men"),
      (assign, reg21, ":num_enemies_dead_men"),
      
      (try_begin),
        (eq, "$group0_has_troops", 1),
        (str_store_class_name, s1, 0),
        (overlay_set_text, "$g_presentation_obj_battle_name0", "str_s1_reg1"),
      (try_end),
      (try_begin),
        (eq, "$group1_has_troops", 1),
        (str_store_class_name, s1, 1),
        (overlay_set_text, "$g_presentation_obj_battle_name1", "str_s1_reg2"),
      (try_end),
      (try_begin),
        (eq, "$group2_has_troops", 1),
        (str_store_class_name, s1, 2),
        (overlay_set_text, "$g_presentation_obj_battle_name2", "str_s1_reg3"),
      (try_end),
      (try_begin),
        (eq, "$group3_has_troops", 1),
        (str_store_class_name, s1, 3),
        (overlay_set_text, "$g_presentation_obj_battle_name3", "str_s1_reg4"),
      (try_end),
      (try_begin),
        (eq, "$group4_has_troops", 1),
        (str_store_class_name, s1, 4),
        (overlay_set_text, "$g_presentation_obj_battle_name4", "str_s1_reg5"),
      (try_end),
      (try_begin),
        (eq, "$group5_has_troops", 1),
        (str_store_class_name, s1, 5),
        (overlay_set_text, "$g_presentation_obj_battle_name5", "str_s1_reg6"),
      (try_end),
      (try_begin),
        (eq, "$group6_has_troops", 1),
        (str_store_class_name, s1, 6),
        (overlay_set_text, "$g_presentation_obj_battle_name6", "str_s1_reg7"),
      (try_end),
      (try_begin),
        (eq, "$group7_has_troops", 1),
        (str_store_class_name, s1, 7),
        (overlay_set_text, "$g_presentation_obj_battle_name7", "str_s1_reg8"),
      (try_end),
      (try_begin),
        (eq, "$group8_has_troops", 1),
        (str_store_class_name, s1, 8),
        (overlay_set_text, "$g_presentation_obj_battle_name8", "str_s1_reg9"),
      (try_end),
      
      (overlay_set_text, "$g_battle_us_ready", "@{!}{reg10}"),
      (overlay_set_text, "$g_battle_us_wounded", "@{!}{reg11}"),
      (overlay_set_text, "$g_battle_us_routed", "@{!}{reg12}"),
      (overlay_set_text, "$g_battle_us_dead", "str_reg13"),
      (overlay_set_text, "$g_battle_allies_ready", "str_reg14"),
      (overlay_set_text, "$g_battle_allies_wounded", "str_reg15"),
      (overlay_set_text, "$g_battle_allies_routed", "str_reg16"),
      (overlay_set_text, "$g_battle_allies_dead", "str_reg17"),
      (overlay_set_text, "$g_battle_enemies_ready", "str_reg18"),
      (overlay_set_text, "$g_battle_enemies_wounded", "str_reg19"),
      (overlay_set_text, "$g_battle_enemies_routed", "str_reg20"),
      (overlay_set_text, "$g_battle_enemies_dead", "str_reg21"),
      
      (assign, ":stat_position_x", 675),
      (assign, ":stat_position_y", 280),
      (val_add, ":stat_position_x", 70),
      (val_add, ":stat_position_y", 60),
      (position_set_x, pos1, ":stat_position_x"),
      (position_set_y, pos1, ":stat_position_y"),
      (overlay_set_position, "$g_battle_us_ready", pos1),
      (val_add, ":stat_position_x", 70),
      (position_set_x, pos1, ":stat_position_x"),
      (overlay_set_position, "$g_battle_us_wounded", pos1),
      (val_add, ":stat_position_x", 70),
      (position_set_x, pos1, ":stat_position_x"),
      (overlay_set_position, "$g_battle_us_routed", pos1),
      (val_add, ":stat_position_x", 70),
      (position_set_x, pos1, ":stat_position_x"),
      (overlay_set_position, "$g_battle_us_dead", pos1),
      (val_add, ":stat_position_x", -210),
      (val_add, ":stat_position_y", -30),
      (position_set_x, pos1, ":stat_position_x"),
      (position_set_y, pos1, ":stat_position_y"),
      (overlay_set_position, "$g_battle_allies_ready", pos1),
      (val_add, ":stat_position_x", 70),
      (position_set_x, pos1, ":stat_position_x"),
      (overlay_set_position, "$g_battle_allies_wounded", pos1),
      (val_add, ":stat_position_x", 70),
      (position_set_x, pos1, ":stat_position_x"),
      (overlay_set_position, "$g_battle_allies_routed", pos1),
      (val_add, ":stat_position_x", 70),
      (position_set_x, pos1, ":stat_position_x"),
      (overlay_set_position, "$g_battle_allies_dead", pos1),
      (val_add, ":stat_position_x", -210),
      (val_add, ":stat_position_y", -30),
      (position_set_x, pos1, ":stat_position_x"),
      (position_set_y, pos1, ":stat_position_y"),
      (overlay_set_position, "$g_battle_enemies_ready", pos1),
      (val_add, ":stat_position_x", 70),
      (position_set_x, pos1, ":stat_position_x"),
      (overlay_set_position, "$g_battle_enemies_wounded", pos1),
      (val_add, ":stat_position_x", 70),
      (position_set_x, pos1, ":stat_position_x"),
      (overlay_set_position, "$g_battle_enemies_routed", pos1),
      (val_add, ":stat_position_x", 70),
      (position_set_x, pos1, ":stat_position_x"),
      (overlay_set_position, "$g_battle_enemies_dead", pos1),
      
      (call_script, "script_update_order_flags_on_map"),
  ]),
  
  # script_set_town_picture
  # Input: none
  # Output: none
  ("set_town_picture",
    [
      (try_begin),
        (party_get_current_terrain, ":cur_terrain", "$current_town"),
        (party_slot_eq,"$current_town",slot_party_type, spt_town),
        (try_begin),
          (this_or_next|eq, ":cur_terrain", rt_steppe),
          (this_or_next|eq, ":cur_terrain", rt_steppe_forest),
          (this_or_next|eq, ":cur_terrain", rt_desert),
          (             eq, ":cur_terrain", rt_desert_forest),
          (set_background_mesh, "mesh_pic_towndes"),
        (else_try),
          (this_or_next|eq, ":cur_terrain", rt_snow),
          (             eq, ":cur_terrain", rt_snow_forest),
          (set_background_mesh, "mesh_pic_townsnow"),
        (else_try),
          (set_background_mesh, "mesh_pic_town1"),
        (try_end),
      (else_try),
        (try_begin),
          (this_or_next|eq, ":cur_terrain", rt_steppe),
          (this_or_next|eq, ":cur_terrain", rt_steppe_forest),
          (this_or_next|eq, ":cur_terrain", rt_desert),
          (             eq, ":cur_terrain", rt_desert_forest),
          (set_background_mesh, "mesh_pic_castledes"),
        (else_try),
          (this_or_next|eq, ":cur_terrain", rt_snow),
          (             eq, ":cur_terrain", rt_snow_forest),
          (set_background_mesh, "mesh_pic_castlesnow"),
        (else_try),
          (set_background_mesh, "mesh_pic_castle1"),
        (try_end),
      (try_end),
  ]),
  
  
  # script_consume_food
  # Input: arg1: order of the food to be consumed
  # Output: none
  ("consume_food",
    [(store_script_param, ":selected_food", 1),
      (troop_get_inventory_capacity, ":capacity", "trp_player"),
      (try_for_range, ":cur_slot", 0, ":capacity"),
        (troop_get_inventory_slot, ":cur_item", "trp_player", ":cur_slot"),
        (this_or_next|is_between, ":cur_item", bebidas_begin, bebidas_end), #chief anade para bebidas consumo
        (is_between, ":cur_item", food_begin, food_end),
        (troop_get_inventory_slot_modifier, ":item_modifier", "trp_player", ":cur_slot"),
        (neq, ":item_modifier", imod_rotten),
        (item_slot_eq, ":cur_item", slot_item_is_checked, 0),
        (item_set_slot, ":cur_item", slot_item_is_checked, 1),
        (val_sub, ":selected_food", 1),
        (lt, ":selected_food", 0),
        (assign, ":capacity", 0),
        (troop_inventory_slot_get_item_amount, ":cur_amount", "trp_player", ":cur_slot"),
        (val_sub, ":cur_amount", 1),
        (troop_inventory_slot_set_item_amount, "trp_player", ":cur_slot", ":cur_amount"),
      (try_end),
  ]),
  
  
  
  # script_calculate_troop_score_for_center
  # Input: arg1 = troop_no, arg2 = center_no
  # Output: reg0 = score
  ("calculate_troop_score_for_center",
    [(store_script_param, ":troop_no", 1),
      (store_script_param, ":center_no", 2),
      (assign, ":num_center_points", 1),
      (try_for_range, ":cur_center", centers_begin, centers_end),
        (assign, ":center_owned", 0),
        (try_begin),
          (eq, ":troop_no", "trp_player"),
          (party_slot_eq, ":cur_center", slot_town_lord, stl_reserved_for_player),
          (assign, ":center_owned", 1),
        (try_end),
        (this_or_next|party_slot_eq, ":cur_center", slot_town_lord, ":troop_no"),
        (eq, ":center_owned", 1),
        (try_begin),
          (party_slot_eq, ":cur_center", slot_party_type, spt_town),
          (val_add, ":num_center_points", 4),
        (else_try),
          (party_slot_eq, ":cur_center", slot_party_type, spt_castle),
          (val_add, ":num_center_points", 2),
        (else_try),
          (val_add, ":num_center_points", 1),
        (try_end),
      (try_end),
      (troop_get_slot, ":troop_renown", ":troop_no", slot_troop_renown),
      (store_add, ":score", 500, ":troop_renown"),
      (val_div, ":score", ":num_center_points"),
      (store_random_in_range, ":random", 50, 100),
      (val_mul, ":score", ":random"),
      (try_begin),
        (party_slot_eq, ":center_no", slot_center_last_taken_by_troop, ":troop_no"),
        (val_mul, ":score", 3),
        (val_div, ":score", 2),
      (try_end),
      (try_begin),
        (eq, ":troop_no", "trp_player"),
        (is_between, "$players_kingdom", kingdoms_begin, kingdoms_end),
        (faction_get_slot, ":faction_leader", "$players_kingdom", slot_faction_leader),
        (call_script, "script_troop_get_player_relation", ":faction_leader"),
        (assign, ":leader_relation", reg0),
        #(troop_get_slot, ":leader_relation", ":faction_leader", slot_troop_player_relation),
        (val_mul, ":leader_relation", 2),
        (val_add, ":score", ":leader_relation"),
      (try_end),
      (assign, reg0, ":score"),
  ]),
  
  
  # script_assign_lords_to_empty_centers
  # Input: none
  # Output: none
  #Now ONLY called from the start
  ("assign_lords_to_empty_centers",
    [
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (display_message, "str_assigning_lords_to_empty_centers"),
        (str_store_string, s65, "str_assign_lords_to_empty_centers_just_happened"),
        (call_script, "script_add_notification_menu", "mnu_debug_alert_from_s65", 0, 0),
      (try_end),
      
      (try_for_range, ":faction", kingdoms_begin, kingdoms_end),
        (faction_set_slot, ":faction", slot_faction_temp_slot, 0),
      (try_end),
      
      (try_for_range, ":active_npc", 0, active_npcs_end),
        (troop_set_slot, ":active_npc", slot_troop_temp_slot, 0),
      (try_end),
      
      #Factions will keep one unassigned center in reserve, unless they have landless lords
      (try_for_range, ":cur_center", centers_begin, centers_end),
        (party_get_slot, ":center_lord", ":cur_center", slot_town_lord),
        (try_begin),
          (this_or_next|eq, ":center_lord", stl_unassigned),
          (eq, ":center_lord", stl_rejected_by_player),
          (store_faction_of_party, ":center_faction", ":cur_center"),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_party_name, s4, ":cur_center"),
            (str_store_faction_name, s5, ":center_faction"),
            (display_message, "str_s4_of_the_s5_is_unassigned"),
          (try_end),
          
          (faction_get_slot, ":number_of_unassigned_centers_plus_landless_lords", ":center_faction", slot_faction_temp_slot),
          (val_add, ":number_of_unassigned_centers_plus_landless_lords", 1),
          (faction_set_slot,  ":center_faction", slot_faction_temp_slot, ":number_of_unassigned_centers_plus_landless_lords"),
        (else_try),
          (eq, ":center_lord", stl_reserved_for_player),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_party_name, s4, ":cur_center"),
            (str_store_faction_name, s5, ":center_faction"),
            (display_message, "str_s4_of_the_s5_is_reserved_for_player"),
          (try_end),
          
        (else_try),
          (ge, ":center_lord", 0),
          (troop_set_slot, ":center_lord", slot_troop_temp_slot, 1),
        (try_end),
      (try_end),
      
      (try_for_range, ":active_npc", 0, active_npcs_end),
        (troop_slot_eq, ":active_npc", slot_troop_occupation, slto_kingdom_hero),
        (this_or_next|gt, ":active_npc", "trp_player"),
        (eq, "$player_has_homage", 1),
        
        (troop_slot_eq, ":active_npc", slot_troop_temp_slot, 0),
        (store_faction_of_troop, ":npc_faction", ":active_npc"),
        
        (is_between, ":npc_faction", npc_kingdoms_begin, npc_kingdoms_end),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (str_store_troop_name, s4, ":active_npc"),
          (str_store_faction_name, s5, ":npc_faction"),
          (display_message, "str_s4_of_the_s5_has_no_fiefs"),
        (try_end),
        
        (faction_get_slot, ":number_of_unassigned_centers_plus_landless_lords", ":npc_faction", slot_faction_temp_slot),
        (val_add, ":number_of_unassigned_centers_plus_landless_lords", 1),
        (faction_set_slot,  ":npc_faction", slot_faction_temp_slot, ":number_of_unassigned_centers_plus_landless_lords"),
      (try_end),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (try_for_range, ":faction", kingdoms_begin, kingdoms_end),
          (faction_get_slot, reg4, ":faction", slot_faction_temp_slot),
          (str_store_faction_name, s4, ":faction"),
          (display_message, "str_s4_unassigned_centers_plus_landless_lords_=_reg4"),
        (try_end),
      (try_end),
      
      (try_for_range, ":cur_center", centers_begin, centers_end),
        (party_get_slot, ":center_lord", ":cur_center", slot_town_lord),
        (this_or_next|eq, ":center_lord", stl_unassigned),
        (eq, ":center_lord", stl_rejected_by_player),
        
        (store_faction_of_party, ":center_faction", ":cur_center"),
        (is_between, ":center_faction", kingdoms_begin, kingdoms_end),
        (neg|faction_slot_eq, ":center_faction", slot_faction_leader, "trp_player"),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (str_store_party_name, s5, ":cur_center"),
          (try_begin),
            (neg|faction_slot_ge, ":center_faction", slot_faction_temp_slot, 2),
            (str_store_faction_name, s4, ":center_faction"),
            (display_message, "str_s4_holds_s5_in_reserve"),
          (try_end),
        (try_end),
        
        (faction_slot_ge, ":center_faction", slot_faction_temp_slot, 2),
        
        #(display_message, "@Considering grant of {s5}"),
        
        (assign, ":best_lord", -1),
        (assign, ":best_lord_score", -1),
        (try_begin),
          (eq, ":center_lord", stl_unassigned),
          (try_begin),
            (eq, "$players_kingdom", ":center_faction"),
            (eq, "$player_has_homage", 1),
            (assign, ":best_lord", stl_reserved_for_player),
            (call_script, "script_calculate_troop_score_for_center", "trp_player", ":cur_center"),
            (assign, ":best_lord_score", reg0),
          (try_end),
        (try_end),
        
        (try_for_range, ":cur_troop", active_npcs_begin, active_npcs_end),
          (troop_slot_eq, ":cur_troop", slot_troop_occupation, slto_kingdom_hero),
          (store_troop_faction, ":troop_faction", ":cur_troop"),
          (eq, ":troop_faction", ":center_faction"),
          
          (call_script, "script_calculate_troop_score_for_center", ":cur_troop", ":cur_center"),
          (assign, ":score", reg0),
          
          #This prioritizes granting of centers for troops which do not already have one
          (try_begin),
            (troop_slot_eq, ":cur_troop", slot_troop_temp_slot, 0),
            (is_between, ":cur_center", villages_begin, villages_end),
            (val_mul, ":score", 10),
          (try_end),
          
          (gt, ":score", ":best_lord_score"),
          (assign, ":best_lord_score", ":score"),
          (assign, ":best_lord", ":cur_troop"),
        (try_end),
        
        #Adjust count of centers and lords
        (try_begin),
          (this_or_next|ge, ":best_lord", 0),
          (eq, ":best_lord", stl_reserved_for_player),
          
          (faction_get_slot, ":landless_lords_plus_unassigned_centers", ":center_faction", slot_faction_temp_slot),
          (val_sub, ":landless_lords_plus_unassigned_centers", 1),
          
          (try_begin),
            (eq, ":best_lord", stl_reserved_for_player),
            (troop_slot_eq, "trp_player", slot_troop_temp_slot, 0),
            (troop_set_slot, "trp_player", slot_troop_temp_slot, 1),
            (val_sub, ":landless_lords_plus_unassigned_centers", 1),
          (else_try),
            (troop_slot_eq, ":best_lord", slot_troop_temp_slot, 0),
            (troop_set_slot, ":best_lord", slot_troop_temp_slot, 1),
            (val_sub, ":landless_lords_plus_unassigned_centers", 1),
          (try_end),
          
          (faction_set_slot, ":center_faction", slot_faction_temp_slot, ":landless_lords_plus_unassigned_centers"),
        (try_end),
        
        #Give the center to the lord
        (try_begin),
          (ge, ":best_lord", 0),
          (call_script, "script_give_center_to_lord", ":cur_center", ":best_lord", 1),
        (else_try),
          (eq, ":best_lord", stl_reserved_for_player),
          (party_set_slot, ":cur_center", slot_town_lord, stl_reserved_for_player),
          (try_begin), #grant bound villages to player, if granting a castle
            (party_slot_eq, ":cur_center", slot_party_type, spt_castle),
            #				(assign, ":give_at_least_one_village", 0),
            (try_for_range, ":cur_village", villages_begin, villages_end),
              #					(eq, ":give_at_least_one_village", 0),
              (party_slot_eq, ":cur_village", slot_village_bound_center, ":cur_center"),
              (party_slot_eq, ":cur_village", slot_town_lord, stl_unassigned),
              (party_set_slot, ":cur_village", slot_town_lord, stl_reserved_for_player),
              #					(assign, ":give_at_least_one_village", 1),
            (try_end),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  
  # script_create_village_farmer_party
  # Input: arg1 = village_no
  # Output: reg0 = party_no
  ("create_village_farmer_party",
    [(store_script_param, ":village_no", 1),
      (party_get_slot, ":town_no", ":village_no", slot_village_market_town),
      (store_faction_of_party, ":party_faction", ":town_no"),
      
      
      #    (store_faction_of_party, ":town_faction", ":town_no"),
      #    (try_begin),
      #		(neq, ":town_faction", ":party_faction"),
      #		(assign, ":town_no", -1),
      #		(assign, ":score_to_beat", 9999),
      #		(try_for_range, ":other_town", towns_begin, towns_end),
      #			(store_faction_of_party, ":other_town_faction", ":town_no"),
      #			(store_relation, ":relation", ":other_town_faction", ":party_faction"),
      #			(ge, ":relation", 0),
      
      #			(store_distance_to_party_from_party, ":distance", ":village_no", ":other_town"),
      #			(lt, ":distance", ":score_to_beat"),
      #			(assign, ":town_no", ":other_town"),
      #			(assign, ":score_to_beat", ":distance"),
      #		(try_end),
      #	(try_end),
      
      (try_begin),
        (is_between, ":town_no", walled_centers_begin, walled_centers_end),
        (set_spawn_radius, 0),
        (spawn_around_party, ":village_no", "pt_village_farmers"),
        (assign, ":new_party", reg0),
        
        (party_get_slot, reg1, ":town_no", slot_town_farmer_visit_starts),
        (val_add, reg1, 1),
        (party_set_slot, ":town_no", slot_town_farmer_visit_starts, reg1),
        
        (party_set_faction, ":new_party", ":party_faction"),
        (party_set_slot, ":new_party", slot_party_home_center, ":village_no"),
        (party_set_slot, ":new_party", slot_party_last_traded_center, ":village_no"),
        
        (party_set_slot, ":new_party", slot_party_type, spt_village_farmer),
        (party_set_slot, ":new_party", slot_party_ai_state, spai_trading_with_town),
        (party_set_slot, ":new_party", slot_party_ai_object, ":town_no"),
        (party_set_ai_behavior, ":new_party", ai_bhvr_travel_to_party),
        (party_set_ai_object, ":new_party", ":town_no"),
        (party_set_flags, ":new_party", pf_default_behavior, 0),
        (store_sub, ":item_to_price_slot", slot_town_trade_good_prices_begin, trade_goods_begin),
        (try_for_range, ":cur_goods", trade_goods_begin, trade_goods_end),
          (store_add, ":cur_good_price_slot", ":cur_goods", ":item_to_price_slot"),
          (party_get_slot, ":cur_village_price", ":village_no", ":cur_good_price_slot"),
          (party_set_slot, ":new_party", ":cur_good_price_slot", ":cur_village_price"),
        (try_end),
        (assign, reg0, ":new_party"),
      (try_end),
      
  ]),
  
  #script_do_party_center_trade
  # INPUT: arg1 = party_no, arg2 = center_no, arg3 = percentage_change_in_center
  # OUTPUT: reg0 = total_change
  ("do_party_center_trade",
    [
      (store_script_param, ":party_no", 1),
      (store_script_param, ":center_no", 2),
      
      (try_begin),
        (eq, "$cheat_mode", 3),
        (str_store_party_name, s1, ":center_no"),
        (display_message, "@{!}DEBUG : {s1} is trading with villagers"),
      (try_end),
      
      (store_script_param, ":percentage_change", 3), #this should probably always be a constant. Currently it is 25.
      
      (party_get_slot, ":origin", ":party_no", slot_party_last_traded_center),
      (party_set_slot, ":party_no", slot_party_last_traded_center, ":center_no"),
      
      (assign, ":total_change", 0),
      (store_sub, ":item_to_price_slot", slot_town_trade_good_prices_begin, trade_goods_begin),
      (try_for_range, ":cur_good", trade_goods_begin, trade_goods_end),
        (store_add, ":cur_good_price_slot", ":cur_good", ":item_to_price_slot"),
        (party_get_slot, ":cur_merchant_price", ":party_no", ":cur_good_price_slot"),
        (party_get_slot, ":cur_center_price", ":center_no", ":cur_good_price_slot"),
        (store_sub, ":price_dif", ":cur_merchant_price", ":cur_center_price"),
        (assign, ":cur_change", ":price_dif"),
        (val_abs, ":cur_change"),
        (val_add, ":total_change", ":cur_change"),
        (val_mul, ":cur_change", ":percentage_change"),
        (val_div, ":cur_change", 100),
        
        #This is to reconvert from absolute value
        (try_begin),
          (lt, ":price_dif", 0),
          (val_mul, ":cur_change", -1),
        (try_end),
        
        (assign, ":initial_price", ":cur_center_price"),
        
        #The new price for the caravan or peasant is set before the change, so the prices in the trading town have full effect on the next center
        (party_set_slot, ":party_no", ":cur_good_price_slot", ":cur_center_price"),
        
        (val_add, ":cur_center_price", ":cur_change"),
        (party_set_slot, ":center_no", ":cur_good_price_slot", ":cur_center_price"),
        
        (try_begin),
          (eq, "$cheat_mode", 3),
          (str_store_party_name, s3, ":origin"),
          (str_store_party_name, s4, ":center_no"),
          (str_store_item_name, s5, ":cur_good"),
          (assign, reg4, ":initial_price"),
          (assign, reg5, ":cur_center_price"),
          (display_log_message, "@{!}DEBUG -- Trade of {s5} from {s3} to {s4} brings price from {reg4} to {reg5}"),
        (try_end),
        
      (try_end),
      (assign, reg0, ":total_change"),
  ]),
  
  #script_player_join_faction
  # INPUT: arg1 = faction_no
  # OUTPUT: none
  ("player_join_faction",
    [
      (store_script_param, ":faction_no", 1),
      (try_begin),
        (eq, "$players_kingdom", "fac_player_supporters_faction"),
        (neq, ":faction_no", "fac_player_supporters_faction"),
        (call_script, "script_deactivate_player_faction", 1),
      (try_end),
      (assign,"$players_kingdom",":faction_no"),
      (faction_set_slot, "fac_player_supporters_faction", slot_faction_ai_state, sfai_default),
      (assign, "$players_oath_renounced_against_kingdom", 0),
      (assign, "$players_oath_renounced_given_center", 0),
      (assign, "$players_oath_renounced_begin_time", 0),
      
      #update player relations with all factions
      (try_for_range,":other_kingdom",npc_kingdoms_begin,npc_kingdoms_end),
        (faction_slot_eq, ":other_kingdom", slot_faction_state, sfs_active),
        
        (try_begin),
          (eq, ":other_kingdom", ":faction_no"),
          (troop_get_slot, ":renown", "trp_player", slot_troop_renown),
          (val_div, ":renown", 200),

          (store_relation, ":relation", ":faction_no", "fac_player_faction"),
          (val_add, ":relation", ":renown"),
          (store_sub, ":difference", 10, ":relation"), #VC-3960 at least cooperative
          (try_begin),
            (ge, ":difference", 1),
            (val_add, ":renown", ":difference"),
          (try_end),
          
          (call_script, "script_change_player_relation_with_faction", ":faction_no", ":renown"),
          
        (else_try),
          (store_relation, ":difference", ":other_kingdom", ":faction_no"),
          (try_begin),
            (lt, ":difference", 0), #at war with new faction?
            (call_script, "script_player_set_faction_relation_ceiling", ":other_kingdom", -10, 1),   #VC-3960 use same mechanic as script_diplomacy_start_war_between_kingdoms for consistency
          (else_try),
            (ge, ":difference", 30), #allied with new faction?
            (call_script, "script_change_player_relation_with_faction", ":other_kingdom", 10),  #VC-3960 use same mechanic as script_diplomacy_start_alliance_between_kingdoms for consistency
          (try_end),
        (try_end),
      (try_end),
      
      #VC-3875 player's lair follows him/her
      (try_begin),
        (eq, "$lair_on", 1),
        (call_script, "script_give_center_to_lord", "p_yourlair", "trp_player", 0),
      (try_end),
      
      (try_for_range, ":cur_center", centers_begin, centers_end),
        #Give center to kingdom if player is the owner
        (party_slot_eq, ":cur_center", slot_town_lord, "trp_player"),
        (call_script, "script_give_center_to_faction_while_maintaining_lord", ":cur_center", ":faction_no"),
      (else_try),
        #Give center to kingdom if part of player faction
        (store_faction_of_party, ":cur_center_faction", ":cur_center"),
        (eq, ":cur_center_faction", "fac_player_supporters_faction"),
        (call_script, "script_give_center_to_faction_while_maintaining_lord", ":cur_center", ":faction_no"),
      (try_end),
      
      (try_for_range, ":quest_no", lord_quests_begin, lord_quests_end),
        (check_quest_active, ":quest_no"),
        (quest_get_slot, ":quest_giver_troop", ":quest_no", slot_quest_giver_troop),
        (store_troop_faction, ":quest_giver_faction", ":quest_giver_troop"),
        (store_relation, ":quest_giver_faction_relation", "fac_player_faction", ":quest_giver_faction"),
        (lt, ":quest_giver_faction_relation", 0),
        (call_script, "script_abort_quest", ":quest_no", 0),
      (try_end),
      (try_for_range, ":quest_no", lord_quests_begin_2, lord_quests_end_2),
        (check_quest_active, ":quest_no"),
        (quest_get_slot, ":quest_giver_troop", ":quest_no", slot_quest_giver_troop),
        (store_troop_faction, ":quest_giver_faction", ":quest_giver_troop"),
        (store_relation, ":quest_giver_faction_relation", "fac_player_faction", ":quest_giver_faction"),
        (lt, ":quest_giver_faction_relation", 0),
        (call_script, "script_abort_quest", ":quest_no", 0),
      (try_end),
      
      (try_begin),
        (troop_get_slot, ":spouse", "trp_player", slot_troop_spouse),
        (is_between, ":spouse", kingdom_ladies_begin, kingdom_ladies_end),
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (str_store_troop_name, s4, ":spouse"),
          (display_message, "@{!}DEBUG - {s4} faction changed by marriage, case 1"),
        (try_end),
        
        (troop_set_faction, ":spouse", "$players_kingdom"),
      (try_end),
      
      (try_for_range, ":center", centers_begin, centers_end),
        (store_faction_of_party, ":center_faction", ":faction_no"),
        (neq, ":center_faction", "$players_kingdom"),
        (party_slot_eq, ":center", slot_town_lord, stl_reserved_for_player),
        #		(party_set_slot, ":center", slot_town_lord, stl_unassigned),
      (try_end),
      
      (troop_set_slot, "trp_player", slot_troop_stance_on_faction_issue, -1),
      
      #remove prisoners of player's faction if he was member of his own faction. And free companions which is prisoned in that faction.
      (try_for_parties, ":party_no"),
        (store_faction_of_party, ":party_faction", ":party_no"),
        (eq, ":party_faction", ":faction_no"),
        
        (party_get_num_prisoner_stacks, ":num_stacks", ":party_no"),
        (try_for_range_backwards, ":troop_iterator", 0, ":num_stacks"),
          (party_prisoner_stack_get_troop_id, ":cur_troop_id", ":party_no", ":troop_iterator"),
          (store_troop_faction, ":cur_faction", ":cur_troop_id"),
          
          (this_or_next|eq, ":cur_faction", "fac_player_supporters_faction"),
          (this_or_next|eq, ":cur_faction", ":faction_no"),
          (is_between, ":cur_troop_id", companions_begin, companions_end),
          
          (try_begin),
            (troop_is_hero, ":cur_troop_id"),
            (call_script, "script_remove_troop_from_prison", ":cur_troop_id"),
          (try_end),
          
          (party_prisoner_stack_get_size, ":stack_size", ":party_no", ":troop_iterator"),
          (party_remove_prisoners, ":party_no", ":cur_troop_id", ":stack_size"),
          
          (try_begin),
            (is_between, ":cur_troop_id", companions_begin, companions_end),
            
            (try_begin),
              (is_between, ":party_no", towns_begin, towns_end),
              (troop_set_slot, ":cur_troop_id", slot_troop_cur_center, ":party_no"),
            (else_try),
              (store_random_in_range, ":random_town_no", towns_begin, towns_end),
              (troop_set_slot, ":cur_troop_id", slot_troop_cur_center, ":random_town_no"),
            (try_end),
          (try_end),
        (try_end),
      (try_end),
      #remove prisoners end.
      
      #(call_script, "script_store_average_center_value_per_faction"),
      (call_script, "script_update_all_notes"),
      (assign, "$g_recalculate_ais", 1),
  ]),
  
  #script_player_leave_faction
  # INPUT: arg1 = give_back_fiefs
  # OUTPUT: none
  ("player_leave_faction",
    [
      (store_script_param, ":give_back_fiefs", 1),
      
      (call_script, "script_check_and_finish_active_army_quests_for_faction", "$players_kingdom"),
      (assign, ":old_kingdom", "$players_kingdom"),
      (assign, ":old_has_homage", "$player_has_homage"),
      (assign, "$players_kingdom", 0),
      (assign, "$player_has_homage", 0),
      
      #VC-3875 player's lair follows him/her
      (try_begin),
        (eq, "$lair_on", 1),
        (store_faction_of_party, reg1, "p_yourlair"),
        (neq, reg1, "fac_player_supporters_faction"),
        (call_script, "script_give_center_to_lord", "p_yourlair", "trp_player", 0),
      (try_end),
      
      (try_begin),
        (neq, ":give_back_fiefs", 0), #ie, give back fiefs = 1, thereby do it
        (try_for_range, ":cur_center", centers_begin, centers_end),
          (party_slot_eq, ":cur_center", slot_town_lord, "trp_player"),
          (call_script, "script_give_center_to_faction", ":cur_center", "fac_neutral"),
          (call_script, "script_give_center_to_faction", ":cur_center", ":old_kingdom"),
          
          #The following line also occurs when a lord is stripped of his fiefs by an indictment
          (party_set_slot, ":cur_center", slot_town_lord, stl_unassigned),
        (try_end),
      (else_try),
        #If you retain the fiefs
        (try_for_range, ":cur_center", centers_begin, centers_end),
          (party_slot_eq, ":cur_center", slot_town_lord, "trp_player"),
          (call_script, "script_give_center_to_faction", ":cur_center", "fac_player_supporters_faction"),
          (party_set_slot, ":cur_center", slot_town_lord, "trp_player"),
          (troop_get_slot, ":cur_banner", "trp_player", slot_troop_banner_scene_prop),
          (gt, ":cur_banner", 0),
          (val_sub, ":cur_banner", banner_scene_props_begin),
          (val_add, ":cur_banner", banner_map_icons_begin),
          (party_set_banner_icon, ":cur_center", ":cur_banner"),
        (try_end),
        
        (try_for_range, ":cur_center", villages_begin, villages_end),
          (party_get_slot, ":cur_bound_center", ":cur_center", slot_village_bound_center),
          (party_slot_eq, ":cur_center", slot_town_lord, "trp_player"),
          (neg|party_slot_eq, ":cur_bound_center", slot_town_lord, "trp_player"),
          (call_script, "script_give_center_to_faction", ":cur_center", ":old_kingdom"),
        (try_end),
        
        (is_between, ":old_kingdom", kingdoms_begin, kingdoms_end),
        (neq, ":old_kingdom", "fac_player_supporters_faction"),
        (store_relation, ":reln", "fac_player_faction", ":old_kingdom"),
        (store_sub, ":req_dif", -40, ":reln"),
        (call_script, "script_change_player_relation_with_faction", ":old_kingdom", ":req_dif"),
      (try_end),
      
      (try_begin),
        (eq, ":old_has_homage", 1),
        (faction_get_slot, ":faction_leader", ":old_kingdom", slot_faction_leader),
        (call_script, "script_change_player_relation_with_troop", ":faction_leader", -20),
      (try_end),
      
      (try_begin),
        (troop_get_slot, ":spouse", "trp_player", slot_troop_spouse),
        (is_between, ":spouse", kingdom_ladies_begin, kingdom_ladies_end),
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (str_store_troop_name, s4, ":spouse"),
          (display_message, "@{!}DEBUG - {s4} faction changed by marriage, case 3"),
        (try_end),
        
        (troop_set_faction, ":spouse", "fac_player_supporters_faction"),
      (try_end),
      
      #Change relations with players_kingdom when player changes factions
      (try_for_range, ":kingdom", npc_kingdoms_begin, npc_kingdoms_end),
        (try_begin),
          (eq, ":old_kingdom", ":kingdom"),
          (call_script, "script_change_player_relation_with_faction", ":kingdom", -20),  #VC-3960 goes from cooperative to suspicious if the player did nothing to help their cause while part of their faction
          
          #update player relations by old kingdom relations (opposite of join_faction)
        (else_try),
          (store_relation, ":difference", ":old_kingdom", ":kingdom"),
          
          (try_begin),
            (lt, ":difference", 0), #at war with old faction?
            (call_script, "script_change_player_relation_with_faction", ":kingdom", 10),  #VC-3960 neutralize -10 ceiling of joining
          (else_try),
            (ge, ":difference", 30), #allied with old faction?
            (call_script, "script_change_player_relation_with_faction", ":kingdom", -10),  #VC-3960 reverse +10 of joining
          (try_end),
        (try_end),
      (try_end),
      
      (call_script, "script_update_all_notes"),
      (assign, "$g_recalculate_ais", 1),
  ]),
  
  
  ("deactivate_player_faction",
    [
      (store_script_param, ":notify", 1),
      
      (faction_set_slot, "fac_player_supporters_faction", slot_faction_state, sfs_inactive),
      (faction_set_slot, "fac_player_supporters_faction", slot_faction_leader, "trp_player"),
      (assign, "$players_kingdom", 0),
      (assign, "$players_oath_renounced_against_kingdom", 0),
      (assign, "$players_oath_renounced_given_center", 0),
      (assign, "$players_oath_renounced_begin_time", 0),
      (assign, "$control_tax", 0),
      #(call_script, "script_store_average_center_value_per_faction"),
      (call_script, "script_appoint_faction_marshal", "fac_player_supporters_faction", -1),
      
      #end missions and recall minister
      (try_for_range, ":npc", companions_begin, companions_end),
        (troop_slot_eq, ":npc", slot_troop_occupation, slto_player_companion),
        (troop_set_slot, ":npc", slot_troop_days_on_mission, 0),
        (neg|main_party_has_troop, ":npc"),
        (troop_set_slot, ":npc", slot_troop_current_mission, npc_mission_rejoin_when_possible),
      (try_end),
      
      (try_for_range, ":minister_quest", all_quests_begin, all_quests_end),
        (check_quest_active, ":minister_quest"),
        (quest_slot_eq, ":minister_quest", slot_quest_giver_troop, "$g_player_minister"),
        (call_script, "script_abort_quest", ":minister_quest", 0),
      (try_end),
      
      (assign, "$g_player_minister", -1),
      
      (call_script, "script_update_all_notes"),
      
      (try_begin),
        (eq, ":notify", 1),
        (call_script, "script_add_notification_menu", "mnu_notification_player_faction_deactive", 0, 0),
      (try_end),
  ]),
  
  
  #script_activate_player_faction
  # INPUT: arg1 = last_interaction_with_faction
  # OUTPUT: none
  
  #When a player convinces her husband to rebel
  #When a player proclaims herself queen
  #When a player seizes control of a center
  #When a player recruits a lord through intrigue
  #When a player
  ("activate_player_faction",
    [
      (store_script_param, ":liege", 1),
      
      #This moved to top, so that mnu_notification does not occur twice
      (try_begin),
        (faction_slot_eq, "fac_player_supporters_faction", slot_faction_state, sfs_inactive),
        (neg|is_between, ":liege", pretenders_begin, pretenders_end),
        (call_script, "script_add_notification_menu", "mnu_notification_player_faction_active", 0, 0),
      (try_end),
      
      
      (faction_set_slot, "fac_player_supporters_faction", slot_faction_state, sfs_active),
      (faction_set_slot, "fac_player_supporters_faction", slot_faction_leader, ":liege"),
      
      (assign, ":original_kingdom", "$players_kingdom"),
      
      (try_begin),
        (is_between, ":original_kingdom", npc_kingdoms_begin, npc_kingdoms_end),
        (call_script, "script_player_leave_faction", 0), #Ends quests, transfers control of centers
      (try_end),
      
      #Name faction
      (try_begin),
        (is_between, ":liege", active_npcs_begin, active_npcs_end),
        (store_faction_of_troop, ":liege_faction"),
        (is_between, ":liege_faction", npc_kingdoms_begin, npc_kingdoms_end),
        (faction_get_slot, reg0, ":liege_faction", slot_faction_culture),
        (str_store_faction_name, s1, reg0),
        (faction_set_name, "fac_player_supporters_faction", "@{s1} Rebels"),
      (else_try),
        (str_store_troop_name, s2, ":liege"),
        (str_store_string, s1, "str_s2s_rebellion"),
      (try_end),
      (faction_set_color, "fac_player_supporters_faction", "$current_color"),
      
      (assign, "$players_kingdom", "fac_player_supporters_faction"),
      (assign, "$g_player_banner_granted", 1),
      
      
      
      #Any oaths renounced?
      (try_begin),
        (is_between, ":original_kingdom", npc_kingdoms_begin, npc_kingdoms_end),
        
        (faction_get_slot, ":old_leader", ":original_kingdom", slot_faction_leader),
        (call_script, "script_add_log_entry", logent_renounced_allegiance,   "trp_player",  -1, ":old_leader", "$players_kingdom"),
        
        #Initializing renounce war variables
        (assign, "$players_oath_renounced_against_kingdom", ":original_kingdom"),
        (assign, "$players_oath_renounced_given_center", 0),
        (store_current_hours, "$players_oath_renounced_begin_time"),
        
        (try_for_range, ":cur_center", walled_centers_begin, walled_centers_end),
          (store_faction_of_party, ":cur_center_faction", ":cur_center"),
          (party_set_slot, ":cur_center", slot_center_faction_when_oath_renounced, ":cur_center_faction"),
        (try_end),
        (party_set_slot, "$g_center_to_give_to_player", slot_center_faction_when_oath_renounced, "$players_oath_renounced_against_kingdom"),
        
        (store_relation, ":relation", ":original_kingdom", "fac_player_faction"),
        (ge, ":relation", 0),
        (call_script, "script_diplomacy_start_war_between_kingdoms", ":original_kingdom", "fac_player_supporters_faction", logent_faction_declares_war_out_of_personal_enmity),	#MOTO chief pass log entries
      (try_end),
      
      
      (try_begin),
        (troop_get_slot, ":spouse", "trp_player", slot_troop_spouse),
        (is_between, ":spouse", kingdom_ladies_begin, kingdom_ladies_end),
        
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (str_store_troop_name, s4, ":spouse"),
          (display_message, "@{!}DEBUG - {s4} faction changed by marriage, case 2"),
        (try_end),
        
        (troop_set_faction, ":spouse", "fac_player_supporters_faction"),
      (try_end),
      
      
      #(call_script, "script_store_average_center_value_per_faction"),
      (call_script, "script_update_all_notes"),
      (assign, "$g_recalculate_ais", 1),
      
  ]),
  
  
  
  #script_agent_reassign_team
  # INPUT: arg1 = agent_no
  # OUTPUT: none
  ("agent_reassign_team",
    [
      (store_script_param, ":agent_no", 1),
      (get_player_agent_no, ":player_agent"),
      (try_begin),
        (ge, ":player_agent", 0),
        (agent_is_human, ":agent_no"),
        (agent_is_ally, ":agent_no"),
        (agent_get_party_id, ":party_no", ":agent_no"),
        (neq, ":party_no", "p_main_party"),
        (assign, ":continue", 1),
        (store_faction_of_party, ":party_faction", ":party_no"),
        (try_begin),
          (eq, ":party_faction", "$players_kingdom"),
          (is_between, "$players_kingdom", kingdoms_begin, kingdoms_end),
          (faction_slot_eq, "$players_kingdom", slot_faction_marshal, "trp_player"),
          (assign, ":continue", 0),
        (else_try),
          (party_stack_get_troop_id, ":leader_troop_id", ":party_no", 0),
          (neg|is_between, ":leader_troop_id", active_npcs_begin, active_npcs_end),
          (assign, ":continue", 0),
        (try_end),
        (eq, ":continue", 1),
        (agent_get_team, ":player_team", ":player_agent"),
        (val_add, ":player_team", 2),
        (agent_set_team, ":agent_no", ":player_team"),
      (try_end),
  ]),
  
  #script_start_quest
  # INPUT: arg1 = quest_no, arg2 = giver_troop_no, s2 = description_text
  # OUTPUT: none
  ("start_quest",
    [(store_script_param, ":quest_no", 1),
      (store_script_param, ":giver_troop_no", 2),
      
      (quest_set_slot, ":quest_no", slot_quest_giver_troop, ":giver_troop_no"),
      
      (try_begin),
        (eq, ":giver_troop_no", -1),
        (str_store_string, s63, "str_political_suggestion"),
      (else_try),
        (is_between, ":giver_troop_no", active_npcs_begin, active_npcs_end),
        (str_store_troop_name_link, s62, ":giver_troop_no"),
        (str_store_string, s63, "@Given by: {s62}"),
      (else_try),
        (str_store_troop_name, s62, ":giver_troop_no"),
        (str_store_string, s63, "@Given by: {s62}"),
      (try_end),
      (store_current_hours, ":cur_hours"),
      (str_store_date, s60, ":cur_hours"),
      (str_store_string, s60, "@Given on: {s60}"),
      (add_quest_note_from_sreg, ":quest_no", 0, s60, 0),
      (add_quest_note_from_sreg, ":quest_no", 1, s63, 0),
      (add_quest_note_from_sreg, ":quest_no", 2, s2, 0),
      
      (try_begin),
        (quest_slot_ge, ":quest_no", slot_quest_expiration_days, 1),
        (quest_get_slot, reg0, ":quest_no", slot_quest_expiration_days),
        (add_quest_note_from_sreg, ":quest_no", 7, "@You have {reg0} days to finish this quest.", 0),
      (try_end),
      
      #Adding dont_give_again_for_days value
      (try_begin),
        (quest_slot_ge, ":quest_no", slot_quest_dont_give_again_period, 1),
        (quest_get_slot, ":dont_give_again_period", ":quest_no", slot_quest_dont_give_again_period),
        (quest_set_slot, ":quest_no", slot_quest_dont_give_again_remaining_days, ":dont_give_again_period"),
      (try_end),
      (start_quest, ":quest_no", ":giver_troop_no"),
      
      (try_begin),
        (eq, ":quest_no", "qst_report_to_army"),
        (assign, "$number_of_report_to_army_quest_notes", 8),
        (faction_get_slot, ":faction_ai_state", "$players_kingdom", slot_faction_ai_state),
        (call_script, "script_update_report_to_army_quest_note", "$players_kingdom", ":faction_ai_state", -1),
      (try_end),
      
      (display_message, "str_quest_log_updated"),
  ]),
  
  #script_conclude_quest
  # INPUT: arg1 = quest_no
  # OUTPUT: none
  ("conclude_quest",
    [
      (store_script_param, ":quest_no", 1),
      (conclude_quest, ":quest_no"),
      (quest_get_slot, ":quest_giver_troop", ":quest_no", slot_quest_giver_troop),
      (str_store_troop_name, s59, ":quest_giver_troop"),
      (try_begin),
        (gt, ":quest_giver_troop", 0),
        (str_store_troop_name, s59, ":quest_giver_troop"),
        (add_quest_note_from_sreg, ":quest_no", 7, "@This quest has been concluded. Give {s59} the news.", 0),
      (else_try),
        (add_quest_note_from_sreg, ":quest_no", 7, "@This quest has been concluded.", 0),
      (try_end),
  ]),
  
  #script_succeed_quest
  # INPUT: arg1 = quest_no
  # OUTPUT: none
  ("succeed_quest",
    [
      (store_script_param, ":quest_no", 1),
      (succeed_quest, ":quest_no"),
      (quest_get_slot, ":quest_giver_troop", ":quest_no", slot_quest_giver_troop),
      (try_begin),
        (gt, ":quest_giver_troop", 0),
        (str_store_troop_name, s59, ":quest_giver_troop"),
        (add_quest_note_from_sreg, ":quest_no", 7, "@This quest has been successfully completed. Talk to {s59}.", 0),
      (else_try),
        (add_quest_note_from_sreg, ":quest_no", 7, "@This quest has been successfully completed.", 0),
      (try_end),
  ]),
  
  #script_fail_quest
  # INPUT: arg1 = quest_no
  # OUTPUT: none
  ("fail_quest",
    [
      (store_script_param, ":quest_no", 1),
      (fail_quest, ":quest_no"),
      (quest_get_slot, ":quest_giver_troop", ":quest_no", slot_quest_giver_troop),
      (try_begin),
        (gt, ":quest_giver_troop", 0),
        (str_store_troop_name, s59, ":quest_giver_troop"),
        (add_quest_note_from_sreg, ":quest_no", 7, "@This quest has failed. Talk to {s59} to explain the situation.", 0),
      (else_try),
        (add_quest_note_from_sreg, ":quest_no", 7, "@This quest has failed.", 0),
      (try_end),
  ]),
  
  #script_report_quest_troop_positions
  # INPUT: arg1 = quest_no, arg2 = troop_no, arg3 = note_index
  # OUTPUT: none
  ("report_quest_troop_positions",
    [
      (store_script_param, ":quest_no", 1),
      (store_script_param, ":troop_no", 2),
      (store_script_param, ":note_index", 3),
      (call_script, "script_get_information_about_troops_position", ":troop_no", 1),
      (str_store_string, s5, "@At the time quest was given:^{s1}"),
      (add_quest_note_from_sreg, ":quest_no", ":note_index", s5, 1),
      (call_script, "script_update_troop_location_notes", ":troop_no", 1),
  ]),
  
  #script_end_quest
  # INPUT: arg1 = quest_no
  # OUTPUT: none
  ("end_quest",
    [
      (store_script_param, ":quest_no", 1),
      (str_clear, s1),
      (add_quest_note_from_sreg, ":quest_no", 0, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 1, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 2, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 3, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 4, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 5, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 6, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 7, s1, 0),
      (try_begin),
        (neg|check_quest_failed, ":quest_no"),
        (val_add, "$g_total_quests_completed", 1),
      (try_end),
      (try_begin),
        (eq, ":quest_no", "qst_consult_with_minister"),
        (assign, "$g_minister_notification_quest", 0),
      (try_end),
      (complete_quest, ":quest_no"),
      (try_begin),
        (this_or_next|is_between, ":quest_no", mayor_quests_begin, mayor_quests_end),
        (is_between, ":quest_no", mayor_quests_begin_2, mayor_quests_end_2),
        (assign, "$merchant_quest_last_offerer", -1),
        (assign, "$merchant_offered_quest", -1),
      (try_end),
  ]),
  
  #script_cancel_quest
  # INPUT: arg1 = quest_no
  # OUTPUT: none
  ("cancel_quest",
    [(store_script_param, ":quest_no", 1),
      (str_clear, s1),
      (add_quest_note_from_sreg, ":quest_no", 0, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 1, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 2, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 3, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 4, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 5, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 6, s1, 0),
      (add_quest_note_from_sreg, ":quest_no", 7, s1, 0),
      (cancel_quest, ":quest_no"),
      (try_begin),
        (this_or_next|is_between, ":quest_no", mayor_quests_begin, mayor_quests_end),
        (is_between, ":quest_no", mayor_quests_begin_2, mayor_quests_end_2),
        (assign, "$merchant_quest_last_offerer", -1),
        (assign, "$merchant_offered_quest", -1),
      (try_end),
  ]),
  
  ##  #script_get_available_mercenary_troop_and_amount_of_center
  ##  # INPUT: arg1 = center_no
  ##  # OUTPUT: reg0 = mercenary_troop_type, reg1 = amount
  ##  ("get_available_mercenary_troop_and_amount_of_center",
  ##    [(store_script_param, ":center_no", 1),
  ##     (party_get_slot, ":mercenary_troop", ":center_no", slot_center_mercenary_troop_type),
  ##     (party_get_slot, ":mercenary_amount", ":center_no", slot_center_mercenary_troop_amount),
  ##     (party_get_free_companions_capacity, ":free_capacity", "p_main_party"),
  ##     (val_min, ":mercenary_amount", ":free_capacity"),
  ##     (store_troop_gold, ":cur_gold", "trp_player"),
  ##     (call_script, "script_game_get_join_cost", ":mercenary_troop"),
  ##     (assign, ":join_cost", reg0),
  ##     (try_begin),
  ##       (gt, ":join_cost", 0),
  ##       (val_div, ":cur_gold", ":join_cost"),
  ##       (val_min, ":mercenary_amount", ":cur_gold"),
  ##     (try_end),
  ##     (assign, reg0, ":mercenary_troop"),
  ##     (assign, reg1, ":mercenary_amount"),
  ##     ]),
  ##
  
  #script_update_village_market_towns
  # INPUT: none
  # OUTPUT: none
  ("update_village_market_towns",
    [(try_for_range, ":cur_village", villages_begin, villages_end),
        (party_get_slot, reg1, ":cur_village", slot_village_bound_center),	#assure every walled center has food supply (artifact of center placement)
        (party_set_slot, ":cur_village", slot_village_market_town, reg1),
        # (store_faction_of_party, ":village_faction", ":cur_village"),
        # (assign, ":min_dist", 999999),
        # (assign, ":min_dist_town", -1),
        # (try_for_range, ":cur_town", walled_centers_begin, walled_centers_end),
        # (store_faction_of_party, ":town_faction", ":cur_town"),
        # (eq, ":town_faction", ":village_faction"),
        # (store_distance_to_party_from_party, ":cur_dist", ":cur_village", ":cur_town"),
        # (lt, ":cur_dist", ":min_dist"),
        # (assign, ":min_dist", ":cur_dist"),
        # (assign, ":min_dist_town", ":cur_town"),
        # (try_end),
        
        # (try_begin),
        # (gt, ":min_dist_town", -1),
        # (party_set_slot, ":cur_village", slot_village_market_town, ":min_dist_town"),
        # (else_try),
        # (assign, ":min_dist", 999999),
        # (assign, ":min_dist_town", -1),
        # (try_for_range, ":cur_town", walled_centers_begin, walled_centers_end),
        # (store_faction_of_party, ":town_faction", ":cur_town"),
        # (store_relation, ":relation", ":town_faction", ":village_faction"),
        # (ge, ":relation", 0),
        # (store_distance_to_party_from_party, ":cur_dist", ":cur_village", ":cur_town"),
        # (lt, ":cur_dist", ":min_dist"),
        # (assign, ":min_dist", ":cur_dist"),
        # (assign, ":min_dist_town", ":cur_town"),
        # (try_end),
        # (gt, ":min_dist_town", -1),
        # (party_set_slot, ":cur_village", slot_village_market_town, ":min_dist_town"),
        # (try_end),
      (try_end),
  ]),
  
  
  
  #script_update_mercenary_units_of_towns
  # INPUT: none
  # OUTPUT: none
  # reworked by JuJu70 also to ensure save game compatibility
  ("update_mercenary_units_of_towns",
    [(try_for_range, ":town_no", towns_begin, towns_end),
        (assign, ":troop_no", -1),
        (party_get_slot, ":center_culture", ":town_no", slot_center_culture),
        (try_begin),
          (eq, ":town_no", "p_town_11"), #dorestad frisia
          (store_random_in_range, ":troop_no", "trp_watchman", "trp_mercenary_leader"),
        (else_try),
          (eq, ":center_culture", "fac_culture_norse"),
          (store_random_in_range, ":troop_no", "trp_caravan_guard", mercenary_troops_end),
        (else_try),
          (store_random_in_range, ":troop_no", mercenary_troops_begin, mercenary_troops_end),
        (try_end),
        (try_begin),
          (eq, ":troop_no", "trp_regular_sailors"),
          (party_slot_eq, ":town_no",slot_town_port, 1),
        (else_try),
          (eq, ":troop_no", "trp_regular_sailors"),
          (store_random_in_range, ":rand",0, 2),
          (try_begin),
            (eq, ":rand", 0),
            (val_add, ":troop_no", 1),
          (else_try),
            (val_sub, ":troop_no", 1),
          (try_end),
        (try_end),
        (try_begin),
          (party_slot_eq, ":town_no",slot_town_port, 1),
          (neq, ":troop_no", "trp_regular_sailors"),
          (store_random_in_range, ":rand", 0,100),
          (lt, ":rand", 20),
          (assign, ":troop_no","trp_regular_sailors"),
        (try_end),
        (party_set_slot, ":town_no", slot_center_mercenary_troop_type, ":troop_no"),
        
        #chief cambia para los capitanes mercenarios
        (try_begin),
          (eq, ":troop_no", "trp_mercenary_leader"),
          (assign, ":amount", 1), #script chief cambia el numero de mercenarios reclutables
        (else_try),
          (store_random_in_range, ":amount", 3, 9), #script chief cambia el numero de mercenarios reclutables
          (store_skill_level, ":skill", "skl_leadership", "trp_player"), #leadership add men chief
          (val_add, ":amount", ":skill"),
        (try_end),
        
        (party_set_slot, ":town_no", slot_center_mercenary_troop_amount, ":amount"),
      (try_end),
  ]),
  
  #script_update_volunteer_troops_in_village
  # INPUT: arg1 = center_no
  # OUTPUT: none
  # Reworked by JuJu70
  ("update_volunteer_troops_in_village",
    [
      (store_script_param, ":center_no", 1),
      (troop_get_slot, ":religion","trp_player", slot_troop_religion),
      (party_get_slot, ":center_culture", ":center_no", slot_center_culture),
      (troop_get_slot, ":player_renown", "trp_player", slot_troop_renown), #renown
      (party_get_slot, ":center_relation", ":center_no", slot_center_player_relation),
      (party_get_slot, ":faith", ":center_no", slot_center_faithratio),
      (assign, ":troop_isfarmer", slot_faction_tier_1_troop), #basic
      (assign, ":volunteer_troop_tier", 1),
      
      (try_begin),
        (this_or_next|is_between, ":center_no", "p_village_63", "p_village_65"), #center is in Friese
        (this_or_next|is_between, ":center_no", "p_village_110", "p_village_112"), #center is in Friese
        (eq, ":center_no", "p_village_150"), #doccinga
        (store_random_in_range, ":rand", 0, 100),
        (try_begin),
          (gt, ":rand", 25),
          (assign, ":volunteer_troop", "trp_frisian_basic"),
        (else_try),
          (faction_get_slot, ":volunteer_troop", ":center_culture", ":troop_isfarmer"),
        (try_end),
      (else_try),
        (is_between, ":center_no", "p_village_1", "p_village_9"), #Northumbria
        (store_random_in_range, ":rand", 0, 100),
        (try_begin),
          (gt, ":rand", 25),
          (assign, ":volunteer_troop", "trp_angle_slave"),
        (else_try),
          (faction_get_slot, ":volunteer_troop", ":center_culture", ":troop_isfarmer"),
        (try_end),
      (else_try),
        (faction_get_slot, ":volunteer_troop", ":center_culture", ":troop_isfarmer"),
      (try_end),
      (assign, ":upgrade_troop_no", 0),
      (try_begin),
        (party_slot_eq, ":center_no", slot_town_lord, "trp_player"), #player is lord, he dont want spoilers, he want farmers with spear and shield
        (ge, ":center_relation", 20), #player has good relation with center
        (store_random_in_range, ":rand", 0, 100),
        (try_begin),
          (gt, ":rand", 50),
          (store_random_in_range, ":random", 0, 20),
          (try_begin),
            (gt, ":random", 10),
            (troop_get_upgrade_troop, ":upgrade_troop_no", ":volunteer_troop", 1),
          (else_try),
            (troop_get_upgrade_troop, ":upgrade_troop_no", ":volunteer_troop", 0),
          (try_end),
          (try_begin),
            (le, ":upgrade_troop_no", 0),
            (troop_get_upgrade_troop, ":upgrade_troop_no", ":volunteer_troop", 0),
          (try_end),
          (gt, ":upgrade_troop_no", 0),
          (assign, ":volunteer_troop",":upgrade_troop_no"),
          (val_add, ":volunteer_troop_tier", 1),
        (try_end),
      (else_try),
        (ge, ":center_relation", 60), #player has good relation with center
        (store_random_in_range, ":random", 0, 20),
        (try_begin),
          (gt, ":random", 10),
          (troop_get_upgrade_troop, ":upgrade_troop_no", ":volunteer_troop", 1),
        (else_try),
          (troop_get_upgrade_troop, ":upgrade_troop_no", ":volunteer_troop", 0),
        (try_end),
        (try_begin),
          (le, ":upgrade_troop_no", 0),
          (troop_get_upgrade_troop, ":upgrade_troop_no", ":volunteer_troop", 0),
        (try_end),
        (gt, ":upgrade_troop_no", 0),
        (assign, ":volunteer_troop",":upgrade_troop_no"),
        (val_add, ":volunteer_troop_tier", 1),
      (try_end),
      
      (store_div, ":tier_upgrades", ":center_relation", 20),
      (store_character_level, ":level", ":volunteer_troop"),
      (try_begin),
        (lt, ":level", 18),
        (try_for_range, ":unused", 0, ":tier_upgrades"),
          (store_random_in_range, ":random_no", 0, 100),
          (lt, ":random_no", 10),
          (store_random_in_range, ":random_no", 0, 2),
          (troop_get_upgrade_troop, ":upgrade_troop_no", ":volunteer_troop", ":random_no"),
          (try_begin),
            (le, ":upgrade_troop_no", 0),
            (troop_get_upgrade_troop, ":upgrade_troop_no", ":volunteer_troop", 0),
          (try_end),
          (gt, ":upgrade_troop_no", 0),
          (val_add, ":volunteer_troop_tier", 1),
          (assign, ":volunteer_troop", ":upgrade_troop_no"),
        (try_end),
      (try_end),
      
      ##renown is important for men want to join to player
      (assign, ":upper_limit", 7),
      (try_begin),
        (ge, ":player_renown", 100),
        (store_sub, ":renown_bonus", ":player_renown", 100),
        (val_div, ":renown_bonus", 100),
        (val_add, ":upper_limit", ":renown_bonus"),
      (try_end),
      (try_begin),
        (party_slot_eq, ":center_no", slot_town_lord, "trp_player"), #player is lord
        (val_add, ":upper_limit", 2),
      (try_end),
      ## religious adjustment
      (try_begin),
        (eq, ":religion", 1),
        (val_mul, ":upper_limit", ":faith"),
        (val_div, ":upper_limit", 100),
        (val_add, ":upper_limit", 1),
      (else_try),
        (eq, ":religion", 2),
        (store_sub, ":p_faith", 100, ":faith"),
        (val_mul, ":upper_limit", ":p_faith"),
        (val_div, ":upper_limit", 100),
        (val_add, ":upper_limit", 1),
      (try_end),
      ## charisma adjustment
      (store_attribute_level, ":charisma", "trp_player", ca_charisma),
      (val_div, ":charisma", 10),
      (val_add, ":upper_limit", ":charisma"),
      (store_skill_level, ":leadership", "skl_leadership", "trp_player"),
      (val_div, ":leadership", 5),
      (val_add, ":upper_limit", ":leadership"),
      (try_begin),
        (ge, ":center_relation", 20), #chief change to 20
        (store_div, ":rec_bonus", ":center_relation", 10),
        (val_add, ":upper_limit", ":rec_bonus"),
      (else_try),
        (lt, ":center_relation", 0),
        (assign, ":upper_limit", 0),
      (try_end),
      
      (val_mul, ":upper_limit", 3),
      (store_add, ":amount_random_divider", 2, ":volunteer_troop_tier"),
      (val_div, ":upper_limit", ":amount_random_divider"),
      
      (store_random_in_range, ":amount", 0, ":upper_limit"),
      (party_set_slot, ":center_no", slot_center_volunteer_troop_type, ":volunteer_troop"),
      (party_set_slot, ":center_no", slot_center_volunteer_troop_amount, ":amount"),
  ]),
  
  #script_update_npc_volunteer_troops_in_village
  # INPUT: arg1 = center_no
  # OUTPUT: none
  ("update_npc_volunteer_troops_in_village",
    [
      (store_script_param, ":center_no", 1),
      (party_get_slot, ":center_culture", ":center_no", slot_center_culture),
      (faction_get_slot, ":volunteer_troop", ":center_culture", slot_faction_tier_1_troop),
      (assign, ":volunteer_troop_tier", 1),
      (try_for_range, ":unused", 0, 5),
        (store_random_in_range, ":random_no", 0, 100),
        (lt, ":random_no", 10),
        (store_random_in_range, ":random_no", 0, 2),
        (troop_get_upgrade_troop, ":upgrade_troop_no", ":volunteer_troop", ":random_no"),
        (try_begin),
          (le, ":upgrade_troop_no", 0),
          (troop_get_upgrade_troop, ":upgrade_troop_no", ":volunteer_troop", 0),
        (try_end),
        (gt, ":upgrade_troop_no", 0),
        (val_add, ":volunteer_troop_tier", 1),
        (assign, ":volunteer_troop", ":upgrade_troop_no"),
      (try_end),
      
      (assign, ":upper_limit", 24),
      
      (store_character_level, ":level", "trp_player"),
      (store_mul, ":adj_level", ":level", 12),
      (val_div, ":adj_level", 20),
      (val_add, ":upper_limit", ":adj_level"),
      
      (store_add, ":amount_random_divider", 2, ":volunteer_troop_tier"),
      (val_div, ":upper_limit", ":amount_random_divider"),
      
      (store_random_in_range, ":amount", 0, ":upper_limit"),
      (party_set_slot, ":center_no", slot_center_npc_volunteer_troop_type, ":volunteer_troop"),
      (party_set_slot, ":center_no", slot_center_npc_volunteer_troop_amount, ":amount"),
  ]),
  
  #script_update_companion_candidates_in_taverns
  # INPUT: none
  # OUTPUT: none
  ("update_companion_candidates_in_taverns",
    [
      (try_begin),
        (eq, "$cheat_mode", 1),
        (display_message, "str_shuffling_companion_locations"),
      (try_end),
      
      (try_for_range, ":troop_no", companions_begin, companions_end),
        (troop_set_slot, ":troop_no", slot_troop_cur_center, -1),
        (troop_slot_eq, ":troop_no", slot_troop_days_on_mission, 0),
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_inactive),
        
        (neg|troop_slot_ge, ":troop_no", slot_troop_prisoner_of_party, 0),
        
        (store_random_in_range, ":town_no", towns_begin, towns_end),
        (try_begin),
          (neg|troop_slot_eq, ":troop_no", slot_troop_home, ":town_no"),
          (neg|troop_slot_eq, ":troop_no", slot_troop_first_encountered, ":town_no"),
          (troop_set_slot, ":troop_no", slot_troop_cur_center, ":town_no"),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, 4, ":troop_no"),
            (str_store_party_name, 5, ":town_no"),
            (display_message, "@{!}{s4} is in {s5}"),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  #script_update_ransom_brokers
  # INPUT: none
  # OUTPUT: none
  ("update_ransom_brokers",
    [(try_for_range, ":town_no", towns_begin, towns_end),
        (party_set_slot, ":town_no", slot_center_ransom_broker, 0),
      (try_end),
      
      (try_for_range, ":troop_no", ransom_brokers_begin, "trp_ransom_broker_20"),
        (store_random_in_range, ":town_no", towns_begin, towns_end),
        (party_set_slot, ":town_no", slot_center_ransom_broker, ":troop_no"),
      (try_end),
      
      (party_set_slot,"p_town_2",slot_center_ransom_broker,"trp_ramun_the_slave_trader"),
  ]),
  
  #script_update_tavern_travellers
  # INPUT: none
  # OUTPUT: none
  ("update_tavern_travellers",
    [(try_for_range, ":town_no", towns_begin, towns_end),
        (party_set_slot, ":town_no", slot_center_tavern_traveler, 0),
      (try_end),
      
      (try_for_range, ":troop_no", tavern_travelers_begin, tavern_travelers_end),
        (store_random_in_range, ":town_no", towns_begin, towns_end),
        (party_set_slot, ":town_no", slot_center_tavern_traveler, ":troop_no"),
        (assign, ":end_cond", 15),
        (try_for_range, ":unused", 0, ":end_cond"),
          (store_random_in_range, ":info_faction", kingdoms_begin, kingdoms_end),
          (faction_slot_eq, ":info_faction", slot_faction_state, sfs_active),
          (neq, ":info_faction", "$players_kingdom"),
          (neq, ":info_faction", "fac_player_supporters_faction"),
          (party_set_slot, ":town_no", slot_center_traveler_info_faction, ":info_faction"),
          (assign, ":end_cond", 0),
        (try_end),
      (try_end),
      
      (troop_set_slot, "trp_belligerent_drunk", slot_troop_cur_center, "p_town_1"),
  ]),
  
  #script_update_villages_infested_by_bandits
  # INPUT: none
  # OUTPUT: none
  ("update_villages_infested_by_bandits",
    [#(try_for_range, ":village_no", villages_begin, villages_end),
      (store_script_param, ":village_no", 1),
      (try_begin),
        (check_quest_active, "qst_eliminate_bandits_infesting_village"),
        (quest_slot_eq, "qst_eliminate_bandits_infesting_village", slot_quest_target_center, ":village_no"),
        (quest_get_slot, ":cur_state", "qst_eliminate_bandits_infesting_village", slot_quest_current_state),
        (val_add, ":cur_state", 1),
        (try_begin),
          (lt, ":cur_state", 3),
          (quest_set_slot, "qst_eliminate_bandits_infesting_village", slot_quest_current_state, ":cur_state"),
        (else_try),
          (party_set_slot, ":village_no", slot_village_infested_by_bandits, 0),
          (call_script, "script_abort_quest", "qst_eliminate_bandits_infesting_village", 2),
        (try_end),
      (else_try),
        (check_quest_active, "qst_deal_with_bandits_at_lords_village"),
        (quest_slot_eq, "qst_deal_with_bandits_at_lords_village", slot_quest_target_center, ":village_no"),
        (quest_get_slot, ":cur_state", "qst_deal_with_bandits_at_lords_village", slot_quest_current_state),
        (val_add, ":cur_state", 1),
        (try_begin),
          (lt, ":cur_state", 3),
          (quest_set_slot, "qst_deal_with_bandits_at_lords_village", slot_quest_current_state, ":cur_state"),
        (else_try),
          (party_set_slot, ":village_no", slot_village_infested_by_bandits, 0),
          (call_script, "script_abort_quest", "qst_deal_with_bandits_at_lords_village", 2),
        (try_end),
      (else_try),
        (party_set_slot, ":village_no", slot_village_infested_by_bandits, 0),
        (store_random_in_range, ":random_no", 0, 100),
        (assign, ":continue", 1),
        (try_begin),
          (check_quest_active, "qst_collect_taxes"),
          (quest_slot_eq, "qst_collect_taxes", slot_quest_target_center, ":village_no"),
          (assign, ":continue", 0),
        (else_try),
          (check_quest_active, "qst_train_peasants_against_bandits"),
          (quest_slot_eq, "qst_train_peasants_against_bandits", slot_quest_target_center, ":village_no"),
          (assign, ":continue", 0),
        (try_end),
        (eq, ":continue", 1),
        (lt, ":random_no", 3),
        (store_random_in_range, ":random_no", 0, 5),
        (store_add, ":bandit_troop", "trp_bandit", ":random_no"),
        (party_set_slot, ":village_no", slot_village_infested_by_bandits, ":bandit_troop"),
        #Reduce prosperity of the village by 3: reduce to -1
        (call_script, "script_change_center_prosperity", ":village_no", -1),
        (val_add, "$newglob_total_prosperity_from_bandits", -1),
        (try_begin),
          (eq, "$cheat_mode", 2),
          (str_store_party_name, s1, ":village_no"),
          (display_message, "@{!}DEBUG --{s1} is infested by bandits."),
        (try_end),
      (try_end),
      # (try_end),
  ]),
  
  #script_update_booksellers
  # INPUT: none
  # OUTPUT: none
  ("update_booksellers",
    [(try_for_range, ":town_no", towns_begin, towns_end),
        (party_set_slot, ":town_no", slot_center_tavern_bookseller, 0),
      (try_end),
      
      (try_for_range, ":troop_no", tavern_booksellers_begin, tavern_booksellers_end),
        (store_random_in_range, ":town_no", towns_begin, towns_end),
        (party_set_slot, ":town_no", slot_center_tavern_bookseller, ":troop_no"),
      (try_end),
  ]),
  
  #script_update_tavern_minstels
  # INPUT: none
  # OUTPUT: none
  ("update_tavern_minstrels",
    [(try_for_range, ":town_no", towns_begin, towns_end),
        (party_set_slot, ":town_no", slot_center_tavern_minstrel, 0),
      (try_end),
      
      #    (try_for_range, ":troop_no", tavern_minstrels_begin, tavern_minstrels_end), #chief add quastuosas here Happy widow in VC
      (try_for_range, ":troop_no", tavern_minstrels_begin, quastuosa_end), #chief add quastuosas here Happy widow in VC
        
        (store_random_in_range, ":town_no", towns_begin, towns_end),
        (party_set_slot, ":town_no", slot_center_tavern_minstrel, ":troop_no"),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (str_store_troop_name, s4, ":troop_no"),
          (str_store_party_name, s5, ":town_no"),
          
          (display_message, "str_s4_is_at_s5"),
        (try_end),
      (try_end),
      
      
  ]),
  
  ("update_other_taverngoers",
    [
      (store_random_in_range, ":fight_promoter_tavern", towns_begin, towns_end),
      (troop_set_slot, "trp_fight_promoter", slot_troop_cur_center, ":fight_promoter_tavern"),
      
      (store_random_in_range, ":belligerent_drunk_tavern", towns_begin, towns_end),
      (troop_set_slot, "trp_belligerent_drunk", slot_troop_cur_center, ":belligerent_drunk_tavern"),
      
  ]),
  
  
  #script_update_faction_notes
  # INPUT: faction_no
  # OUTPUT: none
  ("update_faction_notes",
    [
      (store_script_param, ":faction_no", 1),
      
      (try_begin),
        (this_or_next|faction_slot_eq, ":faction_no", slot_faction_state, sfs_inactive),
        (neg|is_between, ":faction_no", kingdoms_begin, kingdoms_end),
        (faction_set_note_available, ":faction_no", 0),
      (else_try),
        (faction_set_note_available, ":faction_no", 1),
      (try_end),
  ]),
  
  #script_update_faction_political_notes
  # INPUT: faction_no
  # OUTPUT: s11 dialog description of faction stability
  ("update_faction_political_notes", [
      (store_script_param, ":faction_no", 1),
      
      (try_begin),
        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_defeated),
        (str_store_faction_name, s5, ":faction_no"),
        (str_store_string, s11, "@The {s5} has been defeated!"),
      
      (else_try),
        (call_script, "script_evaluate_realm_stability", ":faction_no"),
        (add_faction_note_from_sreg, ":faction_no", 2, "str_instability_reg0_of_lords_are_disgruntled_reg1_are_restless", 0),
        
        #move this part from dialogs to avoid multiple copy/paste errors
        (assign, ":instability_index", reg0),
        (val_add, ":instability_index", reg0),
        (val_add, ":instability_index", reg1),
        
        (str_store_faction_name, s12, ":faction_no"),
        (try_begin),
          (ge, ":instability_index", 60),
          (str_store_string, s11, "str_the_s12_is_a_labyrinth_of_rivalries_and_grudges_lords_ignore_their_lieges_summons_and_many_are_ripe_to_defect"),
        (else_try),
          (ge, ":instability_index", 40),
          (str_store_string, s11, "str_the_s12_is_shaky_many_lords_do_not_cooperate_with_each_other_and_some_might_be_tempted_to_defect_to_a_liege_that_they_consider_more_worthy"),
        (else_try),
          (ge, ":instability_index", 20),
          (str_store_string, s11, "str_the_s12_is_fairly_solid_some_lords_bear_enmities_for_each_other_but_they_tend_to_stand_together_against_outside_enemies"),
        (else_try),
          (str_store_string, s11, "str_the_s12_is_a_rock_of_stability_politically_speaking_whatever_the_lords_may_think_of_each_other_they_fight_as_one_against_the_common_foe"),
        (try_end),
        
        (try_for_range, ":lord", active_npcs_begin, active_npcs_end),
          (troop_slot_eq, ":lord", slot_troop_occupation, slto_kingdom_hero),
          (store_faction_of_troop, ":lord_faction", ":lord"),
          (eq, ":lord_faction", ":faction_no"),
          (call_script, "script_update_troop_political_notes", ":lord"),
        (try_end),
      (try_end),
  ]),
  
  
  
  #script_update_faction_traveler_notes
  # INPUT: faction_no
  # OUTPUT: none
  ("update_faction_traveler_notes",
    [(store_script_param, ":faction_no", 1),
      (assign, ":total_men", 0),
      (try_for_parties, ":cur_party"),
        (store_faction_of_party, ":center_faction", ":cur_party"),
        (eq, ":center_faction", ":faction_no"),
        (party_get_num_companions, ":num_men", ":cur_party"),
        (val_add, ":total_men", ":num_men"),
      (try_end),
      (str_store_faction_name, s5, ":faction_no"),
      (assign, reg1, ":total_men"),
      (add_faction_note_from_sreg, ":faction_no", 1, "@The {s5} has a strength of {reg1} men in total.", 1),
  ]),
  
  
  #script_update_troop_notes
  # INPUT: troop_no
  # OUTPUT: none
  ("update_troop_notes",
    [
      ##      (store_script_param, ":troop_no", 1),
      ##     (str_store_troop_name, s54, ":troop_no"),
      ##     (try_begin),
      ##       (eq, ":troop_no", "trp_player"),
      ##       (this_or_next|eq, "$player_has_homage", 1),
      ##		(eq, "$players_kingdom", "fac_player_supporters_faction"),
      ##       (assign, ":troop_faction", "$players_kingdom"),
      ##     (else_try),
      ##       (store_troop_faction, ":troop_faction", ":troop_no"),
      ##     (try_end),
      ##
      ##	 (str_clear, s49),
      ##	 (try_begin),
      ##		(is_between, ":troop_no", lords_begin, kingdom_ladies_end),
      ##		(troop_get_slot, reg1, ":troop_no", slot_troop_age),
      ##		(str_store_string, s49, "str__age_reg1_family_"),
      ##
      ##		(try_for_range, ":aristocrat", lords_begin, kingdom_ladies_end),
      ##			(call_script, "script_troop_get_family_relation_to_troop", ":aristocrat", ":troop_no"),
      ##			(gt, reg0, 0),
      ##
      ##			(try_begin),
      ##				(neg|is_between, ":aristocrat", kingdom_ladies_begin, kingdom_ladies_end),
      ##				(str_store_troop_name_link, s12, ":aristocrat"),
      ##				(call_script, "script_troop_get_relation_with_troop", ":aristocrat", ":troop_no"),
      ##				(str_store_string, s49, "str_s49_s12_s11_rel_reg0"),
      ##			(else_try),
      ##				(str_store_troop_name, s12, ":aristocrat"),
      ##				(str_store_string, s49, "str_s49_s12_s11"),
      ##			(try_end),
      ##
      ##		(try_end),
      ##	 (try_end),
      ##
      ##     (try_begin),
      ##       (neq, ":troop_no", "trp_player"),
      ##       (neg|is_between, ":troop_faction", kingdoms_begin, kingdoms_end),
      ##       (str_clear, s54),
      ##       (add_troop_note_from_sreg, ":troop_no", 0, s54, 0),
      ##       (add_troop_note_from_sreg, ":troop_no", 1, s54, 0),
      ##       (add_troop_note_from_sreg, ":troop_no", 2, s54, 0),
      ###     (else_try),
      ###       (is_between, ":troop_no", kingdom_ladies_begin, kingdom_ladies_end),
      ###       (str_clear, s54),
      ###       (add_troop_note_from_sreg, ":troop_no", 0, s54, 0),
      ###       (add_troop_note_from_sreg, ":troop_no", 1, s54, 0),
      ###       (add_troop_note_from_sreg, ":troop_no", 2, s54, 0),
      ##     (else_try),
      ##       (is_between, ":troop_no", pretenders_begin, pretenders_end),
      ##       (neg|troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
      ##       (neq, ":troop_no", "$supported_pretender"),
      ##       (troop_get_slot, ":orig_faction", ":troop_no", slot_troop_original_faction),
      ##       (try_begin),
      ##         (faction_slot_eq, ":orig_faction", slot_faction_state, sfs_active),
      ##         (faction_slot_eq, ":orig_faction", slot_faction_has_rebellion_chance, 1),
      ##         (str_store_faction_name_link, s56, ":orig_faction"),
      ##         (add_troop_note_from_sreg, ":troop_no", 0, "@{s54} is a claimant to the throne of {s56}.", 0),
      ##         (add_troop_note_tableau_mesh, ":troop_no", "tableau_troop_note_mesh"),
      ##       (else_try),
      ##         (str_clear, s54),
      ##         (add_troop_note_from_sreg, ":troop_no", 0, s54, 0),
      ##         (add_troop_note_from_sreg, ":troop_no", 1, s54, 0),
      ##         (add_troop_note_from_sreg, ":troop_no", 2, s54, 0),
      ##       (try_end),
      ##     (else_try),
      ##       (faction_get_slot, ":faction_leader", ":troop_faction", slot_faction_leader),
      ##       (str_store_troop_name_link, s55, ":faction_leader"),
      ##       (str_store_faction_name_link, s56, ":troop_faction"),
      ##       (assign, ":troop_is_player_faction", 0),
      ##       (assign, ":troop_is_faction_leader", 0),
      ##       (try_begin),
      ##         (eq, ":troop_faction", "fac_player_faction"),
      ##         (assign, ":troop_is_player_faction", 1),
      ##       (else_try),
      ##         (eq, ":faction_leader", ":troop_no"),
      ##         (assign, ":troop_is_faction_leader", 1),
      ##       (try_end),
      ##       (assign, ":num_centers", 0),
      ##       (str_store_string, s58, "@nowhere"),
      ##       (try_for_range_backwards, ":cur_center", centers_begin, centers_end),
      ##         (party_slot_eq, ":cur_center", slot_town_lord, ":troop_no"),
      ##         (try_begin),
      ##           (eq, ":num_centers", 0),
      ##           (str_store_party_name_link, s58, ":cur_center"),
      ##         (else_try),
      ##           (eq, ":num_centers", 1),
      ##           (str_store_party_name_link, s57, ":cur_center"),
      ##           (str_store_string, s58, "@{s57} and {s58}"),
      ##         (else_try),
      ##           (str_store_party_name_link, s57, ":cur_center"),
      ##           (str_store_string, s58, "@{!}{s57}, {s58}"),
      ##         (try_end),
      ##         (val_add, ":num_centers", 1),
      ##       (try_end),
      ##       (troop_get_type, reg3, ":troop_no"),
      ##       (val_mod, reg3, 2),
      ##       (troop_get_slot, reg5, ":troop_no", slot_troop_renown),
      ##       (str_clear, s59),
      ##       (try_begin),
      ###         (troop_get_slot, ":relation", ":troop_no", slot_troop_player_relation),
      ##         (call_script, "script_troop_get_player_relation", ":troop_no"),
      ##         (assign, ":relation", reg0),
      ##         (store_add, ":normalized_relation", ":relation", 100),
      ##         (val_add, ":normalized_relation", 5),
      ##         (store_div, ":str_offset", ":normalized_relation", 10),
      ##         (val_clamp, ":str_offset", 0, 20),
      ##         (store_add, ":str_id", "str_relation_mnus_100_ns",  ":str_offset"),
      ##         (neq, ":str_id", "str_relation_plus_0_ns"),
      ##         (str_store_string, s60, "@{reg3?She:He}"),
      ##         (str_store_string, s59, ":str_id"),
      ##         (str_store_string, s59, "@{!}^{s59}"),
      ##       (try_end),
      ##
      ##	#lord recruitment changes begin
      ##	#This sends a bunch of political information to s47.
      ##
      ##
      ##
      ##
      ##	    #refresh registers
      ##        (assign, reg9, ":num_centers"),
      ##        (troop_get_type, reg3, ":troop_no"),
      ##        (val_mod, reg3, 2),
      ##        (troop_get_slot, reg5, ":troop_no", slot_troop_renown),
      ##		(assign, reg4, ":troop_is_faction_leader"),
      ##		(assign, reg6, ":troop_is_player_faction"),
      ##
      ##        (add_troop_note_from_sreg, ":troop_no", 0, "str_reg6reg4s54_is_the_ruler_of_s56_s54_is_a_vassal_of_s55_of_s56_renown_reg5_reg9reg3shehe_is_the_reg3ladylord_of_s58reg3shehe_has_no_fiefss59_s49", 0),
      ##	#lord recruitment changes end
      ##
      ##        (add_troop_note_tableau_mesh, ":troop_no", "tableau_troop_note_mesh"),
      ##     (try_end),
  ]),
  
  #script_update_troop_location_notes
  # INPUT: troop_no
  # OUTPUT: none
  ("update_troop_location_notes",
    [
      (store_script_param, ":troop_no", 1),
      (store_script_param, ":see_or_hear", 2),
      (try_begin),
        (call_script, "script_get_information_about_troops_position", ":troop_no", 1),
        (neq, reg0, 0),
        
        (call_script, "script_search_troop_prisoner_of_party", ":troop_no"),
        (eq, reg0, -1),
        
        (troop_get_type, reg1, ":troop_no"),
        (val_mod, reg1, 2),
        (try_begin),
          (eq, ":see_or_hear", 0),
          (add_troop_note_from_sreg, ":troop_no", 2, "@According to the last time you saw {reg1?her:him}, {s1}", 1),
        (else_try),
          (add_troop_note_from_sreg, ":troop_no", 2, "@According to the last time you heard about {reg1?her:him}, {s1}", 1),
        (try_end),
      (try_end),
  ]),
  
  #script_update_troop_location_notes_prisoned
  # INPUT: troop_no
  # OUTPUT: none
  ("update_troop_location_notes_prisoned",
    [
      (store_script_param, ":troop_no", 1),
      (store_script_param, ":capturer_faction_no", 2),
      
      (troop_get_type, reg1, ":troop_no"),
      (val_mod, reg1, 2),
      (str_store_faction_name_link, s1, ":capturer_faction_no"),
      
      (add_troop_note_from_sreg, ":troop_no", 2, "str_reg1shehe_is_prisoner_of_s1", 1),
  ]),
  
  ("update_troop_political_notes",
    [
      (store_script_param, ":troop_no", 1),
      (try_begin),
        (str_clear, s47),
        
        (store_faction_of_troop, ":troop_faction", ":troop_no"),
        (neq, ":troop_faction", "fac_adventurers"),
        (faction_get_slot, ":faction_leader", ":troop_faction", slot_faction_leader),
        
        (str_clear, s40),
        (assign, ":logged_a_rivalry", 0),
        (try_for_range, ":kingdom_hero", active_npcs_begin, active_npcs_end),
          (call_script, "script_troop_get_relation_with_troop", ":troop_no", ":kingdom_hero"),
          (lt, reg0, -10),
          
          (str_store_troop_name_link, s39, ":kingdom_hero"),
          (try_begin),
            (eq, ":logged_a_rivalry", 0),
            (str_store_string, s40, "str_s39_rival"),
            (assign, ":logged_a_rivalry", 1),
          (else_try),
            (str_store_string, s41, "str_s40"),
            (str_store_string, s40, "str_s41_s39_rival"),
          (try_end),
          
        (try_end),
        
        (str_clear, s46),
        (str_clear, s49),
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (try_begin),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_martial),
            (str_store_string, s46, "str_reputation_cheat_mode_only_martial_"),
          (else_try),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_debauched),
            (str_store_string, s46, "str_reputation_cheat_mode_only_debauched_"),
          (else_try),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_selfrighteous),
            (str_store_string, s46, "str_reputation_cheat_mode_only_pitiless_"),
          (else_try),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_cunning),
            (str_store_string, s46, "str_reputation_cheat_mode_only_calculating_"),
          (else_try),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_quarrelsome),
            (str_store_string, s46, "str_reputation_cheat_mode_only_quarrelsome_"),
          (else_try),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_goodnatured),
            (str_store_string, s46, "str_reputation_cheat_mode_only_goodnatured_"),
          (else_try),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_upstanding),
            (str_store_string, s46, "str_reputation_cheat_mode_only_upstanding_"),
          (else_try),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_conventional),
            (str_store_string, s46, "str_reputation_cheat_mode_only_conventional_"),
          (else_try),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_adventurous),
            (str_store_string, s46, "str_reputation_cheat_mode_only_adventurous_"),
          (else_try),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_otherworldly),
            (str_store_string, s46, "str_reputation_cheat_mode_only_romantic_"),
          (else_try),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_moralist),
            (str_store_string, s46, "str_reputation_cheat_mode_only_moralist_"),
          (else_try),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_ambitious),
            (str_store_string, s46, "str_reputation_cheat_mode_only_ambitious_"),
          (else_try),
            (troop_get_slot, reg11, ":troop_no", slot_lord_reputation_type),
            (str_store_string, s46, "str_reputation_cheat_mode_only_reg11_"),
          (try_end),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (try_for_range, ":love_interest_slot", slot_troop_love_interest_1, slot_troop_love_interests_end),
              (troop_get_slot, ":love_interest", ":troop_no", ":love_interest_slot"),
              (is_between, ":love_interest", kingdom_ladies_begin, kingdom_ladies_end),
              (str_store_troop_name_link, s39, ":love_interest"),
              (call_script, "script_troop_get_relation_with_troop", ":troop_no", ":love_interest"),
              (str_store_string, s2, "str_love_interest"),
              (try_begin),
                (troop_slot_eq, ":troop_no", slot_troop_betrothed, ":love_interest"),
                (str_store_string, s2, "str_betrothed"),
              (try_end),
              (str_store_string, s40, "str_s40_s39_s2_reg0"),
            (try_end),
          (try_end),
          
        (try_end),
        
        (str_store_string, s45, "str_other_relations_s40_"),
        
        (str_clear, s44),
        (try_begin),
          (neq, ":troop_no", ":faction_leader"),
          (call_script, "script_troop_get_relation_with_troop", ":troop_no", ":faction_leader"),
          (str_store_string, s44, "str_relation_with_liege_reg0_"),
        (try_end),
        
        (str_clear, s48),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (store_current_hours, ":hours"),
          (gt, ":hours", 0),
          #				(display_message, "@{!}Updating political factors"),
          (call_script, "script_calculate_troop_political_factors_for_liege", ":troop_no", ":faction_leader"),
          (str_store_string, s48, "str_sense_of_security_military_reg1_court_position_reg3_"),
        (try_end),
        (str_store_string, s47, "str_s46s45s44s48"),
        
        (add_troop_note_from_sreg, ":troop_no", 3, "str_political_details_s47_", 1),
        
      (try_end),
  ]),
  
  #script_update_center_notes
  # INPUT: center_no
  # OUTPUT: none
  ("update_center_notes",
    [
      ##      (store_script_param, ":center_no", 1),
      ##
      ##     (party_get_slot, ":lord_troop", ":center_no", slot_town_lord),
      ##     (try_begin),
      ##       (ge, ":lord_troop", 0),
      ##       (store_troop_faction, ":lord_faction", ":lord_troop"),
      ##       (str_store_troop_name_link, s1, ":lord_troop"),
      ##       (try_begin),
      ##         (eq, ":lord_troop", "trp_player"),
      ##         (gt, "$players_kingdom", 0),
      ##         (str_store_faction_name_link, s2, "$players_kingdom"),
      ##       (else_try),
      ##         (str_store_faction_name_link, s2, ":lord_faction"),
      ##       (try_end),
      ##       (str_store_party_name, s50, ":center_no"),
      ##       (try_begin),
      ##         (party_slot_eq, ":center_no", slot_party_type, spt_town),
      ##         (str_store_string, s51, "@The town of {s50}"),
      ##       (else_try),
      ##         (party_slot_eq, ":center_no", slot_party_type, spt_village),
      ##         (party_get_slot, ":bound_center", ":center_no", slot_village_bound_center),
      ##         (str_store_party_name_link, s52, ":bound_center"),
      ##         (str_store_string, s51, "@The village of {s50} near {s52}"),
      ##       (else_try),
      ##         (str_store_string, s51, "@{!}{s50}"),
      ##       (try_end),
      ##       (str_store_string, s2, "@{s51} belongs to {s1} of {s2}.^"),
      ##     (else_try),
      ##       (str_clear, s2),
      ##     (try_end),
      ##     (try_begin),
      ##       (is_between, ":center_no", villages_begin, villages_end),
      ##     (else_try),
      ##       (assign, ":num_villages", 0),
      ##       (try_for_range_backwards, ":village_no", villages_begin, villages_end),
      ##         (party_slot_eq, ":village_no", slot_village_bound_center, ":center_no"),
      ##         (try_begin),
      ##           (eq, ":num_villages", 0),
      ##           (str_store_party_name_link, s8, ":village_no"),
      ##         (else_try),
      ##           (eq, ":num_villages", 1),
      ##           (str_store_party_name_link, s7, ":village_no"),
      ##           (str_store_string, s8, "@{s7} and {s8}"),
      ##         (else_try),
      ##           (str_store_party_name_link, s7, ":village_no"),
      ##           (str_store_string, s8, "@{!}{s7}, {s8}"),
      ##         (try_end),
      ##         (val_add, ":num_villages", 1),
      ##       (try_end),
      ##       (try_begin),
      ##         (eq, ":num_villages", 0),
      ##         (str_store_string, s2, "@{s2}It has no villages.^"),
      ##       (else_try),
      ##         (store_sub, reg0, ":num_villages", 1),
      ##         (str_store_string, s2, "@{s2}{reg0?Its villages are:Its village is} {s8}.^"),
      ##       (try_end),
      ##     (try_end),
      ##     (call_script, "script_get_prosperity_text_to_s50", ":center_no"),
      ##     (add_party_note_from_sreg, ":center_no", 0, "@{s2}Its prosperity is: {s50}", 0),
      ##     (add_party_note_tableau_mesh, ":center_no", "tableau_center_note_mesh"),
  ]),
  
  
  #script_update_center_recon_notes
  # INPUT: center_no
  # OUTPUT: none
  ("update_center_recon_notes",
    [(store_script_param, ":center_no", 1),
      (try_begin),
        (this_or_next|is_between, ":center_no", towns_begin, towns_end),
        (is_between, ":center_no", castles_begin, castles_end),
        (party_get_slot, ":center_food_store", ":center_no", slot_party_food_store),
        (call_script, "script_center_get_food_consumption", ":center_no"),
        (assign, ":food_consumption", reg0),
        (try_begin),
          (gt, ":food_consumption", 0),
          (store_div, reg6, ":center_food_store", ":food_consumption"),
          (party_collect_attachments_to_party, ":center_no", "p_collective_ally"),
          (party_get_num_companions, reg5, "p_collective_ally"),
          (add_party_note_from_sreg, ":center_no", 1, "@The current garrison consists of {reg5} men.^It should have food stocks for {reg6} days.", 1),
        (try_end),
      (try_end),
  ]),
  
  #script_update_all_notes
  # INPUT: none
  # OUTPUT: none
  ("update_all_notes",
    [
      (call_script, "script_update_troop_notes", "trp_player"),
      (try_for_range, ":troop_no", active_npcs_begin, active_npcs_end),
        (this_or_next|troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
        (this_or_next|troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_lady),
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_inactive_pretender),
        (call_script, "script_update_troop_notes", ":troop_no"),
      (try_end),
      (try_for_range, ":center_no", centers_begin, centers_end),
        (call_script, "script_update_center_notes", ":center_no"),
      (try_end),
      (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
        (call_script, "script_update_faction_notes", ":faction_no"),
      (try_end),
  ]),
  
  #script_agent_troop_get_banner_mesh
  # INPUT: agent_no, troop_no
  # OUTPUT: reg0 - banner_mesh
  ("agent_troop_get_banner_mesh",
    [
      (store_script_param, ":agent_no", 1),
      (store_script_param, ":troop_no", 2),
      (assign, ":banner_troop", -1),
      (assign, ":banner_mesh", banner_default),
      (try_begin),
        (lt, ":agent_no", 0),
        (try_begin),
          (game_in_multiplayer_mode),
          (try_begin),
            (is_presentation_active,"prsnt_multiplayer_item_select"),
            (multiplayer_get_my_player,":player"),
            (player_is_active,":player"),
            (try_begin),
              #if player banners are not allowed, use the default banner mesh
              (eq, "$g_multiplayer_allow_player_banners", 1),
              (player_get_banner_id,":player_banner",":player"),
              (ge,":player_banner",0),
              (store_add,":banner_mesh",":player_banner",banner_meshes_begin),
            (else_try),
              (player_get_team_no,":team",":player"),
              (team_get_faction, ":team_faction_no",":team"),
              (faction_get_slot, ":banner_mesh", ":team_faction_no", slot_faction_banner),
            (try_end),
          (try_end),
        (else_try),
          (is_between, ":troop_no", armor_merchants_begin, weapon_merchants_end),
          (assign, ":banner_troop", "trp_player"),
        (else_try),
          (ge, ":troop_no", 0),
          (this_or_next|troop_slot_ge, ":troop_no", slot_troop_banner_scene_prop, 1),
          (eq, ":troop_no", "trp_player"),
          (assign, ":banner_troop", ":troop_no"),
        (else_try),
          (is_between, ":troop_no", companions_begin, companions_end),
          (assign, ":banner_troop", "trp_player"),
          # (else_try),
          # (assign, ":banner_mesh", "mesh_banner_default"),
        (try_end),
      (else_try),
        (eq, "$g_is_quick_battle", 1),
        (agent_get_team, ":agent_team", ":agent_no"),
        (try_begin),
          (eq, ":agent_team", 0),
          (assign, ":banner_mesh", "$g_quick_battle_team_0_banner"),
        (else_try),
          (assign, ":banner_mesh", "$g_quick_battle_team_1_banner"),
        (try_end),
      (else_try),
        (game_in_multiplayer_mode),
        (agent_get_group, ":agent_group", ":agent_no"),
        (try_begin),
          (neg|player_is_active, ":agent_group"),
          (agent_get_player_id, ":agent_group", ":agent_no"),
        (try_end),
        (try_begin),
          #if player banners are not allowed, use the default banner mesh
          (eq, "$g_multiplayer_allow_player_banners", 1),
          (player_is_active, ":agent_group"),
          (player_get_banner_id, ":player_banner", ":agent_group"),
          (ge, ":player_banner", 0),
          (store_add, ":banner_mesh", ":player_banner", banner_meshes_begin),
          # (assign, ":already_used", 0),
          # (try_for_range, ":cur_faction", npc_kingdoms_begin, npc_kingdoms_end), #wrong client data check
          # (faction_slot_eq, ":cur_faction", slot_faction_banner, ":banner_mesh"),
          # (assign, ":already_used", 1),
          # (try_end),
          # (eq, ":already_used", 0), #otherwise use the default banner mesh
        (else_try),
          (agent_get_team, ":agent_team", ":agent_no"),
          (team_get_faction, ":team_faction_no", ":agent_team"),
          
          (try_begin),
            (agent_is_human, ":agent_no"),
            (faction_get_slot, ":banner_mesh", ":team_faction_no", slot_faction_banner),
          (else_try),
            (agent_get_rider, ":rider_agent_no", ":agent_no"),
            #(agent_get_position, pos1, ":agent_no"),
            #(position_get_x, ":pos_x", pos1),
            #(position_get_y, ":pos_y", pos1),
            #(assign, reg0, ":pos_x"),
            #(assign, reg1, ":pos_y"),
            #(assign, reg2, ":agent_no"),
            #(display_message, "@{!}agent_no:{reg2}, pos_x:{reg0} , posy:{reg1}"),
            (try_begin),
              (ge, ":rider_agent_no", 0),
              (agent_is_active, ":rider_agent_no"),
              (agent_get_team, ":rider_agent_team", ":rider_agent_no"),
              (team_get_faction, ":rider_team_faction_no", ":rider_agent_team"),
              (faction_get_slot, ":banner_mesh", ":rider_team_faction_no", slot_faction_banner),
            (else_try),
              (assign, ":banner_mesh", "mesh_banner_extra17"),
            (try_end),
          (try_end),
        (try_end),
      (else_try),
        (agent_get_troop_id, ":troop_id", ":agent_no"),
        (this_or_next|troop_slot_ge,  ":troop_id", slot_troop_banner_scene_prop, 1),
        (eq, ":troop_no", "trp_player"),
        (assign, ":banner_troop", ":troop_id"),
      (else_try),
        (agent_get_party_id, ":agent_party", ":agent_no"),
        (try_begin),
          (lt, ":agent_party", 0),
          (is_between, ":troop_id", companions_begin, companions_end),
          (main_party_has_troop, ":troop_id"),
          (assign, ":agent_party", "p_main_party"),
        (try_end),
        (ge, ":agent_party", 0),
        (party_get_template_id, ":party_template", ":agent_party"),
        (try_begin),
          (eq, ":party_template", "pt_deserters"),
          (assign, ":banner_mesh", "mesh_banner_extra17"),
        (else_try),
          (is_between, ":agent_party", centers_begin, centers_end),
          (is_between, ":troop_id", companions_begin, companions_end),
          (neq, "$talk_context", tc_tavern_talk),
          #this should be a captured companion in prison
          (assign, ":banner_troop", "trp_player"),
        (else_try),
          (is_between, ":agent_party", centers_begin, centers_end),
          (party_get_slot, ":town_lord", "$g_encountered_party", slot_town_lord),
          (ge, ":town_lord", 0),
          (assign, ":banner_troop", ":town_lord"),
        (else_try),
          (this_or_next|party_slot_eq, ":agent_party", slot_party_type, spt_kingdom_hero_party),
          (eq, ":agent_party", "p_main_party"),
          (party_get_num_companion_stacks, ":num_stacks", ":agent_party"),
          (gt, ":num_stacks", 0),
          (party_stack_get_troop_id, ":leader_troop_id", ":agent_party", 0),
          (this_or_next|troop_slot_ge,  ":leader_troop_id", slot_troop_banner_scene_prop, 1),
          (eq, ":leader_troop_id", "trp_player"),
          (assign, ":banner_troop", ":leader_troop_id"),
        (try_end),
      (else_try), #Check if we are in a tavern
        (eq, "$talk_context", tc_tavern_talk),
        (neq, ":troop_no", "trp_player"),
        (assign, ":banner_mesh", "mesh_banner_extra21"),
      (else_try), #can't find party, this can be a town guard
        (neq, ":troop_no", "trp_player"),
        (is_between, "$g_encountered_party", walled_centers_begin, walled_centers_end),
        (party_get_slot, ":town_lord", "$g_encountered_party", slot_town_lord),
        (ge, ":town_lord", 0),
        (assign, ":banner_troop", ":town_lord"),
      (try_end),
      (try_begin),
        (ge, ":banner_troop", 0),
        (troop_slot_ge, ":banner_troop", slot_troop_banner_scene_prop, 1),
        (troop_get_slot, ":banner_spr", ":banner_troop", slot_troop_banner_scene_prop),
        (ge, ":banner_spr", banner_scene_props_begin),
        (store_sub, ":banner_mesh", ":banner_spr", banner_scene_props_begin),
        (val_add, ":banner_mesh", banner_meshes_begin),
      (try_end),
      (assign, reg0, ":banner_mesh"),
  ]),
  
  #script_shield_item_set_banner
  # INPUT: agent_no
  # OUTPUT: none
  ("shield_item_set_banner",
    [
      (store_script_param, ":tableau_no",1),
      (store_script_param, ":agent_no", 2),
      (store_script_param, ":troop_no", 3),
      #MOTO bug fix chief
      (try_begin),
        (agent_is_active, ":agent_no"),
        (call_script, "script_agent_troop_get_banner_mesh", ":agent_no", ":troop_no"),
        
      (else_try),
        (call_script, "script_agent_troop_get_banner_mesh", -1, ":troop_no"),
      (try_end),
      #MOTO bug fix end
      (cur_item_set_tableau_material, ":tableau_no", reg0),
  ]),
  
  #script_troop_agent_set_banner
  # INPUT: agent_no
  # OUTPUT: none
  ("troop_agent_set_banner",
    [
      (store_script_param, ":tableau_no",1),
      (store_script_param, ":agent_no", 2),
      (store_script_param, ":troop_no", 3),
      #MOTO bug fix chief
      (try_begin),
        (agent_is_active, ":agent_no"),
        (call_script, "script_agent_troop_get_banner_mesh", ":agent_no", ":troop_no"),
        
      (else_try),
        (call_script, "script_agent_troop_get_banner_mesh", -1, ":troop_no"),
      (try_end),
      #MOTO bug fix end
      (cur_agent_set_banner_tableau_material, ":tableau_no", reg0),
  ]),
  
  #script_troop_agent_set_standard
  # INPUT: agent_no, troop_no
  # OUTPUT: none
  ("troop_agent_set_standard",
    [
      (store_script_param, ":agent_no", 1),
      (store_script_param, ":troop_no", 2),
      
      (try_begin),
        (agent_is_active, ":agent_no"),
        (call_script, "script_agent_troop_get_banner_mesh", ":agent_no", ":troop_no"),
      (else_try),
        (call_script, "script_agent_troop_get_banner_mesh", -1, ":troop_no"),
      (try_end),
      
      (val_sub, reg0, banner_meshes_begin),
      #(display_message, "@TEST: banner number: {reg0}"),
      (val_add, reg0, standard_mesh_strings_begin),
      (cur_item_add_mesh, reg0),
  ]),
  
  #script_troop_agent_set_dragon_standard
  # INPUT: agent_no, troop_no
  # OUTPUT: none
  # ("troop_agent_set_dragon_standard", [
      # (store_script_param, ":agent_no", 1),
      # (store_script_param, ":troop_no", 2),
      
      # (try_begin),
        # (agent_is_active, ":agent_no"),
        # (agent_get_team, reg0, ":agent_no"),
        # (team_get_faction, ":faction", reg0),
      # (else_try),
        # (neq,":troop_no",-1),
        # (store_troop_faction, ":faction", ":troop_no"),
      # (else_try),
        # #MP choose inventory
        # (game_in_multiplayer_mode),
        # (is_presentation_active,"prsnt_multiplayer_item_select"),
        # (multiplayer_get_my_player,":player"),
        # (player_is_active,":player"),
        # (player_get_team_no,":team",":player"),
        # (team_get_faction, ":faction",":team"),
      # (else_try),
        # (assign, ":faction",-1),
      # (try_end),
      
      # (try_begin),
        # (eq, ":faction", "fac_kingdom_5"),  #Wessex
        # (assign, ":banner_mesh", "str_standard_dragon1"),
      # (else_try),
        # (is_between, ":faction", "fac_kingdom_9", "fac_kingdom_14"),  #Britons
        # (store_sub, ":banner_mesh", ":faction", "fac_kingdom_9"),
        # (val_add, ":banner_mesh", "str_standard_dragon2"),
      # (else_try),
        # (assign, ":banner_mesh", "str_standard_dragon8"),
      # (try_end),
      
      # (cur_item_add_mesh, ":banner_mesh"),
  # ]),
  
  ##  #script_shield_item_set_banner
  ##  # INPUT: agent_no
  ##  # OUTPUT: none
  ##  ("shield_item_set_banner",
  ##    [
  ##       (store_script_param, ":tableau_no",1),
  ##       (store_script_param, ":agent_no", 2),
  ##       (store_script_param, ":troop_no", 3),
  ##       (assign, ":banner_troop", -1),
  ##       (try_begin),
  ##         (lt, ":agent_no", 0),
  ##         (try_begin),
  ##           (ge, ":troop_no", 0),
  ##           (troop_slot_ge, ":troop_no", slot_troop_banner_scene_prop, 0),
  ##           (assign, ":banner_troop", ":troop_no"),
  ##         (else_try),
  ##           (assign, ":banner_troop", -2),
  ##         (try_end),
  ##       (else_try),
  ##         (agent_get_troop_id, ":troop_id", ":agent_no"),
  ##         (troop_slot_ge,  ":troop_id", slot_troop_custom_banner_flag_type, 0),
  ##         (assign, ":banner_troop", ":troop_id"),
  ##       (else_try),
  ##         (agent_get_party_id, ":agent_party", ":agent_no"),
  ##         (try_begin),
  ##           (lt, ":agent_party", 0),
  ##           (is_between, ":troop_id", companions_begin, companions_end),
  ##           (main_party_has_troop, ":troop_id"),
  ##           (assign, ":agent_party", "p_main_party"),
  ##         (try_end),
  ##         (ge, ":agent_party", 0),
  ##         (party_get_template_id, ":party_template", ":agent_party"),
  ##         (try_begin),
  ##           (eq, ":party_template", "pt_deserters"),
  ##           (assign, ":banner_troop", -3),
  ##         (else_try),
  ##           (is_between, ":agent_party", centers_begin, centers_end),
  ##           (party_get_slot, ":town_lord", "$g_encountered_party", slot_town_lord),
  ##           (ge, ":town_lord", 0),
  ##           (assign, ":banner_troop", ":town_lord"),
  ##         (else_try),
  ##           (this_or_next|party_slot_eq, ":agent_party", slot_party_type, spt_kingdom_hero_party),
  ##           (             eq, ":agent_party", "p_main_party"),
  ##           (party_get_num_companion_stacks, ":num_stacks", ":agent_party"),
  ##           (gt, ":num_stacks", 0),
  ##           (party_stack_get_troop_id, ":leader_troop_id", ":agent_party", 0),
  ##           (troop_slot_ge,  ":leader_troop_id", slot_troop_banner_scene_prop, 1),
  ##           (assign, ":banner_troop", ":leader_troop_id"),
  ##         (try_end),
  ##       (else_try), #Check if we are in a tavern
  ##         (eq, "$talk_context", tc_tavern_talk),
  ##         (neq, ":troop_no", "trp_player"),
  ##         (assign, ":banner_troop", -4),
  ##       (else_try), #can't find party, this can be a town guard
  ##         (neq, ":troop_no", "trp_player"),
  ##         (is_between, "$g_encountered_party", walled_centers_begin, walled_centers_end),
  ##         (party_get_slot, ":town_lord", "$g_encountered_party", slot_town_lord),
  ##         (ge, ":town_lord", 0),
  ##         (assign, ":banner_troop", ":town_lord"),
  ##       (try_end),
  ##       (cur_item_set_tableau_material, ":tableau_no", ":banner_troop"),
  ##     ]),
  
  # script_process_shield_tableau MOTO chief
  # INPUT: banner_mesh, flag 0 for device
  # OUTPUT: none
  ("process_shield_tableau", [
      (store_script_param, ":banner_mesh", 1),
      (store_script_param, ":surface_mesh", 2),
      (store_script_param, ":just_bg", 3),
      (set_fixed_point_multiplier, 100),
      
      (store_sub, ":banner_index", banner_default, banner_meshes_begin),
      (assign, ":background_color", banner_bg_default),
      
      (try_begin),
        (is_between, ":banner_mesh", banner_meshes_begin, kingdom_banner_meshes_end),
        (store_sub, ":banner_index", ":banner_mesh", banner_meshes_begin),
        (troop_get_slot, ":background_color", "trp_banner_background_color_array", ":banner_index"),
      (try_end),
      
      (cur_tableau_set_background_color, ":background_color"),
      
      (try_begin),
        (eq, ":just_bg", 0),
        (init_position, pos1),
        (set_fixed_point_multiplier, 1000),
        (position_set_x, pos1, -500),
        (position_set_y, pos1, 290),
        (set_fixed_point_multiplier, 100),
        
        (position_get_scale_y, reg0, pos1), #amazingly, this corrects distortion
        (val_mul, reg0, 445),
        (val_div, reg0, 1000),
        (position_set_scale_y, pos1, reg0),
        
        (store_add, ":arms_mesh", ":banner_index", arms_meshes_begin),
        (cur_tableau_add_mesh, ":arms_mesh", pos1, 117),
        
        (init_position, pos1),
        (position_set_x, pos1, -100),
        (position_set_z, pos1, 10),
        
        (try_begin),
          (gt, ":background_color", 0),
          (cur_tableau_add_mesh_with_vertex_color, "mesh_white_bg_plane_a", pos1, 1000, 100, ":background_color"),
        (else_try),
          (cur_tableau_add_mesh_with_vertex_color, "mesh_white_bg_plane_a", pos1, 1000, 100, banner_bg_default),
        (try_end),
      (try_end),
      
      (init_position, pos1),
      (position_set_z, pos1, 20),
      (cur_tableau_add_mesh, ":surface_mesh", pos1, 0),
      
      (cur_tableau_set_camera_parameters, 0, 200, 100, 0, 100000),
  ]),
  
  #script_manage_legs_in_cur_tableau
  # INPUT: troop_no
  # OUTPUT: none
  ("manage_legs_in_cur_tableau", [
      (store_script_param, ":troop_no", 1),
      (cur_tableau_clear_override_items),
      (troop_get_inventory_slot, ":armor", ":troop_no", ek_body),
      (assign, ":flags", 0),
      
      (try_begin),
        (gt, ":armor", itm_no_item),
        (item_has_property, ":armor", itp_replaces_helm),
        (val_or, ":flags", af_override_head),
      (try_end),
      
      (try_begin),
        (gt, ":flags", 0),
        (cur_tableau_set_override_flags, ":flags"),
      (try_end),
  ]),
  
  #script_add_troop_to_cur_tableau
  # INPUT: troop_no
  # OUTPUT: none
  ("add_troop_to_cur_tableau",
    [
      (store_script_param, ":troop_no",1),
      
      (set_fixed_point_multiplier, 100),
      (assign, ":banner_mesh", -1),
      (troop_get_slot, ":banner_spr", ":troop_no", slot_troop_banner_scene_prop),
      (try_begin),
        (ge, ":banner_spr", banner_scene_props_begin),
        (store_sub, ":banner_mesh", ":banner_spr", banner_scene_props_begin),
        (val_add, ":banner_mesh", banner_meshes_begin),
      (try_end),
      
      (cur_tableau_clear_override_items),
      
      #       (cur_tableau_set_override_flags, af_override_fullhelm),
      (cur_tableau_set_override_flags, af_override_head|af_override_weapons),
      
      (init_position, pos2),
      (cur_tableau_set_camera_parameters, 1, 6, 6, 10, 10000),
      
      (init_position, pos5),
      (assign, ":eye_height", 162),
      (store_mul, ":camera_distance", ":troop_no", 87323),
      #       (val_mod, ":camera_distance", 5),
      (assign, ":camera_distance", 139),
      (store_mul, ":camera_yaw", ":troop_no", 124337),
      (val_mod, ":camera_yaw", 50),
      (val_add, ":camera_yaw", -25),
      (store_mul, ":camera_pitch", ":troop_no", 98123),
      (val_mod, ":camera_pitch", 20),
      (val_add, ":camera_pitch", -14),
      (assign, ":animation", "anim_stand_man"),
      
      ##       (troop_get_inventory_slot, ":horse_item", ":troop_no", ek_horse),
      ##       (try_begin),
      ##         (gt, ":horse_item", 0),
      ##         (assign, ":eye_height", 210),
      ##         (cur_tableau_add_horse, ":horse_item", pos2, anim_horse_stand, 0),
      ##         (assign, ":animation", anim_ride_0),
      ##         (position_set_z, pos5, 125),
      ##         (try_begin),
      ##           (is_between, ":camera_yaw", -10, 10), #make sure horse head doesn't obstruct face.
      ##           (val_min, ":camera_pitch", -5),
      ##         (try_end),
      ##       (try_end),
      (position_set_z, pos5, ":eye_height"),
      
      # camera looks towards -z axis
      (position_rotate_x, pos5, -90),
      (position_rotate_z, pos5, 180),
      
      # now apply yaw and pitch
      (position_rotate_y, pos5, ":camera_yaw"),
      (position_rotate_x, pos5, ":camera_pitch"),
      (position_move_z, pos5, ":camera_distance", 0),
      (position_move_x, pos5, 5, 0),
      
      (try_begin),
        (ge, ":banner_mesh", 0),
        
        (init_position, pos1),
        (position_set_z, pos1, -1500),
        (position_set_x, pos1, 265),
        (position_set_y, pos1, 400),
        (position_transform_position_to_parent, pos3, pos5, pos1),
        (cur_tableau_add_mesh, ":banner_mesh", pos3, 400, 0),
      (try_end),
      
      (call_script, "script_manage_legs_in_cur_tableau", ":troop_no"),
      (cur_tableau_add_troop, ":troop_no", pos2, ":animation" , 0),
      
      (cur_tableau_set_camera_position, pos5),
      
      (copy_position, pos8, pos5),
      (position_rotate_x, pos8, -90), #y axis aligned with camera now. z is up
      (position_rotate_z, pos8, 30),
      (position_rotate_x, pos8, -60),
      (cur_tableau_add_sun_light, pos8, 175,150,125),
  ]),
  
  #script_add_troop_to_cur_tableau_for_character
  # INPUT: troop_no
  # OUTPUT: none
  ("add_troop_to_cur_tableau_for_character",
    [
      (store_script_param, ":troop_no",1),
      
      (set_fixed_point_multiplier, 100),
      
      (cur_tableau_clear_override_items),
      (cur_tableau_set_override_flags, af_override_fullhelm),
      ##       (cur_tableau_set_override_flags, af_override_head|af_override_weapons),
      
      (init_position, pos2),
      (cur_tableau_set_camera_parameters, 1, 4, 8, 10, 10000),
      
      (init_position, pos5),
      (assign, ":cam_height", 150),
      #       (val_mod, ":camera_distance", 5),
      (assign, ":camera_distance", 360),
      (assign, ":camera_yaw", -15),
      (assign, ":camera_pitch", -18),
      (assign, ":animation", anim_stand_man),
      
      (position_set_z, pos5, ":cam_height"),
      
      # camera looks towards -z axis
      (position_rotate_x, pos5, -90),
      (position_rotate_z, pos5, 180),
      
      # now apply yaw and pitch
      (position_rotate_y, pos5, ":camera_yaw"),
      (position_rotate_x, pos5, ":camera_pitch"),
      (position_move_z, pos5, ":camera_distance", 0),
      (position_move_x, pos5, 5, 0),
      
      (call_script, "script_manage_legs_in_cur_tableau", ":troop_no"),
      
      (try_begin),
        (troop_is_hero, ":troop_no"),
        (cur_tableau_add_troop, ":troop_no", pos2, ":animation", -1),
      (else_try),
        (store_mul, ":random_seed", ":troop_no", 126233),
        (val_mod, ":random_seed", 1000),
        (val_add, ":random_seed", 1),
        (cur_tableau_add_troop, ":troop_no", pos2, ":animation", ":random_seed"),
      (try_end),
      (cur_tableau_set_camera_position, pos5),
      
      (copy_position, pos8, pos5),
      (position_rotate_x, pos8, -90), #y axis aligned with camera now. z is up
      (position_rotate_z, pos8, 30),
      (position_rotate_x, pos8, -60),
      (cur_tableau_add_sun_light, pos8, 175,150,125),
  ]),
  
  #script_add_troop_to_cur_tableau_for_inventory
  # INPUT: troop_no
  # OUTPUT: none
  ("add_troop_to_cur_tableau_for_inventory",
    [
      (store_script_param, ":troop_no",1),
      (store_mod, ":side", ":troop_no", 4), #side flag is inside troop_no value
      (val_div, ":troop_no", 4), #removing the flag bit
      (val_mul, ":side", 90), #to degrees
      
      (set_fixed_point_multiplier, 100),
      
      (cur_tableau_clear_override_items),
      
      (init_position, pos2),
      (position_rotate_z, pos2, ":side"),
      (cur_tableau_set_camera_parameters, 1, 4, 6, 10, 10000),
      
      (init_position, pos5),
      (assign, ":cam_height", 105),
      #       (val_mod, ":camera_distance", 5),
      (assign, ":camera_distance", 380),
      (assign, ":camera_yaw", -15),
      (assign, ":camera_pitch", -18),
      (assign, ":animation", anim_stand_man),
      
      (position_set_z, pos5, ":cam_height"),
      
      # camera looks towards -z axis
      (position_rotate_x, pos5, -90),
      (position_rotate_z, pos5, 180),
      
      # now apply yaw and pitch
      (position_rotate_y, pos5, ":camera_yaw"),
      (position_rotate_x, pos5, ":camera_pitch"),
      (position_move_z, pos5, ":camera_distance", 0),
      (position_move_x, pos5, 5, 0),
      
      (call_script, "script_manage_legs_in_cur_tableau", ":troop_no"),
      
      (try_begin),
        (troop_is_hero, ":troop_no"),
        (cur_tableau_add_troop, ":troop_no", pos2, ":animation", -1),
      (else_try),
        (store_mul, ":random_seed", ":troop_no", 126233),
        (val_mod, ":random_seed", 1000),
        (val_add, ":random_seed", 1),
        (cur_tableau_add_troop, ":troop_no", pos2, ":animation", ":random_seed"),
      (try_end),
      (cur_tableau_set_camera_position, pos5),
      
      (copy_position, pos8, pos5),
      (position_rotate_x, pos8, -90), #y axis aligned with camera now. z is up
      (position_rotate_z, pos8, 30),
      (position_rotate_x, pos8, -60),
      (cur_tableau_add_sun_light, pos8, 175,150,125),
  ]),
  
  #script_add_troop_to_cur_tableau_for_profile
  # INPUT: troop_no
  # OUTPUT: none
  ("add_troop_to_cur_tableau_for_profile",
    [
      (store_script_param, ":troop_no",1),
      
      (set_fixed_point_multiplier, 100),
      
      (cur_tableau_clear_override_items),
      
      (cur_tableau_set_camera_parameters, 1, 4, 6, 10, 10000),
      
      (init_position, pos5),
      (assign, ":cam_height", 105),
      #       (val_mod, ":camera_distance", 5),
      (assign, ":camera_distance", 380),
      (assign, ":camera_yaw", -15),
      (assign, ":camera_pitch", -18),
      (assign, ":animation", anim_stand_man),
      
      (position_set_z, pos5, ":cam_height"),
      
      # camera looks towards -z axis
      (position_rotate_x, pos5, -90),
      (position_rotate_z, pos5, 180),
      
      # now apply yaw and pitch
      (position_rotate_y, pos5, ":camera_yaw"),
      (position_rotate_x, pos5, ":camera_pitch"),
      (position_move_z, pos5, ":camera_distance", 0),
      (position_move_x, pos5, 5, 0),
      
      (profile_get_banner_id, ":profile_banner"),
      (try_begin),
        (ge, ":profile_banner", 0),
        (init_position, pos2),
        (val_add, ":profile_banner", banner_meshes_begin),
        (position_set_x, pos2, -175),
        (position_set_y, pos2, -300),
        (position_set_z, pos2, 180),
        (position_rotate_x, pos2, 90),
        (position_rotate_y, pos2, -195),
        (cur_tableau_add_mesh, ":profile_banner", pos2, 0, 0),
      (try_end),
      
      (call_script, "script_manage_legs_in_cur_tableau", ":troop_no"),
      
      (init_position, pos2),
      (try_begin),
        (troop_is_hero, ":troop_no"),
        (cur_tableau_add_troop, ":troop_no", pos2, ":animation", -1),
      (else_try),
        (store_mul, ":random_seed", ":troop_no", 126233),
        (val_mod, ":random_seed", 1000),
        (val_add, ":random_seed", 1),
        (cur_tableau_add_troop, ":troop_no", pos2, ":animation", ":random_seed"),
      (try_end),
      (cur_tableau_set_camera_position, pos5),
      
      (copy_position, pos8, pos5),
      (position_rotate_x, pos8, -90), #y axis aligned with camera now. z is up
      (position_rotate_z, pos8, 30),
      (position_rotate_x, pos8, -60),
      (cur_tableau_add_sun_light, pos8, 175,150,125),
  ]),
  
  #script_add_troop_to_cur_tableau_for_retirement
  # INPUT: type
  # OUTPUT: none
  ("add_troop_to_cur_tableau_for_retirement", [
      (store_script_param, ":type", 1),
      (cur_tableau_set_override_flags, af_override_everything),
      
      (try_begin),
        (eq, ":type", 0),
        # (cur_tableau_add_override_item, "itm_pilgrim_hood"),
        (cur_tableau_add_override_item, "itm_pilgrim_disguise"),
        # (cur_tableau_add_override_item, "itm_carbatinae_5s"),
        (assign, ":animation", "anim_pose_1"),
      (else_try),
        (eq, ":type", 1),
        (cur_tableau_add_override_item, "itm_hood_01"),
        (cur_tableau_add_override_item, "itm_btunic_14"),
        (cur_tableau_add_override_item, "itm_carbatinae_5s"),
        (cur_tableau_add_override_item, "itm_seax_1"),
        (assign, ":animation", "anim_pose_1"),
      (else_try),
        (eq, ":type", 2),
        (cur_tableau_add_override_item, "itm_linen_tunic"),
        (cur_tableau_add_override_item, "itm_carbatinae_5s"),
        (assign, ":animation", "anim_pose_2"),
      (else_try),
        (eq, ":type", 3),
        (cur_tableau_add_override_item, "itm_robe"),
        (cur_tableau_add_override_item, "itm_carbatinae_vc1"),
        (assign, ":animation", "anim_pose_2"),
      (else_try),
        (eq, ":type", 4),
        (cur_tableau_add_override_item, "itm_bl_tunic01"),
        (cur_tableau_add_override_item, "itm_carbatinae_vc1s"),
        (assign, ":animation", "anim_pose_3"),
      (else_try),
        (eq, ":type", 5),
        (cur_tableau_add_override_item, "itm_robe"),
        #(cur_tableau_add_override_item, "itm_woolen_hose"),
        (cur_tableau_add_override_item, "itm_viking_helm"),
        (assign, ":animation", "anim_pose_3"),
      (else_try),
        (eq, ":type", 6),
        (cur_tableau_add_override_item, "itm_robe"),
        (cur_tableau_add_override_item, "itm_carbatinae_vc1s"),
        (cur_tableau_add_override_item, "itm_spatha_8"),
        (assign, ":animation", "anim_pose_4"),
      (else_try),
        (eq, ":type", 7),
        (cur_tableau_add_override_item, "itm_nobleman_outfit"),
        #  (cur_tableau_add_override_item, "itm_blue_hose"),
        (cur_tableau_add_override_item, "itm_spatha_8"),
        (assign, ":animation", "anim_pose_4"),
      (else_try),
        (eq, ":type", 8),
        (cur_tableau_add_override_item, "itm_robe"),
        #  (cur_tableau_add_override_item, "itm_woolen_hose"),
        (cur_tableau_add_override_item, "itm_spatha_8"),
        (assign, ":animation", "anim_pose_4"),
      (else_try),
        ##      (eq, ":type", 9),
        (cur_tableau_add_override_item, "itm_mail_shirt_11"),
        (cur_tableau_add_override_item, "itm_carbatinae_1"),
        (cur_tableau_add_override_item, "itm_lyre"),
        (assign, ":animation", "anim_pose_5"),
        ##    (else_try), #not used
        ##      (cur_tableau_add_override_item, "itm_heraldic_mail_with_tabard"),
        ##      (cur_tableau_add_override_item, "itm_carbatinae_1"),
        ##      (cur_tableau_add_override_item, "itm_spatha_8"),
        ##      (assign, ":animation", "anim_pose_5"),
      (try_end),
      
      ##    (set_fixed_point_multiplier, 100),
      ##    (cur_tableau_set_background_color, 0x00000000),
      ##    (cur_tableau_set_ambient_light, 10,11,15),
      
      ##     (init_position, pos8),
      ##     (position_set_x, pos8, -210),
      ##     (position_set_y, pos8, 200),
      ##     (position_set_z, pos8, 300),
      ##     (cur_tableau_add_point_light, pos8, 550,500,450),
      
      
      (set_fixed_point_multiplier, 100),
      (cur_tableau_set_camera_parameters, 1, 6, 6, 10, 10000),
      (assign, ":cam_height", 155),
      (assign, ":camera_distance", 575),
      (assign, ":camera_yaw", -5),
      (assign, ":camera_pitch", 10),
      
      (init_position, pos5),
      (position_set_z, pos5, ":cam_height"),
      # camera looks towards -z axis
      (position_rotate_x, pos5, -90),
      (position_rotate_z, pos5, 180),
      # now apply yaw and pitch
      (position_rotate_y, pos5, ":camera_yaw"),
      (position_rotate_x, pos5, ":camera_pitch"),
      (position_move_z, pos5, ":camera_distance", 0),
      (position_move_x, pos5, 60, 0),
      
      (init_position, pos2),
      (cur_tableau_add_troop, "trp_player", pos2, ":animation", 0),
      (cur_tableau_set_camera_position, pos5),
      
      (copy_position, pos8, pos5),
      (position_rotate_x, pos8, -90), #y axis aligned with camera now. z is up
      (position_rotate_z, pos8, 30),
      (position_rotate_x, pos8, -60),
      (cur_tableau_add_sun_light, pos8, 175,150,125),
  ]),
  
  #script_add_troop_to_cur_tableau_for_party
  # INPUT: troop_no
  # OUTPUT: none
  ("add_troop_to_cur_tableau_for_party",
    [
      (store_script_param, ":troop_no",1),
      (assign, ":show_horse", 1),
      (call_script, "script_add_troop_to_cur_tableau_for_party_extended", ":troop_no", ":show_horse"),
  ]),
  
  
  # Extended version of the script, it can display portrait with or without the horse
  #script_add_troop_to_cur_tableau_for_party_extended
  # INPUT: troop_no, show_horse
  ("add_troop_to_cur_tableau_for_party_extended",
    [
      (store_script_param, ":troop_no",1),
      (store_script_param, ":show_horse",2),
      
      (store_mod, ":hide_weapons", ":troop_no", 2), #hide_weapons flag is inside troop_no value
      (val_div, ":troop_no", 2), #removing the flag bit
      
      (set_fixed_point_multiplier, 100),
      
      (cur_tableau_clear_override_items),
      (try_begin),
        (eq, ":hide_weapons", 1),
        (cur_tableau_set_override_flags, af_override_fullhelm|af_override_head|af_override_weapons),
      (try_end),
      
      (init_position, pos2),
      (cur_tableau_set_camera_parameters, 1, 6, 6, 10, 10000),
      
      (init_position, pos5),
      (assign, ":cam_height", 105),
      #       (val_mod, ":camera_distance", 5),
      (assign, ":camera_distance", 450),
      (assign, ":camera_yaw", 15),
      (assign, ":camera_pitch", -18),
      (assign, ":animation", anim_stand_man),
      
      (troop_get_inventory_slot, ":horse_item", ":troop_no", ek_horse),
      (try_begin),
        (eq, ":show_horse", 1),
        (gt, ":horse_item", 0),
        (eq, ":hide_weapons", 0),
        (cur_tableau_add_horse, ":horse_item", pos2, "anim_horse_stand", 0),
        (assign, ":animation", "anim_ride_0"),
        (assign, ":camera_yaw", 23),
        (assign, ":cam_height", 150),
        (assign, ":camera_distance", 550),
      (try_end),
      (position_set_z, pos5, ":cam_height"),
      
      # camera looks towards -z axis
      (position_rotate_x, pos5, -90),
      (position_rotate_z, pos5, 180),
      
      # now apply yaw and pitch
      (position_rotate_y, pos5, ":camera_yaw"),
      (position_rotate_x, pos5, ":camera_pitch"),
      (position_move_z, pos5, ":camera_distance", 0),
      (position_move_x, pos5, 5, 0),
      
      (call_script, "script_manage_legs_in_cur_tableau", ":troop_no"),
      
      (try_begin),
        (troop_is_hero, ":troop_no"),
        (cur_tableau_add_troop, ":troop_no", pos2, ":animation", -1),
      (else_try),
        (store_mul, ":random_seed", ":troop_no", 126233),
        (val_mod, ":random_seed", 1000),
        (val_add, ":random_seed", 1),
        (cur_tableau_add_troop, ":troop_no", pos2, ":animation", ":random_seed"),
      (try_end),
      (cur_tableau_set_camera_position, pos5),
      
      (copy_position, pos8, pos5),
      (position_rotate_x, pos8, -90), #y axis aligned with camera now. z is up
      (position_rotate_z, pos8, 30),
      (position_rotate_x, pos8, -60),
      (cur_tableau_add_sun_light, pos8, 175,150,125),
  ]),
  
  #script_get_prosperity_text_to_s50
  # INPUT: center_no
  # OUTPUT: none
  ("get_prosperity_text_to_s50",
    [(store_script_param, ":center_no", 1),
      (party_get_slot, ":prosperity", ":center_no", slot_town_prosperity),
      (val_div, ":prosperity", 20),
      (try_begin),
        (eq, ":prosperity", 0), #0..19
        (str_store_string, s50, "@Very Poor"),
      (else_try),
        (eq, ":prosperity", 1), #20..39
        (str_store_string, s50, "@Poor"),
      (else_try),
        (eq, ":prosperity", 2), #40..59
        (str_store_string, s50, "@Average"),
      (else_try),
        (eq, ":prosperity", 3), #60..79
        (str_store_string, s50, "@Rich"),
      (else_try),
        (str_store_string, s50, "@Very Rich"), #80..99
      (try_end),
  ]),
  
  #script_spawn_lairs
  # INPUT: spawn point
  # OUTPUT: none
  ("spawn_lairs", [
      (store_script_param, ":bandit_spawn_point", 1),
      (party_get_slot, ":bandit_lair_party", ":bandit_spawn_point", slot_party_lair_party),
      (try_begin),
        (le, ":bandit_lair_party", "p_spawn_points_end"),
        
        (party_get_slot, ":bandit_template", ":bandit_spawn_point", slot_party_bandit_type),
        (set_spawn_radius, 10), #chief cambiado
        (party_template_get_slot, ":bandit_lair_template", ":bandit_template", slot_party_template_lair_type),
        (gt, ":bandit_lair_template", 0),
        (spawn_around_party, ":bandit_spawn_point", ":bandit_lair_template"),
        (assign, ":new_camp", reg0),
        (party_set_slot, ":new_camp", slot_party_type, spt_bandit_lair),
        
        (str_store_party_name, s4, ":new_camp"),
        #(party_set_flags, ":new_camp", pf_icon_mask, 1),
        (party_get_current_terrain, ":new_camp_terrain", ":new_camp"),
        ##	(position_get_z, ":elevation", pos4),
        ##	(position_get_y, ":lair_y", pos4),
        
        (assign, ":center_too_close", 0),
        (try_for_range, ":center", centers_begin, centers_end),
          (eq, ":center_too_close", 0),
          (store_distance_to_party_from_party, ":distance", ":new_camp", ":center"),
          (lt, ":distance", 5),
          (assign, ":center_too_close", 1),
        (try_end),
        
        (party_get_slot, ":flags", ":bandit_spawn_point", slot_party_spawn_flags),
        (try_begin),
          (eq, ":center_too_close", 0),
          (store_and, reg1, ":flags", spsf_coastal),
          (neq, reg1, 0),	#landed sea raiders
          
          (neq, ":new_camp_terrain", rt_water),
          (neq, ":new_camp_terrain", rt_river),
          (neq, ":new_camp_terrain", rt_bridge),
          (party_get_position, pos4, ":new_camp"),
          (map_get_water_position_around_position, pos5, pos4, 4),
          
          (party_set_slot, ":bandit_spawn_point", slot_party_lair_party, ":new_camp"),
          (party_set_flags, ":new_camp", pf_disabled, 1),
          
        (else_try),
          (eq, ":center_too_close", 0),
          (store_and, reg1, ":flags", spsf_coastal|spsf_seaborne),
          (eq, reg1, 0),	#land bandits
          
          (neq, ":new_camp_terrain", rt_water),
          (neq, ":new_camp_terrain", rt_river),
          (neq, ":new_camp_terrain", rt_bridge),
          
          #keep icon out of water
          (party_get_position, pos4, ":new_camp"),
          (try_begin),
            (map_get_water_position_around_position, pos5, pos4, 1),
            (assign, ":center_too_close", 1),
            
          (else_try),
            (party_set_slot, ":bandit_spawn_point", slot_party_lair_party, ":new_camp"),
            (party_set_flags, ":new_camp", pf_disabled, 1),
            (assign, ":center_too_close", 0),
          (try_end),
          
          (eq, ":center_too_close", 0),
          
        (else_try),
          (remove_party, ":new_camp"),
        (try_end),
      (try_end),
  ]),
  
  #script_spawn_bandits
  # INPUT: $spawn_party_max_size
  # OUTPUT: none
  ("spawn_bandits",
    [
      #count parties per spawn point
      (try_for_range, ":spawn_point", spawn_points_begin, spawn_points_end),
        (party_set_slot, ":spawn_point", slot_party_spawned_count, 0),
      (try_end),
      
      (try_for_parties, ":spawned_party"),
        (gt, ":spawned_party", "p_spawn_points_end"),
        (party_get_slot, ":spawn_point", ":spawned_party", slot_party_spawn_point),
        (neq, ":spawn_point", 0),
        (party_get_slot, ":count", ":spawn_point", slot_party_spawned_count),
        (val_add, ":count", 1),
        (party_set_slot, ":spawn_point", slot_party_spawned_count, ":count"),
      (try_end),
      
      (set_spawn_radius,1),
      
      #as parties get larger, make fewer of them
      (assign, reg0, "$spawn_party_max_size"),
      (val_min, reg0, phase_out),
      (store_sub, ":proportion", phase_out, reg0),
      
      (try_begin),
        (eq, "$bandit_quantity_option", 0),  #less
        (val_mul, ":proportion", 2),
        (val_div, ":proportion", 3),
      (else_try),
        (eq, "$bandit_quantity_option", 2),  #more
        (val_mul, ":proportion", 3),
        (val_div, ":proportion", 2),
      (try_end),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (store_mul, reg1, 15, reg0),
        (val_div, reg1, 10),
        (val_add, reg1, 5),
        (display_message, "@{!}DEBUG : Doing spawn bandit script. Limit {reg0} set {reg1} actual"),
      (try_end),
      
      (try_for_range, ":spawn_point", spawn_points_begin, spawn_points_end),
        (party_get_slot, ":party_max", ":spawn_point", slot_party_spawn_target_spawns),
        (val_mul, ":party_max", ":proportion"),
        
        (store_character_level, ":value", "trp_player"),
        (val_sub, ":value", 12), #lowest troop level -- player can't train troops if he/she is less than this
        (try_begin),
          (lt, ":value", 0),
          (val_mul, ":value", 2),
          (val_add, ":value", 100),
          (val_mul, ":party_max", ":value"),
          (val_div, ":party_max", 100),
        (try_end),
        
        (try_begin),
          (is_between, ":spawn_point", laired_spawn_points_begin, laired_spawn_points_end),
          (neg|party_slot_ge, ":spawn_point", slot_party_lair_party, "p_spawn_points_end"), #no lair?
          (val_div, ":party_max", 3),
        (try_end),
        
        (val_div, ":party_max", phase_out),
        (val_add, ":party_max", 1),
        (neg|party_slot_ge, ":spawn_point", slot_party_spawned_count, ":party_max"), #spawn point lacks parties?
        
        (party_get_slot, ":radius", ":spawn_point", slot_party_spawn_radius),
        (set_spawn_radius, ":radius"),
        (party_get_slot, ":party_template", ":spawn_point", slot_party_bandit_type),
        (spawn_around_party, ":spawn_point", ":party_template"),
        (assign, ":spawned_party", reg0),
        
        (party_get_slot, ":flags", ":spawn_point", slot_party_spawn_flags),
        (assign, ":keep_it", 1),
        (try_begin),
          (store_and, reg1, ":flags", spsf_seaborne),
          (neq, reg1, 0),
          (party_get_position, pos0, ":spawn_point"),
          (map_get_water_position_around_position, pos1, pos0, ":radius"),
          (party_set_position, ":spawned_party", pos1),
          
        (else_try),
          (store_and, reg1, ":flags", spsf_coastal),
          (neq, reg1, 0),
          (party_get_position, pos0, ":spawn_point"),
          (map_get_land_position_around_position, pos1, pos0, ":radius"),
          (party_set_position, ":spawned_party", pos1),
          (party_get_current_terrain, reg1, ":spawned_party"),
          (try_begin),
            (eq, reg1, rt_bridge),
            (assign, ":keep_it", 0),
          (try_end),
        (try_end),
        
        (try_begin),
          (eq, ":keep_it", 0),
          (remove_party, ":spawned_party"),
          
        (else_try),
          (party_set_slot, ":spawned_party", slot_party_spawn_point, ":spawn_point"),
          (party_set_ai_behavior, ":spawned_party", ai_bhvr_patrol_party),
          (party_set_ai_object, ":spawned_party", ":spawn_point"),
          (call_script, "script_update_party_icon", ":spawned_party"),
        (try_end),
      (try_end), #spawn point loop
      
      (try_begin),
        (store_num_parties_of_template, ":num_parties", "pt_looters"),
        (neq, ":num_parties", 0), #leave first time for after campaign type is set (see mnu_start_game_4)
        (store_mul, ":party_max", 65, ":proportion"),  #every other village
        (val_div, ":party_max", phase_out),
        (val_add, ":party_max", 10),  #allow two per every land mass on map
        (lt,":num_parties",":party_max"),  #was 33 at mount&blade, 50 in warband
        (set_spawn_radius, 25),
        # (store_sub, ":num_new_parties", ":party_max", ":num_parties"),
        # (try_for_range, ":unused", 0, ":num_new_parties"),
        (store_random_in_range,":spawn_point",villages_begin,villages_end),
        (spawn_around_party,":spawn_point","pt_looters"),
        # (assign, ":spawned_party_id", reg0), this done when quest started plus it creates quest parties far from player
        # (try_begin),
        # (check_quest_active, "qst_deal_with_looters"),
        # (party_set_flags, ":spawned_party_id", pf_quest_party, 1),
        # (else_try),
        # (party_set_flags, ":spawned_party_id", pf_quest_party, 0),
        (party_set_slot, reg0, slot_party_spawn_point, ":spawn_point"),
        (party_set_ai_behavior, reg0, ai_bhvr_patrol_party),
        (party_set_ai_object, reg0, ":spawn_point"),
        (call_script, "script_update_party_icon", reg0),
        # (try_end),
      (try_end),
      
      (try_begin),
        (store_num_parties_of_template, ":num_parties", "pt_deserters"),
        (store_mul, ":target", "$bandit_quantity_option", 5),
        (val_add, ":target", 10),  #10-20
        
        (lt,":num_parties",":target"), #chief cambia tb
        (set_spawn_radius, 4),
        (try_for_range, ":troop_no", active_npcs_begin, active_npcs_end),
          (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
          (store_random_in_range, ":random_no", 0, 100),
          (lt, ":random_no", 5),
          (troop_get_slot, ":spawn_point", ":troop_no", slot_troop_leaded_party),
          (store_troop_faction, ":troop_faction", ":troop_no"),
          (neq, ":troop_faction", "fac_player_supporters_faction"),
          (neq, ":troop_faction", "fac_adventurers"),
          (gt, ":spawn_point", 0),
          (neg|party_is_in_any_town, ":spawn_point"),
          ##         (party_get_attached_to, ":attached_party_no", ":spawn_point"),
          ##         (lt, ":attached_party_no", 0),#in wilderness
          (spawn_around_party, ":spawn_point", "pt_deserters"),
          (assign, ":spawned_party", reg0),
          (faction_get_slot, ":tier_1_troop", ":troop_faction", slot_faction_tier_1_troop),
          (faction_get_slot, ":tier_2_troop", ":troop_faction", slot_faction_tier_2_troop),
          (store_character_level, ":level", "trp_player"),
          (store_mul, ":max_number_to_add", ":level", 2),
          (val_add, ":max_number_to_add", 11),
          (store_random_in_range, ":number_to_add", 10, ":max_number_to_add"),
          (party_add_members, ":spawned_party", ":tier_1_troop", ":number_to_add"),
          (store_random_in_range, ":random_no", 1, 4),
          (try_for_range, ":unused", 0, ":random_no"),
            (party_upgrade_with_xp, ":spawned_party", 1000000, 0),
          (try_end),
          (party_get_num_companion_stacks, ":num_companion_stacks",":spawned_party"),
          (try_for_range_backwards, ":stack_no", 0, ":num_companion_stacks"),
            (party_stack_get_troop_id, ":stack_troop",":spawned_party",":stack_no"),
            (this_or_next|eq, ":stack_troop", "trp_briton_standard_bearer"),
            (this_or_next|eq, ":stack_troop", "trp_saxon_standard_bearer"),
            (this_or_next|eq, ":stack_troop", "trp_norse_standard_bearer"),
            (this_or_next|eq, ":stack_troop", "trp_scotch_standard_bearer"),
            (this_or_next|eq, ":stack_troop", "trp_angle_standard_bearer"),
            (eq, ":stack_troop", "trp_irish_standard_bearer"),
            (party_stack_get_size, ":stack_size", ":spawned_party", ":stack_no"),
            (party_remove_members, ":spawned_party", ":stack_troop", ":stack_size"),
            (party_add_members,":spawned_party", ":tier_2_troop", ":stack_size"),
          (try_end),
          (party_get_position, pos0,  ":spawned_party"),
          (party_set_ai_behavior, ":spawned_party", ai_bhvr_patrol_location),
          (party_set_ai_patrol_radius, ":spawned_party", 30),
          (party_set_ai_target_position, ":spawned_party", pos0),
          (party_set_slot, ":spawned_party", slot_party_ai_state, spai_patrolling_around_center),
          (call_script, "script_update_party_icon", ":spawned_party"),
          
          ##         (str_store_party_name, s1, ":spawn_point"),
          ##         (call_script, "script_get_closest_center", ":spawn_point"),
          ##         (try_begin),
          ##           (gt, reg0, 0),
          ##           (str_store_party_name, s2, reg0),
          ##         (else_try),
          ##           (str_store_string, s2, "@unknown place"),
          ##         (try_end),
          ##         (assign, reg1, ":number_to_add"),
          ##         (display_message, "@{reg1} Deserters spawned from {s1}, near {s2}."),
        (try_end),
      (try_end), #deserters ends
  ]),
  
  #script_count_mission_casualties_from_agents
  # INPUT: none
  # OUTPUT: none
  ("count_mission_casualties_from_agents",
    [(party_clear, "p_player_casualties"),
      (party_clear, "p_enemy_casualties"),
      (party_clear, "p_ally_casualties"),
      (assign, "$any_allies_at_the_last_battle", 0),
      #(assign, "$num_routed_us", 0), #these should not assign to 0 here to protect routed agents to spawn again in next turns.
      #(assign, "$num_routed_allies", 0),
      #(assign, "$num_routed_enemies", 0),
      
      #initialize all routed counts of troops
      (try_for_agents, ":cur_agent"),
        (agent_is_human, ":cur_agent"),
        (agent_get_party_id, ":agent_party", ":cur_agent"),
        (agent_get_troop_id, ":agent_troop_id", ":cur_agent"),
        (troop_set_slot, ":agent_troop_id", slot_troop_player_routed_agents, 0),
        (troop_set_slot, ":agent_troop_id", slot_troop_ally_routed_agents, 0),
        (troop_set_slot, ":agent_troop_id", slot_troop_enemy_routed_agents, 0),
      (try_end),
      
      (try_for_agents, ":cur_agent"),
        (agent_is_human, ":cur_agent"),
        (agent_get_party_id, ":agent_party", ":cur_agent"),
        (try_begin),
          (neq, ":agent_party", "p_main_party"),
          (agent_is_ally, ":cur_agent"),
          (assign, "$any_allies_at_the_last_battle", 1),
        (try_end),
        #count routed agents in player party, ally parties and enemy parties
        (try_begin),
          (this_or_next|agent_slot_eq, ":cur_agent", slot_agent_vc_wounded, 77),# new for VC-1123
          (agent_is_routed, ":cur_agent"),
          (agent_get_slot, ":agent_was_running_away", ":cur_agent", slot_agent_is_running_away),
          (eq, ":agent_was_running_away", 1),
          (try_begin),
            (agent_get_troop_id, ":routed_ag_troop_id", ":cur_agent"),
            #only enemies
            #only regulars
            
            (try_begin),
              (eq, ":agent_party", "p_main_party"),
              (val_add, "$num_routed_us", 1),
            (else_try),
              (agent_is_ally, ":cur_agent"),
              (val_add, "$num_routed_allies", 1),
            (else_try),
              #for now only count and include routed enemy agents in new routed party.
              (val_add, "$num_routed_enemies", 1),
              (agent_get_party_id, ":routed_ag_party_id", ":cur_agent"),
              (try_begin),
                (party_is_active,":routed_ag_party_id"),
                (store_faction_of_party, ":faction_of_routed_agent_party", ":routed_ag_party_id"),
                (faction_get_slot, ":num_routed_agents_in_this_faction", ":faction_of_routed_agent_party", slot_faction_num_routed_agents),
                (val_add, ":num_routed_agents_in_this_faction", 1),
                (faction_set_slot, ":faction_of_routed_agent_party", slot_faction_num_routed_agents, ":num_routed_agents_in_this_faction"),
              (try_end),
              (party_add_members, "p_routed_enemies", ":routed_ag_troop_id", 1),
            (try_end),
          (try_end),
          (agent_get_troop_id, ":agent_troop_id", ":cur_agent"),
          (try_begin),
            (eq, ":agent_party", "p_main_party"),
            (troop_get_slot, ":player_routed_agents", ":agent_troop_id", slot_troop_player_routed_agents),
            (val_add, ":player_routed_agents", 1),
            (troop_set_slot, ":agent_troop_id", slot_troop_player_routed_agents, ":player_routed_agents"),
            
          (else_try),
            (agent_is_ally, ":cur_agent"),
            (troop_get_slot, ":ally_routed_agents", ":agent_troop_id", slot_troop_ally_routed_agents),
            (val_add, ":ally_routed_agents", 1),
            (troop_set_slot, ":agent_troop_id", slot_troop_ally_routed_agents, ":ally_routed_agents"),
            
          (else_try),
            (troop_get_slot, ":enemy_routed_agents", ":agent_troop_id", slot_troop_enemy_routed_agents),
            (val_add, ":enemy_routed_agents", 1),
            (troop_set_slot, ":agent_troop_id", slot_troop_enemy_routed_agents, ":enemy_routed_agents"),
            
          (try_end),
        (try_end),
        #count and save killed agents in player party, ally parties and enemy parties
        (this_or_next|agent_slot_eq, ":cur_agent", slot_agent_vc_wounded, 77),# new for VC-1123
        (neg|agent_is_alive, ":cur_agent"),
        (agent_get_troop_id, ":agent_troop_id", ":cur_agent"),
        (try_begin),
          (this_or_next|eq, ":agent_troop_id", "trp_dog"),	#VC-2111
          (eq, ":agent_party", "p_main_party"),
          (party_add_members, "p_player_casualties", ":agent_troop_id", 1),
          (try_begin),
            (agent_is_wounded, ":cur_agent"),
            (party_wound_members, "p_player_casualties", ":agent_troop_id", 1),
          (try_end),
        (else_try),
          (agent_is_ally, ":cur_agent"),
          (party_add_members, "p_ally_casualties", ":agent_troop_id", 1),
          (try_begin),
            (agent_is_wounded, ":cur_agent"),
            (party_wound_members, "p_ally_casualties", ":agent_troop_id", 1),
          (try_end),
        (else_try),
          (party_add_members, "p_enemy_casualties", ":agent_troop_id", 1),
          (try_begin),
            (agent_is_wounded, ":cur_agent"),
            (party_wound_members, "p_enemy_casualties", ":agent_troop_id", 1),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  #script_get_max_skill_of_player_party
  # INPUT: arg1 = skill_no
  # OUTPUT: reg0 = max_skill, reg1 = skill_owner_troop_no
  ("get_max_skill_of_player_party",
    [(store_script_param, ":skill_no", 1),
      (party_get_num_companion_stacks, ":num_stacks","p_main_party"),
      (store_skill_level, ":max_skill", ":skill_no", "trp_player"),
      (assign, ":skill_owner", "trp_player"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop","p_main_party",":i_stack"),
        (troop_is_hero, ":stack_troop"),
        (neg|troop_is_wounded, ":stack_troop"),
        (store_skill_level, ":cur_skill", ":skill_no", ":stack_troop"),
        (gt, ":cur_skill", ":max_skill"),
        (assign, ":max_skill", ":cur_skill"),
        (assign, ":skill_owner", ":stack_troop"),
      (try_end),
      (party_get_skill_level, reg0, "p_main_party", ":skill_no"),
      ##     (assign, reg0, ":max_skill"),
      (assign, reg1, ":skill_owner"),
  ]),
  
  #script_upgrade_hero_party
  # INPUT: arg1 = party_id, arg2 = xp_amount
  ("upgrade_hero_party",
    [
      (store_script_param, ":party_no", 1),
      (store_script_param, ":xp_amount", 2),
      (party_upgrade_with_xp, ":party_no", ":xp_amount", 0),
  ]),
  
  #script_get_improvement_details
  # INPUT: arg1 = improvement
  # OUTPUT: reg0 = base_cost
  ("get_improvement_details",
    [(store_script_param, ":improvement_no", 1),
      (try_begin),
        (eq, ":improvement_no", slot_center_has_manor),
        (str_store_string, s0, "@Mead Hall"),
        (str_store_string, s1, "@A manor lets you rest at the village and pay your troops half wages while you rest."),
        (assign, reg0, 8000),
      (else_try),
        (eq, ":improvement_no", slot_center_has_fish_pond),
        (str_store_string, s0, "@Mill"),
        (str_store_string, s1, "@A mill increases village prosperity by 5%."),
        (assign, reg0, 6000),
      (else_try),
        (eq, ":improvement_no", slot_center_has_watch_tower),
        (str_store_string, s0, "@Watchtower"),
        (str_store_string, s1, "@A watchtower lets the villagers raise the alarm earlier. The time it takes for enemies to loot the village increases by 50%."),
        (assign, reg0, 5000),
      (else_try),
        (eq, ":improvement_no", slot_center_has_school),
        (str_store_string, s0, "@School"),
        (str_store_string, s1, "@A school increases the loyalty of the villagers by +1 every month."),
        (assign, reg0, 9000),
      (else_try),
        (eq, ":improvement_no", slot_center_has_messenger_post),
        (str_store_string, s0, "@Messenger Post"),
        (str_store_string, s1, "@A messenger post lets the inhabitants send you a message whenever enemies are nearby, even if you are far away."),
        (assign, reg0, 4000),
      (else_try),
        (eq, ":improvement_no", slot_center_has_prisoner_tower),
        (str_store_string, s0, "@Prison Tower"),
        (str_store_string, s1, "@A prison tower reduces the chance that captives here will run away. It includes a torturer."),
        (assign, reg0, 7000),
        #buildings begin
      (else_try),
        (eq, ":improvement_no", slot_center_has_temple1),
        (str_store_string, s0, "@Christian Monastery"),
        (str_store_string, s1, "@A monastery promotes Christianity."),
        (assign, reg0, 9000),
      (else_try),
        (eq, ":improvement_no", slot_center_has_temple3),
        (str_store_string, s0, "@Temple to god Odin"),
        (str_store_string, s1, "@A temple promotes pagan practices."),
        (assign, reg0, 9000),
      (else_try),
        (eq, ":improvement_no", slot_center_has_monastery1),
        (str_store_string, s0, "@Christian Church"),
        (str_store_string, s1, "@A church promotes Christianity."),
        (assign, reg0, 14000),
      (else_try),
        (eq, ":improvement_no", slot_center_has_monastery3),
        (str_store_string, s0, "@Shrine to Norse Gods"),
        (str_store_string, s1, "@A shrine promotes pagan practices."),
        (assign, reg0, 14000),
      (else_try),
        (eq, ":improvement_no", slot_center_has_blacksmith),
        (str_store_string, s0, "@Blacksmith"),
        (str_store_string, s1, "@A blacksmith manufactures and sells tools and weapons."),
        (assign, reg0, 4000),
      (else_try),
        (eq, ":improvement_no", slot_center_has_guild),
        (str_store_string, s0, "@Merchants' Rest Hall"),
        (str_store_string, s1, "@A rest hall supports the merchant community, adding prosperity."),
        (assign, reg0, 8000),
      (else_try),
        (eq, ":improvement_no", slot_center_has_university),
        (str_store_string, s0, "@Scriptorium"),
        (str_store_string, s1, "@A scriptorium increases your renown as a patron of knowledge."),
        (assign, reg0, 16000),
      (else_try),
        (eq, ":improvement_no", slot_center_has_slavemarket),
        (str_store_string, s0, "@Slave Market"),
        (str_store_string, s1, "@A slave market increases the prosperity and tariffs of the port town, but reduces your reputation."),
        (assign, reg0, 14000),
      (else_try),
        (eq, ":improvement_no", slot_center_has_brewery),
        (str_store_string, s0, "@Brewery"),
        (str_store_string, s1, "@A brewery increases the prosperity and loyalty of the fort."),
        (assign, reg0, 5000),
        #buildings
      (try_end),
  ]),
  
  #script_cf_troop_agent_is_alive
  # INPUT: arg1 = troop_id
  ("cf_troop_agent_is_alive",
    [(store_script_param, ":troop_no", 1),
      (assign, ":alive_count", 0),
      (try_for_agents, ":cur_agent"),
        (agent_get_troop_id, ":cur_agent_troop", ":cur_agent"),
        (eq, ":troop_no", ":cur_agent_troop"),
        (agent_is_alive, ":cur_agent"),
        (val_add, ":alive_count", 1),
      (try_end),
      (gt, ":alive_count", 0),
  ]),
  
  #script_cf_village_recruit_volunteers_cond
  # INPUT: none
  # OUTPUT: none
  ("cf_village_recruit_volunteers_cond",
    [
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (display_message, "str_checking_volunteer_availability_script"),
      (try_end),
      
      (neg|party_slot_eq, "$current_town", slot_village_state, svs_looted),
      (neg|party_slot_eq, "$current_town", slot_village_state, svs_being_raided),
      (neg|party_slot_ge, "$current_town", slot_village_infested_by_bandits, 1),
      (store_faction_of_party, ":village_faction", "$current_town"),
      (party_get_slot, ":center_relation", "$current_town", slot_center_player_relation),
      (store_relation, ":village_faction_relation", ":village_faction", "fac_player_faction"),
      
      (ge, ":center_relation", 0),
      (try_begin),
        (eq, "$cheat_mode", 1),
        (display_message, "str_center_relation_at_least_zero"),
      (try_end),
      
      
      
      
      (this_or_next|ge, ":center_relation", 5),
      (this_or_next|eq, ":village_faction", "$players_kingdom"),
      (this_or_next|ge, ":village_faction_relation", 0),
      (this_or_next|eq, ":village_faction", "$supported_pretender_old_faction"),
      (eq, "$players_kingdom", 0),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (display_message, "str_relationfaction_conditions_met"),
      (try_end),
      
      
      (party_slot_ge, "$current_town", slot_center_volunteer_troop_amount, 1),
      (party_slot_ge, "$current_town", slot_center_volunteer_troop_type, 1),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (display_message, "str_troops_available"),
      (try_end),
      
      
      (party_get_free_companions_capacity, ":free_capacity", "p_main_party"),
      (ge, ":free_capacity", 1),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (display_message, "str_party_has_capacity"),
      (try_end),
      
      
  ]),
  
  #script_village_recruit_volunteers_recruit
  # INPUT: none
  # OUTPUT: none
  ("village_recruit_volunteers_recruit",
    [(party_get_slot, ":volunteer_troop", "$current_town", slot_center_volunteer_troop_type),
      (party_get_slot, ":volunteer_amount", "$current_town", slot_center_volunteer_troop_amount),
      (party_get_free_companions_capacity, ":free_capacity", "p_main_party"),
      (val_min, ":volunteer_amount", ":free_capacity"),
      (store_troop_gold, ":gold", "trp_player"),
      (call_script, "script_cost_per_village_recruit"),  #MOTO chief variable cost
      (assign, ":recruit_cost", reg0),
      (store_div, ":gold_capacity", ":gold", ":recruit_cost"),#10 denars per man MOTO chief change to variable cost
      (val_min, ":volunteer_amount", ":gold_capacity"),
      (party_add_members, "p_main_party", ":volunteer_troop", ":volunteer_amount"),
      (party_set_slot, "$current_town", slot_center_volunteer_troop_amount, -1),
      (store_mul, ":cost", ":volunteer_amount", ":recruit_cost"),#10 denars per man MOTO chief change to variable cost
      (troop_remove_gold, "trp_player", ":cost"),
  ]),
  
  #script_get_troop_item_amount
  # INPUT: arg1 = troop_no, arg2 = item_no
  # OUTPUT: reg0 = item_amount
  ("get_troop_item_amount",
    [(store_script_param, ":troop_no", 1),
      (store_script_param, ":item_no", 2),
      (troop_get_inventory_capacity, ":inv_cap", ":troop_no"),
      (assign, ":count", 0),
      (try_for_range, ":i_slot", 0, ":inv_cap"),
        (troop_get_inventory_slot, ":cur_item", ":troop_no", ":i_slot"),
        (eq, ":cur_item", ":item_no"),
        (val_add, ":count", 1),
      (try_end),
      (assign, reg0, ":count"),
  ]),
  
  #script_get_name_from_dna_to_s50
  # INPUT: arg1 = dna
  # OUTPUT: s50 = name
  ("get_name_from_dna_to_s50",
    [(store_script_param, ":dna", 1),
      (store_sub, ":num_names", names_end, names_begin),
      (store_sub, ":num_surnames", surnames_end, surnames_begin),
      (assign, ":selected_name", ":dna"),
      (val_mod, ":selected_name", ":num_names"),
      (assign, ":selected_surname", ":dna"),
      (val_div, ":selected_surname", ":num_names"),
      (val_mod, ":selected_surname", ":num_surnames"),
      (val_add, ":selected_name", names_begin),
      (val_add, ":selected_surname", surnames_begin),
      (str_store_string, s50, ":selected_name"),
      (str_store_string, s50, ":selected_surname"),
  ]),
  
  #script_change_center_prosperity
  # INPUT: arg1 = center_no, arg2 = difference
  # OUTPUT: none
  ("change_center_prosperity",
    [(store_script_param, ":center_no", 1),
      (store_script_param, ":difference", 2),
      (party_get_slot, ":old_prosperity", ":center_no", slot_town_prosperity),
      (store_add, ":new_prosperity", ":old_prosperity", ":difference"),
      (val_clamp, ":new_prosperity", 0, 100),
      (store_div, ":old_state", ":old_prosperity", 20),
      (store_div, ":new_state", ":new_prosperity", 20),
      
      (try_begin),
        (neq, ":old_state", ":new_state"),
        (neg|is_between, ":center_no", castles_begin, castles_end),
        
        (str_store_party_name_link, s2, ":center_no"),
        (call_script, "script_get_prosperity_text_to_s50", ":center_no"),
        (str_store_string, s3, s50),
        (party_set_slot, ":center_no", slot_town_prosperity, ":new_prosperity"),
        (call_script, "script_get_prosperity_text_to_s50", ":center_no"),
        (str_store_string, s4, s50),
        (try_begin),
          (party_slot_eq, ":center_no", slot_town_lord, "trp_player"),
          (display_message, "@The prosperity of {s2} has changed from {s3} to {s4}."),
        (try_end),
        (call_script, "script_update_center_notes", ":center_no"),
      (else_try),
        (party_set_slot, ":center_no", slot_town_prosperity, ":new_prosperity"),
      (try_end),
      
      (try_begin),
        (store_current_hours, ":hours"),
        (gt, ":hours", 1),
        (store_sub, ":actual_difference", ":new_prosperity", ":old_prosperity"),
        (try_begin),
          (lt, ":actual_difference", 0),
          (val_add, "$newglob_total_prosperity_losses", ":actual_difference"),
        (else_try),
          (gt, ":actual_difference", 0),
          (val_add, "$newglob_total_prosperity_gains", ":actual_difference"),
        (try_end),
      (try_end),
      
      #This will add up all non-trade prosperity
      (try_begin),
        (eq, "$cheat_mode", 3),
        (assign, reg4, "$newglob_total_prosperity_from_bandits"),
        (assign, reg5, "$newglob_total_prosperity_from_caravan_trade"),
        (assign, reg7, "$newglob_total_prosperity_from_villageloot"),
        (assign, reg8, "$newglob_total_prosperity_from_townloot"),
        (assign, reg9, "$newglob_total_prosperity_from_village_trade"),
        (assign, reg10, "$newglob_total_prosperity_from_convergence"),
        (assign, reg11, "$newglob_total_prosperity_losses"),
        (assign, reg12, "$newglob_total_prosperity_gains"),
        (display_message, "@{!}DEBUG: Total prosperity actual losses: {reg11}"),
        (display_message, "@{!}DEBUG: Total prosperity actual gains: {reg12}"),
        
        (display_message, "@{!}DEBUG: Prosperity changes from random bandits: {reg4}"),
        (display_message, "@{!}DEBUG: Prosperity changes from caravan trades: {reg5}"),
        (display_message, "@{!}DEBUG: Prosperity changes from farmer trades: {reg9}"),
        (display_message, "@{!}DEBUG: Prosperity changes from looted villages: {reg7}"),
        (display_message, "@{!}DEBUG: Prosperity changes from sieges: {reg8}"),
        (display_message, "@{!}DEBUG: Theoretical prosperity changes from convergence: {reg10}"),
      (try_end),
      
  ]),
  
  #script_get_center_ideal_prosperity
  # INPUT: arg1 = center_no
  # OUTPUT: reg0 = ideal_prosperity
  ("get_center_ideal_prosperity",
    [(store_script_param, ":center_no", 1),
      (assign, ":ideal", 65),
      
      (call_script, "script_center_get_goods_availability", ":center_no"),
      (store_mul, ":hardship_index", reg0, 2),
      (val_sub, ":ideal", ":hardship_index"),
      
      (try_begin),
        (is_between, ":center_no", villages_begin, villages_end),
        (party_slot_eq, ":center_no", slot_center_has_fish_pond, 1),
        (val_add, ":ideal", 5),
      (try_end),
      
      (val_max, ":ideal", 0),
      
      (assign, reg0, ":ideal"),
  ]),
  
  ("good_price_affects_good_production",
    [
      (store_script_param, ":center", 1),
      (store_script_param, ":input_item_no", 2),
      (store_script_param, ":production", 3),
      (store_script_param, ":impact_divisor", 4),
      
      (assign, reg4, ":production"),
      
      (try_begin),
        (gt, ":production", 0), #let's take -20 as the zero production rate, although in actuality production can go lower, representing increased demand
        
        (store_sub, ":input_good_price_slot", ":input_item_no", trade_goods_begin),
        (val_add, ":input_good_price_slot", slot_town_trade_good_prices_begin),
        (party_get_slot, ":input_price", ":center", ":input_good_price_slot"),
        
        (try_begin),
          (is_between, ":center", towns_begin, towns_end),
          
          (val_mul, ":input_price", 4),
          (assign, ":number_of_villages", 4),
          (try_for_range, ":village_no", villages_begin, villages_end),
            (party_slot_eq, ":village_no", slot_village_bound_center, ":center"),
            (party_get_slot, ":input_price_at_village", ":village_no", ":input_good_price_slot"),
            (val_add, ":input_price", ":input_price_at_village"),
            (val_add, ":number_of_villages", 1),
          (try_end),
          
          (val_div, ":input_price", ":number_of_villages"),
        (try_end),
        
        (try_begin), #1/2 impact for low prices
          (lt, ":input_price", 1000),
          (val_mul, ":impact_divisor", 2),
        (try_end),
        
        (try_begin),
          (gt, ":impact_divisor", 1),
          (val_sub, ":input_price", 1000),
          (val_div, ":input_price", ":impact_divisor"),
          (val_add, ":input_price", 1000),
        (try_end),
        
        (val_mul, ":production", 1000),
        (val_div, ":production", ":input_price"),
        
        #(assign, reg5, ":production"),
        #(assign, reg3, ":input_price"),
        #(str_store_item_name, s4, ":input_item_no"),
        #(display_message, "@{s4} price of {reg3} reduces production from {reg4} to {reg5}"),
      (try_end),
      
      (assign, reg0, ":production"),
  ]),
  
  
  
  
  #script_get_poorest_village_of_faction
  # INPUT: arg1 = faction_no
  # OUTPUT: reg0 = min_prosperity_village
  ("get_poorest_village_of_faction",
    [(store_script_param, ":faction_no", 1),
      (assign, ":min_prosperity_village", -1),
      (assign, ":min_prosperity", 101),
      (try_for_range, ":village_no", villages_begin, villages_end),
        (store_faction_of_party, ":village_faction", ":village_no"),
        (eq, ":village_faction", ":faction_no"),
        (party_get_slot, ":prosperity", ":village_no", slot_town_prosperity),
        (lt, ":prosperity", ":min_prosperity"),
        (assign, ":min_prosperity", ":prosperity"),
        (assign, ":min_prosperity_village", ":village_no"),
      (try_end),
      (assign, reg0, ":min_prosperity_village"),
  ]),
  
  #script_troop_add_gold
  # INPUT: arg1 = troop_no, arg2 = amount
  # OUTPUT: none
  ("troop_add_gold",
    [
      (store_script_param, ":troop_no", 1),
      (store_script_param, ":amount", 2),
      
      (troop_add_gold, ":troop_no", ":amount"),
      (try_begin),
        (eq, ":troop_no", "trp_player"),
        (play_sound, "snd_money_received"),
      (try_end),
  ]),
  
  #NPC companion changes begin
  ("initialize_npcs",
    [
      
      # set strings
      
      (troop_set_slot, "trp_npc1", slot_troop_morality_type, tmt_egalitarian),  #Caio
      (troop_set_slot, "trp_npc1", slot_troop_morality_value, 4),  #Caio
      (troop_set_slot, "trp_npc1", slot_troop_2ary_morality_type, tmt_aristocratic),  #Caio
      (troop_set_slot, "trp_npc1", slot_troop_2ary_morality_value, -1),
      (troop_set_slot, "trp_npc1", slot_troop_personalityclash_object, "trp_npc7"),  #Caio - Dwywei
      (troop_set_slot, "trp_npc1", slot_troop_personalityclash2_object, "trp_npc16"),  #Caio - beda
      (troop_set_slot, "trp_npc1", slot_troop_personalitymatch_object, "trp_npc2"),  #Caio - Egil
      (troop_set_slot, "trp_npc1", slot_troop_home, "p_hadrian_wall1"), #Hadrian Wall
      (troop_set_slot, "trp_npc1", slot_troop_payment_request, 0),
      (troop_set_slot, "trp_npc1", slot_troop_kingsupport_argument, argument_ruler),
      (troop_set_slot, "trp_npc1", slot_troop_kingsupport_opponent, "trp_npc9"), #clovis
      (troop_set_slot, "trp_npc1", slot_troop_town_with_contacts, "p_town_27"), #bebbanburh
      (troop_set_slot, "trp_npc1", slot_troop_original_faction, 0), #bebbanburh
      (troop_set_slot, "trp_npc1", slot_lord_reputation_type, lrep_roguish), #
      (troop_set_slot, "trp_npc1", slot_troop_religion, 2), # pagan
      (troop_set_slot, "trp_npc1", slot_troop_age, 17),
      
      (troop_set_slot, "trp_npc2", slot_troop_morality_type, tmt_aristocratic), #Egil
      (troop_set_slot, "trp_npc2", slot_troop_morality_value, 4),
      (troop_set_slot, "trp_npc2", slot_troop_2ary_morality_type, tmt_honest),
      (troop_set_slot, "trp_npc2", slot_troop_2ary_morality_value, -1),
      (troop_set_slot, "trp_npc2", slot_troop_personalityclash_object, "trp_npc5"), #egil - beheshtur
      (troop_set_slot, "trp_npc2", slot_troop_personalityclash2_object, "trp_npc9"), #egil - alayen
      (troop_set_slot, "trp_npc2", slot_troop_personalitymatch_object, "trp_npc8"),  #egil - Reginhard
      (troop_set_slot, "trp_npc2", slot_troop_home, "p_town_4"), #Ribe
      (troop_set_slot, "trp_npc2", slot_troop_payment_request, 0),
      (troop_set_slot, "trp_npc2", slot_troop_kingsupport_argument, argument_victory),
      (troop_set_slot, "trp_npc2", slot_troop_kingsupport_opponent, "trp_npc14"), #ailchu
      (troop_set_slot, "trp_npc2", slot_troop_town_with_contacts, "p_town_4"), #Ribe
      (troop_set_slot, "trp_npc2", slot_troop_original_faction, "fac_kingdom_1"), #danmark
      (troop_set_slot, "trp_npc2", slot_lord_reputation_type, lrep_roguish), # picaresco
      (troop_set_slot, "trp_npc2", slot_troop_religion, 2), # pagan
      (troop_set_slot, "trp_npc2", slot_troop_age, 29),
      
      (troop_set_slot, "trp_npc3", slot_troop_morality_type, tmt_humanitarian), #Brunhild
      (troop_set_slot, "trp_npc3", slot_troop_morality_value, 2),
      (troop_set_slot, "trp_npc3", slot_troop_2ary_morality_type, tmt_egalitarian),
      (troop_set_slot, "trp_npc3", slot_troop_2ary_morality_value, -1),
      (troop_set_slot, "trp_npc3", slot_troop_personalityclash_object, "trp_npc14"), #Brunhild - Ailchu
      (troop_set_slot, "trp_npc3", slot_troop_personalityclash2_object, "trp_npc8"), #vs Reginhard
      (troop_set_slot, "trp_npc3", slot_troop_personalitymatch_object, "trp_npc9"), #Brunhild - alayen
      (troop_set_slot, "trp_npc3", slot_troop_home, "p_castle_63"), #Kennemer
      (troop_set_slot, "trp_npc3", slot_troop_payment_request, 0),
      (troop_set_slot, "trp_npc3", slot_troop_kingsupport_argument, argument_lords),
      (troop_set_slot, "trp_npc3", slot_troop_kingsupport_opponent, "trp_npc8"), #Reginhard
      (troop_set_slot, "trp_npc3", slot_troop_town_with_contacts, "p_town_11"), #dorestad
      (troop_set_slot, "trp_npc3", slot_troop_original_faction, 0), #ichamur
      (troop_set_slot, "trp_npc3", slot_lord_reputation_type, lrep_selfrighteous), #
      (troop_set_slot, "trp_npc3", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_npc3", slot_troop_age, 18),
      
      (troop_set_slot, "trp_npc4", slot_troop_morality_type, tmt_aristocratic), #Donnchadh
      (troop_set_slot, "trp_npc4", slot_troop_morality_value, 4),
      (troop_set_slot, "trp_npc4", slot_troop_2ary_morality_type, tmt_honest),
      (troop_set_slot, "trp_npc4", slot_troop_2ary_morality_value, -1),
      (troop_set_slot, "trp_npc4", slot_troop_personalityclash_object, "trp_npc10"), #Donnchadh - Ceawlin
      (troop_set_slot, "trp_npc4", slot_troop_personalityclash2_object, "trp_npc7"), #Donnchadh - Picts
      (troop_set_slot, "trp_npc4", slot_troop_personalitymatch_object, "trp_npc5"), #Donnchadh - Morgant
      (troop_set_slot, "trp_npc4", slot_troop_home, "p_castle_39"), #Dun_Onlaigh
      (troop_set_slot, "trp_npc4", slot_troop_payment_request, 300),
      (troop_set_slot, "trp_npc4", slot_troop_kingsupport_argument, argument_claim),
      (troop_set_slot, "trp_npc4", slot_troop_kingsupport_opponent, "trp_npc13"), #helgi
      (troop_set_slot, "trp_npc4", slot_troop_town_with_contacts, "p_castle_39"), #Dun_Onlaigh
      (troop_set_slot, "trp_npc4", slot_troop_original_faction, 0), #
      (troop_set_slot, "trp_npc4", slot_lord_reputation_type, lrep_cunning), #
      (troop_set_slot, "trp_npc4", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_npc4", slot_troop_age, 35),
      
      (troop_set_slot, "trp_npc5", slot_troop_morality_type, tmt_egalitarian),  ##Morgant
      (troop_set_slot, "trp_npc5", slot_troop_morality_value, 3),  #Morgant
      (troop_set_slot, "trp_npc5", slot_troop_2ary_morality_type, -1),
      (troop_set_slot, "trp_npc5", slot_troop_2ary_morality_value, 0),
      (troop_set_slot, "trp_npc5", slot_troop_personalityclash_object, "trp_npc2"),  #Morgant - Egil
      (troop_set_slot, "trp_npc5", slot_troop_personalityclash2_object, "trp_npc11"),  #Morgant- Solveig
      (troop_set_slot, "trp_npc5", slot_troop_personalitymatch_object, "trp_npc4"),  #Morgant - rolf
      (troop_set_slot, "trp_npc5", slot_troop_home, "p_town_17"), #Halmar
      (troop_set_slot, "trp_npc5", slot_troop_payment_request, 400),
      (troop_set_slot, "trp_npc5", slot_troop_kingsupport_argument, argument_ruler),
      (troop_set_slot, "trp_npc5", slot_troop_kingsupport_opponent, "trp_npc3"), #Brunhild
      (troop_set_slot, "trp_npc5", slot_troop_town_with_contacts, "p_castle_30"), #tulga
      (troop_set_slot, "trp_npc5", slot_troop_original_faction, "fac_kingdom_12"), #Cornubia
      (troop_set_slot, "trp_npc5", slot_lord_reputation_type, lrep_cunning), #
      (troop_set_slot, "trp_npc5", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_npc5", slot_troop_age, 29),
      
      (troop_set_slot, "trp_npc6", slot_troop_morality_type, tmt_aristocratic), #Bodo
      (troop_set_slot, "trp_npc6", slot_troop_morality_value, 4),  #
      (troop_set_slot, "trp_npc6", slot_troop_2ary_morality_type, tmt_honest),
      (troop_set_slot, "trp_npc6", slot_troop_2ary_morality_value, -1),
      (troop_set_slot, "trp_npc6", slot_troop_personalityclash_object, "trp_npc11"), #
      (troop_set_slot, "trp_npc6", slot_troop_personalityclash2_object, "trp_npc13"), #
      (troop_set_slot, "trp_npc6", slot_troop_personalitymatch_object, "trp_npc16"),  #
      (troop_set_slot, "trp_npc6", slot_troop_home, "p_town_1"), #Cantwareburh, cuenta su historia
      (troop_set_slot, "trp_npc6", slot_troop_payment_request, 0),
      (troop_set_slot, "trp_npc6", slot_troop_kingsupport_argument, argument_victory),
      (troop_set_slot, "trp_npc6", slot_troop_kingsupport_opponent, "trp_npc2"), #Egil
      (troop_set_slot, "trp_npc6", slot_troop_town_with_contacts, "p_town_1"), #Cantwareburh
      (troop_set_slot, "trp_npc6", slot_troop_original_faction, 0), #
      (troop_set_slot, "trp_npc6", slot_lord_reputation_type, lrep_custodian), #
      (troop_set_slot, "trp_npc6", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_npc6", slot_troop_age, 43),
      
      (troop_set_slot, "trp_npc7", slot_troop_morality_type, tmt_humanitarian),  #Dwywei
      (troop_set_slot, "trp_npc7", slot_troop_morality_value, 3),  #Dwywei
      (troop_set_slot, "trp_npc7", slot_troop_2ary_morality_type, -1),
      (troop_set_slot, "trp_npc7", slot_troop_2ary_morality_value, 0),
      (troop_set_slot, "trp_npc7", slot_troop_personalityclash_object, "trp_npc1"),  #Dwywei- Caio
      (troop_set_slot, "trp_npc7", slot_troop_personalityclash2_object, "trp_npc4"),  #Dwywei -
      (troop_set_slot, "trp_npc7", slot_troop_personalitymatch_object, "trp_npc16"),  #Dwywei - Beda
      (troop_set_slot, "trp_npc7", slot_troop_home, "p_castle_37"), #Dun Taruo
      (troop_set_slot, "trp_npc7", slot_troop_payment_request, 200),
      (troop_set_slot, "trp_npc7", slot_troop_kingsupport_argument, argument_victory),
      (troop_set_slot, "trp_npc7", slot_troop_kingsupport_opponent, "trp_npc4"), #donnach
      (troop_set_slot, "trp_npc7", slot_troop_town_with_contacts, "p_castle_37"), #Dun Taruo
      (troop_set_slot, "trp_npc7", slot_troop_original_faction, 0), #alban
      (troop_set_slot, "trp_npc7", slot_lord_reputation_type, lrep_custodian), #
      (troop_set_slot, "trp_npc7", slot_troop_religion, 2), # pagan
      (troop_set_slot, "trp_npc7", slot_troop_age, 21),
      
      (troop_set_slot, "trp_npc8", slot_troop_morality_type, tmt_egalitarian), ##Reginhard , frisio
      (troop_set_slot, "trp_npc8", slot_troop_morality_value, 3),  ##
      (troop_set_slot, "trp_npc8", slot_troop_2ary_morality_type, -1),
      (troop_set_slot, "trp_npc8", slot_troop_2ary_morality_value, 0),
      (troop_set_slot, "trp_npc8", slot_troop_personalityclash_object, "trp_npc12"), #le disgusta
      (troop_set_slot, "trp_npc8", slot_troop_personalityclash2_object, "trp_npc3"), # le disgusta Brunhild
      (troop_set_slot, "trp_npc8", slot_troop_personalitymatch_object, "trp_npc13"),  # le gusta
      (troop_set_slot, "trp_npc8", slot_troop_home, "p_town_11"), # Dorestad
      (troop_set_slot, "trp_npc8", slot_troop_payment_request, 0), #no cobra, ya cobra en la quest antes
      (troop_set_slot, "trp_npc8", slot_troop_kingsupport_argument, argument_claim), #argumento para el trono
      (troop_set_slot, "trp_npc8", slot_troop_kingsupport_opponent, "trp_npc5"), #el que se opone a que Regihard sea el kingsupport del player (morgant)
      (troop_set_slot, "trp_npc8", slot_troop_town_with_contacts, "p_town_11"), #Dorestad para encontrar apoyos
      (troop_set_slot, "trp_npc8", slot_troop_original_faction, "fac_kingdom_4"), #frisia
      (troop_set_slot, "trp_npc8", slot_lord_reputation_type, lrep_roguish), #
      (troop_set_slot, "trp_npc8", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_npc8", slot_troop_age, 40),
      
      (troop_set_slot, "trp_npc9", slot_troop_morality_type, tmt_aristocratic), #clovis
      (troop_set_slot, "trp_npc9", slot_troop_morality_value, 2),  #clovis
      (troop_set_slot, "trp_npc9", slot_troop_2ary_morality_type, tmt_honest),
      (troop_set_slot, "trp_npc9", slot_troop_2ary_morality_value, 1),
      (troop_set_slot, "trp_npc9", slot_troop_personalityclash_object, "trp_npc13"), ##clovis vs swedish
      (troop_set_slot, "trp_npc9", slot_troop_personalityclash2_object, "trp_npc2"), ##clovis vs Egil
      (troop_set_slot, "trp_npc9", slot_troop_personalitymatch_object, "trp_npc3"),  #clovis - Brunhild
      (troop_set_slot, "trp_npc9", slot_troop_home, "p_castle_19"), #East engle
      (troop_set_slot, "trp_npc9", slot_troop_payment_request, 300),
      (troop_set_slot, "trp_npc9", slot_troop_kingsupport_argument, argument_lords),
      (troop_set_slot, "trp_npc9", slot_troop_kingsupport_opponent, "trp_npc1"), #Caio
      (troop_set_slot, "trp_npc9", slot_troop_town_with_contacts, "p_castle_19"), #redlaesdham
      (troop_set_slot, "trp_npc9", slot_troop_original_faction, "fac_kingdom_6"), #east engle
      (troop_set_slot, "trp_npc9", slot_lord_reputation_type, lrep_martial), #
      (troop_set_slot, "trp_npc9", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_npc9", slot_troop_age, 34),
      
      (troop_set_slot, "trp_npc10", slot_troop_morality_type, tmt_humanitarian), #Ceawlin
      (troop_set_slot, "trp_npc10", slot_troop_morality_value, 2),
      (troop_set_slot, "trp_npc10", slot_troop_2ary_morality_type, tmt_egalitarian),
      (troop_set_slot, "trp_npc10", slot_troop_2ary_morality_value, 1),
      (troop_set_slot, "trp_npc10", slot_troop_personalityclash_object, "trp_npc4"), #Ceawlin vs rolf
      (troop_set_slot, "trp_npc10", slot_troop_personalityclash2_object, "trp_npc14"), #Ailchu vs #Ceawlin
      (troop_set_slot, "trp_npc10", slot_troop_personalitymatch_object, "trp_npc11"),  #Ceawlin likes Solveig
      (troop_set_slot, "trp_npc10", slot_troop_home, "p_town_12"), #lundenwic
      (troop_set_slot, "trp_npc10", slot_troop_payment_request, 200),
      (troop_set_slot, "trp_npc10", slot_troop_kingsupport_argument, argument_ruler),
      (troop_set_slot, "trp_npc10", slot_troop_kingsupport_opponent, "trp_npc15"), #Agathinos
      (troop_set_slot, "trp_npc10", slot_troop_town_with_contacts, "p_town_12"), #lundenwic
      (troop_set_slot, "trp_npc10", slot_troop_original_faction, "fac_kingdom_5"), #wessex
      (troop_set_slot, "trp_npc10", slot_lord_reputation_type, lrep_benefactor), #
      (troop_set_slot, "trp_npc10", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_npc10", slot_troop_age, 38),
      
      (troop_set_slot, "trp_npc11", slot_troop_morality_type, tmt_egalitarian),  #Solveig
      (troop_set_slot, "trp_npc11", slot_troop_morality_value, 3),
      (troop_set_slot, "trp_npc11", slot_troop_2ary_morality_type, -1),
      (troop_set_slot, "trp_npc11", slot_troop_2ary_morality_value, 0),
      (troop_set_slot, "trp_npc11", slot_troop_personalityclash_object, "trp_npc2"),  #Solveig vs Egil
      (troop_set_slot, "trp_npc11", slot_troop_personalityclash2_object, "trp_npc8"),  #Solveig - Reginhard
      (troop_set_slot, "trp_npc11", slot_troop_personalitymatch_object, "trp_npc3"),  #Solveig likes Brunhild
      (troop_set_slot, "trp_npc11", slot_troop_home, "p_town_5"), #Tunsberg
      (troop_set_slot, "trp_npc11", slot_troop_payment_request, 0),
      (troop_set_slot, "trp_npc11", slot_troop_kingsupport_argument, argument_claim),
      (troop_set_slot, "trp_npc11", slot_troop_kingsupport_opponent, "trp_npc6"), #Bodo
      (troop_set_slot, "trp_npc11", slot_troop_town_with_contacts, "p_town_5"), #Tunsberg
      (troop_set_slot, "trp_npc11", slot_troop_original_faction, 0), #
      (troop_set_slot, "trp_npc11", slot_lord_reputation_type, lrep_custodian), #
      (troop_set_slot, "trp_npc11", slot_troop_religion, 2), # pagan
      (troop_set_slot, "trp_npc11", slot_troop_age, 27),
      
      (troop_set_slot, "trp_npc12", slot_troop_morality_type, tmt_humanitarian), #Asbjorn
      (troop_set_slot, "trp_npc12", slot_troop_morality_value, 3),
      (troop_set_slot, "trp_npc12", slot_troop_2ary_morality_type, -1),
      (troop_set_slot, "trp_npc12", slot_troop_2ary_morality_value, 0),
      (troop_set_slot, "trp_npc12", slot_troop_personalityclash_object, "trp_npc8"), #Reginhard
      (troop_set_slot, "trp_npc12", slot_troop_personalityclash2_object, "trp_npc15"), #Asbjorn - artimenner
      (troop_set_slot, "trp_npc12", slot_troop_personalitymatch_object, "trp_npc6"),  #Asbjorn - Bodo
      (troop_set_slot, "trp_npc12", slot_troop_home, "p_castle_52"), #Heidabyr #story
      (troop_set_slot, "trp_npc12", slot_troop_payment_request, 0),
      (troop_set_slot, "trp_npc12", slot_troop_kingsupport_argument, argument_claim),
      (troop_set_slot, "trp_npc12", slot_troop_kingsupport_opponent, "trp_npc16"), #beda
      (troop_set_slot, "trp_npc12", slot_troop_town_with_contacts, "p_town_4"), #Ribe
      (troop_set_slot, "trp_npc12", slot_troop_original_faction, 0), #
      (troop_set_slot, "trp_npc12", slot_lord_reputation_type, lrep_benefactor), #
      (troop_set_slot, "trp_npc12", slot_troop_religion, 2), # pagan
      (troop_set_slot, "trp_npc12", slot_troop_age, 36),
      
      (troop_set_slot, "trp_npc13", slot_troop_morality_type, tmt_aristocratic), #helgi
      (troop_set_slot, "trp_npc13", slot_troop_morality_value, 3),
      (troop_set_slot, "trp_npc13", slot_troop_2ary_morality_type, -1),
      (troop_set_slot, "trp_npc13", slot_troop_2ary_morality_value, 0),
      (troop_set_slot, "trp_npc13", slot_troop_personalityclash_object, "trp_npc9"), #helgi - clovis
      (troop_set_slot, "trp_npc13", slot_troop_personalityclash2_object, "trp_npc2"), #helgi - egil
      (troop_set_slot, "trp_npc13", slot_troop_personalitymatch_object, "trp_npc8"), #helgi - reginhard
      (troop_set_slot, "trp_npc13", slot_troop_home, "p_castle_53"), #
      (troop_set_slot, "trp_npc13", slot_troop_payment_request, 300),
      (troop_set_slot, "trp_npc13", slot_troop_kingsupport_argument, argument_claim),
      (troop_set_slot, "trp_npc13", slot_troop_kingsupport_opponent, "trp_npc10"), #nizar
      (troop_set_slot, "trp_npc13", slot_troop_town_with_contacts, "p_castle_53"), #s
      (troop_set_slot, "trp_npc13", slot_troop_original_faction, 0), #
      (troop_set_slot, "trp_npc13", slot_lord_reputation_type, lrep_roguish), #
      (troop_set_slot, "trp_npc13", slot_troop_religion, 2), # pagan
      (troop_set_slot, "trp_npc13", slot_troop_age, 30),
      
      (troop_set_slot, "trp_npc14", slot_troop_morality_type, tmt_aristocratic), #Ailchu
      (troop_set_slot, "trp_npc14", slot_troop_morality_value, 4),
      (troop_set_slot, "trp_npc14", slot_troop_2ary_morality_type, tmt_egalitarian),
      (troop_set_slot, "trp_npc14", slot_troop_2ary_morality_value, -1),
      (troop_set_slot, "trp_npc14", slot_troop_personalityclash_object, "trp_npc3"), #Ailchu
      (troop_set_slot, "trp_npc14", slot_troop_personalityclash2_object, "trp_npc10"), #Ailchu - Ceadwil
      (troop_set_slot, "trp_npc14", slot_troop_personalitymatch_object, "trp_npc15"), #Ailchu - agathinos
      (troop_set_slot, "trp_npc14", slot_troop_home, "p_town_24"), #Caiseal
      (troop_set_slot, "trp_npc14", slot_troop_payment_request, 300),
      (troop_set_slot, "trp_npc14", slot_troop_kingsupport_argument, argument_victory),
      (troop_set_slot, "trp_npc14", slot_troop_kingsupport_opponent, "trp_npc11"), #nizar
      (troop_set_slot, "trp_npc14", slot_troop_town_with_contacts, "p_town_24"), #Caiseal
      (troop_set_slot, "trp_npc14", slot_troop_original_faction, 0), #
      (troop_set_slot, "trp_npc14", slot_lord_reputation_type, lrep_selfrighteous), #
      (troop_set_slot, "trp_npc14", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_npc14", slot_troop_age, 26),
      
      (troop_set_slot, "trp_npc15", slot_troop_morality_type, tmt_egalitarian),  #agathinos
      (troop_set_slot, "trp_npc15", slot_troop_morality_value, 2),
      (troop_set_slot, "trp_npc15", slot_troop_2ary_morality_type, tmt_honest),
      (troop_set_slot, "trp_npc15", slot_troop_2ary_morality_value, 1),
      (troop_set_slot, "trp_npc15", slot_troop_personalityclash_object, "trp_npc16"), #aghatinos - klethi
      (troop_set_slot, "trp_npc15", slot_troop_personalityclash2_object, "trp_npc12"), #aghatinos - jeremus
      (troop_set_slot, "trp_npc15", slot_troop_personalitymatch_object, "trp_npc14"), #aghatinos - artimenner
      (troop_set_slot, "trp_npc15", slot_troop_home, "p_castle_54"), #Hordaland
      (troop_set_slot, "trp_npc15", slot_troop_payment_request, 0),
      (troop_set_slot, "trp_npc15", slot_troop_kingsupport_argument, argument_ruler),
      (troop_set_slot, "trp_npc15", slot_troop_kingsupport_opponent, "trp_npc7"), #dwywei
      (troop_set_slot, "trp_npc15", slot_troop_town_with_contacts, "p_town_5"), #Tunsberg
      (troop_set_slot, "trp_npc15", slot_lord_reputation_type, lrep_custodian), #
      (troop_set_slot, "trp_npc15", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_npc15", slot_troop_age, 65),
      
      (troop_set_slot, "trp_npc16", slot_troop_morality_type, tmt_humanitarian), #beda
      (troop_set_slot, "trp_npc16", slot_troop_morality_value, 3),
      (troop_set_slot, "trp_npc16", slot_troop_2ary_morality_type, tmt_honest),
      (troop_set_slot, "trp_npc16", slot_troop_2ary_morality_value, -1),
      (troop_set_slot, "trp_npc16", slot_troop_personalityclash_object, "trp_npc2"), #Egil
      (troop_set_slot, "trp_npc16", slot_troop_personalityclash2_object, "trp_npc11"), #Solveig
      (troop_set_slot, "trp_npc16", slot_troop_personalitymatch_object, "trp_npc15"),  #beda - aghatinos
      (troop_set_slot, "trp_npc16", slot_troop_home, "p_town_16"), #Witan Ceaster
      (troop_set_slot, "trp_npc16", slot_troop_payment_request, 0),
      (troop_set_slot, "trp_npc16", slot_troop_kingsupport_argument, argument_lords),
      (troop_set_slot, "trp_npc16", slot_troop_kingsupport_opponent, "trp_npc12"), #Asbjorn
      (troop_set_slot, "trp_npc16", slot_troop_town_with_contacts, "p_town_16"), #Witan Ceaster
      (troop_set_slot, "trp_npc16", slot_lord_reputation_type, lrep_selfrighteous), #
      (troop_set_slot, "trp_npc16", slot_troop_religion, 1), # christian
      (troop_set_slot, "trp_npc16", slot_troop_age, 63),
      
      (store_sub, "$number_of_npc_slots", slot_troop_strings_end, slot_troop_intro),
      
      (try_for_range, ":npc", companions_begin, companions_end),
        
        
        (try_for_range, ":slot_addition", 0, "$number_of_npc_slots"),
          (store_add, ":slot", ":slot_addition", slot_troop_intro),
          
          (store_mul, ":string_addition", ":slot_addition", 16),
          (store_add, ":string", "str_npc1_intro", ":string_addition"),
          (val_add, ":string", ":npc"),
          (val_sub, ":string", companions_begin),
          
          (troop_set_slot, ":npc", ":slot", ":string"),
        (try_end),
      (try_end),
      
      
      #Post 0907 changes begin
      (call_script, "script_add_log_entry", logent_game_start, "trp_player", -1, -1, -1),
      #Post 0907 changes end
      
      #Rebellion changes begin Pretenders
      (troop_set_slot, "trp_kingdom_1_pretender",  slot_troop_original_faction2, "fac_kingdom_1"), #Danmark
      (troop_set_slot, "trp_kingdom_2_pretender",  slot_troop_original_faction2, "fac_kingdom_2"), #irish aileach
      (troop_set_slot, "trp_kingdom_3_pretender",  slot_troop_original_faction2, "fac_kingdom_3"), #northumbria
      (troop_set_slot, "trp_kingdom_4_pretender",  slot_troop_original_faction2, "fac_kingdom_4"), #wessex
      (troop_set_slot, "trp_kingdom_5_pretender",  slot_troop_original_faction2, "fac_kingdom_5"), #pictish Alban
      (troop_set_slot, "trp_kingdom_6_pretender",  slot_troop_original_faction2, "fac_kingdom_6"), #gwynedd
      
      (troop_set_slot, "trp_kingdom_1_pretender",  slot_troop_original_faction, "fac_kingdom_1"), #Danmark
      (troop_set_slot, "trp_kingdom_2_pretender",  slot_troop_original_faction, "fac_kingdom_18"), #irish aileach
      (troop_set_slot, "trp_kingdom_3_pretender",  slot_troop_original_faction, "fac_kingdom_8"), #northumbria
      (troop_set_slot, "trp_kingdom_4_pretender",  slot_troop_original_faction, "fac_kingdom_5"), #wessex
      (troop_set_slot, "trp_kingdom_5_pretender",  slot_troop_original_faction, "fac_kingdom_20"), #pictish Alban
      (troop_set_slot, "trp_kingdom_6_pretender",  slot_troop_original_faction, "fac_kingdom_9"), #gwynedd
      
      #        (troop_set_slot, "trp_kingdom_1_pretender", slot_troop_support_base,     "p_town_4"), #suno
      #        (troop_set_slot, "trp_kingdom_2_pretender", slot_troop_support_base,     "p_town_11"), #curaw
      #        (troop_set_slot, "trp_kingdom_3_pretender", slot_troop_support_base,     "p_town_18"), #town_18
      #        (troop_set_slot, "trp_kingdom_4_pretender", slot_troop_support_base,     "p_town_12"), #wercheg
      #        (troop_set_slot, "trp_kingdom_5_pretender", slot_troop_support_base,     "p_town_3"), #veluca
      (try_for_range, ":pretender", pretenders_begin, pretenders_end),
        (troop_set_slot, ":pretender", slot_lord_reputation_type, lrep_none),
      (try_end),
      #Rebellion changes end
  ]),
  
  
  
  ("objectionable_action",
    [
      (store_script_param_1, ":action_type"),
      (store_script_param_2, ":action_string"),
      
      (assign, ":grievance_minimum", -2),
      (try_for_range, ":npc", companions_begin, companions_end),
        (main_party_has_troop, ":npc"),
        
        ###Primary morality check
        (try_begin),
          (troop_slot_eq, ":npc", slot_troop_morality_type, ":action_type"),
          (troop_get_slot, ":value", ":npc", slot_troop_morality_value),
          (try_begin),
            (troop_slot_eq, ":npc", slot_troop_morality_state, tms_acknowledged),
            # npc is betrayed, major penalty to player honor and morale
            (troop_get_slot, ":grievance", ":npc", slot_troop_morality_penalties),
            (val_mul, ":value", 2),
            (val_add, ":grievance", ":value"),
            (troop_set_slot, ":npc", slot_troop_morality_penalties, ":grievance"),
          (else_try),
            (this_or_next|troop_slot_eq, ":npc", slot_troop_morality_state, tms_dismissed),
            (eq, "$disable_npc_complaints", 1),
            # npc is quietly disappointed
            (troop_get_slot, ":grievance", ":npc", slot_troop_morality_penalties),
            (val_add, ":grievance", ":value"),
            (troop_set_slot, ":npc", slot_troop_morality_penalties, ":grievance"),
          (else_try),
            # npc raises the issue for the first time
            (troop_slot_eq, ":npc", slot_troop_morality_state, tms_no_problem),
            (gt, ":value", ":grievance_minimum"),
            (assign, "$npc_with_grievance", ":npc"),
            (assign, "$npc_grievance_string", ":action_string"),
            (assign, "$npc_grievance_slot", slot_troop_morality_state),
            (assign, ":grievance_minimum", ":value"),
            (assign, "$npc_praise_not_complaint", 0),
            (try_begin),
              (lt, ":value", 0),
              (assign, "$npc_praise_not_complaint", 1),
            (try_end),
          (try_end),
          
          ###Secondary morality check
        (else_try),
          (troop_slot_eq, ":npc", slot_troop_2ary_morality_type, ":action_type"),
          (troop_get_slot, ":value", ":npc", slot_troop_2ary_morality_value),
          (try_begin),
            (troop_slot_eq, ":npc", slot_troop_2ary_morality_state, tms_acknowledged),
            # npc is betrayed, major penalty to player honor and morale
            (troop_get_slot, ":grievance", ":npc", slot_troop_morality_penalties),
            (val_mul, ":value", 2),
            (val_add, ":grievance", ":value"),
            (troop_set_slot, ":npc", slot_troop_morality_penalties, ":grievance"),
          (else_try),
            (this_or_next|troop_slot_eq, ":npc", slot_troop_2ary_morality_state, tms_dismissed),
            (eq, "$disable_npc_complaints", 1),
            # npc is quietly disappointed
            (troop_get_slot, ":grievance", ":npc", slot_troop_morality_penalties),
            (val_add, ":grievance", ":value"),
            (troop_set_slot, ":npc", slot_troop_morality_penalties, ":grievance"),
          (else_try),
            # npc raises the issue for the first time
            (troop_slot_eq, ":npc", slot_troop_2ary_morality_state, tms_no_problem),
            (gt, ":value", ":grievance_minimum"),
            (assign, "$npc_with_grievance", ":npc"),
            (assign, "$npc_grievance_string", ":action_string"),
            (assign, "$npc_grievance_slot", slot_troop_2ary_morality_state),
            (assign, ":grievance_minimum", ":value"),
            (assign, "$npc_praise_not_complaint", 0),
            (try_begin),
              (lt, ":value", 0),
              (assign, "$npc_praise_not_complaint", 1),
            (try_end),
          (try_end),
        (try_end),
        
        (try_begin),
          (gt, "$npc_with_grievance", 0),
          (eq, "$npc_praise_not_complaint", 0),
          (str_store_troop_name, 4, "$npc_with_grievance"),
          (display_message, "@{s4} looks upset."),
        (try_end),
      (try_end),
  ]),
  
  
  #script_post_battle_personality_clash_check
  ("post_battle_personality_clash_check",
    [
      (try_for_range, ":npc", companions_begin, companions_end),
        (eq, "$disable_npc_complaints", 0),
        
        (main_party_has_troop, ":npc"),
        (neg|troop_is_wounded, ":npc"),
        
        (troop_get_slot, ":other_npc", ":npc", slot_troop_personalityclash2_object),
        (main_party_has_troop, ":other_npc"),
        (neg|troop_is_wounded, ":other_npc"),
        
        #                (store_random_in_range, ":random", 0, 3),
        (try_begin),
          (troop_slot_eq, ":npc", slot_troop_personalityclash2_state, 0),
          (try_begin),
            #                        (eq, ":random", 0),
            (assign, "$npc_with_personality_clash_2", ":npc"),
          (try_end),
        (try_end),
        
      (try_end),
      
      (try_for_range, ":npc", companions_begin, companions_end),
        (troop_slot_eq, ":npc", slot_troop_personalitymatch_state, 0),
        (eq, "$disable_npc_complaints", 0),
        
        (main_party_has_troop, ":npc"),
        (neg|troop_is_wounded, ":npc"),
        
        (troop_get_slot, ":other_npc", ":npc", slot_troop_personalitymatch_object),
        (main_party_has_troop, ":other_npc"),
        (neg|troop_is_wounded, ":other_npc"),
        (assign, "$npc_with_personality_match", ":npc"),
      (try_end),
      
      
      (try_begin),
        (gt, "$npc_with_personality_clash_2", 0),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "str_personality_clash_conversation_begins"),
        (try_end),
        
        (try_begin),
          (main_party_has_troop, "$npc_with_personality_clash_2"),
          (assign, "$npc_map_talk_context", slot_troop_personalityclash2_state),
          (start_map_conversation, "$npc_with_personality_clash_2"),
        (else_try),
          (assign, "$npc_with_personality_clash_2", 0),
        (try_end),
      (else_try),
        (gt, "$npc_with_personality_match", 0),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "str_personality_match_conversation_begins"),
        (try_end),
        
        (try_begin),
          (main_party_has_troop, "$npc_with_personality_match"),
          (assign, "$npc_map_talk_context", slot_troop_personalitymatch_state),
          (start_map_conversation, "$npc_with_personality_match"),
        (else_try),
          (assign, "$npc_with_personality_match", 0),
        (try_end),
      (try_end),
  ]),
  
  #script_event_player_defeated_enemy_party
  # INPUT: none
  # OUTPUT: none
  ("event_player_defeated_enemy_party",
    [(try_begin),
        (check_quest_active, "qst_raid_caravan_to_start_war"),
        (neg|check_quest_concluded, "qst_raid_caravan_to_start_war"),
        (party_slot_eq, "$g_enemy_party", slot_party_type, spt_kingdom_caravan),
        (store_faction_of_party, ":enemy_faction", "$g_enemy_party"),
        (quest_slot_eq, "qst_raid_caravan_to_start_war", slot_quest_target_faction, ":enemy_faction"),
        (quest_get_slot, ":cur_state", "qst_raid_caravan_to_start_war", slot_quest_current_state),
        (quest_get_slot, ":quest_target_amount", "qst_raid_caravan_to_start_war", slot_quest_target_amount),
        (val_add, ":cur_state", 1),
        (quest_set_slot, "qst_raid_caravan_to_start_war", slot_quest_current_state, ":cur_state"),
        (try_begin),
          (ge, ":cur_state", ":quest_target_amount"),
          (quest_get_slot, ":quest_target_faction", "qst_raid_caravan_to_start_war", slot_quest_target_faction),
          (quest_get_slot, ":quest_giver_troop", "qst_raid_caravan_to_start_war", slot_quest_giver_troop),
          (store_troop_faction, ":quest_giver_faction", ":quest_giver_troop"),
          (call_script, "script_diplomacy_start_war_between_kingdoms", ":quest_target_faction", ":quest_giver_faction", logent_faction_declares_war_to_respond_to_provocation),	#MOTO chief pass log entries
          (call_script, "script_succeed_quest", "qst_raid_caravan_to_start_war"),
        (try_end),
      (try_end),
      
  ]),
  
  #script_event_player_captured_as_prisoner
  # INPUT: none
  # OUTPUT: none
  ("event_player_captured_as_prisoner",
    [
      (try_begin),
        (check_quest_active, "qst_raid_caravan_to_start_war"),
        (neg|check_quest_concluded, "qst_raid_caravan_to_start_war"),
        (quest_get_slot, ":quest_target_faction", "qst_raid_caravan_to_start_war", slot_quest_target_faction),
        (store_faction_of_party, ":capturer_faction", "$capturer_party"),
        (eq, ":quest_target_faction", ":capturer_faction"),
        (call_script, "script_fail_quest", "qst_raid_caravan_to_start_war"),
      (try_end),
      (call_script, "script_init_ai_calculation"),
      #Removing followers of the player
      (try_for_range, ":troop_no", active_npcs_begin, active_npcs_end),
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
        (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
        (gt, ":party_no", 0),
        (party_is_active, ":party_no"),
        (party_slot_eq, ":party_no", slot_party_ai_state, spai_accompanying_army),
        (party_slot_eq, ":party_no", slot_party_ai_object, "p_main_party"),
        (call_script, "script_party_set_ai_state", ":party_no", spai_undefined, -1),
        (call_script, "script_calculate_troop_ai", ":troop_no"),
      (try_end),
  ]),
  
  #NPC morale both returns a string and reg0 as the morale value
  ("npc_morale",
    [
      (store_script_param_1, ":npc"),
      
      (troop_get_slot, ":morality_grievances", ":npc", slot_troop_morality_penalties),
      (troop_get_slot, ":personality_grievances", ":npc", slot_troop_personalityclash_penalties),
      (party_get_morale, ":party_morale", "p_main_party"),
      
      (store_sub, ":troop_morale", ":party_morale", ":morality_grievances"),
      (val_sub, ":troop_morale", ":personality_grievances"),
      (val_add, ":troop_morale", 50),
      
      (assign, reg8, ":troop_morale"),
      
      (val_mul, ":troop_morale", 3),
      (val_div, ":troop_morale", 4),
      (val_clamp, ":troop_morale", 0, 100),
      
      (assign, reg5, ":party_morale"),
      (assign, reg6, ":morality_grievances"),
      (assign, reg7, ":personality_grievances"),
      (assign, reg9, ":troop_morale"),
      
      #        (str_store_troop_name, s11, ":npc"),
      #        (display_message, "@{!}{s11}'s morale = PM{reg5} + 50 - MG{reg6} - PG{reg7} = {reg8} x 0.75 = {reg9}"),
      
      (try_begin),
        (lt, ":morality_grievances", 3),
        (str_store_string, 7, "str_happy"),
      (else_try),
        (lt, ":morality_grievances", 15),
        (str_store_string, 7, "str_content"),
      (else_try),
        (lt, ":morality_grievances", 30),
        (str_store_string, 7, "str_concerned"),
      (else_try),
        (lt, ":morality_grievances", 45),
        (str_store_string, 7, "str_not_happy"),
      (else_try),
        (str_store_string, 7, "str_miserable"),
      (try_end),
      
      
      (try_begin),
        (lt, ":personality_grievances", 3),
        (str_store_string, 6, "str_happy"),
      (else_try),
        (lt, ":personality_grievances", 15),
        (str_store_string, 6, "str_content"),
      (else_try),
        (lt, ":personality_grievances", 30),
        (str_store_string, 6, "str_concerned"),
      (else_try),
        (lt, ":personality_grievances", 45),
        (str_store_string, 6, "str_not_happy"),
      (else_try),
        (str_store_string, 6, "str_miserable"),
      (try_end),
      
      
      (try_begin),
        (gt, ":troop_morale", 80),
        (str_store_string, 8, "str_happy"),
        (str_store_string, 63, "str_bar_enthusiastic"),
      (else_try),
        (gt, ":troop_morale", 60),
        (str_store_string, 8, "str_content"),
        (str_store_string, 63, "str_bar_content"),
      (else_try),
        (gt, ":troop_morale", 40),
        (str_store_string, 8, "str_concerned"),
        (str_store_string, 63, "str_bar_weary"),
      (else_try),
        (gt, ":troop_morale", 20),
        (str_store_string, 8, "str_not_happy"),
        (str_store_string, 63, "str_bar_disgruntled"),
      (else_try),
        (str_store_string, 8, "str_miserable"),
        (str_store_string, 63, "str_bar_miserable"),
      (try_end),
      
      
      (str_store_string, 21, "str_npc_morale_report"),
      (assign, reg0, ":troop_morale"),
      
  ]),
  #NPC morale both returns a string and reg0 as the morale value
  
  
  #
  ("retire_companion",
    [
      (store_script_param_1, ":npc"),
      (store_script_param_2, ":length"),
      
      (remove_member_from_party, ":npc", "p_main_party"),
      (troop_set_slot, ":npc", slot_troop_personalityclash_penalties, 0),
      (troop_set_slot, ":npc", slot_troop_morality_penalties, 0),
      (troop_get_slot, ":renown", "trp_player", slot_troop_renown),
      (store_add, ":return_renown", ":renown", ":length"),
      (troop_set_slot, ":npc", slot_troop_occupation, slto_retirement),
      (troop_set_slot, ":npc", slot_troop_return_renown, ":return_renown"),
  ]),
  
  #NPC companion changes end
  
  #script_reduce_companion_morale_for_clash
  #script_calculate_ransom_amount_for_troop
  # INPUT: arg1 = troop_no for companion1 arg2 = troop_no for companion2 arg3 = slot_for_clash_state
  # slot_for_clash_state means: 1=give full penalty to companion1; 2=give full penalty to companion2; 3=give penalty equally
  ("reduce_companion_morale_for_clash",
    [
      (store_script_param, ":companion_1", 1),
      (store_script_param, ":companion_2", 2),
      (store_script_param, ":slot_for_clash_state", 3),
      
      (troop_get_slot, ":clash_state", ":companion_1", ":slot_for_clash_state"),
      (troop_get_slot, ":grievance_1", ":companion_1", slot_troop_personalityclash_penalties),
      (troop_get_slot, ":grievance_2", ":companion_2", slot_troop_personalityclash_penalties),
      (try_begin), #chief, companions only lose morale in sandbox mode
        (neq, "$campaign_type", camp_storyline), #chief only in sandbox
        (try_begin),
          (eq, ":clash_state", pclash_penalty_to_self),
          (val_add, ":grievance_1", 5),
        (else_try),
          (eq, ":clash_state", pclash_penalty_to_other),
          (val_add, ":grievance_2", 5),
        (else_try),
          (eq, ":clash_state", pclash_penalty_to_both),
          (val_add, ":grievance_1", 3),
          (val_add, ":grievance_2", 3),
        (try_end),
      (else_try),
        (val_add, ":grievance_1", 0),
        (val_add, ":grievance_2", 0),
      (try_end),
      (troop_set_slot, ":companion_1", slot_troop_personalityclash_penalties, ":grievance_1"),
      (troop_set_slot, ":companion_2", slot_troop_personalityclash_penalties, ":grievance_2"),
  ]),
  
  #Hunting scripts end
  
  #script_calculate_ransom_amount_for_troop
  # INPUT: arg1 = troop_no
  # OUTPUT: reg0 = ransom_amount
  ("calculate_ransom_amount_for_troop",
    [(store_script_param, ":troop_no", 1),
      #    (store_troop_faction, ":faction_no", ":troop_no"),
      #    (assign, ":ransom_amount", 400),
      # JuJu70 - ransom based on wealth
      (try_begin),
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
        (troop_get_slot, ":wealth", ":troop_no", slot_troop_wealth),
        (try_begin),
          (gt, ":wealth", 10),
          (store_div, ":ransom_amount", ":wealth", 10),
        (else_try),
          (assign, ":ransom_amount", 500),
        (try_end),
      (else_try),
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_lady),
        (assign, ":ransom_amount", 400),
      (try_end),
      (assign, ":male_relative", -9), #for kingdom ladies, otherwise a number otherwise unused in slot_town_lord
      (try_begin),
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_lady),
        (call_script, "script_get_kingdom_lady_social_determinants", ":troop_no"),
        (assign, ":male_relative", reg0),
        (try_begin),
          (le, ":male_relative", -1),
          (val_add, ":ransom_amount", 2500),
        (else_try),
          (troop_get_slot, ":wealthr", ":male_relative", slot_troop_wealth),
          (store_div, ":ransom_amountr", ":wealthr", 10),
          (val_add, ":ransom_amount", ":ransom_amountr"),
        (try_end),
      (try_end),
      (assign, ":num_center_points", 0),
      (try_for_range, ":cur_center", centers_begin, centers_end),
        (this_or_next|party_slot_eq, ":cur_center", slot_town_lord, ":troop_no"),
        (party_slot_eq, ":cur_center", slot_town_lord, ":male_relative"),
        (try_begin),
          (party_slot_eq, ":cur_center", slot_party_type, spt_town),
          (val_add, ":num_center_points", 4),
        (else_try),
          (party_slot_eq, ":cur_center", slot_party_type, spt_castle),
          (val_add, ":num_center_points", 2),
        (else_try),
          (val_add, ":num_center_points", 1),
        (try_end),
      (try_end),
      (val_mul, ":num_center_points", 500),
      (val_add, ":ransom_amount", ":num_center_points"),
      (troop_get_slot, ":renown", ":troop_no", slot_troop_renown),
      (val_mul, ":renown", 2),
      (val_add, ":ransom_amount", ":renown"),
      (store_mul, ":ransom_max_amount", ":ransom_amount", 3),
      (val_div, ":ransom_max_amount", 2),
      (store_random_in_range, ":random_ransom_amount", ":ransom_amount", ":ransom_max_amount"),
      (val_div, ":random_ransom_amount", 100),
      (val_mul, ":random_ransom_amount", 100),
      (try_begin),
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
        (troop_get_slot, ":wealth", ":troop_no", slot_troop_wealth),
        (val_div, ":wealth", 2),
        (try_begin),
          (le, ":random_ransom_amount", ":wealth"),
        (else_try),
          (gt, ":wealth", 200),
          (gt, ":random_ransom_amount", ":wealth"),
          (assign, ":random_ransom_amount", ":wealth"),
          (val_div, ":random_ransom_amount", 100),
          (val_mul, ":random_ransom_amount", 100),
        (else_try),
          (le, ":wealth", 200),
          (assign, ":random_ransom_amount", 200),
        (try_end),
      (else_try),
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_lady),
        (try_begin),
          (gt, ":male_relative", -1),
          (troop_get_slot, ":wealth", ":male_relative", slot_troop_wealth),
          (val_div, ":wealth", 2),
          (try_begin),
            (le, ":random_ransom_amount", ":wealth"),
          (else_try),
            (gt, ":wealth", 200),
            (gt, ":random_ransom_amount", ":wealth"),
            (assign, ":random_ransom_amount", ":wealth"),
            (val_div, ":random_ransom_amount", 100),
            (val_mul, ":random_ransom_amount", 100),
          (else_try),
            (le, ":wealth", 200),
            (assign, ":random_ransom_amount", 200),
          (try_end),
        (else_try),
          (store_faction_of_troop, ":faction", ":troop_no"),
          (faction_get_slot, ":king", ":faction", slot_faction_leader),
          (assign, ":male_relative", ":king"),
          (troop_get_slot, ":wealth", ":king", slot_troop_wealth),
          (val_div, ":wealth", 5),
          (try_begin),
            (le, ":random_ransom_amount", ":wealth"),
          (else_try),
            (gt, ":wealth", 200),
            (gt, ":random_ransom_amount", ":wealth"),
            (assign, ":random_ransom_amount", ":wealth"),
            (val_div, ":random_ransom_amount", 100),
            (val_mul, ":random_ransom_amount", 100),
          (else_try),
            (le, ":wealth", 200),
            (assign, ":random_ransom_amount", 200),
          (try_end),
        (try_end),
      (try_end),
      (assign, reg0, ":random_ransom_amount"),
      (assign, reg1, ":male_relative"),
  ]),
  
  #script_offer_ransom_amount_to_player_for_prisoners_in_party
  # INPUT: arg1 = party_no
  # OUTPUT: reg0 = result (1 = offered, 0 = not offered)
  ("offer_ransom_amount_to_player_for_prisoners_in_party",
    [(store_script_param, ":party_no", 1),
      (assign, ":result", 0),
      (party_get_num_prisoner_stacks, ":num_stacks", ":party_no"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (eq, ":result", 0),
        (party_prisoner_stack_get_troop_id, ":stack_troop", ":party_no", ":i_stack"),
        (troop_is_hero, ":stack_troop"),
        (this_or_next|troop_slot_eq, ":stack_troop", slot_troop_occupation, slto_kingdom_hero),
        (troop_slot_eq, ":stack_troop", slot_troop_occupation, slto_kingdom_lady),
        (store_troop_faction, ":stack_troop_faction", ":stack_troop"),
        (store_random_in_range, ":random_no", 0, 100),
        (try_begin),
          (faction_slot_eq, ":stack_troop_faction", slot_faction_state, sfs_active),
          (le, ":random_no", 5),
          (neq, "$g_ransom_offer_rejected", 1),
          (assign, ":num_stacks", 0), #break
          (assign, ":result", 1),
          (assign, "$g_ransom_offer_troop", ":stack_troop"),
          (assign, "$g_ransom_offer_party", ":party_no"),
          (jump_to_menu, "mnu_enemy_offer_ransom_for_prisoner"),
        (try_end),
      (try_end),
      (assign, reg0, ":result"),
  ]),
  
  # script_event_hero_taken_prisoner_by_player
  # Input: arg1 = troop_no
  # Output: none
  ("event_hero_taken_prisoner_by_player",
    [
      (store_script_param_1, ":troop_no"),
      (try_begin),
        (check_quest_active, "qst_persuade_lords_to_make_peace"),
        (try_begin),
          (quest_slot_eq, "qst_persuade_lords_to_make_peace", slot_quest_target_troop, ":troop_no"),
          (val_mul, ":troop_no", -1),
          (quest_set_slot, "qst_persuade_lords_to_make_peace", slot_quest_target_troop, ":troop_no"),
          (val_mul, ":troop_no", -1),
        (else_try),
          (quest_slot_eq, "qst_persuade_lords_to_make_peace", slot_quest_object_troop, ":troop_no"),
          (val_mul, ":troop_no", -1),
          (quest_set_slot, "qst_persuade_lords_to_make_peace", slot_quest_object_troop, ":troop_no"),
          (val_mul, ":troop_no", -1),
        (try_end),
        (neg|check_quest_concluded, "qst_persuade_lords_to_make_peace"),
        (neg|quest_slot_ge, "qst_persuade_lords_to_make_peace", slot_quest_target_troop, 0),
        (neg|quest_slot_ge, "qst_persuade_lords_to_make_peace", slot_quest_object_troop, 0),
        (call_script, "script_succeed_quest", "qst_persuade_lords_to_make_peace"),
      (try_end),
      (call_script, "script_update_troop_location_notes", ":troop_no", 0),
  ]),
  
  # script_cf_check_hero_can_escape_from_player
  # Input: arg1 = troop_no
  # Output: none (can fail)
  ("cf_check_hero_can_escape_from_player",
    [
      (store_script_param_1, ":troop_no"),
      (assign, ":quest_target", 0),
      (try_begin),
        (check_quest_active, "qst_persuade_lords_to_make_peace"),
        (this_or_next|quest_slot_eq, "qst_persuade_lords_to_make_peace", slot_quest_target_troop, ":troop_no"),
        (quest_slot_eq, "qst_persuade_lords_to_make_peace", slot_quest_object_troop, ":troop_no"),
        (assign, ":quest_target", 1),
      (else_try),
        (is_between, ":troop_no", "trp_sea_raider_leader", "trp_bandit_leaders_end"),
        (try_begin),
          (check_quest_active, "qst_learn_where_merchant_brother_is"),
          (assign, ":quest_target", 1), #always catched
        (else_try),
          (assign, ":quest_target", -1), #always run.
        (try_end),
      (else_try),
        (is_between, ":troop_no", companions_begin, companions_end),
        (assign, ":quest_target", -1), #always run.
      (try_end),
      
      (assign, ":continue", 0),
      (try_begin),
        (eq, ":quest_target", 0), #if not quest target
        (store_random_in_range, ":rand", 0, 100),
        (try_begin),
          (eq, "$special1", 1),
          (val_add, ":rand", 7),
        (try_end),
        (lt, ":rand", hero_escape_after_defeat_chance),
        (assign, ":continue", 1),
      (else_try),
        (eq, ":quest_target", -1), #if (always run) quest target
        (assign, ":continue", 1),
      (try_end),
      
      (eq, ":continue", 1),
  ]),
  
  # script_cf_party_remove_random_regular_troop
  # Input: arg1 = party_no
  # Output: troop_id that has been removed (can fail)
  ("cf_party_remove_random_regular_troop",
    [(store_script_param_1, ":party_no"),
      (store_party_size_wo_prisoners, ":party_size", ":party_no"),
      (assign, reg0, -1),
      (val_div, ":party_size", 2),
      (val_min, ":party_size", 50),
      (try_begin),
        (eq, ":party_size", 0),
        (val_add, ":party_size", 1),
      (try_end),
      (store_random_in_range, ":random_troop", 0, ":party_size"),
      (party_get_num_companion_stacks, ":num_stacks", ":party_no"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop", ":party_no", ":i_stack"),
        (neg|troop_is_hero, ":stack_troop"),
        (party_stack_get_size, ":stack_size", ":party_no", ":i_stack"),
        (val_sub, ":random_troop", ":stack_size"),
        (lt, ":random_troop", 7),
        (assign, ":num_stacks", 0),
        (party_remove_members, ":party_no", ":stack_troop", 1),
        (assign, reg0, ":stack_troop"),
      (try_end),
  ]),
  
  # script_place_player_banner_near_inventory
  # Input: none
  # Output: none
  ("place_player_banner_near_inventory",
    [
      #normal_banner_begin
      (troop_get_slot, ":troop_banner_object", "trp_player", slot_troop_banner_scene_prop),
      #custom_banner_begin
      #    	(troop_get_slot, ":flag_spr", "trp_player", slot_troop_custom_banner_flag_type),
      
      (try_begin),
        #normal_banner_begin
        (gt, ":troop_banner_object", 0),
        (scene_prop_get_instance, ":flag_object", ":troop_banner_object", 0),
        #custom_banner_begin
        #       (ge, ":flag_spr", 0),
        #       (val_add, ":flag_spr", custom_banner_flag_scene_props_begin),
        #       (scene_prop_get_instance, ":flag_object", ":flag_spr", 0),
        (try_begin),
          (ge, ":flag_object", 0),
          (get_player_agent_no, ":player_agent"),
          (agent_get_look_position, pos1, ":player_agent"),
          (position_move_y, pos1, -500),
          (position_rotate_z, pos1, 180),
          (position_set_z_to_ground_level, pos1),
          (position_move_z, pos1, 300),
          (prop_instance_set_position, ":flag_object", pos1),
        (try_end),
        (scene_prop_get_instance, ":pole_object", "spr_banner_pole", 0),
        (try_begin),
          (ge, ":pole_object", 0),
          (position_move_z, pos1, -320),
          (prop_instance_set_position, ":pole_object", pos1),
        (try_end),
      (else_try),
        (init_position, pos1),
        (position_move_z, pos1, -1000000),
        (scene_prop_get_instance, ":flag_object", banner_scene_props_begin, 0),
        (try_begin),
          (ge, ":flag_object", 0),
          (prop_instance_set_position, ":flag_object", pos1),
        (try_end),
        (scene_prop_get_instance, ":pole_object", "spr_banner_pole", 0),
        (try_begin),
          (ge, ":pole_object", 0),
          (prop_instance_set_position, ":pole_object", pos1),
        (try_end),
      (try_end),
  ]),
  
  # script_place_player_banner_near_inventory_bms
  # Input: none
  # Output: none
  ("place_player_banner_near_inventory_bms",
    [
      #normal_banner_begin
      (troop_get_slot, ":troop_banner_object", "trp_player", slot_troop_banner_scene_prop),
      #custom_banner_begin
      #      (troop_get_slot, ":flag_spr", "trp_player", slot_troop_custom_banner_flag_type),
      (try_begin),
        #normal_banner_begin
        (gt, ":troop_banner_object", 0),
        (replace_scene_props, banner_scene_props_begin, ":troop_banner_object"),
        (replace_scene_props, "spr_banner_a", ":troop_banner_object"),#fix for old scenes
        #custom_banner_begin
        #       (ge, ":flag_spr", 0),
        #       (val_add, ":flag_spr", custom_banner_flag_scene_props_begin),
        #       (replace_scene_props, banner_scene_props_begin, ":flag_spr"),
      (try_end),
  ]),
  
  # script_stay_captive_for_hours
  # Input: arg1 = num_hours
  # Output: none
  ("stay_captive_for_hours",
    [
      (store_script_param, ":num_hours", 1),
      (store_current_hours, ":cur_hours"),
      (val_add, ":cur_hours", ":num_hours"),
      (val_max, "$g_check_autos_at_hour", ":cur_hours"),
      (val_add, ":num_hours", 1),
      (rest_for_hours, ":num_hours", 0, 0),
  ]),
  
  # script_set_parties_around_player_ignore_player
  # Input: arg1 = ignore_range, arg2 = num_hours_to_ignore
  # Output: none
  ("set_parties_around_player_ignore_player",
    [(store_script_param, ":ignore_range", 1),
      (store_script_param, ":num_hours", 2),
      (try_for_parties, ":party_no"),
        (party_is_active, ":party_no"),
        (store_distance_to_party_from_party, ":dist", "p_main_party", ":party_no"),
        (lt, ":dist", ":ignore_range"),
        (party_ignore_player, ":party_no", ":num_hours"),
      (try_end),
  ]),
  
  # script_randomly_make_prisoner_heroes_escape_from_party
  # Input: arg1 = party_no, arg2 = escape_chance_mul_1000
  # Output: none
  ("randomly_make_prisoner_heroes_escape_from_party",
    [(store_script_param, ":party_no", 1),
      (store_script_param, ":escape_chance", 2),
      (assign, ":quest_troop_1", -1),
      (assign, ":quest_troop_2", -1),
      ##### player lair (player refuge)
      ##     (try_begin),
      ##	  (eq, ":party_no", "p_yourlair"),
      ##          (val_add, ":escape_chance", 990),
      ##     (try_end),
      ######
      (try_begin),
        (check_quest_active, "qst_rescue_lord_by_replace"),
        (quest_get_slot, ":quest_troop_1", "qst_rescue_lord_by_replace", slot_quest_target_troop),
      (try_end),
      (try_begin),
        (check_quest_active, "qst_deliver_message_to_prisoner_lord"),
        (quest_get_slot, ":quest_troop_2", "qst_deliver_message_to_prisoner_lord", slot_quest_target_troop),
      (try_end),
      
      (party_get_num_prisoner_stacks, ":num_stacks", ":party_no"),
      (try_for_range_backwards, ":i_stack", 0, ":num_stacks"),
        (party_prisoner_stack_get_troop_id, ":stack_troop", ":party_no", ":i_stack"),
        (troop_is_hero, ":stack_troop"),
        ###storyline no prisoners some lords
        (try_begin),
          (eq, "$campaign_type", camp_storyline),
          (this_or_next|eq, ":stack_troop", "trp_kingdom_5_lord"),
          (this_or_next|eq, ":stack_troop", "trp_kingdom_8_lord"),
          (this_or_next|is_between, ":stack_troop", "trp_knight_5_1", "trp_knight_5_6"),
          (this_or_next|eq, ":stack_troop", "trp_knight_8_4"),
          (this_or_next|eq, ":stack_troop", "trp_knight_8_7"),
          (is_between, ":stack_troop", "trp_knight_8_11", "trp_knight_9_1"),	#(knight_8_15	#trp_kingdom_8
          (assign, ":always", 1),
        (else_try),
          (assign, ":always", 0),
        (try_end),
        ######
        (neq, ":stack_troop", ":quest_troop_1"),
        (neq, ":stack_troop", ":quest_troop_2"),
        (troop_slot_eq, ":stack_troop", slot_troop_occupation, slto_kingdom_hero),
        (store_random_in_range, ":random_no", 0, 1000),
        (this_or_next|eq, ":always", 1),
        (lt, ":random_no", ":escape_chance"),
        (party_remove_prisoners, ":party_no", ":stack_troop", 1),
        (call_script, "script_remove_troop_from_prison", ":stack_troop"),
        (str_store_troop_name_link, s1, ":stack_troop"),
        (try_begin),
          (eq, ":party_no", "p_main_party"),
          (str_store_string, s2, "@your party"),
        (else_try),
          (str_store_party_name, s2, ":party_no"),
        (try_end),
        (assign, reg0, 0),
        (try_begin),
          (this_or_next|eq, ":party_no", "p_main_party"),
          (party_slot_eq, ":party_no", slot_town_lord, "trp_player"),
          (assign, reg0, 1),
        (try_end),
        (store_troop_faction, ":troop_faction", ":stack_troop"),
        (str_store_faction_name_link, s3, ":troop_faction"),
        (display_message, "@{reg0?One of your prisoners, :}{s1} of the {s3} has escaped from captivity!", color_terrible_news),
      (try_end),
  ]),
  
  
  # script_fill_tournament_participants_troop
  # Input: arg1 = center_no, arg2 = player_at_center
  # Output: none (fills trp_tournament_participants)
  ("fill_tournament_participants_troop",
    [(store_script_param, ":center_no", 1),
      (store_script_param, ":player_at_center", 2),
      (assign, ":cur_slot", 0),
      
      (try_begin),
        (eq, ":player_at_center", 1),
        (party_get_num_companion_stacks, ":num_stacks", "p_main_party"),
        (try_for_range, ":stack_no", 0, ":num_stacks"),
          (party_stack_get_troop_id, ":cur_troop", "p_main_party", ":stack_no"),
          (troop_is_hero, ":cur_troop"),
          (neq, ":cur_troop", "trp_kidnapped_girl"),
          (troop_set_slot, "trp_tournament_participants", ":cur_slot", ":cur_troop"),
          (val_add, ":cur_slot", 1),
        (try_end),
      (try_end),
      
      (party_collect_attachments_to_party, ":center_no", "p_temp_party"),
      (party_get_num_companion_stacks, ":num_stacks", "p_temp_party"),
      (try_for_range, ":stack_no", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":cur_troop", "p_temp_party", ":stack_no"),
        (troop_is_hero, ":cur_troop"),
        (troop_set_slot, "trp_tournament_participants", ":cur_slot", ":cur_troop"),
        (val_add, ":cur_slot", 1),
      (try_end),
      
      (try_begin),
        (store_random_in_range, ":random_no", 0, 100),
        (lt, ":random_no", 50),
        (troop_set_slot, "trp_tournament_participants", ":cur_slot", "trp_xerina"),
        (val_add, ":cur_slot", 1),
      (try_end),
      (try_begin),
        (store_random_in_range, ":random_no", 0, 100),
        (lt, ":random_no", 50),
        (troop_set_slot, "trp_tournament_participants", ":cur_slot", "trp_dranton"),
        (val_add, ":cur_slot", 1),
      (try_end),
      (try_begin),
        (store_random_in_range, ":random_no", 0, 100),
        (lt, ":random_no", 50),
        (troop_set_slot, "trp_tournament_participants", ":cur_slot", "trp_kradus"),
        (val_add, ":cur_slot", 1),
      (try_end),
      
      (assign, ":begin_slot", ":cur_slot"),
      (try_for_range, ":cur_slot", ":begin_slot", 64),
        (store_random_in_range, ":random_no", 0, 6),
        (try_begin),
          (eq, ":random_no", 0),
          (troop_set_slot, "trp_tournament_participants", ":cur_slot", "trp_regular_fighter"),
        (else_try),
          (eq, ":random_no", 1),
          (troop_set_slot, "trp_tournament_participants", ":cur_slot", "trp_veteran_fighter"),
        (else_try),
          (eq, ":random_no", 2),
          (troop_set_slot, "trp_tournament_participants", ":cur_slot", "trp_champion_fighter"),
        (else_try),
          (eq, ":random_no", 3),
          (troop_set_slot, "trp_tournament_participants", ":cur_slot", "trp_sword_sister"),
        (else_try),
          (eq, ":random_no", 4),
          (troop_set_slot, "trp_tournament_participants", ":cur_slot", "trp_hired_blade"),
        (else_try),
          (troop_set_slot, "trp_tournament_participants", ":cur_slot", "trp_mercenary_swordsman"),
        (try_end),
      (try_end),
  ]),
  
  # script_get_num_tournament_participants
  # Input: none
  # Output: reg0 = num_participants
  ("get_num_tournament_participants",
    [(assign, ":num_participants", 0),
      (try_for_range, ":cur_slot", 0, 64),
        (troop_slot_ge, "trp_tournament_participants", ":cur_slot", 0),
        (val_add, ":num_participants", 1),
      (try_end),
      (assign, reg0, ":num_participants"),
  ]),
  
  # script_get_random_tournament_participant
  # Input: none
  # Output: reg0 = troop_no
  ("get_random_tournament_participant",
    [(call_script, "script_get_num_tournament_participants"),
      (assign, ":num_participants", reg0),
      (store_random_in_range, ":random_troop", 0, ":num_participants"),
      (assign, ":continue", 1),
      (try_for_range, ":cur_slot", 0, 64),
        (eq, ":continue", 1),
        (troop_slot_ge, "trp_tournament_participants", ":cur_slot", 0),
        (val_sub, ":random_troop", 1),
        (lt, ":random_troop", 0),
        (assign, ":continue", 0),
        (troop_get_slot, ":troop_no", "trp_tournament_participants", ":cur_slot"),
        (troop_set_slot, "trp_tournament_participants", ":cur_slot", -1),
      (try_end),
      (assign, reg0, ":troop_no"),
  ]),
  
  # script_add_tournament_participant
  # Input: arg1 = troop_no
  # Output: none
  ("add_tournament_participant",
    [(store_script_param, ":troop_no", 1),
      (assign, ":continue", 1),
      (try_for_range, ":cur_slot", 0, 64),
        (eq, ":continue", 1),
        (troop_slot_eq, "trp_tournament_participants", ":cur_slot", -1),
        (troop_set_slot, "trp_tournament_participants", ":cur_slot", ":troop_no"),
        (assign, ":continue", 0),
      (try_end),
  ]),
  
  # script_get_random_tournament_team_amount_and_size
  # Input: none
  # Output: reg0 = number_of_teams, reg1 = team_size
  ("get_random_tournament_team_amount_and_size",
    [
      (call_script, "script_get_num_tournament_participants"),
      (assign, ":num_participants", reg0),
      (party_get_slot, ":town_max_teams", "$current_town", slot_town_tournament_max_teams),
      (val_add, ":town_max_teams", 1),
      (party_get_slot, ":town_max_team_size", "$current_town", slot_town_tournament_max_team_size),
      (val_add, ":town_max_team_size", 1),
      (assign, ":max_teams", ":num_participants"),
      (val_min, ":max_teams", ":town_max_teams"),
      (assign, ":max_size", ":num_participants"),
      (val_min, ":max_size", ":town_max_team_size"),
      (assign, ":min_size", 1),
      (try_begin),
        (ge, ":num_participants", 32),
        (assign, ":min_size", 2),
        (val_min, ":min_size", ":town_max_team_size"),
      (try_end),
      (assign, ":end_cond", 500),
      (try_for_range, ":unused", 0, ":end_cond"),
        (store_random_in_range, ":random_teams", 2, ":max_teams"),
        (store_random_in_range, ":random_size", ":min_size", ":max_size"),
        (store_mul, ":total_men", ":random_teams", ":random_size"),
        (le, ":total_men", ":num_participants"),
        (store_sub, ":left_men", ":num_participants", ":total_men"),
        (neq, ":left_men", 1),
        (assign, ":end_cond", 0),
      (try_end),
      (try_begin),
        (gt, ":end_cond", 0),
        (assign, ":random_teams", 2),
        (assign, ":random_size", 1),
      (try_end),
      (assign, reg0, ":random_teams"),
      (assign, reg1, ":random_size"),
  ]),
  
  # script_get_troop_priority_point_for_tournament
  # Input: arg1 = troop_no
  # Output: reg0 = troop_point
  ("get_troop_priority_point_for_tournament",
    [(store_script_param, ":troop_no", 1),
      (assign, ":troop_point", 0),
      (try_begin),
        (ge, ":troop_no", 0),
        (val_add, ":troop_point", 40000),
        (try_begin),
          (eq, ":troop_no", "trp_player"),
          (val_add, ":troop_point", 80000),
        (try_end),
        (try_begin),
          (troop_is_hero, ":troop_no"),
          (val_add, ":troop_point", 20000),
        (try_end),
        (try_begin),
          (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_player_companion),
          (val_add, ":troop_point", 10000),
        (else_try),
          (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
          (troop_get_slot, ":renown", ":troop_no", slot_troop_renown),
          (val_add, ":troop_point", ":renown"),
          (val_add, ":troop_point", 1000), #in order to make it more prior than tournament heroes with higher levels
        (else_try),
          (store_character_level, ":level", ":troop_no"),
          (val_add, ":troop_point", ":level"),
        (try_end),
      (try_end),
      (assign, reg0, ":troop_point"),
  ]),
  
  # script_sort_tournament_participant_troops
  # Input: none
  # Output: none (sorts trp_tournament_participants)
  ("sort_tournament_participant_troops",
    [(try_for_range, ":cur_slot", 0, 63),
        (store_add, ":cur_slot_2_begin", ":cur_slot", 1),
        (try_for_range, ":cur_slot_2", ":cur_slot_2_begin", 64),
          (troop_get_slot, ":troop_1", "trp_tournament_participants", ":cur_slot"),
          (troop_get_slot, ":troop_2", "trp_tournament_participants", ":cur_slot_2"),
          (call_script, "script_get_troop_priority_point_for_tournament", ":troop_1"),
          (assign, ":troop_1_point", reg0),
          (call_script, "script_get_troop_priority_point_for_tournament", ":troop_2"),
          (assign, ":troop_2_point", reg0),
          (gt, ":troop_2_point", ":troop_1_point"),
          (troop_set_slot, "trp_tournament_participants", ":cur_slot", ":troop_2"),
          (troop_set_slot, "trp_tournament_participants", ":cur_slot_2", ":troop_1"),
        (try_end),
      (try_end),
  ]),
  
  # script_remove_tournament_participants_randomly
  # Input: arg1 = number_to_be_removed
  # Output: none
  ("remove_tournament_participants_randomly",
    [(store_script_param, ":number_to_be_removed", 1),
      (try_for_range, ":unused", 0, ":number_to_be_removed"),
        (assign, ":total_weight", 0),
        (try_for_range, ":cur_slot", 0, 64),
          (troop_get_slot, ":troop_no", "trp_tournament_participants", ":cur_slot"),
          (ge, ":troop_no", 0),
          (store_character_level, ":level", ":troop_no"),
          (val_min, ":level", 38),
          (store_sub, ":weight", 40, ":level"),
          (val_add, ":total_weight", ":weight"),
        (try_end),
        (store_random_in_range, ":random_weight", 0, ":total_weight"),
        (assign, ":continue", 1),
        (try_for_range, ":cur_slot", 0, 64),
          (eq, ":continue", 1),
          (troop_get_slot, ":troop_no", "trp_tournament_participants", ":cur_slot"),
          (ge, ":troop_no", 0),
          (store_character_level, ":level", ":troop_no"),
          (val_min, ":level", 38),
          (store_sub, ":weight", 40, ":level"),
          (val_sub, ":random_weight", ":weight"),
          (lt, ":random_weight", 0),
          (troop_set_slot, "trp_tournament_participants", ":cur_slot", -1),
          (assign, ":continue", 0),
        (try_end),
      (try_end),
  ]),
  
  # script_end_tournament_fight
  # Input: arg1 = player_team_won (1 or 0)
  # Output: none
  ("end_tournament_fight",
    [(store_script_param, ":player_team_won", 1),
      (call_script, "script_get_num_tournament_participants"),
      (assign, ":num_participants", reg0),
      (store_div, ":needed_to_remove_randomly", ":num_participants", 2),
      #Must remove other participants randomly earlier than adding the winners back to participants
      (call_script, "script_remove_tournament_participants_randomly", ":needed_to_remove_randomly"),
      
      (assign, ":num_needed", "$g_tournament_num_participants_for_fight"),
      (val_div, ":num_needed", 2),
      (get_player_agent_no, ":player_agent"),
      (agent_get_team, ":player_team", ":player_agent"),
      (try_for_agents, ":agent_no"),
        (agent_is_human, ":agent_no"),
        (agent_get_troop_id, ":troop_id", ":agent_no"),
        (neg|is_between, ":troop_id", arena_masters_begin, arena_masters_end),#omit tournament master
        (agent_get_team, ":agent_team", ":agent_no"),
        (assign, ":cur_point", 0),
        (try_begin),
          (eq, ":player_team_won", 1),
          (eq, ":agent_team", ":player_team"),
          (val_add, ":cur_point", 5000000),#Make sure that team members are chosen
        (try_end),
        (agent_get_kill_count, ":kill_count", ":agent_no", 1), #everyone is knocked unconscious
        (store_mul, ":kill_point", ":kill_count", 160000),#Make sure that kill count is the second most important variable
        (val_add, ":cur_point", ":kill_point"),
        (call_script, "script_get_troop_priority_point_for_tournament", ":troop_id"),
        (val_add, ":cur_point", reg0),
        (try_begin),#reset player's point if kill count is one after the first 2 rounds, or if it is zero
          (eq, ":agent_no", ":player_agent"),
          (eq, ":player_team_won", 0),
          (assign, ":not_passed", 1),
          (try_begin),
            (ge, ":kill_count", 2),
            (assign, ":not_passed", 0),
          (else_try),
            (eq, ":kill_count", 1),
            (le, "$g_tournament_cur_tier", 1),
            (assign, ":not_passed", 0),
          (try_end),
          (eq, ":not_passed", 1),
          (assign, ":cur_point", 0),
        (try_end),
        (agent_set_slot, ":agent_no", slot_agent_tournament_point, ":cur_point"),
      (try_end),
      (try_for_range, ":unused", 0, ":num_needed"),
        (assign, ":best_point", 0),
        (assign, ":best_agent_no", -1),
        (try_for_agents, ":agent_no"),
          (agent_is_human, ":agent_no"),
          (agent_get_slot, ":point", ":agent_no", slot_agent_tournament_point),
          (gt, ":point", ":best_point"),
          (assign, ":best_agent_no", ":agent_no"),
          (assign, ":best_point", ":point"),
        (try_end),
        (agent_set_slot, ":best_agent_no", slot_agent_tournament_point, 0),#Do not select the same agent again
        (agent_get_troop_id, ":troop_id", ":best_agent_no"),
        (call_script, "script_add_tournament_participant", ":troop_id"),
      (try_end),
      (assign, "$g_tournament_player_team_won", ":player_team_won"),
      (jump_to_menu, "mnu_town_tournament"),
  ]),
  
  
  # script_get_win_amount_for_tournament_bet
  # Input: none
  # Output: reg0 = win_amount_with_100_denars
  ("get_win_amount_for_tournament_bet",
    [
      (party_get_slot, ":player_odds", "$current_town", slot_town_player_odds),
      (try_begin),
        (eq, "$g_tournament_cur_tier", 0),
        (assign, ":win_amount", 120),
      (else_try),
        (eq, "$g_tournament_cur_tier", 1),
        (assign, ":win_amount", 90),
      (else_try),
        (eq, "$g_tournament_cur_tier", 2),
        (assign, ":win_amount", 60),
      (else_try),
        (eq, "$g_tournament_cur_tier", 3),
        (assign, ":win_amount", 40),
      (else_try),
        (eq, "$g_tournament_cur_tier", 4),
        (assign, ":win_amount", 20),
      (else_try),
        (assign, ":win_amount", 8),
      (try_end),
      (val_mul, ":win_amount", ":player_odds"),
      (val_div, ":win_amount", 100),
      (val_add, ":win_amount", 100), #win amount when 100 denars is placed
      (assign, reg0, ":win_amount"),
  ]),
  
  # script_tournament_place_bet
  # Input: arg1 = bet_amount
  # Output: none
  ("tournament_place_bet",
    [
      (store_script_param, ":bet_amount", 1),
      (call_script, "script_get_win_amount_for_tournament_bet"),
      (assign, ":win_amount", reg0),
      (val_mul, ":win_amount", ":bet_amount"),
      (val_div, ":win_amount", 100),
      (val_sub, ":win_amount", ":bet_amount"),
      (val_add, "$g_tournament_bet_placed", ":bet_amount"),
      (val_add, "$g_tournament_bet_win_amount", ":win_amount"),
      (troop_remove_gold, "trp_player", ":bet_amount"),
      (assign, "$g_tournament_last_bet_tier", "$g_tournament_cur_tier"),
  ]),
  
  # script_calculate_amount_of_cattle_can_be_stolen
  # Input: arg1 = village_no
  # Output: reg0 = max_amount
  ("calculate_amount_of_cattle_can_be_stolen",
    [
      (store_script_param, ":village_no", 1),
      (call_script, "script_get_max_skill_of_player_party", "skl_looting"),
      (assign, ":max_skill", reg0),
      (store_mul, ":can_steal", ":max_skill", 2),
      (call_script, "script_party_count_fit_for_battle", "p_main_party"),
      (store_add, ":num_men_effect", reg0, 10),
      (val_div, ":num_men_effect", 10),
      (val_add, ":can_steal", ":num_men_effect"),
      (party_get_slot, ":num_cattle", ":village_no", slot_village_number_of_cattle),
      (val_min, ":can_steal", ":num_cattle"),
      (assign, reg0, ":can_steal"),
  ]),
  
  
  # # script_draw_banner_to_region
  # # Input: arg1 = troop_no, arg2 = center_pos_x, arg3 = center_pos_y, arg4 = width, arg5 = height, arg6 = stretch_width, arg7 = stretch_height, arg8 = default_scale, arg9 = max_charge_scale, arg10 = drawn_item_type
  # # drawn_item_type is 0 for banners, 1 for shields, 2 for heater shield, 3 for armor
  # # arguments will be used as fixed point values
  # # Output: none
  # ("draw_banner_to_region",
  # [
  # (store_script_param, ":troop_no", 1),
  # (store_script_param, ":center_pos_x", 2),
  # (store_script_param, ":center_pos_y", 3),
  # (store_script_param, ":width", 4),
  # (store_script_param, ":height", 5),
  # (store_script_param, ":stretch_width", 6),
  # (store_script_param, ":stretch_height", 7),
  # (store_script_param, ":default_scale", 8),
  # (store_script_param, ":max_charge_scale", 9),
  # (store_script_param, ":drawn_item_type", 10),
  
  # (troop_get_slot, ":bg_type", ":troop_no", slot_troop_custom_banner_bg_type),
  # (val_add, ":bg_type", custom_banner_backgrounds_begin),
  # (troop_get_slot, ":bg_color_1", ":troop_no", slot_troop_custom_banner_bg_color_1),
  # (troop_get_slot, ":bg_color_2", ":troop_no", slot_troop_custom_banner_bg_color_2),
  # (troop_get_slot, ":num_charges", ":troop_no", slot_troop_custom_banner_num_charges),
  # (troop_get_slot, ":positioning", ":troop_no", slot_troop_custom_banner_positioning),
  # (call_script, "script_get_troop_custom_banner_num_positionings", ":troop_no"),
  # (assign, ":num_positionings", reg0),
  # (val_mod, ":positioning", ":num_positionings"),
  
  # (init_position, pos2),
  # (position_set_x, pos2, ":width"),
  # (position_set_y, pos2, ":height"),
  # (assign, ":default_value", 1),
  # (convert_to_fixed_point, ":default_value"),
  # (position_set_z, pos2, ":default_value"),
  
  # (init_position, pos1),
  # (position_set_x, pos1, ":center_pos_x"),
  # (position_set_y, pos1, ":center_pos_y"),
  # (position_move_z, pos1, -20),
  
  # (init_position, pos3),
  # (position_set_x, pos3, ":default_scale"),
  # (position_set_y, pos3, ":default_scale"),
  # (position_set_z, pos3, ":default_value"),
  
  # (try_begin),
  # (this_or_next|eq, ":bg_type", "mesh_custom_banner_bg"),
  # (this_or_next|eq, ":bg_type", "mesh_custom_banner_fg01"),
  # (this_or_next|eq, ":bg_type", "mesh_custom_banner_fg02"),
  # (this_or_next|eq, ":bg_type", "mesh_custom_banner_fg03"),
  # (this_or_next|eq, ":bg_type", "mesh_custom_banner_fg08"),
  # (this_or_next|eq, ":bg_type", "mesh_custom_banner_fg09"),
  # (this_or_next|eq, ":bg_type", "mesh_custom_banner_fg10"),
  # (this_or_next|eq, ":bg_type", "mesh_custom_banner_fg11"),
  # (this_or_next|eq, ":bg_type", "mesh_custom_banner_fg12"),
  # (this_or_next|eq, ":bg_type", "mesh_custom_banner_fg13"),
  # (this_or_next|eq, ":bg_type", "mesh_custom_banner_fg16"),
  # (eq, ":bg_type", "mesh_custom_banner_fg17"),
  # (cur_tableau_add_mesh_with_scale_and_vertex_color, ":bg_type", pos1, pos2, 0, ":bg_color_1"),
  # (else_try),
  # (cur_tableau_add_mesh_with_scale_and_vertex_color, ":bg_type", pos1, pos3, 0, ":bg_color_1"),
  # (try_end),
  # (position_move_z, pos1, -20),
  # (position_move_x, pos2, ":width"),
  # (position_move_y, pos2, ":height"),
  # (cur_tableau_add_mesh_with_scale_and_vertex_color, "mesh_custom_banner_bg", pos1, pos2, 0, ":bg_color_2"),
  
  # (assign, ":charge_stretch", ":stretch_width"),
  # (val_min, ":charge_stretch", ":stretch_height"),
  # (val_min, ":charge_stretch", ":max_charge_scale"),
  # (call_script, "script_get_custom_banner_charge_type_position_scale_color", "trp_player", ":positioning"),
  
  # (try_begin),
  # (this_or_next|eq, ":drawn_item_type", 2), #heater shield
  # (eq, ":drawn_item_type", 3), #armor
  # (assign, ":change_center_pos", 0),
  # (try_begin),
  # (eq, ":num_charges", 1),
  # (assign, ":change_center_pos", 1),
  # (else_try),
  # (eq, ":num_charges", 2),
  # (eq, ":positioning", 1),
  # (assign, ":change_center_pos", 1),
  # (else_try),
  # (eq, ":num_charges", 3),
  # (eq, ":positioning", 1),
  # (assign, ":change_center_pos", 1),
  # (try_end),
  # (try_begin),
  # (eq, ":change_center_pos", 1),
  # (val_add, ":center_pos_y", 30),
  # (try_end),
  # (try_end),
  
  # (try_begin),
  # (ge, ":num_charges", 1),
  # (val_mul, reg1, ":charge_stretch"),
  # (val_div, reg1, 10000),
  # (position_get_x, ":x", pos0),
  # (position_get_y, ":y", pos0),
  # (val_mul, ":x", ":stretch_width"),
  # (val_mul, ":y", ":stretch_height"),
  # (val_div, ":x", 10000),
  # (val_div, ":y", 10000),
  # (val_add, ":x", ":center_pos_x"),
  # (val_add, ":y", ":center_pos_y"),
  # (position_set_x, pos0, ":x"),
  # (position_set_y, pos0, ":y"),
  # (assign, ":scale_value", reg1),
  # (convert_to_fixed_point, ":scale_value"),
  # (store_mul, ":scale_value_inverse", ":scale_value", -1),
  # (init_position, pos4),
  # (position_set_x, pos4, ":scale_value"),
  # (position_set_y, pos4, ":scale_value"),
  # (position_set_z, pos4, ":scale_value"),
  # (store_div, ":orientation", reg0, 256), #orientation flags
  # (try_begin),
  # (this_or_next|eq, ":orientation", 1),
  # (eq, ":orientation", 3),
  # (position_set_x, pos4, ":scale_value_inverse"),
  # (try_end),
  # (try_begin),
  # (this_or_next|eq, ":orientation", 2),
  # (eq, ":orientation", 3),
  # (position_set_y, pos4, ":scale_value_inverse"),
  # (try_end),
  # (val_mod, reg0, 256), #remove orientation flags
  # (cur_tableau_add_mesh_with_scale_and_vertex_color, reg0, pos0, pos4, 0, reg2),
  # (try_end),
  # (try_begin),
  # (ge, ":num_charges", 2),
  # (val_mul, reg4, ":charge_stretch"),
  # (val_div, reg4, 10000),
  # (position_get_x, ":x", pos1),
  # (position_get_y, ":y", pos1),
  # (val_mul, ":x", ":stretch_width"),
  # (val_mul, ":y", ":stretch_height"),
  # (val_div, ":x", 10000),
  # (val_div, ":y", 10000),
  # (val_add, ":x", ":center_pos_x"),
  # (val_add, ":y", ":center_pos_y"),
  # (position_set_x, pos1, ":x"),
  # (position_set_y, pos1, ":y"),
  
  # (assign, ":scale_value", reg4),
  # (convert_to_fixed_point, ":scale_value"),
  # (store_mul, ":scale_value_inverse", ":scale_value", -1),
  # (init_position, pos4),
  # (position_set_x, pos4, ":scale_value"),
  # (position_set_y, pos4, ":scale_value"),
  # (position_set_z, pos4, ":scale_value"),
  # (store_div, ":orientation", reg3, 256), #orientation flags
  # (try_begin),
  # (this_or_next|eq, ":orientation", 1),
  # (eq, ":orientation", 3),
  # (position_set_x, pos4, ":scale_value_inverse"),
  # (try_end),
  # (try_begin),
  # (this_or_next|eq, ":orientation", 2),
  # (eq, ":orientation", 3),
  # (position_set_y, pos4, ":scale_value_inverse"),
  # (try_end),
  # (val_mod, reg3, 256), #remove orientation flags
  
  # (cur_tableau_add_mesh_with_scale_and_vertex_color, reg3, pos1, pos4, 0, reg5),
  # (try_end),
  # (try_begin),
  # (ge, ":num_charges", 3),
  # (val_mul, reg7, ":charge_stretch"),
  # (val_div, reg7, 10000),
  # (position_get_x, ":x", pos2),
  # (position_get_y, ":y", pos2),
  # (val_mul, ":x", ":stretch_width"),
  # (val_mul, ":y", ":stretch_height"),
  # (val_div, ":x", 10000),
  # (val_div, ":y", 10000),
  # (val_add, ":x", ":center_pos_x"),
  # (val_add, ":y", ":center_pos_y"),
  # (position_set_x, pos2, ":x"),
  # (position_set_y, pos2, ":y"),
  
  # (assign, ":scale_value", reg7),
  # (convert_to_fixed_point, ":scale_value"),
  # (store_mul, ":scale_value_inverse", ":scale_value", -1),
  # (init_position, pos4),
  # (position_set_x, pos4, ":scale_value"),
  # (position_set_y, pos4, ":scale_value"),
  # (position_set_z, pos4, ":scale_value"),
  # (store_div, ":orientation", reg6, 256), #orientation flags
  # (try_begin),
  # (this_or_next|eq, ":orientation", 1),
  # (eq, ":orientation", 3),
  # (position_set_x, pos4, ":scale_value_inverse"),
  # (try_end),
  # (try_begin),
  # (this_or_next|eq, ":orientation", 2),
  # (eq, ":orientation", 3),
  # (position_set_y, pos4, ":scale_value_inverse"),
  # (try_end),
  # (val_mod, reg6, 256), #remove orientation flags
  
  # (cur_tableau_add_mesh_with_scale_and_vertex_color, reg6, pos2, pos4, 0, reg8),
  # (try_end),
  # (try_begin),
  # (ge, ":num_charges", 4),
  # (val_mul, reg10, ":charge_stretch"),
  # (val_div, reg10, 10000),
  # (position_get_x, ":x", pos3),
  # (position_get_y, ":y", pos3),
  # (val_mul, ":x", ":stretch_width"),
  # (val_mul, ":y", ":stretch_height"),
  # (val_div, ":x", 10000),
  # (val_div, ":y", 10000),
  # (val_add, ":x", ":center_pos_x"),
  # (val_add, ":y", ":center_pos_y"),
  # (position_set_x, pos3, ":x"),
  # (position_set_y, pos3, ":y"),
  
  # (assign, ":scale_value", reg10),
  # (convert_to_fixed_point, ":scale_value"),
  # (store_mul, ":scale_value_inverse", ":scale_value", -1),
  # (init_position, pos4),
  # (position_set_x, pos4, ":scale_value"),
  # (position_set_y, pos4, ":scale_value"),
  # (position_set_z, pos4, ":scale_value"),
  # (store_div, ":orientation", reg9, 256), #orientation flags
  # (try_begin),
  # (this_or_next|eq, ":orientation", 1),
  # (eq, ":orientation", 3),
  # (position_set_x, pos4, ":scale_value_inverse"),
  # (try_end),
  # (try_begin),
  # (this_or_next|eq, ":orientation", 2),
  # (eq, ":orientation", 3),
  # (position_set_y, pos4, ":scale_value_inverse"),
  # (try_end),
  # (val_mod, reg9, 256), #remove orientation flags
  
  # (cur_tableau_add_mesh_with_scale_and_vertex_color, reg9, pos3, pos4, 0, reg11),
  # (try_end),
  # ]),
  
  # # script_get_troop_custom_banner_num_positionings
  # # Input: arg1 = troop_no
  # # Output: reg0 = num_positionings
  # ("get_troop_custom_banner_num_positionings",
  # [
  # (store_script_param, ":troop_no", 1),
  # (troop_get_slot, ":num_charges", ":troop_no", slot_troop_custom_banner_num_charges),
  # (try_begin),
  # (eq, ":num_charges", 1),
  # (assign, ":num_positionings", 2),
  # (else_try),
  # (eq, ":num_charges", 2),
  # (assign, ":num_positionings", 4),
  # (else_try),
  # (eq, ":num_charges", 3),
  # (assign, ":num_positionings", 6),
  # (else_try),
  # (assign, ":num_positionings", 2),
  # (try_end),
  # (assign, reg0, ":num_positionings"),
  # ]),
  
  # # script_get_custom_banner_charge_type_position_scale_color
  # # Input: arg1 = troop_no, arg2 = positioning_index
  # # Output: reg0 = type_1
  # #         reg1 = scale_1
  # #         reg2 = color_1
  # #         reg3 = type_2
  # #         reg4 = scale_2
  # #         reg5 = color_2
  # #         reg6 = type_3
  # #         reg7 = scale_3
  # #         reg8 = color_3
  # #         reg9 = type_4
  # #         reg10 = scale_4
  # #         reg11 = color_4
  # ("get_custom_banner_charge_type_position_scale_color",
  # [
  # (store_script_param, ":troop_no", 1),
  # (store_script_param, ":positioning", 2),
  # (troop_get_slot, ":num_charges", ":troop_no", slot_troop_custom_banner_num_charges),
  # (init_position, pos0),
  # (init_position, pos1),
  # (init_position, pos2),
  # (init_position, pos3),
  
  # (troop_get_slot, reg0, ":troop_no", slot_troop_custom_banner_charge_type_1),
  # (val_add, reg0, custom_banner_charges_begin),
  # (troop_get_slot, reg2, ":troop_no", slot_troop_custom_banner_charge_color_1),
  # (troop_get_slot, reg3, ":troop_no", slot_troop_custom_banner_charge_type_2),
  # (val_add, reg3, custom_banner_charges_begin),
  # (troop_get_slot, reg5, ":troop_no", slot_troop_custom_banner_charge_color_2),
  # (troop_get_slot, reg6, ":troop_no", slot_troop_custom_banner_charge_type_3),
  # (val_add, reg6, custom_banner_charges_begin),
  # (troop_get_slot, reg8, ":troop_no", slot_troop_custom_banner_charge_color_3),
  # (troop_get_slot, reg9, ":troop_no", slot_troop_custom_banner_charge_type_4),
  # (val_add, reg9, custom_banner_charges_begin),
  # (troop_get_slot, reg11, ":troop_no", slot_troop_custom_banner_charge_color_4),
  
  # (try_begin),
  # (eq, ":num_charges", 1),
  # (try_begin),
  # (eq, ":positioning", 0),
  # (assign, reg1, 100),
  # (else_try),
  # (assign, reg1, 50),
  # (try_end),
  # (else_try),
  # (eq, ":num_charges", 2),
  # (try_begin),
  # (eq, ":positioning", 0),
  # (position_set_y, pos0, 25),
  # (position_set_y, pos1, -25),
  # (assign, reg1, 40),
  # (assign, reg4, 40),
  # (else_try),
  # (eq, ":positioning", 1),
  # (position_set_x, pos0, -25),
  # (position_set_x, pos1, 25),
  # (assign, reg1, 40),
  # (assign, reg4, 40),
  # (else_try),
  # (eq, ":positioning", 2),
  # (position_set_x, pos0, -25),
  # (position_set_y, pos0, 25),
  # (position_set_x, pos1, 25),
  # (position_set_y, pos1, -25),
  # (assign, reg1, 50),
  # (assign, reg4, 50),
  # (else_try),
  # (position_set_x, pos0, -25),
  # (position_set_y, pos0, -25),
  # (position_set_x, pos1, 25),
  # (position_set_y, pos1, 25),
  # (assign, reg1, 50),
  # (assign, reg4, 50),
  # (try_end),
  # (else_try),
  # (eq, ":num_charges", 3),
  # (try_begin),
  # (eq, ":positioning", 0),
  # (position_set_y, pos0, 33),
  # (position_set_y, pos2, -33),
  # (assign, reg1, 30),
  # (assign, reg4, 30),
  # (assign, reg7, 30),
  # (else_try),
  # (eq, ":positioning", 1),
  # (position_set_x, pos0, -33),
  # (position_set_x, pos2, 33),
  # (assign, reg1, 30),
  # (assign, reg4, 30),
  # (assign, reg7, 30),
  # (else_try),
  # (eq, ":positioning", 2),
  # (position_set_x, pos0, -30),
  # (position_set_y, pos0, 30),
  # (position_set_x, pos2, 30),
  # (position_set_y, pos2, -30),
  # (assign, reg1, 35),
  # (assign, reg4, 35),
  # (assign, reg7, 35),
  # (else_try),
  # (eq, ":positioning", 3),
  # (position_set_x, pos0, -30),
  # (position_set_y, pos0, -30),
  # (position_set_x, pos2, 30),
  # (position_set_y, pos2, 30),
  # (assign, reg1, 35),
  # (assign, reg4, 35),
  # (assign, reg7, 35),
  # (else_try),
  # (eq, ":positioning", 4),
  # (position_set_x, pos0, -25),
  # (position_set_y, pos0, -25),
  # (position_set_y, pos1, 25),
  # (position_set_x, pos2, 25),
  # (position_set_y, pos2, -25),
  # (assign, reg1, 50),
  # (assign, reg4, 50),
  # (assign, reg7, 50),
  # (else_try),
  # (position_set_x, pos0, -25),
  # (position_set_y, pos0, 25),
  # (position_set_y, pos1, -25),
  # (position_set_x, pos2, 25),
  # (position_set_y, pos2, 25),
  # (assign, reg1, 50),
  # (assign, reg4, 50),
  # (assign, reg7, 50),
  # (try_end),
  # (else_try),
  # (try_begin),
  # (eq, ":positioning", 0),
  # (position_set_x, pos0, -25),
  # (position_set_y, pos0, 25),
  # (position_set_x, pos1, 25),
  # (position_set_y, pos1, 25),
  # (position_set_x, pos2, -25),
  # (position_set_y, pos2, -25),
  # (position_set_x, pos3, 25),
  # (position_set_y, pos3, -25),
  # (assign, reg1, 50),
  # (assign, reg4, 50),
  # (assign, reg7, 50),
  # (assign, reg10, 50),
  # (else_try),
  # (position_set_y, pos0, 30),
  # (position_set_x, pos1, -30),
  # (position_set_x, pos2, 30),
  # (position_set_y, pos3, -30),
  # (assign, reg1, 35),
  # (assign, reg4, 35),
  # (assign, reg7, 35),
  # (assign, reg10, 35),
  # (try_end),
  # (try_end),
  # ]),
  
  # # script_get_random_custom_banner
  # # Input: arg1 = troop_no
  # # Output: none
  # ("get_random_custom_banner",
  # [
  # (store_script_param, ":troop_no", 1),
  # (store_random_in_range, ":num_charges", 1, 5),
  # (troop_set_slot, ":troop_no", slot_troop_custom_banner_num_charges, ":num_charges"),
  # (store_random_in_range, ":random_color_index", 0, 42),
  # (call_script, "script_get_custom_banner_color_from_index", ":random_color_index"),
  # (assign, ":color_1", reg0),
  # (troop_set_slot, ":troop_no", slot_troop_custom_banner_bg_color_1, ":color_1"),
  # (assign, ":end_cond", 1),
  # (try_for_range, ":unused", 0, ":end_cond"),
  # (store_random_in_range, ":random_color_index", 0, 42),
  # (call_script, "script_get_custom_banner_color_from_index", ":random_color_index"),
  # (assign, ":color_2", reg0),
  # (try_begin),
  # (call_script, "script_cf_check_color_visibility", ":color_1", ":color_2"),
  # (troop_set_slot, ":troop_no", slot_troop_custom_banner_bg_color_2, ":color_2"),
  # (else_try),
  # (val_add, ":end_cond", 1),
  # (try_end),
  # (try_end),
  # (assign, ":end_cond", 4),
  # (assign, ":cur_charge", 0),
  # (try_for_range, ":unused", 0, ":end_cond"),
  # (store_random_in_range, ":random_color_index", 0, 42),
  # (call_script, "script_get_custom_banner_color_from_index", ":random_color_index"),
  # (assign, ":charge_color", reg0),
  # (try_begin),
  # (call_script, "script_cf_check_color_visibility", ":charge_color", ":color_1"),
  # (call_script, "script_cf_check_color_visibility", ":charge_color", ":color_2"),
  # (store_add, ":cur_slot", ":cur_charge", slot_troop_custom_banner_charge_color_1),
  # (troop_set_slot, ":troop_no", ":cur_slot", ":charge_color"),
  # (store_random_in_range, ":random_charge", custom_banner_charges_begin, custom_banner_charges_end),
  # (val_sub, ":random_charge", custom_banner_charges_begin),
  # (store_add, ":cur_slot", ":cur_charge", slot_troop_custom_banner_charge_type_1),
  # (troop_set_slot, ":troop_no", ":cur_slot", ":random_charge"),
  # (val_add, ":cur_charge", 1),
  # (else_try),
  # (val_add, ":end_cond", 1),
  # (try_end),
  # (try_end),
  # (store_random_in_range, ":random_bg", custom_banner_backgrounds_begin, custom_banner_backgrounds_end),
  # (val_sub, ":random_bg", custom_banner_backgrounds_begin),
  # (troop_set_slot, ":troop_no", slot_troop_custom_banner_bg_type, ":random_bg"),
  # (store_random_in_range, ":random_flag", custom_banner_flag_types_begin, custom_banner_flag_types_end),
  # (val_sub, ":random_flag", custom_banner_flag_types_begin),
  # (troop_set_slot, ":troop_no", slot_troop_custom_banner_flag_type, ":random_flag"),
  # (store_random_in_range, ":random_positioning", 0, 4),
  # (troop_set_slot, ":troop_no", slot_troop_custom_banner_positioning, ":random_positioning"),
  # ]),
  
  # # script_get_custom_banner_color_from_index
  # # Input: arg1 = color_index
  # # Output: reg0 = color
  # ("get_custom_banner_color_from_index",
  # [
  # (store_script_param, ":color_index", 1),
  
  # (assign, ":cur_color", 0xFF000000),
  # (assign, ":red", 0x00),
  # (assign, ":green", 0x00),
  # (assign, ":blue", 0x00),
  # (store_mod, ":mod_i_color", ":color_index", 7),
  # (try_begin),
  # (eq, ":mod_i_color", 0),
  # (assign, ":blue", 0xFF),
  # (else_try),
  # (eq, ":mod_i_color", 1),
  # (assign, ":red", 0xEE),
  # (else_try),
  # (eq, ":mod_i_color", 2),
  # (assign, ":red", 0xFB),
  # (assign, ":green", 0xAC),
  # (else_try),
  # (eq, ":mod_i_color", 3),
  # (assign, ":red", 0x5F),
  # (assign, ":blue", 0xFF),
  # (else_try),
  # (eq, ":mod_i_color", 4),
  # (assign, ":red", 0x05),
  # (assign, ":green", 0x44),
  # (else_try),
  # (eq, ":mod_i_color", 5),
  # (assign, ":red", 0xEE),
  # (assign, ":green", 0xEE),
  # (assign, ":blue", 0xEE),
  # (else_try),
  # (assign, ":red", 0x22),
  # (assign, ":green", 0x22),
  # (assign, ":blue", 0x22),
  # (try_end),
  # (store_div, ":cur_tone", ":color_index", 7),
  # (store_sub, ":cur_tone", 8, ":cur_tone"),
  # (val_mul, ":red", ":cur_tone"),
  # (val_div, ":red", 8),
  # (val_mul, ":green", ":cur_tone"),
  # (val_div, ":green", 8),
  # (val_mul, ":blue", ":cur_tone"),
  # (val_div, ":blue", 8),
  # (val_mul, ":green", 0x100),
  # (val_mul, ":red", 0x10000),
  # (val_add, ":cur_color", ":blue"),
  # (val_add, ":cur_color", ":green"),
  # (val_add, ":cur_color", ":red"),
  # (assign, reg0, ":cur_color"),
  # ]),
  
  # script_cf_check_color_visibility
  # Input: arg1 = color_1, arg2 = color_2
  # Output: none
  # ("cf_check_color_visibility",
    # [
      # (store_script_param, ":color_1", 1),
      # (store_script_param, ":color_2", 2),
      # (store_mod, ":blue_1", ":color_1", 256),
      # (store_div, ":green_1", ":color_1", 256),
      # (val_mod, ":green_1", 256),
      # (store_div, ":red_1", ":color_1", 256 * 256),
      # (val_mod, ":red_1", 256),
      # (store_mod, ":blue_2", ":color_2", 256),
      # (store_div, ":green_2", ":color_2", 256),
      # (val_mod, ":green_2", 256),
      # (store_div, ":red_2", ":color_2", 256 * 256),
      # (val_mod, ":red_2", 256),
      # (store_sub, ":red_dif", ":red_1", ":red_2"),
      # (val_abs, ":red_dif"),
      # (store_sub, ":green_dif", ":green_1", ":green_2"),
      # (val_abs, ":green_dif"),
      # (store_sub, ":blue_dif", ":blue_1", ":blue_2"),
      # (val_abs, ":blue_dif"),
      # (assign, ":max_dif", 0),
      # (val_max, ":max_dif", ":red_dif"),
      # (val_max, ":max_dif", ":green_dif"),
      # (val_max, ":max_dif", ":blue_dif"),
      # (ge, ":max_dif", 64),
  # ]),
  
  # script_get_next_active_kingdom
  # Input: arg1 = faction_no
  # Output: reg0 = faction_no (does not choose player faction)
  ("get_next_active_kingdom",
    [
      (store_script_param, ":faction_no", 1),
      (assign, ":end_cond", kingdoms_end),
      (try_for_range, ":unused", kingdoms_begin, ":end_cond"),
        (val_add, ":faction_no", 1),
        (try_begin),
          (ge, ":faction_no", kingdoms_end),
          (assign, ":faction_no", kingdoms_begin),
        (try_end),
        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        (neq, ":faction_no", "fac_player_supporters_faction"),
        (assign, ":end_cond", 0),
      (try_end),
      (assign, reg0, ":faction_no"),
  ]),
  
  #  # script_store_average_center_value_per_faction
  #  # Input: none
  #  # Output: none (sets $g_average_center_value_per_faction)
  #  ("store_average_center_value_per_faction",
  #    [
  #      (store_sub, ":num_towns", towns_end, towns_begin),
  #      (store_sub, ":num_castles", castles_end, castles_begin),
  #      (assign, ":num_factions", 0),
  #      (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
  #        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
  #        (val_add, ":num_factions", 1),
  #      (try_end),
  #      (val_max, ":num_factions", 1),
  #      (store_mul, "$g_average_center_value_per_faction", ":num_towns", 2),
  #      (val_add, "$g_average_center_value_per_faction", ":num_castles"),
  #      (val_mul, "$g_average_center_value_per_faction", 10),
  #      (val_div, "$g_average_center_value_per_faction", ":num_factions"),
  #     ]),
  
  # script_remove_cattles_if_herd_is_close_to_party
  # Input: arg1 = party_no, arg2 = maximum_number_of_cattles_required
  # Output: reg0 = number_of_cattles_removed
  ("remove_cattles_if_herd_is_close_to_party",
    [
      (store_script_param, ":party_no", 1),
      (store_script_param, ":max_req", 2),
      (assign, ":cur_req", ":max_req"),
      (try_for_parties, ":cur_party"),
        (gt, ":cur_req", 0),
        (party_slot_eq, ":cur_party", slot_party_type, spt_cattle_herd),
        (store_distance_to_party_from_party, ":dist", ":cur_party", ":party_no"),
        (lt, ":dist", 3),
        
        #Do not use the quest herd for "move cattle herd"
        (assign, ":subcontinue", 1),
        (try_begin),
          (check_quest_active, "qst_move_cattle_herd"),
          (quest_slot_eq, "qst_move_cattle_herd", slot_quest_target_party, ":cur_party"),
          (assign, ":subcontinue", 0),
        (try_end),
        (eq, ":subcontinue", 1),
        #Do not use the quest herd for "move cattle herd" ends
        
        (party_count_companions_of_type, ":num_cattle", ":cur_party", "trp_cattle"),
        (try_begin),
          (le, ":num_cattle", ":cur_req"),
          (assign, ":num_added", ":num_cattle"),
          (remove_party, ":cur_party"),
        (else_try),
          (assign, ":num_added", ":cur_req"),
          (party_remove_members, ":cur_party", "trp_cattle", ":cur_req"),
        (try_end),
        (val_sub, ":cur_req", ":num_added"),
        
        
        (try_begin),
          (party_slot_eq, ":party_no", slot_party_type, spt_village),
          (party_get_slot, ":village_cattle_amount", ":party_no", slot_village_number_of_cattle),
          (val_add, ":village_cattle_amount", ":num_added"),
          (party_set_slot, ":party_no", slot_village_number_of_cattle, ":village_cattle_amount"),
        (try_end),
        (try_begin),
          (is_between, ":party_no", villages_begin, villages_end),
          (assign, reg3, ":num_added"),
          (str_store_party_name_link, s1, ":party_no"),
          (display_message, "@You have delivered {reg3} head of cattle to {s1}."),
        (try_end),
        (try_begin),
          (gt, "$cheat_mode", 0),
          (assign, reg4, ":village_cattle_amount"),
          (display_message, "@{!}Village now has {reg4}"),
        (try_end),
      (try_end),
      (store_sub, reg0, ":max_req", ":cur_req"),
  ]),
  
  # script_get_rumor_to_s61
  # Input: rumor_id
  # Output: reg0 = 1 if rumor found, 0 otherwise; s61 will contain rumor string if found
  ("get_rumor_to_s61",
    [
      (store_script_param, ":base_rumor_id", 1), # the script returns the same rumor for the same rumor id, so that one cannot hear all rumors by
      # speaking to a single person.
      
      (store_current_hours, ":cur_hours"),
      (store_div, ":cur_day", ":cur_hours", 24),
      (assign, ":rumor_found", 0),
      (assign, ":num_tries", 3),
      (try_for_range, ":try_no", 0, ":num_tries"),
        (store_mul, ":rumor_id", ":try_no", 6781),
        (val_add, ":rumor_id", ":base_rumor_id"),
        (store_mod, ":rumor_type", ":rumor_id", 17), #chief anade rumors
        (val_add, ":rumor_id", ":cur_hours"),
        (try_begin),
          (eq,  ":rumor_type", 0),
          (try_begin),
            (store_sub, ":range", towns_end, towns_begin),
            (store_mod, ":random_center", ":rumor_id", ":range"),
            (val_add, ":random_center", towns_begin),
            (party_slot_ge, ":random_center", slot_town_has_tournament, 1),
            (neq, ":random_center", "$current_town"),
            (str_store_party_name, s62, ":random_center"),
            (str_store_string, s61, "@I hear that there will be a tournament in {s62} soon."),
            (assign, ":rumor_found", 1),
          (try_end),
        (else_try),
          (eq,  ":rumor_type", 1),
          (try_begin),
            (store_sub, ":range", active_npcs_end, original_kingdom_heroes_begin), #was reversed
            (store_mod, ":random_hero", ":rumor_id", ":range"),
            (val_add, ":random_hero", original_kingdom_heroes_begin),
            (is_between, ":random_hero", active_npcs_begin, active_npcs_end),
            (troop_get_slot, ":personality", ":random_hero", slot_lord_reputation_type),
            (gt, ":personality", 0),
            (store_add, ":rumor_string", ":personality", "str_gossip_about_character_default"),
            (str_store_troop_name, s6, ":random_hero"),
            (str_store_string, s61, ":rumor_string"),
            (assign, ":rumor_found", 1),
          (try_end),
        (else_try),
          (eq,  ":rumor_type", 2),
          (try_begin),
            (store_sub, ":range", trade_goods_end, trade_goods_begin),
            (store_add, ":random_trade_good", ":rumor_id", ":cur_day"),
            (store_mod, ":random_trade_good", ":random_trade_good", ":range"),
            (store_add, ":random_trade_good_slot", ":random_trade_good", slot_town_trade_good_prices_begin),
            (val_add, ":random_trade_good", trade_goods_begin),
            (store_mul, ":min_price", average_price_factor, 3),
            (val_div, ":min_price", 4),
            (assign, ":min_price_center", -1),
            (try_for_range, ":sub_try_no", 0, 10),
              (store_sub, ":range", towns_end, towns_begin),
              (store_add, ":center_rumor_id", ":rumor_id", ":sub_try_no"),
              (store_mod, ":random_center", ":center_rumor_id", ":range"),
              (val_add, ":random_center", towns_begin),
              (neq, ":random_center", "$g_encountered_party"),
              (party_get_slot, ":cur_price", ":random_center", ":random_trade_good_slot"),
              (lt, ":cur_price", ":min_price"),
              (assign, ":min_price", ":cur_price"),
              (assign, ":min_price_center", ":random_center"),
            (try_end),
            (ge, ":min_price_center", 0),
            (str_store_item_name, s62, ":random_trade_good"),
            (str_store_party_name, s63, ":min_price_center"),
            (str_store_string, s61, "@I heard that one can buy {s62} very cheap at {s63}."),
            (assign, ":rumor_found", 1),
          (try_end),
        (else_try),
          (eq,  ":rumor_type", 3),
          (try_begin),
            (store_sub, ":range", trade_goods_end, trade_goods_begin),
            (store_add, ":random_trade_good", ":rumor_id", ":cur_day"),
            (store_mod, ":random_trade_good", ":random_trade_good", ":range"),
            (store_add, ":random_trade_good_slot", ":random_trade_good", slot_town_trade_good_prices_begin),
            (val_add, ":random_trade_good", trade_goods_begin),
            (store_mul, ":max_price", average_price_factor, 5),
            (val_div, ":max_price", 4),
            (assign, ":max_price_center", -1),
            (try_for_range, ":sub_try_no", 0, 10),
              (store_sub, ":range", towns_end, towns_begin),
              (store_add, ":center_rumor_id", ":rumor_id", ":sub_try_no"),
              (store_mod, ":random_center", ":center_rumor_id", ":range"),
              (val_add, ":random_center", towns_begin),
              (neq, ":random_center", "$g_encountered_party"),
              (party_get_slot, ":cur_price", ":random_center", ":random_trade_good_slot"),
              (gt, ":cur_price", ":max_price"),
              (assign, ":max_price", ":cur_price"),
              (assign, ":max_price_center", ":random_center"),
            (try_end),
            (ge, ":max_price_center", 0),
            (str_store_item_name, s62, ":random_trade_good"),
            (str_store_party_name, s63, ":max_price_center"),
            (str_store_string, s61, "@I heard that they pay a very high price for {s62} at {s63}."),
            (assign, ":rumor_found", 1),
          (try_end),
        (try_end),
        #chief rumores
      (else_try),
        (eq,  ":rumor_type", 4),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", rumor_found_chance),
          (str_store_string, s61, "@I heard that the Danish promise to conquer West Seaxe before next spring."),
          (assign, ":rumor_found", 1),
        (try_end),
      (else_try),
        (eq,  ":rumor_type", 5),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", rumor_found_chance),
          (str_store_string, s61, "@Currently, the most requested song the bards perform is the one of King Cadwallon of Gwynedd and King Penda of Mierce invading Northhymbre and penetrating deep into the heart of the old Kingdom of Deira. There King Eadwine amassed his warriors and rode south to meet them in battle. The two armies met at Haedfeldland and engaged in the bloodiest and most brutal battle of the time. Perhaps it is popular because of the recent invasion."),
          (assign, ":rumor_found", 1),
        (try_end),
      (else_try),
        (eq,  ":rumor_type", 6),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", rumor_found_chance),
          (str_store_string, s61, "@I heard the Pope in Rome sent missionaries to convert the Danish!"),
          (assign, ":rumor_found", 1),
        (try_end),
      (else_try),
        (eq,  ":rumor_type", 7),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", rumor_found_chance),
          (str_store_string, s61, "@Ragnar's sons are the most dangerous men in the world. I hear they are relentless, and nobody dares to face up to them."),
          (assign, ":rumor_found", 1),
        (try_end),
      (else_try),
        (eq,  ":rumor_type", 8),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", rumor_found_chance),
          (str_store_string, s61, "@I think the wife of my friend is cheating on him with a bard."),
          (assign, ":rumor_found", 1),
        (try_end),
      (else_try),
        (eq,  ":rumor_type", 9),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", rumor_found_chance),
          (str_store_string, s61, "@I hate bandits! Somebody should kill them all."),
          (assign, ":rumor_found", 1),
        (try_end),
      (else_try),
        (eq,  ":rumor_type", 10),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", rumor_found_chance),
          (str_store_string, s61, "@I would like to know about the Roman times."),
          (assign, ":rumor_found", 1),
        (try_end),
      (else_try),
        (eq,  ":rumor_type", 11),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", rumor_found_chance),
          (str_store_string, s61, "@Many of the nobles of Englaland are very impressed by young princess Hild. She is so beautiful. Too bad she has religious leanings!"),
          (assign, ":rumor_found", 1),
        (try_end),
      (else_try),
        (eq,  ":rumor_type", 12),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", rumor_found_chance),
          (str_store_string, s61, "@A traveller told me the Franks are recruiting men for attacking the Frisians."),
          (assign, ":rumor_found", 1),
        (try_end),
      (else_try),
        (eq,  ":rumor_type", 13),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", rumor_found_chance),
          (str_store_string, s61, "@The Danish have brought all kinds of foreigners with them. It's like the whole world has come to old Englaland."),
          (assign, ":rumor_found", 1),
        (try_end),
      (else_try),
        (eq,  ":rumor_type", 14),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", rumor_found_chance),
          (str_store_string, s61, "@West Seaxe was never as powerful as now. It is logical that their king is considered to be a Brytenwalda."),
          (assign, ":rumor_found", 1),
        (try_end),
      (else_try),
        (eq,  ":rumor_type", 15),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", rumor_found_chance),
          (str_store_string, s61, "@I wish the world were less dangerous. Few men grow old."),
          (assign, ":rumor_found", 1),
        (try_end),
      (else_try),
        (eq,  ":rumor_type", 16),
        (try_begin),
          (store_random_in_range, ":rand", 0, 100),
          (lt, ":rand", rumor_found_chance),
          (str_store_string, s61, "@I've been told the army of the Ui Neill maintains strict discipline and training. For that reason, they are the most powerful in Hibernia!"),
          (assign, ":rumor_found", 1),
        (try_end),
        ###chief rumores acaba
        (try_begin),
          (gt, ":rumor_found", 0),
          (assign, ":num_tries", 0),
        (try_end),
      (try_end),
      (assign, reg0, ":rumor_found"),
  ]),
  
  ("lord_comment_to_s43",
    [(store_script_param, ":lord", 1),
      (store_script_param, ":default_string", 2),
      
      (troop_get_slot,":reputation", ":lord", slot_lord_reputation_type),
      
      (try_begin),
        #some default strings will have added comments for the added commons reputation types
        (try_begin),
          (eq, ":reputation", lrep_roguish),
          (assign, ":reputation", lrep_goodnatured),
        (else_try),
          (eq, ":reputation", lrep_custodian),
          (assign, ":reputation", lrep_cunning),
        (else_try),
          (eq, ":reputation", lrep_benefactor),
          (assign, ":reputation", lrep_goodnatured),
        (try_end),
      (try_end),
      
      (store_add, ":result", ":reputation", ":default_string"),
      
      (str_store_string, 43, ":result"),
      (assign, reg0, ":result"),
      
      
  ]),
  
  #Troop Commentaries begin
  
  # script_add_log_entry
  # Input: arg1 = entry_type, arg2 = event_actor, arg3 = center_object, arg4 = troop_object, arg5 = faction_object
  # Output: none
  ("add_log_entry",
    [(store_script_param, ":entry_type", 1),
      (store_script_param, ":actor", 2),
      (store_script_param, ":center_object", 3),
      (store_script_param, ":troop_object", 4),
      (store_script_param, ":faction_object", 5),
      (assign, ":center_object_lord", -1),
      (assign, ":center_object_faction", -1),
      (assign, ":troop_object_faction", -1),
      
      (try_begin),
        (party_is_active, ":center_object", 0),
        (party_get_slot, ":center_object_lord", ":center_object", slot_town_lord),
        (store_faction_of_party, ":center_object_faction", ":center_object"),
      (else_try),
        (assign, ":center_object_lord", 0),
        (assign, ":center_object_faction", 0),
      (try_end),
      
      (try_begin),
        (is_between, ":troop_object", 0, "trp_local_merchant"),
        (store_troop_faction, ":troop_object_faction", ":troop_object"),
      (else_try),
        (assign, ":troop_object_faction", 0),
      (try_end),
      
      (val_add, "$num_log_entries", 1),
      
      (store_current_hours, ":entry_time"),
      (troop_set_slot, "trp_log_array_entry_type",            "$num_log_entries", ":entry_type"),
      (troop_set_slot, "trp_log_array_entry_time",            "$num_log_entries", ":entry_time"),
      (troop_set_slot, "trp_log_array_actor",                 "$num_log_entries", ":actor"),
      (troop_set_slot, "trp_log_array_center_object",         "$num_log_entries", ":center_object"),
      (troop_set_slot, "trp_log_array_center_object_lord",    "$num_log_entries", ":center_object_lord"),
      (troop_set_slot, "trp_log_array_center_object_faction", "$num_log_entries", ":center_object_faction"),
      (troop_set_slot, "trp_log_array_troop_object",          "$num_log_entries", ":troop_object"),
      (troop_set_slot, "trp_log_array_troop_object_faction",  "$num_log_entries", ":troop_object_faction"),
      (troop_set_slot, "trp_log_array_faction_object",        "$num_log_entries", ":faction_object"),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (assign, reg3, "$num_log_entries"),
        (assign, reg4, ":entry_type"),
        (display_message, "@{!}Log entry {reg3}: type {reg4}"),
        (try_begin),
          (gt, ":center_object", 0),
          (neq, ":entry_type", logent_traveller_attacked),
          (neq, ":entry_type", logent_party_traded),
          (party_is_active, ":center_object"), #sometimes is a troop
          
          (str_store_party_name, s4, ":center_object"),
          (display_message, "@{!}Center: {s4}"),
        (try_end),
        (try_begin),
          (gt, ":troop_object", 0),
          (neq, ":entry_type", logent_traveller_attacked),
          (neq, ":entry_type", logent_party_traded),
          
          (str_store_troop_name, s4, ":troop_object"),
          (display_message, "@{!}Troop: {s4}"),
        (try_end),
        (try_begin),
          (gt, ":center_object_lord", 0),
          (neq, ":entry_type", logent_traveller_attacked),
          (neq, ":entry_type", logent_party_traded),
          
          (str_store_troop_name, s4, ":center_object_lord"),
          (display_message, "@{!}Lord: {s4}"),
        (try_end),
      (try_end),
      
      
      (try_begin),
        (this_or_next|eq, ":entry_type", logent_lord_defeated_by_player),
        (this_or_next|eq, ":entry_type", logent_player_participated_in_major_battle),
        (eq, ":entry_type", logent_player_participated_in_siege),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "@{!}Ally party is present"),
        (try_end),
        (try_for_range, ":hero", active_npcs_begin, active_npcs_end),
          (party_count_companions_of_type, ":hero_present", "p_collective_friends", ":hero"),
          (gt, ":hero_present", 0),
          (troop_set_slot, ":hero", slot_troop_present_at_event, "$num_log_entries"),
          #         (store_sub, ":skip_up_to_here", "$num_log_entries", 1),
          #         (troop_set_slot, ":hero", slot_troop_last_comment_slot, ":skip_up_to_here"),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, s4, ":hero"),
            (display_message, "@{!}{s4} is present at event"),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  
  # script_get_relevant_comment_for_log_entry
  # Input: arg1 = log_entry_no,
  # Output: reg0 = comment_id; reg1 = relevance
  # Notes: 50 is the default relevance.
  # A comment with relevance less than 30 will always be skipped.
  # A comment with relevance 75 or more will never be skipped.
  # A comment with relevance 50 has about 50% chance to be skipped.
  # If there is more than one comment that is not skipped, the system will randomize their relevance values, and then choose the highest one.
  # Also note that the relevance of events decreases as time passes. After three months, relevance reduces to 50%, after 6 months, 25%, etc...
  ("get_relevant_comment_for_log_entry",
    [(store_script_param, ":log_entry_no", 1),
      
      (troop_get_slot, ":entry_type",            "trp_log_array_entry_type",            ":log_entry_no"),
      (troop_get_slot, ":entry_time",            "trp_log_array_entry_time",            ":log_entry_no"),
      (troop_get_slot, ":actor",                 "trp_log_array_actor",                 ":log_entry_no"),
      (troop_get_slot, ":center_object",         "trp_log_array_center_object",         ":log_entry_no"),
      (troop_get_slot, ":center_object_lord",    "trp_log_array_center_object_lord",    ":log_entry_no"),
      (troop_get_slot, ":center_object_faction", "trp_log_array_center_object_faction", ":log_entry_no"),
      (troop_get_slot, ":troop_object",          "trp_log_array_troop_object",          ":log_entry_no"),
      (troop_get_slot, ":troop_object_faction",  "trp_log_array_troop_object_faction",  ":log_entry_no"),
      #MOTO chief faction -1 fix
      #I can't figure out why this is happening, but it is
      #The ONLY way this can happen is if troop_get_faction in script_add_log_entry is somehow handed a factionless troop
      (val_max, ":troop_object_faction", 0),
      #MOTO end faction -1 fix
      (troop_get_slot, ":faction_object",        "trp_log_array_faction_object",        ":log_entry_no"),
      
      (assign, ":relevance", 0),
      (assign, ":comment", -1),
      (assign, ":rejoinder", -1),
      (assign, ":suggested_relation_change", 0),
      
      (troop_get_slot, ":reputation", "$g_talk_troop", slot_lord_reputation_type),
      (store_current_hours, ":current_time"),
      (store_sub, ":entry_hours_elapsed", ":current_time", ":entry_time"),
      
      #Post 0907 changes begin
      (assign, ":players_kingdom_relation", 0), ##the below is so that lords will not congratulate player on attacking neutrals
      (try_begin),
        (gt, "$players_kingdom", 0),
        (store_relation, ":players_kingdom_relation", "$players_kingdom", ":troop_object_faction"),
      (try_end),
      
      (try_begin),
        (eq, "$cheat_mode", -1), #temporarily disabled
        (try_begin),
          (assign, reg5, ":log_entry_no"),
          (assign, reg6, ":entry_type"),
          (assign, reg8, ":entry_time"),
          
          (gt, "$players_kingdom", 0),
          (try_begin),
            (gt, ":troop_object_faction", 0),
            (assign, reg7, ":players_kingdom_relation"),
            (display_message, "@{!}Event n{reg5}, type {reg6}, time {reg8}: player's kingdom relation to troop object = {reg7}"),
          (else_try),
            (gt, ":center_object_faction", 0),
            (assign, reg7, ":players_kingdom_relation"),
            (display_message, "@{!}Event n{reg5}, type {reg6}, time {reg8}: player's kingdom relation to center object faction = {reg7}"),
          (else_try),
            (gt, ":faction_object", 0),
            (assign, reg7, ":players_kingdom_relation"),
            (display_message, "@{!}Event n{reg5}, type {reg6}, time {reg8}: player's kingdom relation to faction object = {reg7}"),
          (else_try),
            (display_message, "@{!}Event n{reg5}, type {reg6}, time {reg8}. No relevant kingdom relation"),
          (try_end),
        (else_try),
          (display_message, "@{!}Event n{reg5}, type {reg6}, time {reg8}. Player unaffiliated"),
        (try_end),
      (try_end),
      
      (try_begin),
        (eq, ":entry_type", logent_game_start),
        (eq, "$g_talk_troop_met", 0),
        (is_between, "$g_talk_troop_faction_relation", -5, 5),
        (is_between, "$g_talk_troop_relation", -5, 5),
        (neq,"$g_talk_troop_faction", "fac_adventurers"),
        
        (assign, ":relevance", 25),
        (troop_get_slot, ":plyr_renown", "trp_player", slot_troop_renown),
        #normal_banner_begin
        (troop_get_slot, ":banner", "trp_player", slot_troop_banner_scene_prop),
        #custom_banner_begin
        #       (troop_get_slot, ":banner", "trp_player", slot_troop_custom_banner_flag_type),
        (store_random_in_range, ":renown_check", 100, 200),
        (try_begin),
          (eq, ":reputation", lrep_none),
          (gt, "$players_kingdom", 0),
          (assign, ":comment", "str_comment_intro_liege_affiliated"),
          (try_begin),
            (faction_slot_eq, "$players_kingdom", slot_faction_leader, "trp_player"),
            (assign, ":comment", "str_comment_intro_liege_affiliated_to_player"),
          (try_end),
        (else_try),
          (eq, "$character_gender",tf_female),
          
          (call_script, "script_troop_get_romantic_chemistry_with_troop", "$g_talk_troop", "trp_player"),
          (assign, ":attraction", reg0),
          (store_random_in_range, ":random", 0, 2),
          (this_or_next|eq, ":random", 0),
          (gt, ":attraction", 10),
          
          (try_begin),
            (this_or_next|gt, ":plyr_renown", ":renown_check"),
            (eq, "$g_disable_condescending_comments", 1),
            (assign, ":comment", "str_comment_intro_female_famous_liege"),
            (val_add, ":comment", ":reputation"),
          (else_try),
            (ge, ":attraction", 9),
            (assign, ":comment", "str_comment_intro_female_admiring_liege"),
            (val_add, ":comment", ":reputation"),
          (else_try),
            (gt, ":banner", 0),
            (assign, ":comment", "str_comment_intro_female_noble_liege"),
            (val_add, ":comment", ":reputation"),
          (else_try),
            (assign, ":comment", "str_comment_intro_female_common_liege"),
            (val_add, ":comment", ":reputation"),
          (try_end),
          #Rejoinders for comments
          
          (try_begin),
            (eq, ":comment", "str_comment_intro_female_common_badtempered"),
            (assign, ":rejoinder", "str_rejoinder_intro_female_common_badtempered"),
          (else_try),
            (eq, ":comment", "str_comment_intro_female_noble_pitiless"),
            (assign, ":rejoinder", "str_rejoinder_intro_female_noble_pitiless"),
          (else_try),
            (eq, ":comment", "str_comment_intro_female_common_pitiless"),
            (assign, ":rejoinder", "str_rejoinder_intro_female_common_pitiless"),
          (else_try),
            (eq, ":comment", "str_comment_intro_female_noble_sadistic"),
            (assign, ":rejoinder", "str_rejoinder_intro_female_noble_sadistic"),
          (else_try),
            (eq, ":comment", "str_comment_intro_female_common_sadistic"),
            (assign, ":rejoinder", "str_rejoinder_intro_female_common_sadistic"),
          (else_try),
            (eq, ":comment", "str_comment_intro_female_common_upstanding"),
            (assign, ":rejoinder", "str_rejoinder_intro_female_common_upstanding"),
          (else_try),
            (eq, ":comment", "str_comment_intro_female_noble_upstanding"),
            (assign, ":rejoinder", "str_rejoinder_intro_female_noble_upstanding"),
          (else_try),
            (eq, ":comment", "str_comment_intro_female_common_martial"),
            (assign, ":rejoinder", "str_rejoinder_intro_female_common_martial"),
          (else_try),
            (eq, ":comment", "str_comment_intro_female_sadistic_admiring"),
            (assign, ":rejoinder", "str_rejoinder_intro_female_sadistic_admiring"),
          (else_try),
            (eq, ":comment", "str_comment_intro_female_badtempered_admiring"),
            (assign, ":rejoinder", "str_rejoinder_intro_female_badtempered_admiring"),
          (else_try),
            (eq, ":comment", "str_comment_intro_female_pitiless_admiring"),
            (assign, ":rejoinder", "str_rejoinder_intro_female_pitiless_admiring"),
          (try_end),
        (else_try),
          #Male character or non-gendered comment
          (try_begin),
            (gt, ":plyr_renown", ":renown_check"),
            (assign, ":comment", "str_comment_intro_famous_liege"),
            (val_add, ":comment", ":reputation"),
          (else_try),
            (gt, ":banner", 0),
            (assign, ":comment", "str_comment_intro_noble_liege"),
            (val_add, ":comment", ":reputation"),
            (try_begin),
              (eq, ":comment", "str_comment_intro_noble_sadistic"),
              (assign, ":rejoinder", "str_rejoinder_intro_noble_sadistic"),
            (try_end),
          (else_try),
            (assign, ":comment", "str_comment_intro_common_liege"),
            (val_add, ":comment", ":reputation"),
          (try_end),
        (try_end),
        #Post 0907 changes end
        
      (else_try),
        (eq, ":entry_type", logent_village_raided),
        (eq, ":actor", "trp_player"),
        (try_begin),
          (eq, ":center_object_lord", "$g_talk_troop"),
          (assign, ":relevance", 200),
          (assign, ":suggested_relation_change", -1),
          (assign, ":comment", "str_comment_you_raided_my_village_default"),
          (try_begin),
            (lt, "$g_talk_troop_faction_relation", -5),
            (this_or_next|eq, ":reputation", lrep_goodnatured),
            (eq, ":reputation", lrep_upstanding),
            (assign, ":comment", "str_comment_you_raided_my_village_enemy_benevolent"),
          (else_try),
            (lt, "$g_talk_troop_faction_relation", -5),
            (this_or_next|eq, ":reputation", lrep_cunning),
            (eq, ":reputation", lrep_selfrighteous),
            (assign, ":comment", "str_comment_you_raided_my_village_enemy_coldblooded"),
          (else_try),
            (lt, "$g_talk_troop_faction_relation", -5),
            (this_or_next|eq, ":reputation", lrep_quarrelsome),
            (eq, ":reputation", lrep_debauched),
            (assign, ":comment", "str_comment_you_raided_my_village_enemy_spiteful"),
          (else_try),
            (lt, "$g_talk_troop_faction_relation", -5),
            (assign, ":comment", "str_comment_you_raided_my_village_enemy"),
          (else_try),
            (lt, "$g_talk_troop_relation", -5),
            (this_or_next|eq, ":reputation", lrep_quarrelsome),
            (eq, ":reputation", lrep_debauched),
            (assign, ":comment", "str_comment_you_raided_my_village_unfriendly_spiteful"),
          (else_try),
            (gt, "$g_talk_troop_relation", 5),
            (assign, ":comment", "str_comment_you_raided_my_village_friendly"),
          (try_end),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_village_extorted),
        (eq, ":actor", "trp_player"),
        (try_begin),
          (eq, ":center_object_lord", "$g_talk_troop"),
          (assign, ":relevance", 30),
          (assign, ":suggested_relation_change", -1),
          (assign, ":comment", "str_comment_you_robbed_my_village_default"),
          (try_begin),
            (lt, "$g_talk_troop_faction_relation", -5),
            (this_or_next|eq, ":reputation", lrep_cunning),
            (eq, ":reputation", lrep_selfrighteous),
            (assign, ":comment", "str_comment_you_robbed_my_village_enemy_coldblooded"),
          (else_try),
            (lt, "$g_talk_troop_faction_relation", -5),
            (assign, ":comment", "str_comment_you_robbed_my_village_enemy"),
          (else_try),
            (gt, "$g_talk_troop_relation", 5),
            (this_or_next|eq, ":reputation", lrep_quarrelsome),
            (eq, ":reputation", lrep_debauched),
            (assign, ":comment", "str_comment_you_robbed_my_village_friendly_spiteful"),
          (else_try),
            (gt, "$g_talk_troop_relation", 5),
            (assign, ":comment", "str_comment_you_robbed_my_village_friendly"),
          (try_end),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_caravan_accosted),
        (eq, ":actor", "trp_player"),
        (eq, ":faction_object", "$g_talk_troop_faction"),
        (eq, ":center_object", -1),
        (eq, ":troop_object", -1),
        
        (faction_slot_eq, "$g_talk_troop_faction", slot_faction_leader, "$g_talk_troop"),
        (assign, ":relevance", 30),
        (assign, ":suggested_relation_change", -1),
        (assign, ":comment", "str_comment_you_accosted_my_caravan_default"),
        (try_begin),
          (lt, "$g_talk_troop_faction_relation", -5),
          (assign, ":comment", "str_comment_you_accosted_my_caravan_enemy"),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_helped_peasants),
        (eq, ":actor", "trp_player"),
        (try_begin),
          (eq, ":center_object_lord", "$g_talk_troop"),
          (assign, ":relevance", 40),
          (assign, ":suggested_relation_change", 0),
          (try_begin),
            (this_or_next|eq, ":reputation", lrep_goodnatured),
            (eq, ":reputation", lrep_upstanding),
            (assign, ":comment", "str_comment_you_helped_villagers_benevolent"),
            (assign, ":suggested_relation_change", 1),
          (else_try),
            (gt, "$g_talk_troop_relation", 5),
            (this_or_next|eq, ":reputation", lrep_quarrelsome),
            (eq, ":reputation", lrep_debauched),
            (assign, ":comment", "str_comment_you_helped_villagers_friendly_cruel"),
            (assign, ":suggested_relation_change", -1),
          (else_try),
            (lt, "$g_talk_troop_relation", -5),
            (this_or_next|eq, ":reputation", lrep_quarrelsome),
            (eq, ":reputation", lrep_debauched),
            (assign, ":comment", "str_comment_you_helped_villagers_unfriendly_spiteful"),
            (assign, ":suggested_relation_change", -1),
          (else_try),
            (gt, "$g_talk_troop_relation", 5),
            (assign, ":comment", "str_comment_you_helped_villagers_friendly"),
          (else_try),
            (this_or_next|eq, ":reputation", lrep_selfrighteous),
            (eq, ":reputation", lrep_debauched),
            (assign, ":comment", "str_comment_you_helped_villagers_cruel"),
            (assign, ":suggested_relation_change", -1),
          (else_try),
            (assign, ":comment", "str_comment_you_helped_villagers_default"),
          (try_end),
        (try_end),
        
        ###Combat events
      (else_try),
        (eq, ":entry_type", logent_castle_captured_by_player),
        
        (try_begin),
          (eq, ":center_object_lord", "$g_talk_troop"),
          (store_faction_of_party, ":current_center_faction", ":center_object"),
          (eq, ":current_center_faction", "$players_kingdom"),
          (neq, "$g_talk_troop_faction", "$players_kingdom"),
          
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          
          (assign, ":comment", "str_comment_you_captured_my_castle_enemy_spiteful"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":center_object_lord", "$g_talk_troop"),
          (store_faction_of_party, ":current_center_faction", ":center_object"),
          (eq, ":current_center_faction", "$players_kingdom"),
          (neq, "$g_talk_troop_faction", "$players_kingdom"),
          
          (this_or_next|eq, ":reputation", lrep_martial),
          (eq, ":reputation", lrep_goodnatured),
          
          (assign, ":comment", "str_comment_you_captured_my_castle_enemy_chivalrous"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":center_object_lord", "$g_talk_troop"),
          (store_faction_of_party, ":current_center_faction", ":center_object"),
          (eq, ":current_center_faction", "$players_kingdom"),
          (neq, "$g_talk_troop_faction", "$players_kingdom"),
          
          (assign, ":comment", "str_comment_you_captured_my_castle_enemy"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_captured_a_castle_allied_spiteful"),
          (assign, ":relevance", 75),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (gt, "$g_talk_troop_relation", 5),
          (assign, ":comment", "str_comment_you_captured_a_castle_allied_friendly"),
          (assign, ":relevance", 75),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_captured_a_castle_allied_unfriendly_spiteful"),
          (assign, ":relevance", 75),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (lt, "$g_talk_troop_relation", -5),
          (assign, ":comment", "str_comment_you_captured_a_castle_allied_unfriendly"),
          (assign, ":relevance", 75),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (assign, ":comment", "str_comment_you_captured_a_castle_allied"),
          (assign, ":relevance", 75),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_player_claims_throne_1),
        (eq, "$players_kingdom", "$g_talk_troop_faction"),
        (faction_slot_eq, "$g_talk_troop_faction", slot_faction_leader, "$g_talk_troop"),
        (assign, ":comment", "str_comment_you_claimed_the_throne_1_player_liege"),
        (assign, ":relevance", 500),
        (lt, "$g_talk_troop_relation", -10),
        
      (else_try),
        (eq, ":entry_type", logent_player_claims_throne_2),
        (eq, "$players_kingdom", "$g_talk_troop_faction"),
        (faction_slot_eq, "$g_talk_troop_faction", slot_faction_leader, "$g_talk_troop"),
        (assign, ":comment", "str_comment_you_claimed_the_throne_2_player_liege"),
        (assign, ":relevance", 500),
        (lt, "$g_talk_troop_relation", -10),
        
      (else_try), #player appointed a commoner
        (eq, ":entry_type", logent_liege_grants_fief_to_vassal),
        (eq, ":actor", "trp_player"),
        (troop_slot_ge, ":troop_object", slot_lord_reputation_type, lrep_roguish),
        (try_begin),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_enfiefed_a_commoner_nasty"),
          (assign, ":relevance", 100),
          (assign, ":suggested_relation_change", -3),
          
        (else_try),
          (eq, ":reputation", lrep_upstanding),
          (assign, ":comment", "str_comment_you_enfiefed_a_commoner_hesitant"),
          (assign, ":relevance", 100),
          (assign, ":suggested_relation_change", -2),
          
        (else_try),
          (this_or_next|eq, ":reputation", lrep_selfrighteous),
          (eq, ":reputation", lrep_martial),
          (assign, ":comment", "str_comment_you_enfiefed_a_commoner_derisive"),
          (assign, ":relevance", 100),
          (assign, ":suggested_relation_change", -4),
          
        (try_end),
        
        #Post 0907 changes begin
      (else_try),
        (this_or_next|eq, ":entry_type", logent_lord_defeated_by_player),
        (eq, ":entry_type", logent_lord_helped_by_player),
        (troop_slot_eq, "$g_talk_troop", slot_troop_present_at_event, ":log_entry_no"),
        (try_begin),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_we_defeated_a_lord_unfriendly_spiteful"),
          (assign, ":relevance", 150),
        (else_try),
          (lt, "$g_talk_troop_relation", -5),
          (assign, ":comment", "str_comment_we_defeated_a_lord_unfriendly"),
          (assign, ":relevance", 150),
        (else_try),
          (this_or_next|eq, ":reputation", lrep_selfrighteous),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_we_defeated_a_lord_cruel"),
          (assign, ":relevance", 150),
        (else_try),
          (eq, ":reputation", lrep_quarrelsome),
          (assign, ":comment", "str_comment_we_defeated_a_lord_cruel"),
          (assign, ":relevance", 150),
        (else_try),
          (eq, ":reputation", lrep_upstanding),
          (assign, ":comment", "str_comment_we_defeated_a_lord_upstanding"),
          (assign, ":relevance", 150),
        (else_try),
          (assign, ":comment", "str_comment_we_defeated_a_lord_default"),
          (assign, ":relevance", 150),
        (try_end),
        
      (else_try),
        (this_or_next|eq, ":entry_type", logent_castle_captured_by_player),
        (eq, ":entry_type", logent_player_participated_in_siege),
        
        (troop_slot_eq, "$g_talk_troop", slot_troop_present_at_event, ":log_entry_no"),
        
        (try_begin),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_we_fought_in_siege_unfriendly_spiteful"),
          (assign, ":relevance", 150),
        (else_try),
          (lt, "$g_talk_troop_relation", -5),
          (assign, ":comment", "str_comment_we_fought_in_siege_unfriendly"),
          (assign, ":relevance", 150),
        (else_try),
          (this_or_next|eq, ":reputation", lrep_selfrighteous),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_we_fought_in_siege_cruel"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", 1),
        (else_try),
          (eq, ":reputation", lrep_quarrelsome),
          (assign, ":comment", "str_comment_we_fought_in_siege_quarrelsome"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", 1),
        (else_try),
          (eq, ":reputation", lrep_upstanding),
          (assign, ":comment", "str_comment_we_fought_in_siege_upstanding"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", 1),
        (else_try),
          (eq, ":reputation", lrep_martial),
          (assign, ":comment", "str_comment_we_fought_in_siege_default"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", 2),
        (else_try),
          (faction_slot_eq, "$players_kingdom", slot_faction_leader, "$g_talk_troop"),
          (assign, ":comment", "str_comment_we_fought_in_siege_default"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", 1),
        (else_try),
          (assign, ":comment", "str_comment_we_fought_in_siege_default"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", 1),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_castle_given_to_lord_by_player),
        
        (try_begin),
          (eq, ":troop_object", "$g_talk_troop"),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_give_castle_in_my_control"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (this_or_next|eq, ":reputation", lrep_martial),
          (eq, ":reputation", lrep_goodnatured),
          (assign, ":comment", "str_comment_you_give_castle_in_my_control"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (assign, ":comment", "str_comment_you_give_castle_in_my_control"),
          (assign, ":relevance", 200),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_player_participated_in_major_battle),
        (troop_slot_eq, "$g_talk_troop", slot_troop_present_at_event, ":log_entry_no"),
        (try_begin),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_we_fought_in_major_battle_unfriendly_spiteful"),
          (assign, ":relevance", 150),
        (else_try),
          (lt, "$g_talk_troop_relation", -5),
          (assign, ":comment", "str_comment_we_fought_in_major_battle_unfriendly"),
          (assign, ":relevance", 150),
        (else_try),
          (this_or_next|eq, ":reputation", lrep_selfrighteous),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_we_fought_in_major_battle_cruel"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", 1),
        (else_try),
          (eq, ":reputation", lrep_quarrelsome),
          (assign, ":comment", "str_comment_we_fought_in_major_battle_cruel"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", 1),
        (else_try),
          (eq, ":reputation", lrep_upstanding),
          (assign, ":comment", "str_comment_we_fought_in_major_battle_upstanding"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", 1),
        (else_try),
          (faction_slot_eq, "$players_kingdom", slot_faction_leader, "$g_talk_troop"),
          (assign, ":comment", "str_comment_we_fought_in_major_battle_default"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", 1),
        (else_try),
          (eq, ":reputation", lrep_martial),
          (assign, ":comment", "str_comment_we_fought_in_major_battle_default"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", 2),
        (else_try),
          (assign, ":comment", "str_comment_we_fought_in_major_battle_default"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", 1),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_player_suggestion_succeeded),
        (try_begin),
          (eq, ":troop_object", "$g_talk_troop"),
          (assign, ":comment", "str_comment_player_suggestion_succeeded"),
          (assign, ":relevance", 200),
          (assign, ":suggested_relation_change", 3),
          
        (try_end),
      (else_try),
        (eq, ":entry_type", logent_player_suggestion_failed),
        (try_begin),
          (eq, ":troop_object", "$g_talk_troop"),
          (assign, ":comment", "str_comment_player_suggestion_failed"),
          (assign, ":relevance", 200),
          (assign, ":suggested_relation_change", -5),
          
        (try_end),
        
        #Post 0907 changes end
        
      (else_try),
        (eq, ":entry_type", logent_lord_defeated_by_player),
        (try_begin),
          (eq, ":troop_object", "$g_talk_troop"),
          (this_or_next|eq, ":reputation", lrep_martial),
          (eq, ":reputation", lrep_goodnatured),
          (assign, ":comment", "str_comment_you_defeated_me_enemy_chivalrous"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (this_or_next|eq, ":reputation", lrep_debauched),
          (eq, ":reputation", lrep_quarrelsome),
          (assign, ":comment", "str_comment_you_defeated_me_enemy_spiteful"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (assign, ":comment", "str_comment_you_defeated_me_enemy"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":troop_object_faction", "$g_talk_troop_faction"),
          (this_or_next|eq, ":reputation", lrep_upstanding),
          (eq, ":reputation", lrep_cunning),
          (assign, ":comment", "str_comment_you_defeated_my_friend_enemy_pragmatic"),
          (assign, ":relevance", 85),
        (else_try),
          (eq, ":troop_object_faction", "$g_talk_troop_faction"),
          (this_or_next|eq, ":reputation", lrep_martial),
          (eq, ":reputation", lrep_goodnatured),
          (assign, ":comment", "str_comment_you_defeated_my_friend_enemy_chivalrous"),
          (assign, ":relevance", 85),
        (else_try),
          (eq, ":troop_object_faction", "$g_talk_troop_faction"),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_defeated_my_friend_enemy_spiteful"),
          (assign, ":relevance", 85),
        (else_try),
          (eq, ":troop_object_faction", "$g_talk_troop_faction"),
          (assign, ":comment", "str_comment_you_defeated_my_friend_enemy"),
          (assign, ":relevance", 85),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (faction_slot_eq, "$players_kingdom", slot_faction_leader, "$g_talk_troop"),
          (assign, ":comment", "str_comment_you_defeated_a_lord_allied_liege"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", 1),
          
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_defeated_a_lord_allied_unfriendly_spiteful"),
          (assign, ":relevance", 65),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_defeated_a_lord_allied_spiteful"),
          (assign, ":relevance", 65),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_upstanding),
          (eq, ":reputation", lrep_martial),
          (assign, ":comment", "str_comment_you_defeated_a_lord_allied_unfriendly_chivalrous"),
          (assign, ":relevance", 65),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (assign, ":comment", "str_comment_you_defeated_a_lord_allied"),
          (assign, ":relevance", 65),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_lord_defeated_by_player),
        (try_begin),
          (eq, ":troop_object", "$g_talk_troop"),
          (this_or_next|eq, ":reputation", lrep_martial),
          (eq, ":reputation", lrep_goodnatured),
          (assign, ":comment", "str_comment_you_defeated_me_enemy_chivalrous"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (this_or_next|eq, ":reputation", lrep_debauched),
          (eq, ":reputation", lrep_quarrelsome),
          (assign, ":comment", "str_comment_you_defeated_me_enemy_spiteful"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (assign, ":comment", "str_comment_you_defeated_me_enemy"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":troop_object_faction", "$g_talk_troop_faction"),
          (this_or_next|eq, ":reputation", lrep_upstanding),
          (eq, ":reputation", lrep_cunning),
          (assign, ":comment", "str_comment_you_defeated_my_friend_enemy_pragmatic"),
          (assign, ":relevance", 85),
        (else_try),
          (eq, ":troop_object_faction", "$g_talk_troop_faction"),
          (this_or_next|eq, ":reputation", lrep_martial),
          (eq, ":reputation", lrep_goodnatured),
          (assign, ":comment", "str_comment_you_defeated_my_friend_enemy_chivalrous"),
          (assign, ":relevance", 85),
        (else_try),
          (eq, ":troop_object_faction", "$g_talk_troop_faction"),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_defeated_my_friend_enemy_spiteful"),
          (assign, ":relevance", 85),
        (else_try),
          (eq, ":troop_object_faction", "$g_talk_troop_faction"),
          (assign, ":comment", "str_comment_you_defeated_my_friend_enemy"),
          (assign, ":relevance", 85),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (faction_slot_eq, "$players_kingdom", slot_faction_leader, "$g_talk_troop"),
          (assign, ":comment", "str_comment_you_defeated_a_lord_allied_liege"),
          (assign, ":relevance", 70),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_defeated_a_lord_allied_unfriendly_spiteful"),
          (assign, ":relevance", 65),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_defeated_a_lord_allied_spiteful"),
          (assign, ":relevance", 65),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_upstanding),
          (eq, ":reputation", lrep_martial),
          (assign, ":comment", "str_comment_you_defeated_a_lord_allied_unfriendly_chivalrous"),
          (assign, ":relevance", 65),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (assign, ":comment", "str_comment_you_defeated_a_lord_allied"),
          (assign, ":relevance", 65),
        (try_end),
        
        #Post 0907 changes begin
      (else_try),
        (eq, ":entry_type", logent_lord_helped_by_player),
        (neq, ":troop_object", "$g_talk_troop"),
        (eq, ":troop_object_faction", "$g_talk_troop_faction"),
        (try_begin),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_upstanding),
          (eq, ":reputation", lrep_martial),
          (assign, ":comment", "str_comment_you_helped_my_ally_unfriendly_chivalrous"),
          (assign, ":relevance", 65),
          (assign, ":suggested_relation_change", 2),
        (else_try),
          (lt, "$g_talk_troop_relation", -5),
          (assign, ":comment", "str_comment_you_helped_my_ally_unfriendly"),
          (assign, ":relevance", 0),
        (else_try),
          (eq, ":reputation", lrep_none),
          (assign, ":comment", "str_comment_you_helped_my_ally_liege"),
          (assign, ":relevance", 65),
          (assign, ":suggested_relation_change", 3),
        (else_try),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_helped_my_ally_unfriendly_spiteful"),
          (assign, ":relevance", 65),
        (else_try),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_helped_my_ally_spiteful"),
          (assign, ":relevance", 65),
        (else_try),
          (this_or_next|eq, ":reputation", lrep_martial),
          (eq, ":reputation", lrep_upstanding),
          (assign, ":comment", "str_comment_you_helped_my_ally_chivalrous"),
          (assign, ":relevance", 65),
          (assign, ":suggested_relation_change", 2),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (assign, ":comment", "str_comment_you_helped_my_ally_default"),
        (try_end),
        
        #Post 0907 changes begin
      (else_try),
        (eq, ":entry_type", logent_player_defeated_by_lord),
        (troop_slot_eq, "$g_talk_troop", slot_troop_present_at_event, ":log_entry_no"),
        (try_begin),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_we_were_defeated_unfriendly_spiteful"),
          (assign, ":relevance", 150),
        (else_try),
          (lt, "$g_talk_troop_relation", -5),
          (assign, ":comment", "str_comment_we_were_defeated_unfriendly"),
          (assign, ":relevance", 150),
        (else_try),
          (this_or_next|eq, ":reputation", lrep_selfrighteous),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_we_were_defeated_cruel"),
          (assign, ":relevance", 150),
        (else_try),
          (assign, ":comment", "str_comment_we_were_defeated_default"),
          (assign, ":relevance", 150),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_player_defeated_by_lord),
        (try_begin),
          (eq, ":troop_object", "$g_talk_troop"),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_I_defeated_you_enemy_spiteful"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (eq, ":reputation", lrep_martial),
          (assign, ":comment", "str_comment_I_defeated_you_enemy_chivalrous"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (this_or_next|eq, ":reputation", lrep_goodnatured),
          (eq, ":reputation", lrep_upstanding),
          (assign, ":comment", "str_comment_I_defeated_you_enemy_benevolent"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (this_or_next|eq, ":reputation", lrep_selfrighteous),
          (eq, ":reputation", lrep_cunning),
          (assign, ":comment", "str_comment_I_defeated_you_enemy_coldblooded"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (assign, ":comment", "str_comment_I_defeated_you_enemy"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (assign, ":comment", "str_comment_I_defeated_you_enemy"),
          (assign, ":relevance", 200),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (gt, "$g_talk_troop_relation", 5),
          (assign, ":comment", "str_comment_you_were_defeated_allied_friendly_spiteful"),
          (assign, ":relevance", 80),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (this_or_next|eq, ":reputation", lrep_selfrighteous),
          (eq, ":reputation", lrep_debauched),
          (lt, "$g_talk_troop_relation", -5),
          (assign, ":comment", "str_comment_you_were_defeated_allied_unfriendly_cruel"),
          (assign, ":relevance", 80),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (le, "$g_talk_troop_relation", 5),
          (assign, ":comment", "str_comment_you_were_defeated_allied_spiteful"),
          (assign, ":relevance", 80),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (eq, ":reputation", lrep_selfrighteous),
          (assign, ":comment", "str_comment_you_were_defeated_allied_pitiless"),
          (assign, ":relevance", 65),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (eq, ":reputation", lrep_upstanding),
          (lt, "$g_talk_troop_relation", -15),
          (assign, ":comment", "str_comment_you_were_defeated_allied_unfriendly_upstanding"),
          (assign, ":relevance", 65),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, "$g_talk_troop_relation", -10),
          (assign, ":comment", "str_comment_you_were_defeated_allied_unfriendly"),
          (assign, ":relevance", 65),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (assign, ":comment", "str_comment_you_were_defeated_allied"),
          (assign, ":relevance", 65),
        (try_end),
        #Post 0907 changes end
        
        #Post 0907 changes begin
      (else_try),
        (eq, ":entry_type", logent_player_retreated_from_lord),
        (troop_slot_eq, "$g_talk_troop", slot_troop_present_at_event, ":log_entry_no"),
        (try_begin),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_abandoned_us_unfriendly_spiteful"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", -5),
        (else_try),
          (lt, "$g_talk_troop_relation", -5),
          (eq, ":reputation", lrep_selfrighteous),
          (assign, ":comment", "str_comment_you_abandoned_us_unfriendly_pitiless"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", -5),
        (else_try),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_abandoned_us_spiteful"),
          (assign, ":suggested_relation_change", -5),
        (else_try),
          (eq, ":reputation", lrep_martial),
          (assign, ":comment", "str_comment_you_abandoned_us_chivalrous"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", -2),
        (else_try),
          (this_or_next|eq, ":reputation", lrep_upstanding),
          (eq, ":reputation", lrep_goodnatured),
          (assign, ":comment", "str_comment_you_abandoned_us_benefitofdoubt"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", -1),
        (else_try),
          (assign, ":comment", "str_comment_you_abandoned_us_default"),
          (assign, ":relevance", 150),
          (assign, ":suggested_relation_change", -2),
        (try_end),
        
        
        #Post 0907 changes end
        
      (else_try),
        (this_or_next|eq, ":entry_type", logent_player_retreated_from_lord),
        (eq, ":entry_type", logent_player_retreated_from_lord_cowardly),
        
        (eq, ":troop_object", "$g_talk_troop"),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (assign, reg7, ":entry_hours_elapsed"),
          (display_message, "@{!}Elapsed hours: {reg7}"),
        (try_end),
        (gt, ":entry_hours_elapsed", 2),
        (try_begin),
          (this_or_next|eq, ":reputation", lrep_selfrighteous),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_ran_from_me_enemy_spiteful"),
          (assign, ":relevance", 25),
        (else_try),
          (eq, ":reputation", lrep_martial),
          (assign, ":comment", "str_comment_you_ran_from_me_enemy_chivalrous"),
          (assign, ":relevance", 25),
        (else_try),
          (this_or_next|eq, ":reputation", lrep_goodnatured),
          (eq, ":reputation", lrep_upstanding),
          (assign, ":comment", "str_comment_you_ran_from_me_enemy_benevolent"),
          (assign, ":relevance", 25),
        (else_try),
          (eq, ":reputation", lrep_cunning),
          (assign, ":comment", "str_comment_you_ran_from_me_enemy_coldblooded"),
          (assign, ":relevance", 25),
        (else_try),
          (assign, ":comment", "str_comment_you_ran_from_me_enemy"),
          (assign, ":relevance", 25),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_player_retreated_from_lord_cowardly),
        (try_begin),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (neq, ":troop_object", "$g_talk_troop"),
          (lt, "$g_talk_troop_relation", 5),
          (eq, ":reputation", lrep_martial),
          (assign, ":comment", "str_comment_you_ran_from_foe_allied_chivalrous"),
          (assign, ":relevance", 80),
          (assign, ":suggested_relation_change", -3),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (neq, ":troop_object", "$g_talk_troop"),
          (eq, ":reputation", lrep_upstanding),
          (assign, ":comment", "str_comment_you_ran_from_foe_allied_upstanding"),
          (assign, ":relevance", 80),
          (assign, ":suggested_relation_change", -1),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (neq, ":troop_object", "$g_talk_troop"),
          (lt, "$g_talk_troop_relation", 5),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_ran_from_foe_allied_spiteful"),
          (assign, ":relevance", 80),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_lord_defeated_but_let_go_by_player),
        (try_begin),
          (eq, ":troop_object", "$g_talk_troop"),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_let_me_go_spiteful"),
          (assign, ":relevance", 300),
          (assign, ":suggested_relation_change", -15),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (ge, "$g_talk_troop_faction_relation", 0),
          (assign, ":comment", "str_comment_you_let_me_go_default"),
          (assign, ":relevance", 300),
          (assign, ":suggested_relation_change", 2),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (lt, "$g_talk_troop_faction_relation", 0),
          (this_or_next|eq, ":reputation", lrep_martial),
          (eq, ":reputation", lrep_upstanding),
          (assign, ":suggested_relation_change", 5),
          (assign, ":relevance", 300),
          (assign, ":comment", "str_comment_you_let_me_go_enemy_chivalrous"),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (lt, "$g_talk_troop_faction_relation", 0),
          (this_or_next|eq, ":reputation", lrep_selfrighteous),
          (eq, ":reputation", lrep_cunning),
          (assign, ":relevance", 300),
          (assign, ":comment", "str_comment_you_let_me_go_enemy_coldblooded"),
        (else_try),
          (eq, ":troop_object", "$g_talk_troop"),
          (lt, "$g_talk_troop_faction_relation", 0),
          (assign, ":relevance", 300),
          (assign, ":comment", "str_comment_you_let_me_go_enemy"),
          (assign, ":suggested_relation_change", 1),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (neq, ":troop_object", "$g_talk_troop"),
          (this_or_next|eq, ":reputation", lrep_martial),
          (eq, ":reputation", lrep_goodnatured),
          (assign, ":comment", "str_comment_you_let_go_a_lord_allied_chivalrous"),
          (assign, ":relevance", 80),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (neq, ":troop_object", "$g_talk_troop"),
          (eq, ":reputation", lrep_upstanding),
          (assign, ":comment", "str_comment_you_let_go_a_lord_allied_upstanding"),
          (assign, ":relevance", 80),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (neq, ":troop_object", "$g_talk_troop"),
          (this_or_next|eq, ":reputation", lrep_cunning),
          (eq, ":reputation", lrep_selfrighteous),
          (assign, ":comment", "str_comment_you_let_go_a_lord_allied_coldblooded"),
          (assign, ":relevance", 80),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (neq, ":troop_object", "$g_talk_troop"),
          (lt, "$g_talk_troop_relation", -5),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (assign, ":comment", "str_comment_you_let_go_a_lord_allied_unfriendly_spiteful"),
          (assign, ":relevance", 80),
        (else_try),
          (eq, "$players_kingdom", "$g_talk_troop_faction"),
          (lt, ":players_kingdom_relation", 0),
          (neq, ":troop_object", "$g_talk_troop"),
          (assign, ":comment", "str_comment_you_let_go_a_lord_allied"),
          (assign, ":relevance", 80),
        (try_end),
        
        #Internal faction relations
        
      (else_try),
        (eq, ":entry_type", logent_pledged_allegiance),
        (eq, ":actor", "trp_player"),
        (try_begin),
          (eq, ":faction_object", "$g_talk_troop_faction"),
          (neq, ":troop_object", "$g_talk_troop"),
          (eq, ":faction_object", "$players_kingdom"), #Ie, no switch of kingdoms
          (assign, ":relevance", 200),
          (try_begin),
            (lt, "$g_talk_troop_relation", -5),
            (eq, ":reputation", lrep_martial),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_martial_unfriendly"),
          (else_try),
            (eq, ":reputation", lrep_martial),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_martial"),
          (else_try),
            (lt, "$g_talk_troop_relation", -5),
            (eq, ":reputation", lrep_quarrelsome),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_quarrelsome_unfriendly"),
          (else_try),
            (eq, ":reputation", lrep_quarrelsome),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_quarrelsome"),
          (else_try),
            (lt, "$g_talk_troop_relation", -5),
            (eq, ":reputation", lrep_selfrighteous),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_selfrighteous_unfriendly"),
          (else_try),
            (eq, ":reputation", lrep_selfrighteous),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_selfrighteous"),
          (else_try),
            (lt, "$g_talk_troop_relation", -5),
            (eq, ":reputation", lrep_cunning),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_cunning_unfriendly"),
          (else_try),
            (eq, ":reputation", lrep_cunning),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_cunning"),
          (else_try),
            (lt, "$g_talk_troop_relation", -5),
            (eq, ":reputation", lrep_debauched),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_debauched_unfriendly"),
          (else_try),
            (eq, ":reputation", lrep_debauched),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_debauched"),
          (else_try),
            (lt, "$g_talk_troop_relation", -5),
            (eq, ":reputation", lrep_goodnatured),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_goodnatured_unfriendly"),
          (else_try),
            (eq, ":reputation", lrep_goodnatured),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_goodnatured"),
          (else_try),
            (lt, "$g_talk_troop_relation", -5),
            (eq, ":reputation", lrep_upstanding),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_upstanding_unfriendly"),
          (else_try),
            (eq, ":reputation", lrep_upstanding),
            (assign, ":comment", "str_comment_pledged_allegiance_allied_upstanding"),
          (try_end),
        (try_end),
        
        
      (else_try),
        (eq, ":entry_type", logent_liege_grants_fief_to_vassal),
        (eq, ":troop_object", "trp_player"),
        (try_begin),
          (eq, ":faction_object", "$g_talk_troop_faction"),
          (neq, ":actor", "$g_talk_troop"),
          (eq, ":faction_object", "$players_kingdom"),
          (assign, ":relevance", 110),
          (try_begin),
            (gt, "$g_talk_troop_relation", 5),
            (this_or_next|eq, ":reputation", lrep_selfrighteous),
            (eq, ":reputation", lrep_debauched),
            (assign, ":comment", "str_comment_our_king_granted_you_a_fief_allied_friendly_cruel"),
          (else_try),
            (gt, "$g_talk_troop_relation", 5),
            (this_or_next|eq, ":reputation", lrep_quarrelsome),
            (eq, ":reputation", lrep_cunning),
            (assign, ":comment", "str_comment_our_king_granted_you_a_fief_allied_friendly_cynical"),
          (else_try),
            (gt, "$g_talk_troop_relation", 5),
            (assign, ":comment", "str_comment_our_king_granted_you_a_fief_allied_friendly"),
          (else_try),
            (is_between, "$g_talk_troop_relation", -5, 5),
            (this_or_next|eq, ":reputation", lrep_quarrelsome),
            (eq, ":reputation", lrep_debauched),
            (assign, ":comment", "str_comment_our_king_granted_you_a_fief_allied_spiteful"),
            (assign, ":suggested_relation_change", -2),
          (else_try),
            (lt, "$g_talk_troop_relation", -5),
            (eq, ":reputation", lrep_upstanding),
            (assign, ":comment", "str_comment_our_king_granted_you_a_fief_allied_unfriendly_upstanding"),
          (else_try),
            (lt, "$g_talk_troop_relation", -5),
            (this_or_next|eq, ":reputation", lrep_quarrelsome),
            (eq, ":reputation", lrep_debauched),
            (assign, ":comment", "str_comment_our_king_granted_you_a_fief_allied_unfriendly_spiteful"),
          (else_try),
            (assign, ":comment", "str_comment_our_king_granted_you_a_fief_allied"),
          (try_end),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_renounced_allegiance),
        (eq, ":actor", "trp_player"),
        (try_begin),
          (eq, ":faction_object", "$g_talk_troop_faction"),
          (neq, ":troop_object", "$g_talk_troop"),
          (try_begin),
            (ge, "$g_talk_troop_faction_relation", 0),
            (neq, "$g_talk_troop_faction", "$players_kingdom"),
            (assign, ":relevance", 180),
            (try_begin),
              (gt, "$g_talk_troop_relation", 5),
              (assign, ":comment", "str_comment_you_renounced_your_alliegance_friendly"),
            (else_try),
              (ge, "$g_talk_troop_relation", 0),
              (eq, ":reputation", lrep_goodnatured),
              (assign, ":comment", "str_comment_you_renounced_your_alliegance_friendly"),
            (else_try),
              (assign, ":comment", "str_comment_you_renounced_your_alliegance_default"),
            (try_end),
          (else_try),
            (lt, "$g_talk_troop_faction_relation", 0),
            (assign, ":relevance", 300),
            (try_begin),
              (ge, "$g_talk_troop_relation", 0),
              (this_or_next|eq, ":reputation", lrep_selfrighteous),
              (eq, ":reputation", lrep_debauched),
              (assign, ":comment", "str_comment_you_renounced_your_alliegance_unfriendly_moralizing"),
            (else_try),
              (gt, "$g_talk_troop_relation", 5),
              (this_or_next|eq, ":reputation", lrep_goodnatured),
              (eq, ":reputation", lrep_upstanding),
              (assign, ":comment", "str_comment_you_renounced_your_alliegance_enemy_friendly"),
            (else_try),
              (gt, "$g_talk_troop_relation", 5),
              (assign, ":comment", "str_comment_you_renounced_your_alliegance_enemy"),
            (else_try),
              (is_between, "$g_talk_troop_relation", -5, 5),
              (this_or_next|eq, ":reputation", lrep_quarrelsome),
              (eq, ":reputation", lrep_debauched),
              (assign, ":comment", "str_comment_you_renounced_your_alliegance_unfriendly_spiteful"),
              (assign, ":suggested_relation_change", -2),
            (else_try),
              (lt, "$g_talk_troop_relation", -5),
              (this_or_next|eq, ":reputation", lrep_quarrelsome),
              (this_or_next|eq, ":reputation", lrep_selfrighteous),
              (eq, ":reputation", lrep_debauched),
              (assign, ":comment", "str_comment_you_renounced_your_alliegance_unfriendly_spiteful"),
            (else_try),
              (assign, ":comment", "str_comment_you_renounced_your_alliegance_default"),
            (try_end),
          (try_end),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_lady_marries_lord),
        (eq, ":troop_object", "trp_player"),
        (try_begin),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          (lt, "$g_talk_troop_relation", -5),
          (assign, ":relevance", 200),
          (assign, ":comment", "str_comment_marriage_normal_nasty"),
        (else_try),
          (call_script, "script_troop_get_family_relation_to_troop", ":actor", "$g_talk_troop"),
          (ge, reg0, 5),
          (assign, ":comment", "str_comment_marriage_normal_family"),
          (assign, ":relevance", 300),
          (assign, ":suggested_relation_change", reg0),
          (val_div, ":suggested_relation_change", 3),
        (else_try),
          (store_faction_of_troop, ":bride_faction", ":actor"),
          (eq, ":bride_faction", "$g_talk_troop_faction"),
          (assign, ":comment", "str_comment_marriage_normal"),
          (assign, ":relevance", 100),
        (try_end),
      (else_try),
        (eq, ":entry_type", logent_lady_elopes_with_lord),
        (eq, ":troop_object", "trp_player"),
        (try_begin),
          (call_script, "script_troop_get_family_relation_to_troop", ":actor", "$g_talk_troop"),
          (ge, reg0, 5),
          (assign, ":comment", "str_comment_marriage_elopement_family"),
          (assign, ":relevance", 300),
          (store_sub, ":suggested_relation_change", 0, reg0),
          (val_div, ":suggested_relation_change", 3),
        (else_try),
          (store_faction_of_troop, ":bride_faction", ":actor"),
          (eq, ":bride_faction", "$g_talk_troop_faction"),
          (faction_slot_eq, ":bride_faction", slot_faction_leader, "$g_talk_troop"),
          (assign, ":comment", "str_comment_marriage_elopement_liege"),
          (assign, ":relevance", 300),
          (assign, ":suggested_relation_change", -10),
        (try_end),
      (else_try), #this is specific to quarrels with the player
        (eq, ":entry_type", logent_lords_quarrel_over_woman),
        (eq, ":actor", "$g_talk_troop"),
        (eq, ":center_object", "trp_player"),
        
        (neg|troop_slot_ge, ":troop_object", slot_troop_spouse, "trp_player"),
        
        (str_store_troop_name, s54, ":troop_object"),
        
        (try_begin),
          (this_or_next|eq, ":reputation", lrep_selfrighteous),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_debauched),
          
          (assign, ":comment", "str_comment_i_quarreled_with_you_over_woman_derisive"),
          (assign, ":relevance", 200),
          (assign, ":suggested_relation_change", -20),
        (else_try),
          (assign, ":comment", "str_comment_i_quarreled_with_you_over_woman_default"),
          (assign, ":relevance", 200),
          (assign, ":suggested_relation_change", -20),
        (try_end),
        
      (else_try),
        (eq, ":entry_type", logent_border_incident_troop_breaks_truce),
        (eq, ":actor", "trp_player"),
        (faction_slot_eq, ":faction_object", slot_faction_leader, "$g_talk_troop"),
        (eq, "$players_kingdom", ":faction_object"),
        
        (assign, ":suggested_relation_change", -10),
        (assign, ":comment", "str_comment_you_broke_truce_as_my_vassal"),
        (assign, ":relevance", 300),
        
      (else_try),
        (eq, ":entry_type", logent_border_incident_troop_attacks_neutral),
        (eq, ":actor", "trp_player"),
        (faction_slot_eq, ":faction_object", slot_faction_leader, "$g_talk_troop"),
        (eq, "$players_kingdom", ":faction_object"),
        
        (assign, ":suggested_relation_change", -3),
        (assign, ":comment", "str_comment_you_attacked_neutral_as_my_vassal"),
        (assign, ":relevance", 200),
        
        #THE FOLLOWING ARE ALL COMPLAINTS SPOKEN BY LORDS WITHIN CONVERATIONS, RATHER THAN COMMENTS WHEN THE PLAYER FIRST SPEAKS TO A LORD
      (else_try), #these need to have the actor and object strings added because they are used outside of "script_get_relevant_comment_to_s42"
        (eq, ":entry_type", logent_ruler_intervenes_in_quarrel),
        (eq, ":center_object", "$g_talk_troop"), #actor is liege lord, center object is loser lord, troop object is winner lord
        (str_store_troop_name, s50, ":actor"),
        (str_store_troop_name, s51, ":center_object"), #s50 is actor, s51 is center object, s54 is troop object
        (str_store_troop_name, s54, ":troop_object"), #s50 is actor, s51 is center object, s54 is troop object
        (assign, ":comment", "str_comment_lord_intervened_against_me"),
        (assign, ":relevance", -1),
        
      (else_try),
        (eq, ":entry_type", logent_lord_protests_marshal_appointment),
        (eq, ":actor", "$g_talk_troop"),
        
        (str_store_troop_name, s51, ":center_object"), #s51 is center object (marshal), s54 is troop object (liege lord),
        (str_store_troop_name, s54, ":troop_object"),
        
        (assign, ":comment", "str_comment_i_protested_marshal_appointment"),
        (assign, ":relevance", -1),
        
      (else_try),
        (eq, ":entry_type", logent_lord_blames_defeat),
        (eq, ":actor", "$g_talk_troop"),
        
        (str_store_troop_name, s51, ":center_object"), #s51 is center object (marshal), s54 is troop object (liege lord),
        (str_store_troop_name, s54, ":troop_object"),
        (str_store_troop_name, s56, ":faction_object"), #faction object is unusual in this circumstance
        
        (assign, ":comment", "str_comment_i_blamed_defeat"),
        (assign, ":relevance", -1),
        
      (else_try),
        (eq, ":entry_type", logent_troop_feels_cheated_by_troop_over_land),
        (eq, ":actor", "$g_talk_troop"),
        
        (str_store_party_name, s51, ":center_object"),
        (str_store_troop_name, s54, ":troop_object"),
        (str_store_troop_name, s56, ":faction_object"), #faction object is unusual in this circumstance
        
        (assign, ":comment", "str_comment_i_was_entitled_to_fief"),
        (assign, ":relevance", -1),
        
      (else_try),
        (eq, ":entry_type", logent_lords_quarrel_over_woman),
        (eq, ":actor", "$g_talk_troop"),
        
        (str_store_troop_name, s51, ":center_object"),
        (str_store_troop_name, s54, ":troop_object"),
        
        (assign, ":comment", "str_comment_i_quarreled_with_troop_over_woman"),
        (assign, ":relevance", -1),
        
      (else_try),
        (eq, ":entry_type", logent_lords_quarrel_over_woman),
        (eq, ":center_object", "$g_talk_troop"),
        
        (str_store_troop_name, s51, ":actor"),
        (str_store_troop_name, s54, ":troop_object"),
        
        (assign, ":comment", "str_comment_i_quarreled_with_troop_over_woman"),
        (assign, ":relevance", -1),
        
      (else_try),
        (eq, ":entry_type", logent_player_stole_cattles_from_village),
        
        (eq, ":actor", "trp_player"),
        (try_begin),
          (eq, ":center_object_lord", "$g_talk_troop"),
          (assign, ":relevance", 200),
          (assign, ":suggested_relation_change", -1),
          (assign, ":comment", "str_comment_you_stole_cattles_from_my_village_default"),
          (try_begin),
            (lt, "$g_talk_troop_faction_relation", -3),
            (this_or_next|eq, ":reputation", lrep_goodnatured),
            (eq, ":reputation", lrep_upstanding),
            (assign, ":comment", "str_comment_you_stole_cattles_from_my_village_enemy_benevolent"),
          (else_try),
            (lt, "$g_talk_troop_faction_relation", -3),
            (this_or_next|eq, ":reputation", lrep_cunning),
            (eq, ":reputation", lrep_selfrighteous),
            (assign, ":comment", "str_comment_you_stole_cattles_from_my_village_enemy_coldblooded"),
          (else_try),
            (lt, "$g_talk_troop_faction_relation", -3),
            (this_or_next|eq, ":reputation", lrep_quarrelsome),
            (eq, ":reputation", lrep_debauched),
            (assign, ":comment", "str_comment_you_stole_cattles_from_my_village_enemy_spiteful"),
          (else_try),
            (lt, "$g_talk_troop_faction_relation", -3),
            (assign, ":comment", "str_comment_you_stole_cattles_from_my_village_enemy"),
          (else_try),
            (lt, "$g_talk_troop_relation", -3),
            (this_or_next|eq, ":reputation", lrep_quarrelsome),
            (eq, ":reputation", lrep_debauched),
            (assign, ":comment", "str_comment_you_stole_cattles_from_my_village_unfriendly_spiteful"),
          (else_try),
            (gt, "$g_talk_troop_relation", 6),
            (assign, ":comment", "str_comment_you_stole_cattles_from_my_village_friendly"),
          (try_end),
        (try_end),
        
      (try_end),
      
      (assign, reg0, ":comment"),
      (assign, reg1, ":relevance"),
      (assign, reg2, ":suggested_relation_change"),
      (assign, reg3, ":rejoinder"),
  ]),
  
  # script_get_relevant_comment_to_s42
  # Input: none
  # Output: reg0 = 1 if comment found, 0 otherwise; s61 will contain comment string if found
  ("get_relevant_comment_to_s42",
    [
      
      (troop_get_slot, ":reputation", "$g_talk_troop", slot_lord_reputation_type),
      (try_begin),
        (eq, "$cheat_mode", 1),
        (store_add, ":rep_string", ":reputation", "str_personality_archetypes"),
        (str_store_string, s15, ":rep_string"),
        (display_message, "@{!}Reputation type: {s15}"),
      (try_end),
      
      
      (assign, ":highest_score_so_far", 50),
      (assign, ":best_comment_so_far", -1),
      (assign, ":rejoinder_to_best_comment_so_far", -1),
      (assign, ":comment_found", 0),
      (assign, ":best_log_entry", -1),
      (assign, ":comment_relation_change", 0),
      (store_current_hours, ":current_time"),
      
      #prevents multiple comments in conversations in same hour
      
      #     (troop_get_slot, ":talk_troop_last_comment_time", "$g_talk_troop", slot_troop_last_comment_time),
      #"$num_log_entries should also be set to one, not zero. This is included in the initialize npcs script, although could be moved to game_start
      (troop_get_slot, ":talk_troop_last_comment_slot", "$g_talk_troop", slot_troop_last_comment_slot),
      (troop_set_slot, "$g_talk_troop", slot_troop_last_comment_slot, "$num_log_entries"),
      
      (store_add, ":log_entries_plus_one", "$num_log_entries", 1),
      (try_for_range, ":log_entry_no", 1, ":log_entries_plus_one"),
        #      It should be log entries plus one, so that the try_ sequence does not stop short of the last log entry
        #      $Num_log_entries is now the number of the last log entry, which begins at "1" rather than "0"
        #      This is so that (le, ":log_entry_no", ":talk_troop_last_comment_slot") works properly
        
        (troop_get_slot, ":entry_time",           "trp_log_array_entry_time",           ":log_entry_no"),
        #      (val_max, ":entry_time", 1), #This is needed for pre-game events to be commented upon, if hours are used rather than the order of events
        (store_sub, ":entry_hours_elapsed", ":current_time", ":entry_time"),
        (try_begin),
          (le, ":log_entry_no", ":talk_troop_last_comment_slot"),
          #         (le, ":entry_time", ":talk_troop_last_comment_time"),
          (try_begin),
            (eq, ":log_entry_no", ":talk_troop_last_comment_slot"),
            (eq, "$cheat_mode", 1),
            (assign, reg5, ":log_entry_no"),
            (display_message, "@{!}Entries up to n{reg5} skipped"),
          (try_end),
          #       I suggest using the log entry number as opposed to time so that events in the same hour can be commented upon
          #       This feels more natural, for example, if there are other lords in the court when the player pledges allegiance
        (else_try),
          #         (le, ":entry_hours_elapsed", 3), #don't comment on really fresh events
          #       (else_try),
          (call_script, "script_get_relevant_comment_for_log_entry", ":log_entry_no"),
          (gt, reg1, 10),
          (assign, ":score", reg1),
          (assign, ":comment", reg0),
          #reg2 is what
          (assign, ":rejoinder", reg3),
          
          (store_random_in_range, ":rand", 70, 140),
          (val_mul, ":score", ":rand"),
          (store_add, ":entry_time_score", ":entry_hours_elapsed", 500), #approx. one month
          (val_mul, ":score", 1000),
          (val_div, ":score", ":entry_time_score"), ###Relevance decreases over time - halved after one month, one-third after two, etc
          (try_begin),
            (gt, ":score", ":highest_score_so_far"),
            (assign, ":highest_score_so_far", ":score"),
            (assign, ":best_comment_so_far",  ":comment"),
            (assign, ":rejoinder_to_best_comment_so_far",  ":rejoinder"),
            (assign, ":best_log_entry", ":log_entry_no"),
            (assign, ":comment_relation_change", reg2),
          (try_end),
        (try_end),
      (try_end),
      
      (try_begin),
        (gt, ":best_comment_so_far", 0),
        (assign, ":comment_found", 1), #comment found print it to s61 now.
        (troop_get_slot, ":actor",                 "trp_log_array_actor",                 ":best_log_entry"),
        (troop_get_slot, ":center_object",         "trp_log_array_center_object",         ":best_log_entry"),
        (troop_get_slot, ":center_object_lord",    "trp_log_array_center_object_lord",    ":best_log_entry"),
        (troop_get_slot, ":center_object_faction", "trp_log_array_center_object_faction", ":best_log_entry"),
        (troop_get_slot, ":troop_object",          "trp_log_array_troop_object",          ":best_log_entry"),
        (troop_get_slot, ":troop_object_faction",  "trp_log_array_troop_object_faction",  ":best_log_entry"),
        (troop_get_slot, ":faction_object",        "trp_log_array_faction_object",        ":best_log_entry"),
        (try_begin),
          (ge, ":actor", 0),
          (str_store_troop_name,   s50, ":actor"),
        (try_end),
        (try_begin),
          (ge, ":center_object", 0),
          (str_store_party_name,   s51, ":center_object"),
        (try_end),
        (try_begin),
          (ge, ":center_object_lord", 0),
          (str_store_troop_name,   s52, ":center_object_lord"),
        (try_end),
        (try_begin),
          (ge, ":center_object_faction", 0),
          (str_store_faction_name, s53, ":center_object_faction"),
        (try_end),
        (try_begin),
          (ge, ":troop_object", 0),
          (str_store_troop_name,   s54, ":troop_object"),
        (try_end),
        (try_begin),
          (is_between, ":troop_object_faction", kingdoms_begin, kingdoms_end),
          (str_store_faction_name, s55, ":troop_object_faction"),
          (str_store_string, s55, "str_the_s55"),
        (else_try),
          (is_between, ":troop_object", bandits_begin, bandits_end),
          (str_store_string, s55, "str_bandits"),
        (else_try),
          (eq, ":troop_object_faction", "fac_deserters"),
          (str_store_string, s55, "str_deserters"),
        (else_try),
          (str_store_string, s55, "str_travellers_on_the_road"),
        (else_try),
          
        (try_end),
        
        (try_begin),
          (ge, ":faction_object", 0),
          (str_store_faction_name, s56, ":faction_object"),
        (try_end),
        (assign, "$g_last_comment_copied_to_s42", ":best_comment_so_far"), #maybe deprecate
        (assign, "$g_rejoinder_to_last_comment", ":rejoinder_to_best_comment_so_far"),
        
        (str_store_string, s42, ":best_comment_so_far"),
      (try_end),
      
      (assign, reg0, ":comment_found"),
      
      (assign, "$log_comment_relation_change", ":comment_relation_change"),
  ]),
  
  #
  ("merchant_road_info_to_s42", #also does itemss to s32
    [
      (store_script_param, ":center", 1),
      
      (assign, ":last_bandit_party_found", -1),
      (assign, ":last_bandit_party_origin", -1),
      (assign, ":last_bandit_party_destination", -1),
      (assign, ":last_bandit_party_hours_ago", -1),
      
      (str_clear, s32),
      
      (str_clear, s42),
      (str_clear, s47), #safe roads
      
      (try_for_range, ":center_to_reset", centers_begin, centers_end),
        (party_set_slot, ":center_to_reset", slot_party_temp_slot_1, 0),
      (try_end),
      
      (assign, ":road_attacks", 0),
      (assign, ":trades", 0),
      
      #first mention all attacks
      (try_for_range, ":log_entry_iterator", 0, "$num_log_entries"),
        (store_sub, ":log_entry_no", "$num_log_entries", ":log_entry_iterator"),
        #how long ago?
        (this_or_next|troop_slot_eq, "trp_log_array_entry_type", ":log_entry_no", logent_traveller_attacked),
        (troop_slot_eq, "trp_log_array_entry_type", ":log_entry_no", logent_party_traded),
        
        #       reference - (call_script, "script_add_log_entry", logent_traveller_attacked, ":winner_party" (actor),  ":origin" (center object), ":destination" (troop_object), ":winner_faction"),
        
        (troop_get_slot, ":origin",         "trp_log_array_center_object",         ":log_entry_no"),
        (troop_get_slot, ":destination",    "trp_log_array_troop_object",          ":log_entry_no"),
        
        (this_or_next|eq, ":origin", ":center"),
        (eq, ":destination", ":center"),
        
        
        (troop_get_slot, ":event_time",            "trp_log_array_entry_time",              ":log_entry_no"),
        (store_current_hours, ":cur_hour"),
        (store_sub, ":hours_ago", ":cur_hour", ":event_time"),
        (assign, reg3, ":hours_ago"),
        
        (lt, ":hours_ago", 672), #four weeks
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (troop_slot_eq, "trp_log_array_entry_type", ":log_entry_no", logent_traveller_attacked),
          (display_message, "str_attack_on_travellers_found_reg3_hours_ago"),
        (else_try),
          (eq, "$cheat_mode", 1),
          (troop_slot_eq, "trp_log_array_entry_type", ":log_entry_no", logent_party_traded),
          (display_message, "str_trade_event_found_reg3_hours_ago"),
        (try_end),
        
        (try_begin), #possibly make script -- get_colloquial_for_time
          (lt, ":hours_ago", 24),
          (str_store_string, s46, "str_a_short_while_ago"),
        (else_try),
          (lt, ":hours_ago", 48),
          (str_store_string, s46, "str_one_day_ago"),
        (else_try),
          (lt, ":hours_ago", 72),
          (str_store_string, s46, "@two days ago"),
        (else_try),
          (lt, ":hours_ago", 154),
          (str_store_string, s46, "str_earlier_this_week"),
        (else_try),
          (lt, ":hours_ago", 240),
          (str_store_string, s46, "str_about_a_week_ago"),
        (else_try),
          (lt, ":hours_ago", 480),
          (str_store_string, s46, "str_about_two_weeks_ago"),
        (else_try),
          (str_store_string, s46, "str_several_weeks_ago"),
        (try_end),
        
        
        
        (troop_get_slot, ":actor", "trp_log_array_actor", ":log_entry_no"),
        (troop_get_slot, ":faction_object", "trp_log_array_faction_object", ":log_entry_no"),
        
        (str_store_string, s39, "str_unknown_assailants"),
        (assign, ":assailants_known", -1),
        (try_begin),
          (party_is_active, ":actor"),
          (store_faction_of_party, ":actor_faction", ":actor"),
          (eq, ":faction_object", ":actor_faction"),
          (assign, ":assailants_known", ":actor"),
          (str_store_party_name, s39, ":assailants_known"),
          (assign, "$g_bandit_party_for_bounty", -1),
          (try_begin), #possibly make script -- get_colloquial_for_faction
            (eq, ":faction_object", "fac_kingdom_1"),
            (str_store_string, s39, "str_vaegirs"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_2"),
            (str_store_string, s39, "str_vaegirs"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_3"),
            (str_store_string, s39, "str_vaegirs"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_4"),
            (str_store_string, s39, "str_vaegirs"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_5"),
            (str_store_string, s39, "str_khergits"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_6"),
            (str_store_string, s39, "str_nords"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_7"),
            (str_store_string, s39, "str_nords"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_8"),
            (str_store_string, s39, "str_vaegirs"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_9"),
            (str_store_string, s39, "str_swadians"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_10"),
            (str_store_string, s39, "str_swadians"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_11"),
            (str_store_string, s39, "str_swadians"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_12"),
            (str_store_string, s39, "str_swadians"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_13"),
            (str_store_string, s39, "str_swadians"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_14"),
            (str_store_string, s39, "str_rhodoks"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_15"),
            (str_store_string, s39, "str_rhodoks"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_16"),
            (str_store_string, s39, "str_rhodoks"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_17"),
            (str_store_string, s39, "str_rhodoks"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_18"),
            (str_store_string, s39, "str_rhodoks"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_19"),
            (str_store_string, s39, "str_rhodoks"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_20"),
            (str_store_string, s39, "str_sarranids"),
          (else_try),
            (eq, ":faction_object", "fac_kingdom_21"),
            (str_store_string, s39, "str_rhodoks"),
          (else_try),
            (eq, ":faction_object", "fac_player_supporters_faction"),
            (str_store_string, s39, "str_your_followers"),
          (else_try), #bandits
            (assign, ":last_bandit_party_found", ":assailants_known"),
            (assign, ":last_bandit_party_origin", ":origin"),
            (assign, ":last_bandit_party_destination", ":destination"),
            (assign, ":last_bandit_party_hours_ago", ":hours_ago"),
          (try_end),
        (try_end),
        
        (try_begin),
          (eq, ":origin", ":center"),
          (troop_slot_eq, "trp_log_array_entry_type", ":log_entry_no", logent_traveller_attacked),
          (party_slot_eq, ":destination", slot_party_temp_slot_1, 0),
          
          (party_set_slot, ":destination", slot_party_temp_slot_1, 1),
          (str_store_party_name, s40, ":destination"),
          (str_store_string, s44, "str_we_have_heard_that_travellers_heading_to_s40_were_attacked_on_the_road_s46_by_s39"),
          (str_store_string, s43, "str_s42"),
          (str_store_string, s42, "str_s43_s44"),
          
          (val_add, ":road_attacks", 1),
          #travellers were attacked on the road to...
        (else_try),
          (eq, ":destination", ":center"),
          (troop_slot_eq, "trp_log_array_entry_type", ":log_entry_no", logent_traveller_attacked),
          (party_slot_eq, ":origin", slot_party_temp_slot_1, 0),
          
          (party_set_slot, ":origin", slot_party_temp_slot_1, 1),
          (str_store_party_name, s40, ":origin"),
          (str_store_string, s44, "str_we_have_heard_that_travellers_coming_from_s40_were_attacked_on_the_road_s46_by_s39"),
          
          (str_store_string, s43, "str_s42"),
          (str_store_string, s42, "str_s43_s44"),
          
          (val_add, ":road_attacks", 1),
          
          #travellers from here traded at...
          #		(else_try),
          #			(eq, ":origin", ":center"),
          #			(troop_slot_eq, "trp_log_array_entry_type", ":log_entry_no", logent_party_traded),
          #			(party_slot_eq, ":destination", slot_party_temp_slot_1, 0),
          
          #			(party_set_slot, ":destination", slot_party_temp_slot_1, 1),
          #			(str_store_party_name, s40, ":destination"),
          #			(str_store_string, s44, "@Travellers headed to {s40} traded there {s46}"),
          #			(str_store_string, s43, "@{s42"),
          #			(str_store_string, s42, "str_s43_s44"),
          
          #caravan from traded at...
        (else_try),
          (eq, ":destination", ":center"),
          (troop_slot_eq, "trp_log_array_entry_type", ":log_entry_no", logent_party_traded),
          (party_slot_eq, ":origin", slot_party_temp_slot_1, 0),
          
          (party_set_slot, ":origin", slot_party_temp_slot_1, 1),
          (str_store_party_name, s40, ":origin"),
          (str_store_string, s44, "str_travellers_coming_from_s40_traded_here_s46"),
          (str_store_string, s43, "str_s42"),
          (str_store_string, s42, "str_s43_s44"),
          
          (val_add, ":trades", 1),
          
          #caravan from traded at...
        (try_end),
        
      (try_end),
      
      
      (try_begin),
        (le, ":trades", 2),
        (eq, ":road_attacks", 0),
        (store_current_hours, ":hours"),
        (lt, ":hours", 168),
        (str_store_string, s42, "str_it_is_still_early_in_the_caravan_season_so_we_have_seen_little_tradings42"),
      (else_try),
        (eq, ":trades", 0),
        (eq, ":road_attacks", 0),
        (str_store_string, s42, "str_there_has_been_very_little_trading_activity_here_recentlys42"),
      (else_try),
        (le, ":trades", 2),
        (eq, ":road_attacks", 0),
        (str_store_string, s42, "str_there_has_some_trading_activity_here_recently_but_not_enoughs42"),
      (else_try),
        (le, ":trades", 2),
        (le, ":road_attacks", 2),
        (str_store_string, s42, "str_there_has_some_trading_activity_here_recently_but_the_roads_are_dangerouss42"),
      (else_try),
        (ge, ":road_attacks", 3),
        (str_store_string, s42, "str_the_roads_around_here_are_very_dangerouss42"),
      (else_try),
        (ge, ":road_attacks", 1),
        (str_store_string, s42, "str_we_have_received_many_traders_in_town_here_although_there_is_some_danger_on_the_roadss42"),
      (else_try),
        (str_store_string, s42, "str_we_have_received_many_traders_in_town_heres42"),
      (try_end),
      
      #do safe roads
      (assign, ":unused_trade_route_found", 0),
      (try_for_range, ":trade_route_slot", slot_town_trade_routes_begin, slot_town_trade_routes_end),
        (party_get_slot, ":trade_center", ":center", ":trade_route_slot"),
        (is_between, ":trade_center", centers_begin, centers_end),
        
        (party_slot_eq, ":trade_center", slot_party_temp_slot_1, 0),
        
        #		(party_get_slot, ":town_lord", ":trade_center", slot_town_lord),
        
        (str_store_party_name, s41, ":trade_center"),
        (try_begin),
          (eq, ":unused_trade_route_found", 1),
          (str_store_string, s44, "str_s44_s41"),
        (else_try),
          (str_store_string, s44, "str_s41"),
        (try_end),
        (assign, ":unused_trade_route_found", 1),
      (try_end),
      (try_begin),
        (eq, ":unused_trade_route_found", 1),
        (str_store_string, s47, "str_there_is_little_news_about_the_caravan_routes_to_the_towns_of_s44_and_nearby_parts_but_no_news_is_good_news_and_those_are_therefore_considered_safe"),
      (try_end),
      
      (assign, ":safe_village_road_found", 0),
      (try_for_range, ":village", villages_begin, villages_end),
        (party_slot_eq, ":village", slot_village_market_town, ":center"),
        (party_slot_eq, ":village", slot_party_temp_slot_1, 0),
        
        #		(party_get_slot, ":town_lord", ":village", slot_town_lord),
        (str_store_party_name, s41, ":village"),
        (try_begin),
          (eq, ":safe_village_road_found", 1),
          (str_store_string, s44, "str_s44_s41"),
        (else_try),
          (str_store_string, s44, "str_s41"),
        (try_end),
        (assign, ":safe_village_road_found", 1),
      (try_end),
      
      (try_begin),
        (eq, ":safe_village_road_found", 1),
        (eq, ":unused_trade_route_found", 1),
        (str_store_string, s47, "str_s47_also_the_roads_to_the_villages_of_s44_and_other_outlying_hamlets_are_considered_safe"),
      (else_try),
        (eq, ":safe_village_road_found", 1),
        (str_store_string, s47, "str_however_the_roads_to_the_villages_of_s44_and_other_outlying_hamlets_are_considered_safe"),
      (try_end),
      
      (str_store_string, s33, "str_we_have_shortages_of"),
      (assign, ":some_shortages_found", 0),
      (try_for_range, ":cur_good", trade_goods_begin, trade_goods_end),
        (store_sub, ":cur_good_price_slot", ":cur_good", trade_goods_begin),
        (val_add, ":cur_good_price_slot", slot_town_trade_good_prices_begin),
        (party_get_slot, ":price", ":center", ":cur_good_price_slot"),
        (gt, ":price", 1100),
        
        (str_store_item_name, s34, ":cur_good"),
        (assign, reg1, ":price"),
        (str_store_string, s33, "str_s33_s34_reg1"),
        
        (assign, ":some_shortages_found", 1),
      (try_end),
      
      (try_begin),
        (eq, ":some_shortages_found", 0),
        (str_store_string, s32, "str_we_have_adequate_stores_of_all_commodities"),
      (else_try),
        (str_store_string, s32, "str_s33_and_some_other_commodities"),
      (try_end),
      
      (assign, reg0, ":last_bandit_party_found"),
      (assign, reg1, ":last_bandit_party_origin"),
      (assign, reg2, ":last_bandit_party_destination"),
      (assign, reg3, ":last_bandit_party_hours_ago"),
      
      
    ]
  ),
  
  ("get_manhunt_information_to_s15",
    [
      (store_script_param, ":quest", 1),
      
      (str_store_string, s15, "str_the_roads_are_full_of_brigands_friend_but_that_name_in_particular_does_not_sound_familiar_good_hunting_to_you_nonetheless"),
      (quest_get_slot, ":quarry", ":quest", slot_quest_target_party),
      (try_begin),
        (is_between, "$g_talk_troop", active_npcs_begin, active_npcs_end),
        (troop_get_slot, ":talk_party", "$g_talk_troop", slot_troop_leaded_party),
      (else_try),
        (gt, "$g_encountered_party", "p_spawn_points_end"),
        (assign, ":talk_party", "$g_encountered_party"),
      (else_try),
        (assign, ":talk_party", -1),
      (try_end),
      
      (try_for_range, ":log_entry", 0, "$num_log_entries"),
        (gt, ":talk_party", -1),
        (troop_get_slot, ":party", "trp_log_array_actor", ":log_entry"),
        (eq, ":party", ":talk_party"),
        (troop_get_slot, ":bandit_party", "trp_log_array_troop_object", ":log_entry"),
        (eq, ":bandit_party", ":quarry"),
        (store_current_hours, ":hours_ago"),
        (troop_get_slot, ":sighting_time", "trp_log_array_entry_time",  ":log_entry"),
        (val_sub, ":hours_ago", ":sighting_time"),
        (try_begin),
          (le, ":hours_ago", 1),
          (str_store_string, s16, "str_less_than_an_hour_ago"),
        (else_try),
          (le, ":hours_ago", 48),
          (assign, reg3, ":hours_ago"),
          (str_store_string, s16, "str_maybe_reg3_hours_ago"),
        (else_try),
          (val_div, ":hours_ago", 24),
          (assign, reg3, ":hours_ago"),
          (str_store_string, s16, "str_reg3_days_ago"),
        (try_end),
        
        (troop_get_slot, ":center", "trp_log_array_center_object", ":log_entry"),
        (str_store_party_name, s17, ":center"),
        (troop_get_slot, ":entry_type", "trp_log_array_entry_type", ":log_entry"),
        (eq, ":entry_type", logent_party_spots_wanted_bandits),
        (str_store_string, s15, "str_youre_in_luck_we_sighted_those_bastards_s16_near_s17_hurry_and_you_might_be_able_to_pick_up_their_trail_while_its_still_hot"),
        
        #		(try_begin),
        #			(eq, ":entry_type", logent_party_chases_wanted_bandits),
        #			(str_store_string, s15, "@You're in luck. We gave chase to those bastards {s16} near {s17}. They have eluded us so far -- but perhaps you will do better..."),
        #		(else_try),
        #			(eq, ":entry_type", logent_party_runs_from_wanted_bandits),
        #			(str_store_string, s15, "@As it happens, they tried to run us down near {s17} {s16}. By the heavens, I hope you teach them a lesson."),
        #		(try_end),
      (try_end),
  ]),
  
  
  #Troop Commentaries end
  
  
  
  ("rebellion_arguments", #Right now, called only in one place. This is only used when for player overtures, and will need some changes if this script is called when NPCs try to suborn lords
    [
      (store_script_param, ":lord", 1),
      (store_script_param, ":argument", 2),
      (store_script_param, ":candidate", 3),
      
      (assign, ":argument_appeal", 0),
      (assign, ":argument_strength", 0),
      
      (troop_get_slot, ":reputation", ":lord", slot_lord_reputation_type),
      
      (store_faction_of_troop, ":lord_faction", ":candidate"),
      (store_faction_of_troop, ":candidate_faction", ":candidate"),
      
      (try_begin),
        (eq, ":candidate", "trp_player"),
        (assign, ":right_to_rule", "$player_right_to_rule"),
      (else_try), #default right to rule of 75 for pretenders claiming throne
        (is_between, ":candidate", pretenders_begin, pretenders_end),
        (troop_slot_eq, ":candidate", slot_troop_original_faction, ":lord_faction"),
        (assign, ":right_to_rule", 75),
      (else_try), #default right to rule of 60 for all other lords
        (assign, ":right_to_rule", 60),
      (try_end),
      (assign, ":p_honor", "$player_honor"),
      (val_clamp, ":p_honor", -100, 101),
      (try_begin),
        (eq, ":argument", argument_claim),
        (store_sub, ":argument_strength", ":right_to_rule", 30),
      (else_try),
        (eq, ":argument", argument_ruler),
        (store_sub, ":argument_strength", ":p_honor", 20),
      (else_try),
        (eq, ":argument", argument_lords),
        (store_sub, ":argument_strength", ":p_honor", 20),
      (else_try),
        #argument_strength is ((5 * number of centers player have) - 40) if argument type is argument_victory
        (eq, ":argument", argument_victory),
        (assign, ":argument_strength", 0),
        (try_for_range, ":center", centers_begin, centers_end),
          (store_faction_of_party, ":center_faction", ":center"),
          (assign, ":argument_strength", -40),
          (try_begin),
            (eq, "$players_kingdom", ":candidate_faction"),
            (this_or_next|eq, ":center_faction", "fac_player_faction"),
            (eq, ":center_faction", "fac_player_supporters_faction"),
            (val_add, ":argument_strength", 5),
          (else_try),
            (eq, ":center_faction", ":candidate_faction"),
            (val_add, ":argument_strength", 5),
          (try_end),
        (try_end),
      (else_try),
        #argument_strength is (20 - 20 * (number of lords in player's faction which not awareded fief by player although there is a fief awarding in future promise))
        (eq, ":argument", argument_benefit),
        (assign, ":argument_strength", 20),
        (try_for_range, ":lord_promised_fief", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":other_faction", ":lord_promised_fief"),
          (neq, ":lord", "$g_talk_troop"),
          (this_or_next|eq, ":other_faction", "fac_player_supporters_faction"),
          (eq, ":other_faction", "$players_kingdom"),
          (troop_slot_eq, ":lord_promised_fief", slot_troop_promised_fief, 1),
          (val_sub, ":argument_strength", 20),
        (try_end),
      (try_end),
      (val_clamp, ":argument_strength", -40, 41),
      
      (try_begin),
        (eq, ":reputation", lrep_martial),
        (try_begin),
          (eq, ":argument", argument_claim),
          (assign, ":argument_appeal", 30),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_claims_to_the_throne_good_there_is_nothing_id_rather_do_than_fight_for_a_good_cause"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_claims_to_the_throne_well_there_is_nothing_id_rather_do_than_fight_for_a_good_cause_but_the_claim_you_make_seems_somewhat_weak"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_lords),
          (assign, ":argument_appeal", 10),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_i_am_pleased_that_you_speak_of_upholding_my_ancient_rights_which_are_sometimes_trod_upon_in_these_sorry_days"),
          (else_try),
            (str_store_string, s15, "str_i_am_pleased_that_you_speak_of_upholding_my_ancient_rights_but_sometimes_men_make_pledges_before_they_are_king_which_they_cannot_keep_once_they_take_the_throne"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_ruler),
          (assign, ":argument_appeal", 0),
          (try_begin),
            (str_store_string, s15, "str_you_speak_of_protecting_the_commons_well_i_supposed_thats_good_but_sometimes_the_commons_overstep_their_boundaries_im_more_concerned_that_your_claim_be_legal_so_i_can_swing_my_sword_with_a_good_conscience"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_benefit),
          (assign, ":argument_appeal", -10),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_giving_me_land_good_i_ask_for_no_more_than_my_due"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_giving_me_land_unfortunately_you_are_not_wellknown_for_rewarding_those_to_whom_you_have_made_such_offers"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_victory),
          (assign, ":argument_appeal", -30),
          (str_store_string, s15, "str_you_speak_of_unifying_calradia_well_i_believe_that_well_always_be_fighting__its_important_that_we_fight_for_a_rightful_cause"),
        (try_end),
      (else_try),
        (eq, ":reputation", lrep_quarrelsome),
        (try_begin),
          (eq, ":argument", argument_claim),
          (assign, ":argument_appeal", -20),
          (str_store_string, s15, "str_you_talk_of_claims_to_the_throne_but_i_leave_bickering_about_legalities_to_the_lawyers_and_clerks"),
        (else_try),
          (eq, ":argument", argument_ruler),
          (assign, ":argument_appeal", -30),
          (str_store_string, s15, "str_you_speak_of_ruling_justly_hah_ill_believe_theres_such_a_thing_as_a_just_king_when_i_see_one"),
        (else_try),
          (eq, ":argument", argument_lords),
          (assign, ":argument_appeal", 0),
          (str_store_string, s15, "str_you_spoke_of_protecting_the_rights_of_the_nobles_if_you_did_youd_be_the_first_king_to_do_so_in_a_very_long_time"),
        (else_try),
          (eq, ":argument", argument_benefit),
          (assign, ":argument_appeal", 30),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_giving_me_land_ay_well_lets_see_if_you_deliver"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_giving_me_land_bah_youre_not_known_for_delivering_on_your_pledges"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_victory),
          (assign, ":argument_appeal", 10),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_unifying_calradia_well_youve_done_a_good_job_at_making_calradia_bend_its_knee_to_you_so_maybe_thats_not_just_talk"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_unifying_calradia_id_be_impressed_if_i_thought_you_could_do_it_but_unfortunately_you_dont"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":reputation", lrep_selfrighteous),
        (try_begin),
          (eq, ":argument", argument_claim),
          (assign, ":argument_appeal", -20),
          (str_store_string, s15, "str_you_speak_of_claims_to_the_throne_well_any_peasant_can_claim_to_be_a_kings_bastard"),
        (else_try),
          (eq, ":argument", argument_ruler),
          (assign, ":argument_appeal", -30),
          (str_store_string, s15, "str_well_its_a_fine_thing_to_court_the_commons_with_promises_but_what_do_you_have_to_offer_me"),
        (else_try),
          (eq, ":argument", argument_lords),
          (assign, ":argument_appeal", 0),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_protecting_the_rights_of_lords_that_would_make_a_fine_change_if_my_rights_as_lord_would_be_respected"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_protecting_the_rights_of_lords_that_would_make_a_fine_change_if_my_rights_as_lord_would_be_respected_however_it_is_easy_for_you_to_make_promises_while_you_are_weak_that_you_have_no_intention_of_keeping_when_you_are_strong"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_benefit),
          (assign, ":argument_appeal", 20),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_giving_me_land_well_my_family_is_of_ancient_and_noble_lineage_so_you_promise_me_no_more_than_my_due_still_your_gesture_is_appreciated"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_giving_me_land_well_you_make_that_pledge_but_i_am_not_impressed"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_victory),
          (assign, ":argument_appeal", 20),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_unifying_calradia_well_much_of_this_land_now_bends_its_knee_to_you_so_perhaps_that_is_not_just_talk"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_unifying_calradia_but_right_now_yours_is_just_one_squabbling_faction_among_many"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":reputation", lrep_cunning),
        (try_begin),
          (eq, ":argument", argument_claim),
          (assign, ":argument_appeal", -30),
          (str_store_string, s15, "str_you_speak_of_claims_well_no_offense_but_a_claim_unsupported_by_might_rarely_prospers"),
        (else_try),
          (eq, ":argument", argument_ruler),
          (assign, ":argument_appeal", 10),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_protecting_the_commons_well_i_suppose_that_will_make_for_a_more_prosperous_realm_ive_always_tried_to_treat_my_peasants_decently_saves_going_to_bed_worrying_about_whether_youll_wake_up_with_the_roof_on_fire"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_protecting_the_commons_very_well_but_remember_that_peasants_are_more_likely_to_cause_trouble_if_you_make_promises_then_dont_deliver_than_if_you_never_made_the_promise_in_the_first_place"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_lords),
          (assign, ":argument_appeal", 15),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_protecting_the_rights_of_lords_good_youd_be_well_advised_to_do_that__men_fight_better_for_a_king_wholl_respect_their_rights"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_protecting_the_rights_of_lords_very_well_but_remember__failing_to_keep_promises_which_you_made_while_scrambling_up_the_throne_is_the_quickest_way_to_topple_off_of_it_once_you_get_there"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_benefit),
          (assign, ":argument_appeal", -20),
          (str_store_string, s15, "str_you_speak_of_giving_me_land_very_good_but_often_i_find_that_when_a_man_makes_too_many_promises_trying_to_get_to_the_top_he_has_trouble_keeping_them_once_he_reaches_it"),
        (else_try),
          (eq, ":argument", argument_victory),
          (assign, ":argument_appeal", 20),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_unifying_calradia_well_many_have_said_that_you_might_very_well_be_the_one_to_do_it"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_unifying_calradia_well_all_the_kings_say_that_im_not_sure_that_you_will_succeed_while_they_fail"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":reputation", lrep_debauched),
        (try_begin),
          (eq, ":argument", argument_claim),
          (assign, ":argument_appeal", -20),
          (str_store_string, s15, "str_you_speak_of_claims_do_you_think_i_care_for_the_nattering_of_lawyers"),
        (else_try),
          (eq, ":argument", argument_ruler),
          (assign, ":argument_appeal", -20),
          (str_store_string, s15, "str_you_speak_of_protecting_the_commons_how_kind_of_you_i_shall_tell_my_swineherd_all_about_your_sweet_promises_no_doubt_he_will_become_your_most_faithful_vassal"),
        (else_try),
          (eq, ":argument", argument_lords),
          (assign, ":argument_appeal", -10),
          (str_store_string, s15, "str_you_speak_of_protecing_the_rights_of_lords_such_sweet_words_but_ill_tell_you_this__the_only_rights_that_are_respected_in_this_world_are_the_rights_to_dominate_whoever_is_weaker_and_to_submit_to_whoever_is_stronger"),
        (else_try),
          (eq, ":argument", argument_benefit),
          (assign, ":argument_appeal", 20),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_giving_me_land_yes_very_good__but_you_had_best_deliver"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_giving_me_land_hah_perhaps_all_those_others_to_whom_you_promised_lands_will_simply_step_aside"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_victory),
          (assign, ":argument_appeal", 10),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_unifying_calradia_you_may_indeed_humble_the_other_kings_of_this_land_and_in_that_case_i_would_hope_that_you_would_remember_me_as_your_faithful_servant"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_unifying_calradia_but_you_are_weak_and_i_think_that_you_will_remain_weak"),
          (try_end),
        (try_end),
      (else_try),
        (eq, ":reputation", lrep_goodnatured),
        (try_begin),
          (eq, ":argument", argument_claim),
          (assign, ":argument_appeal", 10),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_claims_its_good_for_a_king_to_have_a_strong_claim_although_admittedly_im_more_concerned_that_he_rules_just_ly_than_with_legalities_anyway_your_claim_seems_wellfounded_to_me"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_claims_but_your_claim_seems_a_bit_weak_to_me"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_ruler),
          (assign, ":argument_appeal", 20),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_protecting_the_commons_i_like_that_my_tenants_are_a_happy_lot_i_think_but_i_hear_of_others_in_other_estates_that_arent_so_fortunate"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_protecting_the_commons_im_glad_to_hear_you_say_that_but_do_me_a_favor__dont_promise_the_commons_anything_you_cant_deliver_thats_a_sure_way_to_get_them_to_rebel_and_it_breaks_my_heart_to_have_to_put_them_down"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_lords),
          (assign, ":argument_appeal", 0),
          (str_store_string, s15, "str_you_speak_of_protecting_the_rights_of_lords_well_very_good_i_suppose_but_you_know__we_lords_can_take_of_ourselves_its_the_common_folk_who_need_a_strong_king_to_look_out_for_them_to_my_mind"),
        (else_try),
          (eq, ":argument", argument_benefit),
          (assign, ":argument_appeal", -15),
          (str_store_string, s15, "str_you_speak_of_giving_me_land_its_kind_of_you_really_though_that_is_not_necessary"),
        (else_try),
          (eq, ":argument", argument_victory),
          (assign, ":argument_appeal", -25),
          (str_store_string, s15, "str_you_speak_of_unifying_calradia_well_maybe_you_can_unite_this_land_by_the_sword_but_im_not_sure_that_this_will_make_you_a_good_ruler"),
        (try_end),
      (else_try),
        (eq, ":reputation", lrep_upstanding),
        (try_begin),
          (eq, ":argument", argument_claim),
          (assign, ":argument_appeal", 10),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_claims_a_king_must_have_a_strong_legal_claim_for_there_not_to_be_chaos_in_the_realm_and_yours_is_wellestablished"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_claims_a_king_must_have_a_strong_legal_claim_for_there_not_to_be_chaos_in_the_realm_but_your_claim_is_not_so_strong"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_lords),
          (assign, ":argument_appeal", -5),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_protecting_the_rights_of_lords_it_is_of_course_important_that_a_king_respect_the_rights_of_his_vassals_although_i_worry_that_a_king_who_took_a_throne_without_proper_cause_would_not_rule_with_justice"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_protecting_the_rights_of_lords_it_is_of_course_important_that_a_king_respect_the_rights_of_his_vassals_however_i_would_like_to_know_that_you_would_indeed_deliver_on_your_promises"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_ruler),
          (assign, ":argument_appeal", 5),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_protecting_the_commons_i_would_be_pleased_to_serve_a_king_who_respected_the_rights_of_his_subjects_although_i_worry_that_a_king_who_took_a_throne_without_proper_cause_would_not_rule_with_justice"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_protecting_the_commons_i_would_be_pleased_to_serve_a_king_who_respected_the_rights_of_his_subjects_however_i_would_like_to_know_that_you_would_indeed_deliver_on_your_promises"),
          (try_end),
        (else_try),
          (eq, ":argument", argument_benefit),
          (assign, ":argument_appeal", -40),
          (str_store_string, s15, "str_i_am_not_swayed_by_promises_of_reward"),
        (else_try),
          (eq, ":argument", argument_victory),
          (assign, ":argument_appeal", 10),
          (try_begin),
            (gt, ":argument_strength", 0),
            (str_store_string, s15, "str_you_speak_of_unifying_calradia_it_would_be_good_to_bring_peace_to_the_realm_and_i_believe_that_you_are_strong_enough_to_do_so"),
          (else_try),
            (str_store_string, s15, "str_you_speak_of_unifying_calradia_it_would_be_good_to_bring_peace_the_realm_but_with_your_kingdom_in_its_current_state_i_worry_that_you_are_just_bringing_more_discord"),
          (try_end),
        (try_end),
      (try_end),
      
      (str_store_string, s14, "str_s15"),
      
      (assign, reg0, ":argument_appeal"),
      (assign, reg1, ":argument_strength"),
  ]),
  
  
  
  #Rebellion changes end
  
  # script_get_culture_with_party_faction_for_music
  # Input: arg1 = party_no
  # Output: reg0 = culture
  ("get_culture_with_party_faction_for_music",
    [
      (store_script_param, ":party_no", 1),
      (store_faction_of_party, ":faction_no", ":party_no"),
      (try_begin),
        (this_or_next|eq, ":faction_no", "fac_player_faction"),
        (eq, ":faction_no", "fac_player_supporters_faction"),
        (assign, ":faction_no", "$players_kingdom"),
      (try_end),
      (try_begin),
        (is_between, ":party_no", centers_begin, centers_end),
        (this_or_next|eq, ":faction_no", "fac_player_supporters_faction"),
        (neg|is_between, ":faction_no", kingdoms_begin, kingdoms_end),
        (party_get_slot, ":faction_no", ":party_no", slot_center_original_faction),
      (try_end),
      (call_script, "script_get_culture_with_faction_for_music", ":faction_no"),
  ]),
  
  # script_get_culture_with_faction_for_music
  # Input: arg1 = party_no
  # Output: reg0 = culture
  ("get_culture_with_faction_for_music", #more music variety
    [
      (store_script_param, ":faction_no", 1),
      
      (try_begin),
        (is_between, ":faction_no", "fac_kingdom_1", "fac_kingdom_21"),
        (store_random_in_range, ":value", 0, 6),
        (try_begin),
          (eq, ":value", 0),
          (assign, ":result", mtf_culture_1),
        (else_try),
          (eq, ":value", 1),
          (assign, ":result", mtf_culture_2),
        (else_try),
          (eq, ":value", 2),
          (assign, ":result", mtf_culture_3),
        (else_try),
          (eq, ":value", 3),
          (assign, ":result", mtf_culture_4),
        (else_try),
          (eq, ":value", 4),
          (assign, ":result", mtf_culture_5),
        (else_try),
          (eq, ":value", 5),
          (assign, ":result", mtf_culture_6),
        (else_try),
          (assign, ":result", mtf_culture_6),
        (try_end),
      (else_try),
        (store_random_in_range, ":value", 0, 6),
        (try_begin),
          (eq, ":value", 0),
          (assign, ":result", mtf_culture_1),
        (else_try),
          (eq, ":value", 1),
          (assign, ":result", mtf_culture_2),
        (else_try),
          (eq, ":value", 2),
          (assign, ":result", mtf_culture_3),
        (else_try),
          (eq, ":value", 3),
          (assign, ":result", mtf_culture_4),
        (else_try),
          (eq, ":value", 4),
          (assign, ":result", mtf_culture_5),
        (else_try),
          (eq, ":value", 5),
          (assign, ":result", mtf_culture_6),
        (else_try),
          (assign, ":result", 0), #no culture, including player with no bindings to another kingdom
        (try_end),
      (try_end),
      
      (assign, reg0, ":result"),
  ]),
  
  # script_music_set_situation_with_culture
  # Input: arg1 = music_situation
  # Output: none
  ("music_set_situation_with_culture",
    [
      (store_script_param, ":situation", 1),
      (assign, ":culture", 0), #no culture
      (assign, ":party_no", "p_main_party"), #no factions / cultures system for music, it is random
      (try_begin),
        (this_or_next|eq, ":situation", mtf_sit_town),
        (this_or_next|eq, ":situation", mtf_sit_day),
        (this_or_next|eq, ":situation", mtf_sit_night),
        (this_or_next|eq, ":situation", mtf_sit_town_infiltrate),
        (eq, ":situation", mtf_sit_encounter_hostile),
        (call_script, "script_get_culture_with_party_faction_for_music", ":party_no"),
        (val_or, ":culture", reg0),
      (else_try),
        (this_or_next|eq, ":situation", mtf_sit_ambushed),
        (eq, ":situation", mtf_sit_fight),
        (call_script, "script_get_culture_with_party_faction_for_music", ":party_no"),
        (val_or, ":culture", reg0),
        (call_script, "script_get_culture_with_party_faction_for_music", "p_main_party"),
        (val_or, ":culture", reg0),
        (call_script, "script_get_closest_center", "p_main_party"),
        (call_script, "script_get_culture_with_party_faction_for_music", reg0),
        (val_or, ":culture", reg0),
      (else_try),
        (eq, ":situation", mtf_sit_multiplayer_fight),
        (call_script, "script_get_culture_with_faction_for_music", "$g_multiplayer_team_1_faction"),
        (val_or, ":culture", reg0),
        (call_script, "script_get_culture_with_faction_for_music", "$g_multiplayer_team_2_faction"),
        (val_or, ":culture", reg0),
      (else_try),
        (eq, ":situation", mtf_sit_travel),
        (call_script, "script_get_culture_with_party_faction_for_music", "p_main_party"),
        (val_or, ":culture", reg0),
        (call_script, "script_get_closest_center", "p_main_party"),
        (call_script, "script_get_culture_with_party_faction_for_music", reg0),
        (val_or, ":culture", reg0),
      (else_try),
        (eq, ":situation", mtf_sit_victorious),
        (call_script, "script_get_culture_with_party_faction_for_music", "p_main_party"),
        (val_or, ":culture", reg0),
      (else_try),
        (eq, ":situation", mtf_sit_killed),
        (call_script, "script_get_culture_with_party_faction_for_music", ":party_no"),
        (val_or, ":culture", reg0),
      (try_end),
      (try_begin),
        (this_or_next|eq, ":situation", mtf_sit_town),
        (eq, ":situation", mtf_sit_day),
        (try_begin),
          (is_currently_night),
          (assign, ":situation", mtf_sit_night),
        (try_end),
      (try_end),
      (music_set_situation, ":situation"),
      (assign,":culture",mtf_culture_all), #chief
      (music_set_culture, ":culture"),
  ]),
  
  
  # script_combat_music_set_situation_with_culture
  # Input: none
  # Output: none
  ("combat_music_set_situation_with_culture",
    [
      (assign, ":situation", mtf_sit_fight),
      (assign, ":num_allies", 0),
      (assign, ":num_enemies", 0),
      (try_for_agents, ":agent_no"),
        (agent_is_alive, ":agent_no"),
        (agent_is_human, ":agent_no"),
        (agent_get_troop_id, ":agent_troop_id", ":agent_no"),
        (store_character_level, ":troop_level", ":agent_troop_id"),
        (val_add,  ":troop_level", 10),
        (val_mul, ":troop_level", ":troop_level"),
        (try_begin),
          (agent_is_ally, ":agent_no"),
          (val_add, ":num_allies", ":troop_level"),
        (else_try),
          (val_add, ":num_enemies", ":troop_level"),
        (try_end),
      (try_end),
      (val_mul, ":num_allies", 4), #play ambushed music if we are 2 times outnumbered.
      (val_div, ":num_allies", 3),
      (try_begin),
        (lt, ":num_allies", ":num_enemies"),
        (assign, ":situation", mtf_sit_ambushed),
      (try_end),
      #VC-1772
      (assign, ":continue_music", 0),
      (try_begin),
        (store_current_scene,":current_scene"),
        (is_between,":current_scene", "scn_frisa_beach", "scn_river_ambush_1"), #no in special scenes
        (assign, ":continue_music", 1),
      (try_end),
      (try_begin),
        (this_or_next|eq, "$ambient_music_in_battle", 1),
        (eq, ":continue_music", 1),
        (assign, ":situation", mtf_sit_ambient_music),
      (try_end),
      
      (call_script, "script_music_set_situation_with_culture", ":situation"),
  ]),
  
  # script_play_victorious_sound
  # Input: none
  # Output: none
  ("play_victorious_sound",
    [
      (call_script, "script_music_set_situation_with_culture", mtf_sit_victorious),
      #      (play_cue_track, "track_victorious_neutral_1"),
      #      (play_track, "track_victorious_neutral_1", 1),
  ]),
  
  # script_set_items_for_tournament
  # Input: arg1 = horse_chance, arg2 = lance_chance (with horse only), arg3 = sword_chance, arg4 = axe_chance, arg5 = bow_chance (without horse only), arg6 = javelin_chance (with horse only), arg7 = mounted_bow_chance (with horse only), arg8 = crossbow_sword_chance, arg9 = armor_item_begin, arg10 = helm_item_begin
  # Output: none (sets mt_arena_melee_fight items)
  # ("set_items_for_tournament",
    # [
      # (store_script_param, ":horse_chance", 1),
      # (store_script_param, ":lance_chance", 2),
      # (store_script_param, ":sword_chance", 3),
      # (store_script_param, ":axe_chance", 4),
      # (store_script_param, ":bow_chance", 5),
      # (store_script_param, ":javelin_chance", 6),
      # (store_script_param, ":mounted_bow_chance", 7),
      # (store_script_param, ":crossbow_sword_chance", 8),
      # (store_script_param, ":armor_item_begin", 9),
      # (store_script_param, ":helm_item_begin", 10),
      # (store_add, ":total_chance", ":sword_chance", ":axe_chance"),
      # (val_add, ":total_chance", ":crossbow_sword_chance"),
      # (try_for_range, ":i_ep", 0, 32),
        # (mission_tpl_entry_clear_override_items, "mt_arena_melee_fight", ":i_ep"),
        # (assign, ":has_horse", 0),
        # (store_div, ":cur_team", ":i_ep", 8),
        # (try_begin),
          # (store_random_in_range, ":random_no", 0, 100),
          # (lt, ":random_no", ":horse_chance"),
          # (assign, ":has_horse", 1),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_javelin_jinetes"), #chief cambia caballo practice_horse a daga
        # (try_end),
        # (try_begin),
          # (eq, ":has_horse", 1),
          # (store_add, ":cur_total_chance", ":total_chance", ":lance_chance"),
          # (val_add, ":cur_total_chance", ":javelin_chance"),
          # (val_add, ":cur_total_chance", ":mounted_bow_chance"),
        # (else_try),
          # (store_add, ":cur_total_chance", ":total_chance", ":bow_chance"),
        # (try_end),
        # (store_random_in_range, ":random_no", 0, ":cur_total_chance"),
        # (store_add, ":cur_shield_item", "itm_arena_shield_red", ":cur_team"),
        # (try_begin),
          # (val_sub, ":random_no", ":sword_chance"),
          # (lt, ":random_no", 0),
          # (try_begin),
            # (store_random_in_range, ":sub_random_no", 0, 100),
            # (lt, ":sub_random_no", 50),
            # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_arena_lance"), #chief cambia tipo de espada de practice_sword
            # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", ":cur_shield_item"),
            # #            (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_practice_shield"),
          # (else_try),
            # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_heavy_practice_sword"),
          # (try_end),
        # (else_try),
          # (val_sub, ":random_no", ":axe_chance"),
          # (lt, ":random_no", 0),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_practice_axe"),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", ":cur_shield_item"),
          # #         (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_practice_shield"),
        # (else_try),
          # (val_sub, ":random_no", ":crossbow_sword_chance"),
          # (lt, ":random_no", 0),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_arena_sword"), #chief cambia tipo de espada de practice_sword
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", ":cur_shield_item"), #chief cambia
        # (else_try),
          # (eq, ":has_horse", 0),
          # (val_sub, ":random_no", ":bow_chance"),
          # (lt, ":random_no", 0),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_arena_sword"),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", ":cur_shield_item"),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_practice_dagger"),
        # (else_try),
          # (eq, ":has_horse", 1), #chief cambiado torneo
          # (val_sub, ":random_no", ":lance_chance"),
          # (lt, ":random_no", 0),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_practice_lance"),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", ":cur_shield_item"),
          # #          (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_practice_shield"),
        # (else_try),
          # (eq, ":has_horse", 1),
          # (val_sub, ":random_no", ":javelin_chance"),
          # (lt, ":random_no", 0),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_practice_staff"),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", ":cur_shield_item"),
          # #          (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_practice_shield"),
        # (else_try),
          # (eq, ":has_horse", 1), #chief cambiado
          # (val_sub, ":random_no", ":mounted_bow_chance"),
          # (lt, ":random_no", 0),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_arena_sword"),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", ":cur_shield_item"),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_practice_dagger"),
        # (else_try),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", "itm_practice_dagger"),
        # (try_end),
        # (try_begin),
          # (ge, ":armor_item_begin", 0),
          # (store_add, ":cur_armor_item", ":armor_item_begin", ":cur_team"),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", ":cur_armor_item"),
        # (try_end),
        # (try_begin),
          # (ge, ":helm_item_begin", 0),
          # (store_add, ":cur_helm_item", ":helm_item_begin", ":cur_team"),
          # (mission_tpl_entry_add_override_item, "mt_arena_melee_fight", ":i_ep", ":cur_helm_item"),
        # (try_end),
      # (try_end),
  # ]),
  
  # script_custom_battle_end
  # Input: none
  # Output: none
  ("custom_battle_end",
    [
      (assign, "$g_custom_battle_team1_death_count", 0),
      (assign, "$g_custom_battle_team2_death_count", 0),
      (get_player_agent_no, ":player_agent"),
      (try_begin),
        (ge, ":player_agent", 0),
        (agent_get_team, ":player_team", ":player_agent"),
      (else_try),
        (assign, ":player_team", 0),
      (try_end),
      (try_for_agents, ":cur_agent"),
        (agent_is_human, ":cur_agent"),
        (neg|agent_is_alive, ":cur_agent"),
        (agent_get_team, ":cur_team", ":cur_agent"),
        (try_begin),
          (eq, ":cur_team", ":player_team"),
          (val_add, "$g_custom_battle_team1_death_count", 1),
        (else_try),
          (val_add, "$g_custom_battle_team2_death_count", 1),
        (try_end),
      (try_end),
  ]),
  
  # script_remove_troop_from_prison
  # Input: troop_no
  # Output: none
  #Other search terms: release, peace
  
  ("remove_troop_from_prison",
    [
      (store_script_param, ":troop_no", 1),
      (troop_set_slot, ":troop_no", slot_troop_prisoner_of_party, -1),
      (try_begin),
        (eq, "$do_not_cancel_quest", 0),
        (check_quest_active, "qst_rescue_lord_by_replace"),
        (quest_slot_eq, "qst_rescue_lord_by_replace", slot_quest_target_troop, ":troop_no"),
        (call_script, "script_cancel_quest", "qst_rescue_lord_by_replace"),
      (try_end),
      (try_begin),
        (eq, "$do_not_cancel_quest", 0),
        (check_quest_active, "qst_rescue_prisoner"),
        (quest_slot_eq, "qst_rescue_prisoner", slot_quest_target_troop, ":troop_no"),
        (call_script, "script_cancel_quest", "qst_rescue_prisoner"),
      (try_end),
      (try_begin),
        (check_quest_active, "qst_deliver_message_to_prisoner_lord"),
        (quest_slot_eq, "qst_deliver_message_to_prisoner_lord", slot_quest_target_troop, ":troop_no"),
        (call_script, "script_cancel_quest", "qst_deliver_message_to_prisoner_lord"),
      (try_end),
  ]),
  
  # script_debug_variables
  # Input: two variables which will be examined by coder, this script is only for debugging.
  # Output: none
  # ("debug_variables",
    # [
      # (store_script_param, ":unused", 1),
      # (store_script_param, ":unused_2", 2),
  # ]),
  
  #lord recruitment scripts begin
  ("troop_describes_troop_to_s15",
    [
      (store_script_param, ":troop_1", 1),
      (store_script_param, ":troop_2", 2),
      
      
      (str_store_troop_name, s15, ":troop_2"),
      
      (try_begin),
        (eq, ":troop_2", "trp_player"),
        (str_store_string, s15, "str_you"),
      (else_try),
        (eq, ":troop_2", ":troop_1"),
        (str_store_string, s15, "str_myself"),
      (else_try),
        (call_script, "script_troop_get_family_relation_to_troop", ":troop_2", ":troop_1"),
        (gt, reg0, 0),
        (str_store_string, s15, "str_my_s11_s15"),
      (else_try),
        (call_script, "script_troop_get_relation_with_troop", ":troop_2", ":troop_1"),
        (ge, reg0, 20),
        (str_store_string, s15, "str_my_friend_s15"),
      (try_end),
      
  ]),
  
  ("troop_describes_quarrel_with_troop_to_s14",
    #perhaps replace this with get_relevant_comment at a later date
    [
      (store_script_param, ":troop", 1),
      (store_script_param, ":troop_2", 2),
      
      (str_store_troop_name, s15, ":troop"),
      (str_store_troop_name, s16, ":troop_2"),
      
      (str_store_string, s14, "str_stop_gap__s15_is_the_rival_of_s16"),
      
      (try_begin),
        (eq, ":troop", "$g_talk_troop"),
        (call_script, "script_cf_test_lord_incompatibility_to_s17", ":troop", ":troop_2"),
        (str_store_string, s14, s17),
      (else_try),
        (eq, ":troop_2", "$g_talk_troop"),
        (call_script, "script_cf_test_lord_incompatibility_to_s17", ":troop_2", ":troop"),
        (str_store_string, s14, s17),
      (else_try),
        (str_store_string, s14, "str_general_quarrel"),
      (try_end),
      
  ]),
  
  ("cf_test_lord_incompatibility_to_s17", #writes rivalry chance to reg0
    [
      
      (store_script_param, ":source_lord", 1),
      (store_script_param, ":target_lord", 2),
      
      
      (assign, ":chance_of_rivalry", 0),
      
      (troop_get_slot, ":source_reputation", ":source_lord", slot_lord_reputation_type),
      (troop_get_slot, ":target_reputation", ":target_lord", slot_lord_reputation_type),
      
      (troop_get_type, reg15, ":target_lord"),
      (val_mod, reg15, 2),
      
      (str_store_troop_name, s18, ":target_lord"),
      
      (assign, ":divisor", 1),
      
      (call_script, "script_troop_get_family_relation_to_troop", ":target_lord", ":source_lord"),
      (assign, ":family_relationship", reg0),
      
      (try_begin),
        (gt, ":family_relationship", 0),
        (store_div, ":family_divisor", reg0, 5),
        (val_add, ":divisor", ":family_divisor"),
        (str_store_string, s18, "str_my_s11_s18"),
      (else_try),
        (gt, ":target_reputation", lrep_upstanding),
        (this_or_next|eq, ":source_reputation", lrep_debauched),
        (eq, ":source_reputation", lrep_selfrighteous),
        (str_store_string, s18, "str_the_socalled_s11_s18"),
      (try_end),
      
      
      (try_begin), #test if reps are compatible
        (eq, ":source_reputation", lrep_martial),
        (is_between, ":family_relationship", 1, 5), #uncles and cousins
        
        (assign, ":chance_of_rivalry", 100),
        (str_store_string, s17, "str_s18_would_cheat_me_of_my_inheritance_by_heaven_i_know_my_rights_and_im_not_going_to_back_down"),
      (else_try),
        (eq, ":source_reputation", lrep_martial),
        (eq, ":target_reputation", lrep_quarrelsome),
        (str_store_string, s17, "str_s18_once_questioned_my_honour_and_my_bravery_i_long_for_the_day_when_i_can_meet_him_in_battle_and_make_him_retract_his_statement"),
        (assign, ":chance_of_rivalry", 50),
        
      (else_try),
        (eq, ":source_reputation", lrep_martial),
        (eq, ":target_reputation", lrep_upstanding),
        (str_store_string, s17, "str_s18_once_questioned_my_judgment_in_battle_by_heaven_would_he_have_us_shirk_our_duty_to_smite_our_sovereigns_foes"),
        (assign, ":chance_of_rivalry", 50),
        
      (else_try),
        (eq, ":target_reputation", lrep_martial),
        (is_between, ":family_relationship", 1, 5),
        
        (assign, ":chance_of_rivalry", 100),
        (str_store_string, s17, "str_s18_seems_to_think_he_has_the_right_to_some_of_my_property_well_he_does_not"),
        
      (else_try),
        (eq, ":source_reputation", lrep_quarrelsome),
        (eq, ":target_reputation", lrep_martial),
        (str_store_string, s17, "str_s18_once_took_something_i_said_amiss_stubborn_bastard_wont_give_it_up_and_keeps_trying_to_get_me_to_recant_my_words"),
        (assign, ":chance_of_rivalry", 50),
        
      (else_try),
        (eq, ":source_reputation", lrep_quarrelsome),
        (eq, ":target_reputation", lrep_cunning),
        (str_store_string, s17, "str_s18_is_a_crafty_weasel_and_i_dont_trust_him_one_bit"),
        (assign, ":chance_of_rivalry", 100),
        
        
      (else_try),
        (eq, ":source_reputation", lrep_debauched),
        (eq, ":target_reputation", lrep_upstanding),
        (str_store_string, s17, "str_s18_i_despite_him_he_puts_on_such_a_nauseating_display_of_virtue_and_thinks_nothing_of_insulting_his_betters"),
        (assign, ":chance_of_rivalry", 100),
        
        #debauched insults upstanding
        
      (else_try),
        (eq, ":source_reputation", lrep_debauched),
        (eq, ":target_reputation", lrep_selfrighteous),
        (str_store_string, s17, "str_s18_entered_into_a_little_deal_with_me_and_is_now_trying_to_wriggle_out_of_it"),
        (assign, ":chance_of_rivalry", 100),
        
        #debauched insults selfrighteous
        
        
        
      (else_try),
        (eq, ":source_reputation", lrep_selfrighteous),
        (eq, ":target_reputation", lrep_debauched),
        (str_store_string, s17, "str_s18_once_ran_an_errand_for_me_and_now_thinks_i_owe_him_something_i_owe_his_ilk_nothing"),
        (assign, ":chance_of_rivalry", 100),
        #selfrighteous dismisses debauched
        
      (else_try),
        (eq, ":source_reputation", lrep_selfrighteous),
        (eq, ":target_reputation", lrep_goodnatured),
        (str_store_string, s17, "str_s18_is_soft_and_weak_and_not_fit_to_govern_a_fief_and_i_have_always_detested_him"),
        (assign, ":chance_of_rivalry", 100),
        
        
        
      (else_try),
        (eq, ":source_reputation", lrep_cunning),
        (eq, ":target_reputation", lrep_quarrelsome),
        (str_store_string, s17, "str_s18_is_a_quarrelsome_oaf_and_a_liability_in_my_opinion_and_ive_let_him_know_as_much"),
        (assign, ":chance_of_rivalry", 100),
        #cunning insults quarrelsome
        
      (else_try),
        (eq, ":source_reputation", lrep_cunning),
        (eq, ":target_reputation", lrep_goodnatured),
        (str_store_string, s17, "str_s18_i_am_sorry_to_say_is_far_too_softhearted_a_man_to_be_given_any_kind_of_responsibility_his_chivalry_will_allow_the_enemy_to_flee_to_fight_another_day_and_will_cost_the_lives_of_my_own_faithful_men"),
        (assign, ":chance_of_rivalry", 100),
        
        
      (else_try),
        (eq, ":source_reputation", lrep_goodnatured),
        (eq, ":target_reputation", lrep_cunning),
        (str_store_string, s17, "str_s18_seems_to_have_something_against_me_for_some_reason_i_dont_like_to_talk_ill_of_people_but_i_think_hes_can_be_a_bit_of_a_cad_sometimes"),
        (assign, ":chance_of_rivalry", 100),
        
      (else_try),
        (eq, ":source_reputation", lrep_goodnatured),
        (eq, ":target_reputation", lrep_selfrighteous),
        (str_store_string, s17, "str_s18_has_always_treated_me_contemptuously_although_i_have_done_him_no_wrong"),
        (assign, ":chance_of_rivalry", 100),
        
        
        
      (else_try),
        (eq, ":source_reputation", lrep_upstanding),
        (eq, ":target_reputation", lrep_debauched),
        (str_store_string, s17, "str_s18_is_thoroughly_dishonorable_and_a_compulsive_spinner_of_intrigues_which_i_fear_will_drag_us_into_wars_or_incite_rebellions"),
        (assign, ":chance_of_rivalry", 50),
        
      (else_try),
        (eq, ":source_reputation", lrep_upstanding),
        (eq, ":target_reputation", lrep_martial),
        (str_store_string, s17, "str_s18_disappoints_me_i_once_scolded_for_his_rashness_in_battle_and_he_took_offense_i_do_not_care_to_apologize_for_my_efforts_to_save_his_life_and_the_lives_of_his_men"),
        (assign, ":chance_of_rivalry", 50),
        
        #for commons
      (else_try),
        (this_or_next|eq, ":source_reputation", lrep_upstanding),
        (this_or_next|eq, ":source_reputation", lrep_martial),
        (eq, ":source_reputation", lrep_selfrighteous),
        (eq, ":target_reputation", lrep_roguish),
        (str_store_string, s17, "str_s18_squanders_money_and_carouses_in_a_way_most_unbefitting_a_noble_by_doing_so_he_disgraces_us_all"),
        (assign, ":chance_of_rivalry", 100),
        
        
      (else_try),
        (eq, ":source_reputation", lrep_roguish),
        (this_or_next|eq, ":target_reputation", lrep_upstanding),
        (this_or_next|eq, ":target_reputation", lrep_martial),
        (eq, ":target_reputation", lrep_selfrighteous),
        (str_store_string, s17, "str_s18_has_been_speaking_ill_of_me_behind_my_back_or_so_they_say"),
        (assign, ":chance_of_rivalry", 100),
        
        
      (else_try),
        (this_or_next|eq, ":source_reputation", lrep_quarrelsome),
        (this_or_next|eq, ":source_reputation", lrep_martial),
        (eq, ":source_reputation", lrep_selfrighteous),
        (eq, ":target_reputation", lrep_custodian),
        (str_store_string, s17, "str_s18_is_a_disgrace_reg3shehe_consorts_with_merchants_lends_money_at_interest_uses_coarse_language_and_shows_no_attempt_to_uphold_the_dignity_of_the_honor_bestowed_upon_reg3herhim"),
        (assign, ":chance_of_rivalry", 100),
        
      (else_try),
        (eq, ":source_reputation", lrep_custodian),
        (this_or_next|eq, ":target_reputation", lrep_quarrelsome),
        (this_or_next|eq, ":target_reputation", lrep_martial),
        (eq, ":target_reputation", lrep_selfrighteous),
        (str_store_string, s17, "str_s18_has_condemned_me_for_engaging_in_commerce_what_could_possibly_be_wrong_with_that"),
        (assign, ":chance_of_rivalry", 100),
        
        
      (else_try),
        (this_or_next|eq, ":source_reputation", lrep_debauched),
        (this_or_next|eq, ":source_reputation", lrep_martial),
        (eq, ":source_reputation", lrep_selfrighteous),
        (eq, ":target_reputation", lrep_benefactor),
        (str_store_string, s17, "str_s18_i_have_heard_has_been_encouraging_seditious_ideas_among_the_peasantry__a_foolish_move_which_endangers_us_all"),
        (assign, ":chance_of_rivalry", 100),
        
        
      (else_try),
        (eq, ":source_reputation", lrep_benefactor),
        (this_or_next|eq, ":target_reputation", lrep_debauched),
        (this_or_next|eq, ":target_reputation", lrep_martial),
        (eq, ":target_reputation", lrep_selfrighteous),
        (str_store_string, s17, "str_s18_has_called_me_out_for_the_way_i_deal_with_my_tenants_well_so_be_it_if_i_teach_them_that_they_are_the_equal_of_anyone_with_socalled_gentle_blood_what_is_it_to_reg3herhim"),
        (assign, ":chance_of_rivalry", 100),
        
        
        #lady incompatibilities
      (else_try),
        (eq, ":source_reputation", lrep_conventional),
        (this_or_next|eq, ":target_reputation", lrep_martial),
        (eq, ":target_reputation", lrep_selfrighteous),
        (str_store_string, s17, "str_a_most_gallant_gentleman_who_knows_how_to_treat_a_lady"),
        (assign, ":chance_of_rivalry", -50),
        
      (else_try),
        (eq, ":source_reputation", lrep_conventional),
        (eq, ":target_reputation", lrep_quarrelsome),
        (str_store_string, s17, "str_a_base_cad"),
        (assign, ":chance_of_rivalry", 50),
        
        
      (else_try),
        (eq, ":source_reputation", lrep_adventurous),
        (eq, ":target_reputation", lrep_cunning),
        (str_store_string, s17, "str_a_man_who_treats_me_as_his_equal_which_is_rare"),
        (assign, ":chance_of_rivalry", -50),
        
      (else_try),
        (eq, ":source_reputation", lrep_adventurous),
        (this_or_next|eq, ":target_reputation", lrep_martial),
        (eq, ":target_reputation", lrep_debauched),
        (str_store_string, s17, "str_appears_to_value_me_with_his_estate_and_his_horse_as_prizes_worth_having"),
        (assign, ":chance_of_rivalry", 50),
        
      (else_try),
        (eq, ":source_reputation", lrep_adventurous),
        (neq, ":target_reputation", lrep_goodnatured),
        
        (str_store_string, s17, "str_a_bit_dull_but_what_can_you_expect"),
        (assign, ":chance_of_rivalry", 10),
        
      (else_try),
        (eq, ":source_reputation", lrep_otherworldly),
        (call_script, "script_troop_get_romantic_chemistry_with_troop", ":source_lord", ":target_lord"),
        (ge, reg0, 10),
        (str_store_string, s17, "str_the_man_whom_destiny_intends_for_me"),
        (assign, ":chance_of_rivalry", -50),
        
      (else_try),
        (eq, ":source_reputation", lrep_otherworldly),
        (lt, reg0, 0),
        
        (str_store_string, s17, "str_is_not_right_for_me__i_cannot_say_why_but_he_makes_my_skin_crawl"),
        (assign, ":chance_of_rivalry", 50),
        
        
      (else_try),
        (eq, ":source_reputation", lrep_ambitious),
        (this_or_next|eq, ":target_reputation", lrep_selfrighteous),
        (eq, ":target_reputation", lrep_cunning),
        (str_store_string, s17, "str_is_a_man_who_clearly_intends_to_make_his_mark_in_the_world"),
        (assign, ":chance_of_rivalry", -20),
        
      (else_try),
        (eq, ":source_reputation", lrep_ambitious),
        (eq, ":target_reputation", lrep_goodnatured),
        
        (str_store_string, s17, "str_is_a_layabout_a_naif_prey_for_others_who_are_cleverer_than_he"),
        (assign, ":chance_of_rivalry", 30),
        
        
      (else_try),
        (eq, ":source_reputation", lrep_moralist),
        (eq, ":target_reputation", lrep_upstanding),
        
        (str_store_string, s17, "str_is_a_man_of_stalwart_character"),
        (assign, ":chance_of_rivalry", -50),
        
      (else_try),
        (eq, ":source_reputation", lrep_moralist),
        (this_or_next|eq, ":target_reputation", lrep_debauched),
        (eq, ":target_reputation", lrep_cunning),
        
        (str_store_string, s17, "str_appears_to_be_a_man_of_low_morals"),
        (assign, ":chance_of_rivalry", 50),
        
      (else_try),
        (eq, ":source_reputation", lrep_moralist),
        (eq, ":target_reputation", lrep_quarrelsome),
        
        (str_store_string, s17, "str_appears_to_be_a_man_who_lacks_selfdiscipline"),
        (assign, ":chance_of_rivalry", 50),
        
      (else_try),
        
        
      (try_end),
      
      (val_div, ":chance_of_rivalry", ":divisor"),
      
      (assign, reg0, ":chance_of_rivalry"),
      
      (neq, ":chance_of_rivalry", 0),
      #	(eq, ":incompatibility_found", 1), #cf can be removed with this
      
  ]),
  
  ("troop_get_romantic_chemistry_with_troop", #source is lady, target is man
    [
      (store_script_param, ":source_lady", 1),
      (store_script_param, ":target_lord", 2),
      (store_random_in_range, "$romantic_attraction_seed", 0, 7),
      (store_add, ":chemistry_sum", ":source_lady", ":target_lord"),
      (val_add, ":chemistry_sum", "$romantic_attraction_seed"),
      
      #This calculates (modula ^ 2) * 3
      (store_mod, ":chemistry_remainder", ":chemistry_sum", 6),
      (val_mul, ":chemistry_remainder", ":chemistry_remainder"), #0, 1, 4, 9, 16, 25
      (val_mul, ":chemistry_remainder", 3), #0, 3, 12, 27, 48, 75
      
      (store_attribute_level, ":romantic_chemistry", ":target_lord", ca_charisma),
      (val_sub, ":romantic_chemistry", ":chemistry_remainder"),
      
      (val_mul, ":romantic_chemistry", 2),
      (try_begin),
        (troop_slot_eq, ":source_lady", slot_lord_reputation_type, lrep_adventurous),
        (val_add,":romantic_chemistry", 4),
      (else_try),
        (troop_slot_eq, ":source_lady", slot_lord_reputation_type, lrep_otherworldly),
        (val_add,":romantic_chemistry", 2),
      (else_try),
        (troop_slot_eq, ":source_lady", slot_lord_reputation_type, lrep_ambitious),
        (val_sub,":romantic_chemistry", 2),
      (else_try),
        (troop_slot_eq, ":source_lady", slot_lord_reputation_type, lrep_moralist),
        (val_sub,":romantic_chemistry", 3),
      (try_end),
      
      (assign, reg0, ":romantic_chemistry"),
      
      #examples :
      #For a charisma of 18, yields (18 - 0) * 2 = 36, (18 - 3) * 2 = 30, (18 - 12) * 2 = 12, (18 - 27) * 2 = -18, (18 - 48) * 2 = -60
      #For a charisma of 10, yields (10 - 0) * 2 = 20, (10 - 3) * 2 = 14, (10 - 12) * 2 = -4, (10 - 27) * 2 = -34, (10 - 48) * 2 = -76
      #For a charisma of 7, yields  (7 - 0) * 2 = 14,  (7 - 3) * 2 = 8,   (7 - 12) * 2 = -10, (7 - 27) * 2 = -40,  (7 - 48) * 2 = -82
      
      #15 is high attraction, 0 is moderate attraction, -76 is lowest attraction
  ]),
  
  
  # ("cf_troop_get_romantic_attraction_to_troop", #source is lady, target is man
    # [
      
      # (store_script_param, ":source_lady", 1),
      # (store_script_param, ":target_lord", 2),
      
      # (assign, ":weighted_romantic_assessment", 0),
      
      # (troop_get_type, ":source_is_female", ":source_lady"),
      # (val_mod, ":source_is_female", 2),
      # (eq, ":source_is_female", 1),
      # (troop_get_type, ":target_is_female", ":target_lord"),
      # (val_mod, ":target_is_female", 2),
      # (eq, ":target_is_female", 0),
      
      # (call_script, "script_troop_get_romantic_chemistry_with_troop", ":source_lady", ":target_lord"),
      # (assign, ":romantic_chemistry", reg0),
      # (store_attribute_level, ":hulk", ":target_lord", ca_strength),
      # (val_sub, ":hulk", 17),
      # #objective attraction - average renown
      # (troop_get_slot, ":modified_renown", ":target_lord", slot_troop_renown),
      # (assign, ":lady_status", 60),
      # (val_div, ":modified_renown", 5),
      # (val_sub, ":modified_renown", ":lady_status"),
      # (val_min, ":modified_renown", 60),
      
      
      
      # #weight values
      # (try_begin),
        # (assign, ":personality_match", 0),
        # (call_script, "script_cf_test_lord_incompatibility_to_s17", ":source_lady", ":target_lord"),
        # (store_sub, ":personality_match", 0, reg0),
      # (try_end),
      
      # (troop_get_slot, ":lady_reputation", ":source_lady", slot_lord_reputation_type),
      # (try_begin),
        # (eq, ":lady_reputation", lrep_ambitious),
        # (val_mul, ":modified_renown", 2),
        # (val_div, ":romantic_chemistry", 2),
      # (else_try),
        # (eq, ":lady_reputation", lrep_otherworldly),
        # (val_div, ":modified_renown", 2),
        # (val_mul, ":romantic_chemistry", 2),
        # (val_add, ":romantic_chemistry", ":hulk"),
      # (else_try),
        # (eq, ":lady_reputation", lrep_adventurous),
        # (val_div, ":modified_renown", 2),
        # (val_add, ":romantic_chemistry", ":hulk"),
      # (else_try),
        # (eq, ":lady_reputation", lrep_moralist),
        # (val_div, ":modified_renown", 2),
        # (val_div, ":romantic_chemistry", 2),
      # (else_try),
        # (eq, ":lady_reputation", lrep_conventional),
        # (val_mul, ":modified_renown", 5),
        # (val_div, ":modified_renown", 6),
        # (val_add, ":romantic_chemistry", ":hulk"),
      # (try_end),
      
      # (val_add, ":weighted_romantic_assessment", ":romantic_chemistry"),
      # (val_add, ":weighted_romantic_assessment", ":personality_match"),
      # (val_add, ":weighted_romantic_assessment", ":modified_renown"),
      
      # (assign, reg0, ":weighted_romantic_assessment"),
      
  # ]),
  
  
  ("cf_random_political_event", #right now, just enmities
    [
      
      (store_random_in_range, ":lord_1", active_npcs_begin, active_npcs_end),
      (store_random_in_range, ":lord_2", active_npcs_begin, active_npcs_end),
      
      (troop_slot_eq, ":lord_1", slot_troop_occupation, slto_kingdom_hero),
      (troop_slot_eq, ":lord_2", slot_troop_occupation, slto_kingdom_hero),
      
      (neq, ":lord_1", ":lord_2"),
      
      (val_add, "$total_political_events", 1),
      
      (store_troop_faction, ":lord_1_faction", ":lord_1"),
      (store_troop_faction, ":lord_2_faction", ":lord_2"),
      
      (neq, ":lord_1_faction", "fac_adventurers"),
      (neq, ":lord_2_faction", "fac_adventurers"),
      
      (assign, reg8, "$total_political_events"),
      
      
      (faction_get_slot, ":faction_1_leader", ":lord_1_faction", slot_faction_leader),
      (faction_get_slot, ":faction_2_leader", ":lord_2_faction", slot_faction_leader),
      
      (this_or_next|eq, ":lord_1_faction", ":lord_2_faction"),
      (this_or_next|eq, ":lord_1", ":faction_1_leader"),
      (eq, ":lord_2", ":faction_2_leader"),
      
      
      (call_script, "script_troop_get_relation_with_troop", ":lord_1", ":lord_2"),
      (assign, ":relation", reg0),
      
      
      (store_random_in_range, ":random", 0, 100),
      
      (try_begin),
        #reconciliation
        #The chance of a liege reconciling two quarreling vassals is equal to (relationship with lord 1 x relationship with lord 2) / 4
        
        (eq, ":lord_1_faction", ":lord_2_faction"),
        (neq, ":faction_1_leader", "trp_player"),
        
        (le, ":relation", -10),
        
        #		(ge, "$total_political_events", 5000),
        
        (call_script, "script_troop_get_relation_with_troop", ":lord_1", ":faction_1_leader"),
        (gt, reg0, 0),
        (assign, ":lord_1_leader_rel", reg0),
        
        (call_script, "script_troop_get_relation_with_troop", ":lord_2", ":faction_1_leader"),
        (gt, reg0, 0),
        (store_mul, ":reconciliation_chance", ":lord_1_leader_rel", reg0),
        (val_div, ":reconciliation_chance", 4),	#was 2 before
        
        (le, ":random", ":reconciliation_chance"),
        
        (str_store_troop_name, s4, ":faction_1_leader"),
        (str_store_troop_name, s5, ":lord_1"),
        (str_store_troop_name, s6, ":lord_2"),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "str_check_reg8_s4_reconciles_s5_and_s6_"),
        (try_end),
        
        (call_script, "script_troop_change_relation_with_troop", ":lord_1", ":lord_2", 20),
        (val_add, "$total_random_quarrel_changes", 20),
      (else_try),	#lord intervenes in quarrel
        (eq, ":lord_1_faction", ":lord_2_faction"),
        
        (le, ":relation", -10),
        #		(ge, ":random", 50),
        (try_begin),
          (eq, ":faction_1_leader", "trp_player"),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (display_message, "str_diagnostic__player_should_receive_consultation_quest_here_if_not_already_active"),
          (try_end),
          (neg|check_quest_active, "qst_consult_with_minister"),
          (neg|check_quest_active, "qst_resolve_dispute"),
          (eq, "$g_minister_notification_quest", 0),
          (assign, "$g_minister_notification_quest", "qst_resolve_dispute"),
          (quest_set_slot, "qst_resolve_dispute", slot_quest_target_troop, ":lord_1"),
          (quest_set_slot, "qst_resolve_dispute", slot_quest_object_troop, ":lord_2"),
          
          (call_script, "script_add_notification_menu", "mnu_notification_player_should_consult", 0, 0),
          
          
        (else_try),
          (call_script, "script_troop_get_relation_with_troop", ":lord_1", ":faction_1_leader"),
          (assign, ":lord_1_rel_w_leader", reg0),
          
          (call_script, "script_troop_get_relation_with_troop", ":lord_2", ":faction_1_leader"),
          (assign, ":lord_2_rel_w_leader", reg0),
          
          (store_random_in_range, ":another_random", -5, 5),
          
          (val_add, ":lord_1_rel_w_leader", ":another_random"),
          
          (try_begin),
            (ge, ":lord_1_rel_w_leader", ":lord_2_rel_w_leader"),
            (assign, ":winner_lord", ":lord_1"),
            (assign, ":loser_lord", ":lord_2"),
          (else_try),
            (assign, ":loser_lord", ":lord_1"),
            (assign, ":winner_lord", ":lord_2"),
          (try_end),
          
          (str_store_troop_name, s4, ":faction_1_leader"),
          (str_store_troop_name, s5, ":winner_lord"),
          (str_store_troop_name, s6, ":loser_lord"),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (display_message, "str_check_reg8_s4_rules_in_s5s_favor_in_quarrel_with_s6_"),
          (try_end),
          
          (call_script, "script_add_log_entry", logent_ruler_intervenes_in_quarrel, ":faction_1_leader",  ":loser_lord", ":winner_lord", ":lord_1_faction"), #faction leader is actor, loser lord is center object, winner lord is troop_object
          
          (call_script, "script_troop_change_relation_with_troop", ":winner_lord", ":faction_1_leader", 10),
          (call_script, "script_troop_change_relation_with_troop", ":loser_lord", ":faction_1_leader", -20),
          (val_add, "$total_random_quarrel_changes", -10),
          
        (try_end),
        
        
      (else_try), #new quarrel - companions
        (is_between, ":lord_1", companions_begin, companions_end),
        (is_between, ":lord_2", companions_begin, companions_end),
        
        (ge, ":relation", -10),
        (this_or_next|troop_slot_eq, ":lord_1", slot_troop_personalityclash_object, ":lord_2"),
        (troop_slot_eq, ":lord_1", slot_troop_personalityclash2_object, ":lord_2"),
        
        (str_store_troop_name, s5, ":lord_1"),
        (str_store_troop_name, s6, ":lord_2"),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "str_check_reg8_new_rivalry_generated_between_s5_and_s6"),
        (try_end),
        
        (call_script, "script_troop_change_relation_with_troop", ":lord_1", ":lord_2", -30),
        (val_add, "$total_random_quarrel_changes", -30),
        
        
      (else_try), #new quarrel - others
        (eq, ":lord_1_faction", ":lord_2_faction"),
        
        (ge, ":relation", -10), #can have two quarrels
        
        (call_script, "script_cf_test_lord_incompatibility_to_s17", ":lord_1", ":lord_2"),
        (assign, ":chance_of_enmity", reg0),
        (gt, ":chance_of_enmity", 0),
        
        
        (lt, ":random", ":chance_of_enmity"), #50 or 100 percent, usually
        
        
        (str_store_troop_name, s5, ":lord_1"),
        (str_store_troop_name, s6, ":lord_2"),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "str_check_reg8_new_rivalry_generated_between_s5_and_s6"),
        (try_end),
        
        (call_script, "script_troop_change_relation_with_troop", ":lord_1", ":lord_2", -30),
        (val_add, "$total_random_quarrel_changes", -30),
        
        #		(call_script, "script_update_troop_notes", ":lord_1"),
        #		(call_script, "script_update_troop_notes", ":lord_2"),
      (else_try), #a lord attempts to suborn a character
        (store_current_hours, ":hours"),
        (ge, ":hours", 24),
        (neq, "$campaign_type", camp_storyline),
        (neq, ":lord_1_faction", ":lord_2_faction"),
        #		(eq, ":lord_1", ":faction_1_leader"),
        (is_between, ":lord_1_faction", kingdoms_begin, kingdoms_end),
        
        (call_script, "script_cf_troop_can_intrigue", ":lord_2", 0),
        (neq, ":lord_2", ":faction_2_leader"),
        (neq, ":lord_2", ":faction_1_leader"),
        
        (str_store_troop_name, s5, ":faction_1_leader"),
        (str_store_troop_name, s6, ":lord_2"),
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (display_message, "str_check_reg8_s5_attempts_to_win_over_s6"),
        (try_end),
        
        (call_script, "script_calculate_troop_political_factors_for_liege", ":lord_2", ":faction_1_leader"),
        (assign, ":lord_1_score", reg0),
        
        (call_script, "script_calculate_troop_political_factors_for_liege", ":lord_2", ":faction_2_leader"),
        (assign, ":faction_2_leader_score", reg0),
        
        (try_begin),
          (gt, ":lord_1_score", ":faction_2_leader_score"),
          
          
          (try_begin),
            (ge, "$cheat_mode", 1),
            (str_store_troop_name, s4, ":lord_2"),
            (display_message, "@{!}DEBUG - {s4} faction changed in subornment"),
          (try_end),
          
          (call_script, "script_change_troop_faction", ":lord_2", ":lord_1_faction"),
        (try_end),
      (try_end),
      
      
      
  ]),
  
  
  #this calculates the average number of rivalries per lord, giving a rough indication of how easily a faction may be divided
  #fairly expensive in terms of CPU
  ("evaluate_realm_stability",
    
    [
      (store_script_param, ":realm", 1),
      
      (assign, ":total_lords", 0),
      (assign, ":total_restless_lords", 0),
      (assign, ":total_disgruntled_lords", 0),
      #	(neq, ":realm", "fac_adventurers"),
      (faction_get_slot, ":liege", ":realm", slot_faction_leader),
      
      (try_for_range, ":lord", active_npcs_begin, active_npcs_end),
        (troop_slot_eq, ":lord", slot_troop_occupation, slto_kingdom_hero),
        (store_troop_faction, ":lord_faction", ":lord"),
        (eq, ":lord_faction", ":realm"),
        (val_add, ":total_lords", 1),
        
        (call_script, "script_calculate_troop_political_factors_for_liege", ":lord", ":liege"),
        (try_begin),
          (le, reg3, defection_trigger),
          (val_add, ":total_disgruntled_lords", 1),
        (else_try),
          (le, reg3, 10),
          (val_add, ":total_restless_lords", 1),
        (try_end),
      (try_end),
      
      (try_begin),
        (gt, ":total_lords", 0),
        (store_mul, ":instability_quotient", ":total_disgruntled_lords", 100),
        (val_div, ":instability_quotient", ":total_lords"),
        
        (store_mul, ":restless_quotient", ":total_restless_lords", 100),
        (val_div, ":restless_quotient", ":total_lords"),
        
        (store_mul, ":combined_quotient", ":instability_quotient", 2),
        (val_add, ":combined_quotient", ":restless_quotient"),
        (faction_set_slot, ":realm", slot_faction_instability, ":combined_quotient"),
        
        (assign, reg0, ":instability_quotient"),
        (assign, reg1, ":restless_quotient"),
        (assign, reg1, ":restless_quotient"),
      (else_try),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (str_store_faction_name, s1, ":realm"),
          (display_message, "str_s1_has_no_lords"),
        (try_end),
        (assign, reg0, 0),
        (assign, reg1, 0),
      (try_end),
      
  ]),
  
  
  
  #lord recruitment scripts end
  
  #called from game_event_simulate_battle
  #Includes a number of consequences that follow on battles, mostly affecting relations between different NPCs
  #This only fires from complete victories
  ("battle_political_consequences",
    [
      (store_script_param, ":defeated_party", 1),
      (store_script_param, ":winner_party", 2),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (str_store_party_name, s4, ":winner_party"),
        (str_store_party_name, s5, ":defeated_party"),
        (display_message, "str_do_political_consequences_for_s4_victory_over_s5"),
      (try_end),
      
      (store_faction_of_party, ":winner_faction", ":winner_party"),
      (try_begin),
        (eq, ":winner_party", "p_main_party"),
        (assign, ":winner_faction", "$players_kingdom"),
      (try_end),
      
      (party_get_template_id, ":defeated_party_template", ":defeated_party"),
      
      #did the battle involve travellers?
      (try_begin),
        (this_or_next|eq, ":defeated_party_template", "pt_village_farmers"),
        (eq, ":defeated_party_template", "pt_kingdom_caravan_party"),
        
        
        (party_get_slot, ":destination", ":defeated_party", slot_party_ai_object),
        (party_get_slot, ":origin", ":defeated_party", slot_party_last_traded_center),
        
        (call_script, "script_add_log_entry", logent_traveller_attacked, ":winner_party",  ":origin", ":destination", ":winner_faction"),
        
        (try_begin),
          (eq, "$cheat_mode", 2),
          (neg|is_between, ":winner_faction", kingdoms_begin, kingdoms_end),
          (str_store_string, s65, "str_bandits_attacked_a_party_on_the_roads_so_a_bounty_is_probably_available"),
          (call_script, "script_add_notification_menu", "mnu_debug_alert_from_s65", 0, 0),
          
          (str_store_party_name, s15, ":origin"),
          (str_store_party_name, s16, ":destination"),
          (display_message, "str_travellers_attacked_on_road_from_s15_to_s16"),
        (try_end),
        
        
        #by logging the faction and the party, we can verify that the party number is unlikely to have been reassigned - or at any rate, that the factions have not changed
      (try_end),
      
      (assign, ":renown_ratio", 1),  #avoid div by zero below
      (assign, ":lt_renown", 1),  #avoid div by zero below
      (try_begin),
        (party_collect_attachments_to_party, ":defeated_party", "p_temp_party_2"),
        (party_get_num_companion_stacks, ":num_stacks", "p_temp_party_2"),
        (try_begin),
          (try_for_range, ":troop_iterator", 0, ":num_stacks"),
            (party_stack_get_troop_id, ":cur_troop_id", "p_temp_party_2", ":troop_iterator"),
            (troop_slot_eq, ":cur_troop_id", slot_troop_occupation, slto_kingdom_hero),
            (troop_get_slot, ":l_renown", ":cur_troop_id", slot_troop_renown),
            (val_add, ":lt_renown", ":l_renown"),
          (try_end),
        (try_end),
        (try_begin),
          (this_or_next|eq, ":defeated_party_template", "pt_village_farmers"),
          (this_or_next|eq, ":defeated_party_template", "pt_village_defenders"),
          (this_or_next|eq, ":defeated_party_template", "pt_runaway_serfs"),
          (this_or_next|eq, ":defeated_party_template", "pt_spy"),
          (this_or_next|eq, ":defeated_party_template", "pt_sacrificed_messenger"),
          (this_or_next|eq, ":defeated_party_template", "pt_sacerdotes_party"),
          (this_or_next|eq, ":defeated_party_template", "pt_messenger_party"),
          (eq, ":defeated_party_template", "pt_paganos_party"),
          (assign, ":l_renown", 3),
          (val_add, ":lt_renown", ":l_renown"),
        (else_try),
          (this_or_next|eq, ":defeated_party_template", "pt_looters"),
          (this_or_next|eq, ":defeated_party_template", "pt_spy_partners"),
          (this_or_next|eq, ":defeated_party_template", "pt_fisher_ship"),
          (this_or_next|eq, ":defeated_party_template", "pt_traveller_ship"),
          (this_or_next|eq, ":defeated_party_template", "pt_scout_party"),
          (this_or_next|eq, ":defeated_party_template", "pt_leaded_looters"),
          (this_or_next|eq, ":defeated_party_template", "pt_walrus_hunter"),
          (this_or_next|eq, ":defeated_party_template", "pt_wessex_foragers"),
          (this_or_next|eq, ":defeated_party_template", "pt_norway_looters"),
          (eq, ":defeated_party_template", "pt_bandits_awaiting_ransom"),
          (assign, ":l_renown", 5),
          (val_add, ":lt_renown", ":l_renown"),
        (else_try),
          (this_or_next|eq, ":defeated_party_template", "pt_troublesome_bandits"),
          (this_or_next|eq, ":defeated_party_template", "pt_mountain_bandits"),
          (this_or_next|eq, ":defeated_party_template", "pt_fianna"),
          (this_or_next|eq, ":defeated_party_template", "pt_frank_looters_1"),
          (this_or_next|eq, ":defeated_party_template", "pt_frank_looters_2"),
          (this_or_next|eq, ":defeated_party_template", "pt_raider_party"),
          (this_or_next|eq, ":defeated_party_template", "pt_slave_trader_ship"),
          (eq, ":defeated_party_template", "pt_forest_bandits"),
          (assign, ":l_renown", 15),
          (val_add, ":lt_renown", ":l_renown"),
        (else_try),
          (this_or_next|eq, ":defeated_party_template", "pt_steppe_bandits"),
          (this_or_next|eq, ":defeated_party_template", "pt_taiga_bandits"),
          (this_or_next|eq, ":defeated_party_template", "pt_desert_bandits"),
          (this_or_next|eq, ":defeated_party_template", "pt_deserters"),
          (this_or_next|eq, ":defeated_party_template", "pt_kingdom_caravan_party"),
          (this_or_next|eq, ":defeated_party_template", "pt_merchant_caravan"),
          (this_or_next|eq, ":defeated_party_template", "pt_ambushers"),
          (this_or_next|eq, ":defeated_party_template", "pt_wessex_patrol"),
          (this_or_next|eq, ":defeated_party_template", "pt_village_elder"),
          (eq, ":defeated_party_template", "pt_sea_raiders"),
          (assign, ":l_renown", 25),
          (val_add, ":lt_renown", ":l_renown"),
        (else_try),
          (this_or_next|is_between, ":defeated_party_template", "pt_sea_raiders_ships", "pt_chimney_smoke"),
          (this_or_next|eq, ":defeated_party_template", "pt_manhunters"),
          (this_or_next|eq, ":defeated_party_template", "pt_sea_king_danish"),
          (this_or_next|eq, ":defeated_party_template", "pt_sea_king_norweg"),
          (this_or_next|eq, ":defeated_party_template", "pt_sea_raiders2"),
          (eq, ":defeated_party_template", "pt_adv_party"),
          (assign, ":l_renown", 30),
          (val_add, ":lt_renown", ":l_renown"),
        (else_try),
          (this_or_next|party_slot_eq, ":defeated_party", slot_party_type, spt_town),
          (party_slot_eq, ":defeated_party", slot_party_type, spt_castle),
          (assign, ":l_renown", 3),
          (val_add, ":lt_renown", ":l_renown"),
        (try_end),
      (try_end),
      
      #winner consequences:
      #1)   leader improves relations with other leaders
      #2)  Player given credit for victory if the victorious party is following the player's advice
      (try_begin),
        (party_get_template_id, ":winner_party_template", ":winner_party"),
        (this_or_next|eq, ":winner_party_template", "pt_adv_party"),
        (eq, ":winner_party_template", "pt_kingdom_hero_party"),
        (neq, ":winner_party", "p_main_party"),
        #Do not do for player party, as is included in post-battle dialogs
        (assign, ":base_renown", 40),
        (assign, ":base_renown_l", 10),
        
        (party_stack_get_troop_id, ":winner_leader", ":winner_party", 0),
        (is_between, ":winner_leader", active_npcs_begin, active_npcs_end),
        
        (store_faction_of_party, ":winner_faction", ":winner_party"),
        (assign, ":wt_renown", 1),  #avoid eventual div by zero below
        
        (party_collect_attachments_to_party, ":winner_party", "p_temp_party_2"),
        (party_get_num_companion_stacks, ":num_stacks", "p_temp_party_2"),
        (try_for_range, ":troop_iterator", 0, ":num_stacks"),
          (party_stack_get_troop_id, ":cur_troop_id", "p_temp_party_2", ":troop_iterator"),
          (is_between, ":cur_troop_id", active_npcs_including_player_begin, active_npcs_end),
          (try_begin),
            (eq, ":cur_troop_id", "trp_kingdom_heroes_including_player_begin"),
            (assign, ":cur_troop_id", "trp_player"),
          (try_end),
          (troop_get_slot, ":w_renown", ":cur_troop_id", slot_troop_renown),
          (val_add, ":wt_renown", ":w_renown"),
        (try_end),
        (party_clear, "p_temp_party_2"),
        (store_mul, ":wtm_renown", ":wt_renown", ":wt_renown"),
        (store_div, ":renown_ratio", ":wtm_renown", ":lt_renown"),
        
        (party_collect_attachments_to_party, ":winner_party", "p_temp_party_2"),
        (party_get_num_companion_stacks, ":num_stacks", "p_temp_party_2"),
        (try_for_range, ":troop_iterator", 0, ":num_stacks"),
          (party_stack_get_troop_id, ":cur_troop_id", "p_temp_party_2", ":troop_iterator"),
          (is_between, ":cur_troop_id", active_npcs_including_player_begin, active_npcs_end),
          (try_begin),
            (eq, ":cur_troop_id", "trp_kingdom_heroes_including_player_begin"),
            (assign, ":cur_troop_id", "trp_player"),
          (try_end),
          (troop_get_slot, ":w_renown", ":cur_troop_id", slot_troop_renown),
          (try_begin),
            (this_or_next|eq, ":defeated_party_template", "pt_adv_party"),
            (eq, ":defeated_party_template", "pt_kingdom_hero_party"),
            (store_mul, ":adj_renown", ":w_renown", ":base_renown_l"),
          (else_try),
            (store_mul, ":adj_renown", ":w_renown", ":base_renown"),
          (try_end),
          (store_div, ":renown_boost", ":adj_renown", ":renown_ratio"),
          (neq, ":cur_troop_id", "trp_player"),
          (val_add, ":w_renown", ":renown_boost"),
          (troop_set_slot, ":cur_troop_id", slot_troop_renown, ":w_renown"),
          
          (try_begin),
            (troop_get_slot, ":winner_lord_party", ":cur_troop_id", slot_troop_leaded_party),
            (party_is_active, ":winner_lord_party"),
            (call_script, "script_cf_party_under_player_suggestion", ":winner_lord_party"),
            (call_script, "script_add_log_entry", logent_player_suggestion_succeeded, "trp_player", -1, ":cur_troop_id", -1),
          (try_end),
          
          
          (store_faction_of_troop, ":troop_faction", ":cur_troop_id"),
          (eq, ":troop_faction", ":winner_faction"),
          (neq, ":cur_troop_id", ":winner_leader"),
          
          (try_begin),
            (eq, "$cheat_mode", 4),
            (str_store_troop_name, s15, ":cur_troop_id"),
            (str_store_troop_name, s16, ":winner_leader"),
            (display_message, "str_s15_shares_joy_of_victory_with_s16"),
          (try_end),
          
          (call_script, "script_troop_change_relation_with_troop", ":cur_troop_id", ":winner_leader", 6),
          (val_add, "$total_battle_ally_changes", 3),
          
        (try_end),
        (party_clear, "p_temp_party_2"),
      (try_end),
      
      #consequences of defeat,
      #1) -1 relation with lord per lord, plus -15 if there is an incompatible marshal
      #2)  losers under player suggestion blame the player
      #3) Some losers resent the victor lord
      #4) Possible quarrels over defeat
      (try_begin),
        (party_collect_attachments_to_party, ":defeated_party", "p_temp_party_2"),
        (party_get_num_companion_stacks, ":num_stacks", "p_temp_party_2"),
        (this_or_next|eq, ":winner_party_template", "pt_adv_party"),
        (eq, ":winner_party_template", "pt_kingdom_hero_party"),
        (try_begin),
          (gt, "$marshal_defeated_in_battle", 0),
          (str_store_troop_name, s15, "$marshal_defeated_in_battle"),
          (store_faction_of_troop, ":defeated_marshal_faction", "$marshal_defeated_in_battle"),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (display_message, "str_faction_marshal_s15_involved_in_defeat"),
          (try_end),
        (else_try),
          (eq, "$marshal_defeated_in_battle", "trp_player"),
          (eq, ":defeated_party", "p_main_party"),
          (faction_slot_eq, "fac_player_supporters_faction", slot_faction_state, sfs_active),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (display_message, "str_player_faction_marshal_involved_in_defeat"),
          (try_end),
        (else_try),
          (assign, "$marshal_defeated_in_battle", -1),
        (try_end),
        
        (try_for_range, ":troop_iterator", 0, ":num_stacks"),
          (party_stack_get_troop_id, ":cur_troop_id", "p_temp_party_2", ":troop_iterator"),
          (troop_slot_eq, ":cur_troop_id", slot_troop_occupation, slto_kingdom_hero),
          (is_between, ":cur_troop_id", active_npcs_including_player_begin, active_npcs_end),
          (try_begin),
            (eq, ":cur_troop_id", "trp_kingdom_heroes_including_player_begin"),
            (assign, ":cur_troop_id", "trp_player"),
          (try_end),
          (troop_get_slot, ":l_renown", ":cur_troop_id", slot_troop_renown),
          (try_begin),
            (this_or_next|eq, ":defeated_party_template", "pt_adv_party"),
            (eq, ":defeated_party_template", "pt_kingdom_hero_party"),
            (store_mul, ":adjl_renown", ":l_renown", ":base_renown_l"),
          (else_try),
            (store_mul, ":adjl_renown", ":l_renown", ":base_renown"),
          (try_end),
          (store_div, ":renown_loss", ":adjl_renown", ":renown_ratio"),
          (try_begin),
            (gt, ":l_renown", 400),
            (neq, ":cur_troop_id", "trp_player"),
            (val_sub, ":l_renown", ":renown_loss"),
            (troop_set_slot, ":cur_troop_id", slot_troop_renown, ":l_renown"),
          (try_end),
          
          (try_begin), #is party under suggestion?
            (troop_get_slot, ":defeated_lord_party", ":cur_troop_id", slot_troop_leaded_party),
            (party_is_active, ":defeated_lord_party"),
            
            #is party under suggestion?
            (call_script, "script_cf_party_under_player_suggestion", ":defeated_lord_party"),
            (call_script, "script_add_log_entry", logent_player_suggestion_failed, "trp_player", -1, ":cur_troop_id", -1),
          (try_end),
          
          
          (store_faction_of_troop, ":troop_faction", ":cur_troop_id"),
          
          (faction_get_slot, ":faction_leader", ":troop_faction", slot_faction_leader),
          (neq, ":cur_troop_id", ":faction_leader"),
          (gt, ":faction_leader", -1),
          #Lose one point relation with liege
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, s14, ":cur_troop_id"),
            (str_store_faction_name, s15, ":troop_faction"),
            
            (display_message, "str_s14_of_s15_defeated_in_battle_loses_one_point_relation_with_liege"),
          (try_end),
          
          (try_begin),
            (this_or_next|neq, ":faction_leader", "trp_player"), #if leader is zero at beginning of game. I'm not entirely sure how this could happen...
            (eq, "$players_kingdom", ":troop_faction"),
            
            (call_script, "script_troop_change_relation_with_troop", ":cur_troop_id", ":faction_leader", -1),
            (val_add, "$total_battle_ally_changes", -1),
          (try_end),
          
          
          (call_script, "script_faction_inflict_war_damage_on_faction", ":winner_faction", ":troop_faction", 10),
          
          
          (try_begin),
            (this_or_next|is_between, ":winner_leader", active_npcs_begin, active_npcs_end),
            (eq, ":winner_leader", "trp_player"),
            
            (this_or_next|neq, ":winner_leader", "trp_player"), #prevents winner leader being zero, for whatever reason
            (eq, ":winner_party", "p_main_party"),
            
            (this_or_next|troop_slot_eq, ":cur_troop_id", slot_lord_reputation_type, lrep_quarrelsome),
            (this_or_next|troop_slot_eq, ":cur_troop_id", slot_lord_reputation_type, lrep_selfrighteous),
            (troop_slot_eq, ":cur_troop_id", slot_lord_reputation_type, lrep_debauched),
            
            (call_script, "script_troop_change_relation_with_troop", ":cur_troop_id", ":winner_leader", -1),
            (val_add, "$total_battle_enemy_changes", -1),
            
            (try_begin),
              (eq, "$cheat_mode", 1),
              (str_store_troop_name, s14, ":cur_troop_id"),
              (str_store_troop_name, s15, ":winner_leader"),
              
              (display_message, "str_s14_defeated_in_battle_by_s15_loses_one_point_relation"),
            (try_end),
            
            
          (try_end),
          
          (gt, "$marshal_defeated_in_battle", -1),
          (eq, ":troop_faction", ":defeated_marshal_faction"),
          (str_store_troop_name, s14, ":cur_troop_id"),
          
          (call_script, "script_cf_test_lord_incompatibility_to_s17", ":cur_troop_id", "$marshal_defeated_in_battle"),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (display_message, "str_s14_blames_s15_for_defeat"),
          (try_end),
          
          (call_script, "script_add_log_entry", logent_lord_blames_defeat, ":cur_troop_id", "$marshal_defeated_in_battle", ":faction_leader", ":winner_faction"),
          
          (call_script, "script_troop_change_relation_with_troop", ":cur_troop_id", ":faction_leader", -15),
          (val_add, "$total_battle_ally_changes", -15),
          
          (neq, "$marshal_defeated_in_battle", ":faction_leader"),
          (call_script, "script_troop_change_relation_with_troop", ":cur_troop_id", "$marshal_defeated_in_battle", -15),
          (val_add, "$total_battle_ally_changes", -15),
          
        (try_end),
        
        (party_clear, "p_temp_party_2"),
      (try_end),
  ]),
  
  ("faction_inflict_war_damage_on_faction",
    [
      (store_script_param, ":actor_faction", 1),
      (store_script_param, ":target_faction", 2),
      (store_script_param, ":amount", 3),
      
      
      (store_add, ":slot_war_damage", ":target_faction", slot_faction_war_damage_inflicted_on_factions_begin),
      (val_sub, ":slot_war_damage", kingdoms_begin),
      (faction_get_slot, ":cur_war_damage", ":actor_faction", ":slot_war_damage"),
      
      (val_add, ":cur_war_damage", ":amount"),
      (faction_set_slot, ":actor_faction", ":slot_war_damage", ":cur_war_damage"),
      
      
      (try_begin),
        (ge, "$cheat_mode", 1),
        (str_store_faction_name, s4, ":actor_faction"),
        (str_store_faction_name, s5, ":target_faction"),
        (assign, reg3, ":cur_war_damage"),
        (assign, reg4, ":amount"),
        (display_message, "@{!}{s4} inflicts {reg4} damage on {s5}, raising total inflicted to {reg3}"),
      (try_end),
      
      
      (faction_get_slot, ":faction_marshal", ":target_faction", slot_faction_marshal),
      (try_begin),
        (ge, ":faction_marshal", 0),
        (gt, ":amount", 0),
        
        (troop_get_slot, ":controversy", ":faction_marshal", slot_troop_controversy),
        (val_add, ":controversy", ":amount"),
        (val_min, ":controversy", 100),
        (troop_set_slot, ":faction_marshal", slot_troop_controversy, ":controversy"),
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (str_store_troop_name, s4, ":faction_marshal"),
          (assign, reg4, ":amount"),
          (assign, reg5, ":controversy"),
          (display_message, "@{!}War damage raises {s4}'s controversy by {reg4} to {reg5}"),
        (try_end),
      (try_end),
      
      (faction_get_slot, ":faction_marshal", ":actor_faction", slot_faction_marshal),
      (try_begin),
        (ge, ":faction_marshal", 0),
        (val_div, ":amount", 3),
        (gt, ":amount", 0),
        
        
        (troop_get_slot, ":controversy", ":faction_marshal", slot_troop_controversy),
        (val_sub, ":controversy", ":amount"),
        (val_max, ":controversy", 0),
        (troop_set_slot, ":faction_marshal", slot_troop_controversy, ":controversy"),
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (str_store_troop_name, s4, ":faction_marshal"),
          (assign, reg4, ":amount"),
          (assign, reg5, ":controversy"),
          (display_message, "@{!}War damage lowers {s4}'s controversy by {reg4} to {reg5}"),
        (try_end),
      (try_end),
      
      
      
  ]),
  
  ("calculate_troop_political_factors_for_liege",
    [
      (store_script_param, ":troop", 1),
      (store_script_param, ":liege", 2),
      
      (troop_get_slot, ":lord_reputation", ":troop", slot_lord_reputation_type),
      
      (store_faction_of_troop, ":faction", ":liege"),
      
      
      (try_begin),
        (eq, ":faction", "fac_player_faction"),
        (assign, ":faction", "fac_player_supporters_faction"),
      (try_end),
      
      (assign, ":liege_is_undeclared_rebel", 0),
      (try_begin),
        (neg|faction_slot_eq, ":faction", slot_faction_leader, ":liege"),
        #the liege is a rebel
        (assign, ":liege_is_undeclared_rebel", 1),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (str_store_troop_name, s32, ":liege"),
          (display_message, "str_s32_is_undeclared_rebel"),
        (try_end),
      (try_end),
      
      (assign, ":result_for_material", 0),
      (assign, ":penalty_for_changing_sides", 0),
      
      
      
      #FACTOR 1 - MILITARY SECURITY
      (assign, ":result_for_security", 0),
      
      #find the lord's home
      (assign, ":base_center", -1),
      (try_begin),
        (is_between, ":troop", active_npcs_begin, active_npcs_end),
        (try_for_range, ":center", centers_begin, centers_end),
          (eq, ":base_center", -1),
          (party_slot_eq, ":center", slot_town_lord, ":troop"),
          (assign, ":base_center", ":center"),
        (try_end),
      (try_end),
      
      (assign, ":faction_has_base", 0),
      
      #add up all other centers for the security value
      (try_for_range, ":center", centers_begin, centers_end),
        (neq, ":center", ":base_center"),
        (gt, ":base_center", 0),
        
        (try_begin),
          (is_between, ":center", towns_begin, towns_end),
          (assign, ":weight", 9000),
        (else_try),
          (is_between, ":center", castles_begin, castles_end),
          (assign, ":weight", 6000),
        (else_try),
          (assign, ":weight", 1000),
        (try_end),
        
        (store_distance_to_party_from_party, ":distance", ":base_center", ":center"),
        (val_add, ":distance", 10),
        (val_div, ":weight", ":distance"),
        (val_div, ":weight", ":distance"),
        
        (store_faction_of_party, ":center_faction", ":center"),
        
        (try_begin),
          (eq, ":center_faction", ":faction"),
          
          (assign, ":faction_has_base", 1),
          (val_add, ":result_for_security", ":weight"),
        (else_try),
          (neq, ":center_faction", ":faction"),
          (store_relation, ":center_relation", ":center_faction", ":faction"),
          
          (try_begin), #potentially hostile center
            (this_or_next|eq, ":liege_is_undeclared_rebel", 1),
            (lt, ":center_relation", 0),
            (val_div, ":weight", 2),
          (else_try), #neutral center
            (val_div, ":weight", 4),
          (try_end),
          
          (val_sub, ":result_for_security", ":weight"),
        (try_end),
      (try_end),
      
      
      #if a faction controls no other centers, then there is a small bonus
      (try_begin),
        (eq, ":faction_has_base", 0),
        (val_add, ":result_for_security", 20),
        (try_begin),
          (eq, "$cheat_mode", 2),
          (display_message, "str_small_bonus_for_no_base"),
        (try_end),
      (try_end),
      (val_clamp, ":result_for_security", -100, 100),
      
      
      (assign, ":result_for_security_weighted", ":result_for_security"),
      (try_begin),
        (eq, ":lord_reputation", lrep_cunning),
        (val_mul, ":result_for_security_weighted", 2),
      (else_try),
        (eq, ":lord_reputation", lrep_martial),
        (val_div, ":result_for_security_weighted", 2),
      (try_end),
      
      
      
      #FACTOR 2 - INTERNAL FACTION POLITICS
      #this is a calculation of how much influence the lord believes he will have in each faction
      (assign, ":result_for_political", 0),
      
      (try_for_range, ":loop_var", "trp_kingdom_heroes_including_player_begin", active_npcs_end),
        (assign, ":kingdom_hero", ":loop_var"),
        
        (this_or_next|troop_slot_eq, ":kingdom_hero", slot_troop_occupation, slto_kingdom_hero),
        (this_or_next|eq, ":kingdom_hero", "trp_kingdom_heroes_including_player_begin"),
        (is_between, ":kingdom_hero", pretenders_begin, pretenders_end),
        
        (store_faction_of_troop, ":kingdom_hero_faction", ":kingdom_hero"),
        
        (try_begin),
          (eq, ":loop_var", "trp_kingdom_heroes_including_player_begin"),
          (assign, ":kingdom_hero", "trp_player"),
          (assign, ":kingdom_hero_faction", "$players_kingdom"),
          (try_begin), #do not count player relation if the player is trying to suborn the character. this has the slight potential for a miscalculation, if the script is called from outside dialogs and $g_talk_troop has not been reset
            (eq, "$g_talk_troop", ":troop"),
            (store_faction_of_troop, ":cur_faction", ":troop"),
            (eq, ":cur_faction", ":faction"),
            (assign, ":kingdom_hero_faction", 0),
          (try_end),
        (try_end),
        
        (eq, ":kingdom_hero_faction", ":faction"),
        (neg|faction_slot_eq, ":kingdom_hero_faction", slot_faction_leader, ":kingdom_hero"),
        (neq, ":liege_is_undeclared_rebel", 1),
        (neg|is_between, ":kingdom_hero", pretenders_begin, pretenders_end),
        
        
        (call_script, "script_troop_get_relation_with_troop", ":troop", ":kingdom_hero"),
        (assign, ":troop_rel_w_hero", reg0),
        
        (call_script, "script_troop_get_relation_with_troop", ":kingdom_hero", ":liege"),
        (assign, ":hero_rel_w_liege", reg0),
        
        (store_mul, ":lord_political_weight", ":troop_rel_w_hero", ":hero_rel_w_liege"),
        (val_div, ":lord_political_weight", 100),
        
        (try_begin),
          (eq, "$cheat_mode", 2), #disabled
          (eq, "$g_talk_troop", ":troop"),
          (str_store_faction_name, s20, ":kingdom_hero_faction"),
          (str_store_troop_name, s15, ":kingdom_hero"),
          (assign, reg15, ":lord_political_weight"),
          (display_message, "str_s15_considered_member_of_faction_s20_weight_of_reg15"),
        (try_end),
        
        (val_add, ":result_for_political", ":lord_political_weight"),
      (try_end),
      
      (val_clamp, ":result_for_political", -100, 101), #lords portion represents half
      
      (try_begin),
        (call_script, "script_troop_get_relation_with_troop", ":troop", ":liege"),
        (assign, ":liege_relation", reg0),
        (val_add, ":result_for_political", ":liege_relation"),
      (try_end),
      
      (val_div, ":result_for_political", 2),
      
      (val_clamp, ":result_for_political", -100, 101), #liege portion represents half
      
      (assign, ":result_for_political_weighted", ":result_for_political"),
      
      (try_begin),
        (this_or_next|eq, ":lord_reputation", lrep_goodnatured),
        (eq, ":lord_reputation", lrep_quarrelsome),
        (val_mul, ":result_for_political_weighted", 2),
      (try_end),
      
      #FACTOR 3 - PROMISES AND OTHER ANTICIPATED GAINS
      #lord's calculation of anticipated gains
      (assign, ":result_for_material", 0),
      (assign, ":result_for_material_weighted", ":result_for_material"),
      
      
      #FACTOR 4 - IDEOLOGY
      #lord's calculation of ideological comfort
      # (try_begin),
        # #Originally, the argument section was not used for a non-player liege. Actually, it can be used
        # (eq, 1, 0),
        # (neq, ":liege", "trp_player"),
        # (neq, ":liege", "$supported_pretender"), #player is advocate for pretender
        # (assign, ":argument_strength", 0),
        # (assign, ":argument_appeal", 0),
        # (assign, ":result_for_argument", 0),
      # (else_try),	#only if the recruitment candidate is either the player, or a supported pretender
        (troop_get_slot, ":recruitment_argument", ":troop", slot_lord_recruitment_argument),
        
        (call_script, "script_rebellion_arguments", ":troop", ":recruitment_argument", ":liege"),
        (assign, ":argument_appeal", reg0),
        (assign, ":argument_strength", reg1),
        
        (store_add, ":result_for_argument", ":argument_appeal", ":argument_strength"),
        
        (store_skill_level, ":player_persuasion_skill", "skl_persuasion", "trp_player"),
        (try_begin),
          (gt, ":result_for_argument", 0),
          #make sure player is the one making the overture
          
          #if player has 0 persuasion, ":result_for_argument" will be multiplied by 3/10.
          (store_add, ":player_persuasion_skill_plus_5_mul_066", ":player_persuasion_skill", 5),
          (val_mul, ":player_persuasion_skill_plus_5_mul_066", 2),
          (val_div, ":player_persuasion_skill_plus_5_mul_066", 3),
          
          (val_mul, ":result_for_argument", ":player_persuasion_skill_plus_5_mul_066"),
          (val_div, ":result_for_argument", 10),
        (else_try),
          (lt, ":result_for_argument", 0),
          (store_sub, ":ten_minus_player_persuasion_skill", 10, ":player_persuasion_skill"),
          (val_mul, ":result_for_argument", ":ten_minus_player_persuasion_skill"),
          (val_div, ":result_for_argument", 10),
        (try_end),
        
        (try_begin),
          (neq, ":liege", "trp_player"),
          (neq, ":liege", "$supported_pretender"), #player is advocate for pretender
          (val_div, ":argument_strength", 2),
          (val_div, ":argument_appeal", 2),
          (val_div, ":result_for_argument", 2),
        (try_end),
        
      # (try_end),
      
      #	(try_begin),
      #		(eq, ":lord_reputation", lrep_cunning),
      #		(val_div, ":result_for_ideological_weighted", 2),
      #	(else_try),
      #		(eq, ":lord_reputation", lrep_upstanding),
      #		(val_mul, ":result_for_ideological_weighted", 2),
      #	(try_end),
      
      
      #FACTOR 5 - PENALTY FOR CHANGING SIDES
      (try_begin), #no penalty for the incumbent
        (store_faction_of_troop, ":cur_faction", ":troop"),
        (eq, ":cur_faction", ":faction"),
        (assign, ":penalty_for_changing_sides", 0),
      (else_try), #penalty for the player
        (eq, ":liege", "trp_player"),
        (store_sub, ":penalty_for_changing_sides", 60, "$player_right_to_rule"),
      (else_try), #same culture, such as a pretender
        (troop_get_slot, ":orig_faction_of_lord", ":troop", slot_troop_original_faction),
        (troop_get_slot, ":orig_faction_of_liege", ":liege", slot_troop_original_faction),
        (eq, ":orig_faction_of_lord", ":orig_faction_of_liege"),
        (assign, ":penalty_for_changing_sides", 10),
      (else_try), #a liege from a different culture
        (assign, ":penalty_for_changing_sides", 50),
      (try_end),
      (val_clamp, ":penalty_for_changing_sides", 0, 101),
      
      (assign, ":penalty_for_changing_sides_weighted", ":penalty_for_changing_sides"),
      (try_begin),
        (eq, ":lord_reputation", lrep_debauched),
        (val_div, ":penalty_for_changing_sides_weighted", 2),
      (else_try),
        (eq, ":lord_reputation", lrep_upstanding),
        (val_mul, ":penalty_for_changing_sides_weighted", 2),
      (try_end),
      
      
      
      (assign, reg1, ":result_for_security"),
      (assign, reg2, ":result_for_security_weighted"),
      (assign, reg3, ":result_for_political"),
      (assign, reg4, ":result_for_political_weighted"),
      (assign, reg5, ":result_for_material"),
      (assign, reg6, ":result_for_material_weighted"),
      (assign, reg7, ":argument_strength"),
      (assign, reg17, ":argument_appeal"),
      
      (assign, reg8, ":result_for_argument"),
      (assign, reg9, ":penalty_for_changing_sides"),
      (assign, reg10, ":penalty_for_changing_sides_weighted"),
      
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (eq, "$g_talk_troop", ":troop"),
        (str_store_troop_name, s20, ":troop"),
        (str_store_faction_name, s21, ":faction"),
        (str_store_troop_name, s22, ":liege"),
        
        (display_message, "@{!}G_talk_troop {s20} evaluates being vassal to {s22} of {s21}"),
        
        (display_message, "str_base_result_for_security_reg1"),
        (display_message, "str_result_for_security_weighted_by_personality_reg2"),
        (display_message, "str_base_result_for_political_connections_reg3"),
        (display_message, "str_result_for_political_connections_weighted_by_personality_reg4"),
        #		(display_message, "@{!}Result for anticipated_gains: {reg5}"),
        #		(display_message, "@{!}Result for anticipated_gains weighted by personality: {reg6}"),
        
        (try_begin),
          (this_or_next|eq, ":liege", "trp_player"),
          (eq, ":liege", "$supported_pretender"), #player is advocate for pretender
          (display_message, "str_result_for_argument_strength_reg7"),
          (display_message, "str_result_for_argument_appeal_reg17"),
          (display_message, "str_combined_result_for_argument_modified_by_persuasion_reg8"),
        (try_end),
        (display_message, "str_base_changing_sides_penalty_reg9"),
        (display_message, "str_changing_sides_penalty_weighted_by_personality_reg10"),
      (try_end),
      
      (store_add, ":total", ":result_for_security_weighted", ":result_for_political_weighted"),
      (val_add, ":total", ":result_for_material_weighted"),
      (val_add, ":total", ":result_for_argument"),
      (val_sub, ":total", ":penalty_for_changing_sides_weighted"),
      
      
      (assign, reg0, ":total"),
      
      (try_begin),
        (eq, "$cheat_mode", 2),
        (display_message, "@{!}DEBUG -- Analyzing lord allegiances, combined bonuses and penalties = {reg0}"),
        #(display_message, "str_combined_bonuses_and_penalties_=_reg0"),
      (try_end),
  ]),
  
  
  
  ("cf_troop_can_intrigue",
    #This script should be called from dialogs, and also prior to any event which might result in a lord changing sides
    [
      (store_script_param, ":troop", 1),
      (store_script_param, ":skip_player_party", 2),
      
      (troop_get_slot, ":led_party_1", ":troop", slot_troop_leaded_party),
      (party_is_active, ":led_party_1"),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (eq, ":troop", "$g_talk_troop"),
        (display_message, "str_intrigue_test_troop_party_is_active"),
      (try_end),
      
      (party_get_battle_opponent, ":battle_opponent", ":led_party_1"),
      (le, ":battle_opponent", 0), #battle opponent can be 0 for an attached party?
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (eq, ":troop", "$g_talk_troop"),
        (display_message, "str_intrigue_test_troop_party_is_not_in_battle"),
      (try_end),
      
      (troop_slot_eq, ":troop", slot_troop_prisoner_of_party, -1),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (eq, ":troop", "$g_talk_troop"),
        (display_message, "str_intrigue_test_troop_is_not_prisoner"),
      (try_end),
      
      (party_get_attached_to, ":led_party_1_attached", ":led_party_1"),
      
      (store_faction_of_party, ":led_party_1_faction", ":led_party_1"),
      
      (assign, ":other_lords_nearby", 0),
      (try_for_range, ":troop_2", active_npcs_begin, active_npcs_end),
        (neq, ":troop", ":troop_2"),
        (eq, ":other_lords_nearby", 0),
        
        (troop_slot_eq, ":troop_2", slot_troop_occupation, slto_kingdom_hero),
        
        (troop_get_slot, ":led_party_2", ":troop_2", slot_troop_leaded_party),
        (party_is_active, ":led_party_2"),
        (neq, ":led_party_1", ":led_party_2"),
        
        (store_faction_of_party, ":led_party_2_faction", ":led_party_2"),
        (eq, ":led_party_1_faction", ":led_party_2_faction"),
        
        (try_begin),
          (eq, ":led_party_1_attached", -1),
          (store_distance_to_party_from_party, ":distance", ":led_party_1", ":led_party_2"),
          (lt, ":distance", 3),
          (assign, ":other_lords_nearby", 1),
        (else_try),
          (is_between, ":led_party_1_attached", walled_centers_begin, walled_centers_end),
          (party_get_attached_to, ":led_party_2_attached", ":led_party_2"),
          (eq, ":led_party_1_attached", ":led_party_2_attached"),
          (assign, ":other_lords_nearby", 1),
        (try_end),
      (try_end),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (eq, ":troop", "$g_talk_troop"),
        (display_message, "str_intrigue_test_troop_is_nearby"),
      (try_end),
      
      (try_begin),
        (eq, ":skip_player_party", 0),
        #temporary spot
      (try_end),
      
      (eq, ":other_lords_nearby", 0),
  ]),
  
  
  ("troop_change_relation_with_troop",
    [
      (store_script_param, ":troop1", 1),
      (store_script_param, ":troop2", 2),
      (store_script_param, ":amount", 3),
      
      (try_begin),
        (eq, ":troop1", "trp_player"),
        (call_script, "script_change_player_relation_with_troop", ":troop2", ":amount"),
      (else_try),
        (eq, ":troop2", "trp_player"),
        (call_script, "script_change_player_relation_with_troop", ":troop1", ":amount"),
      (else_try),
        (eq, ":troop1", ":troop2"),
        
      (else_try),
        (call_script, "script_troop_get_relation_with_troop", ":troop1", ":troop2"),
        (store_add, ":new_relation", reg0, ":amount"),
        
        (val_clamp, ":new_relation", -100, 101),
        
        (try_begin),
          (eq, ":new_relation", 0),
          (assign, ":new_relation", 1), #this removes the need for a separate "met" slot - any non-zero relation will be a met
        (try_end),
        
        (store_add, ":troop1_slot_for_troop2", ":troop2", slot_troop_relations_begin),
        (troop_set_slot, ":troop1", ":troop1_slot_for_troop2", ":new_relation"),
        
        (store_add, ":troop2_slot_for_troop1", ":troop1", slot_troop_relations_begin),
        (troop_set_slot, ":troop2", ":troop2_slot_for_troop1", ":new_relation"),
      (try_end),
      
      
      (try_begin), #generate controversy if troops are in the same faciton
        (neq, ":troop1", ":troop2"),
        (lt, ":amount", -5),
        (try_begin),
          (eq, ":troop1", "trp_player"),
          (assign, ":faction1", "$players_kingdom"),
        (else_try),
          (store_faction_of_troop, ":faction1", ":troop1"),
        (try_end),
        (try_begin),
          (eq, ":troop2", "trp_player"),
          (assign, ":faction2", "$players_kingdom"),
        (else_try),
          (store_faction_of_troop, ":faction2", ":troop2"),
        (try_end),
        (eq, ":faction1", ":faction2"),
        (is_between, ":faction1", kingdoms_begin, kingdoms_end),
        
        (store_mul, ":controversy_generated", ":amount", -1),
        
        (troop_get_slot, ":controversy1", ":troop1", slot_troop_controversy),
        (val_add, ":controversy1", ":controversy_generated"),
        (val_min, ":controversy1", 100),
        (troop_set_slot, ":troop1", slot_troop_controversy, ":controversy1"),
        
        (troop_get_slot, ":controversy2", ":troop2", slot_troop_controversy),
        (val_add, ":controversy2", ":controversy_generated"),
        (val_min, ":controversy2", 100),
        (troop_set_slot, ":troop2", slot_troop_controversy, ":controversy2"),
        
      (try_end),
      
      (try_begin),
        (is_between, ":troop1", active_npcs_begin, active_npcs_end),
        (is_between, ":troop2", active_npcs_begin, active_npcs_end),
        (neq, ":troop1", ":troop2"),
        
        (try_begin),
          (gt, ":amount", 0),
          (val_add, "$total_relation_adds", ":amount"),
        (else_try),
          (val_sub, "$total_relation_subs", ":amount"),
        (try_end),
      (try_end),
      
      (try_begin),
        (eq, "$cheat_mode", 4), #change back to 4
        
        (is_between, ":troop1", active_npcs_begin, active_npcs_end),
        (is_between, ":troop2", active_npcs_begin, active_npcs_end),
        (neq, ":troop1", ":troop2"),
        
        (str_store_troop_name, s20, ":troop1"),
        (str_store_troop_name, s15, ":troop2"),
        (assign, reg4, ":amount"),
        (assign, reg14, ":new_relation"),
        (display_message, "str_s20_relation_with_s15_changed_by_reg4_to_reg14"),
        
        (assign, reg4, "$total_relation_adds"),
        (display_message, "str_total_additions_reg4"),
        (assign, reg4, "$total_relation_subs"),
        (display_message, "str_total_subtractions_reg4"),
        
        (assign, reg4, "$total_courtship_quarrel_changes"),
        (display_message, "@{!}DEBUG -- Total courtship quarrel changes: {reg4}"),
        
        (assign, reg4, "$total_random_quarrel_changes"),
        (display_message, "@{!}DEBUG -- Total random quarrel changes: {reg4}"),
        
        (assign, reg4, "$total_battle_ally_changes"),
        (display_message, "@{!}DEBUG -- Total battle changes for allies: {reg4}"),
        
        (assign, reg4, "$total_battle_enemy_changes"),
        (display_message, "@{!}DEBUG -- Total battle changes for enemies: {reg4}"),
        
        (assign, reg4, "$total_promotion_changes"),
        (display_message, "@{!}DEBUG -- Total promotion changes: {reg4}"),
        
        (assign, reg4, "$total_feast_changes"),
        (display_message, "@{!}DEBUG -- Total feast changes: {reg4}"),
        
        (assign, reg4, "$total_policy_dispute_changes"),
        (assign, reg5, "$number_of_controversial_policy_decisions"),
        (display_message, "@{!}DEBUG -- Total policy dispute changes: {reg4} from {reg5} decisions"),
        
        (assign, reg4, "$total_indictment_changes"),
        (display_message, "@{!}DEBUG -- Total faction switch changes: {reg4}"),
        
        (assign, reg4, "$total_no_fief_changes"),
        (display_message, "@{!}DEBUG -- Total no fief changes: {reg4}"),
        
        (assign, reg4, "$total_relation_changes_through_convergence"),
        (display_message, "@{!}DEBUG -- Total changes through convergence: {reg4}"),
        
        (assign, reg4, "$total_vassal_days_responding_to_campaign"),
        (display_message, "@{!}DEBUG -- Total vassal responses to campaign: {reg4}"),
        
        (assign, reg4, "$total_vassal_days_on_campaign"),
        (display_message, "@{!}DEBUG -- Total vassal campaign days: {reg4}"),
        
        (val_max, "$total_vassal_days_on_campaign", 1),
        (store_mul, ":response_rate", "$total_vassal_days_responding_to_campaign", 100),
        (val_div, ":response_rate", "$total_vassal_days_on_campaign"),
        (assign, reg4, ":response_rate"),
        (display_message, "@{!}DEBUG -- Vassal response rate: {reg4}"),
        
        
        
        #		(assign, reg4, "$total_joy_battle_changes"),
        #		(display_message, "@{!}DEBUG -- Total joy of battle changes"),
        
      (try_end),
      
  ]),
  
  
  ("troop_get_relation_with_troop",
    [
      (store_script_param, ":troop1", 1),
      (store_script_param, ":troop2", 2),
      
      (assign, ":relation", 0),
      (try_begin),
        (this_or_next|eq, ":troop1", -1),
        (eq, ":troop2", -1),
        
        #Possibly switch to relation with liege
        (assign, ":relation", 0),
      (else_try),
        (eq, ":troop1", "trp_player"),
        (call_script, "script_troop_get_player_relation", ":troop2"),
        (assign, ":relation", reg0),
      (else_try),
        (eq, ":troop2", "trp_player"),
        (call_script, "script_troop_get_player_relation", ":troop1"),
        (assign, ":relation", reg0),
      (else_try),
        (store_add, ":troop1_slot_for_troop2", ":troop2", slot_troop_relations_begin),
        (troop_get_slot, ":relation", ":troop1", ":troop1_slot_for_troop2"),
      (try_end),
      
      
      (val_clamp, ":relation", -100, 101),
      (assign, reg0, ":relation"),
      
  ]),
  
  
  
  ("appoint_faction_marshal",
    [
      (store_script_param, ":faction_no", 1),
      (store_script_param, ":faction_marshal", 2),
      
      (faction_get_slot, ":faction_leader", ":faction_no", slot_faction_leader),
      (faction_get_slot, ":old_marshal", ":faction_no", slot_faction_marshal),
      
      (faction_set_slot, ":faction_no", slot_faction_marshal, ":faction_marshal"),
      
      (try_begin),
        (ge, ":old_marshal", 0),
        (troop_get_slot, ":old_marshal_party", ":old_marshal", slot_troop_leaded_party),
        (party_is_active, ":old_marshal_party"),
        (party_set_marshal, ":old_marshal_party", 0),
      (try_end),
      
      (try_begin),
        (ge, ":faction_marshal", 0),
        (troop_get_slot, ":new_marshal_party", ":faction_marshal", slot_troop_leaded_party),
        (party_is_active, ":new_marshal_party"),
        (party_set_marshal,":new_marshal_party", 1),
      (try_end),
      
      (try_begin),
        (neq, ":faction_marshal", ":faction_leader"),
        (neq, ":faction_marshal", ":old_marshal"),
        (this_or_next|eq, ":faction_marshal", "trp_player"),
        (is_between, ":faction_marshal", active_npcs_begin, active_npcs_end),
        
        (this_or_next|neq, ":faction_no", "fac_player_supporters_faction"),
        (neg|check_quest_active, "qst_rebel_against_kingdom"),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (str_store_faction_name, s15, ":faction_no"),
          (display_message, "str_checking_lord_reactions_in_s15"),
        (try_end),
        
        (call_script, "script_troop_change_relation_with_troop", ":faction_marshal", ":faction_leader", 5),
        (val_add, "$total_promotion_changes", 5),
        
        (try_for_range, ":lord", active_npcs_begin, active_npcs_end),
          (troop_slot_eq, ":lord", slot_troop_occupation, slto_kingdom_hero),
          (store_faction_of_troop, ":lord_faction", ":lord"),
          (eq, ":lord_faction", ":faction_no"),
          
          (neq, ":lord", ":faction_marshal"),
          (neq, ":lord", ":faction_leader"),
          
          (call_script, "script_troop_get_relation_with_troop", ":faction_marshal", ":lord"),
          #			(try_begin),
          #				(eq, "$cheat_mode", 1),
          #				(str_store_troop_name, s14, ":lord"),
          #				(str_store_troop_name, s17, ":faction_marshal"),
          #				(display_message, "@{!}{s14}'s relation with {s17} is {reg0}"),
          #			(try_end),
          (store_sub, ":adjust_relations", reg0, 10),
          (val_div, ":adjust_relations", 15),
          (neq, ":adjust_relations", 0),
          
          #Not negatively affected if they favored the lord
          (try_begin),
            (troop_slot_eq, ":lord", slot_troop_stance_on_faction_issue, ":faction_marshal"),
            (val_add, ":adjust_relations", 1),
            (val_max, ":adjust_relations", 0),
          (try_end),
          
          (call_script, "script_troop_change_relation_with_troop", ":faction_leader", ":lord", ":adjust_relations"),
          (val_add, "$total_promotion_changes", ":adjust_relations"),
          
          (lt, ":adjust_relations", -2),
          (store_random_in_range, ":random", 1, 10),
          
          (val_add, ":adjust_relations", ":random"),
          
          (lt, ":adjust_relations", 0),
          
          (str_store_troop_name, s14, ":lord"),
          (str_store_troop_name, s15, ":faction_marshal"),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (display_message, "str_s14_protests_the_appointment_of_s15_as_marshal"),
          (try_end),
          
          (call_script, "script_troop_change_relation_with_troop", ":faction_leader", ":lord", -10),
          (call_script, "script_troop_change_relation_with_troop", ":faction_marshal", ":lord", -5),
          (val_add, "$total_promotion_changes", -15),
          
          (call_script, "script_add_log_entry", logent_lord_protests_marshal_appointment, ":lord",  ":faction_marshal", ":faction_leader", "$g_encountered_party_faction"),
          
        (try_end),
      (try_end),
      
  ]),
  
  ("lord_get_home_center",
    [
      (store_script_param, ":troop_no", 1),
      (store_faction_of_troop, ":faction_no", ":troop_no"),
      (assign, ":result", -1),
      
      (assign, ":end_val", walled_centers_end),
      (try_for_range, ":center_no", walled_centers_begin, ":end_val"),
        (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
        
        (store_faction_of_party, ":center_faction", ":center_no"),
        (try_begin),
          (eq, ":center_faction", ":faction_no"),
          (assign, ":result", ":center_no"),
          (assign, ":end_val", ":center_no"),	#break loop
        (else_try),
          (eq, "$cheat_mode", 1),
          (assign, reg1, ":troop_no"),
          (assign, reg2, ":faction_no"),
          (assign, reg3, ":center_no"),
          (assign, reg4, ":center_faction"),
          (display_message, "@{!}script_lord_get_home_center: troop {reg1} of faction {reg2} lord of center {reg3} of faction {reg4}"),
        (try_end),
      (try_end),
      
      #NOTE : In old code if a lord has no walled center then home city of this lord is assigning to
      #faction leader's home city. Now I changed this to assign home cities more logical and homogeneous.
      #In new code if a lord has no walled center then his home city becomes his village's border_city.
      #This means his home city becomes owner city of his village. If he has no village then as last change
      #his home city become faction leader's home city.
      (try_begin),
        (eq, ":result", -1),
        (assign, ":end_val", villages_end),
        (try_for_range, ":center_no", villages_begin, ":end_val"),
          (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
          (party_get_slot, ":bound_center", ":center_no", slot_village_bound_center),
          (is_between, ":bound_center", walled_centers_begin, walled_centers_end),
          
          (store_faction_of_party, ":center_faction", ":bound_center"),
          (try_begin),
            (eq, ":center_faction", ":faction_no"),
            (assign, ":result", ":bound_center"),
            (assign, ":end_val", ":center_no"),	#break loop
          (else_try),
            (eq, "$cheat_mode", 1),
            (assign, reg1, ":troop_no"),
            (assign, reg2, ":faction_no"),
            (assign, reg3, ":center_no"),
            (assign, reg4, ":center_faction"),
            (assign, reg5, ":bound_center"),
            (display_message, "@{!}script_lord_get_home_center: troop {reg1} of faction {reg2} lord of village {reg3} bound to center {reg5} of faction {reg4}"),
          (try_end),
        (try_end),
      (try_end),
      
      #If lord has no walled center and is player faction, then assign player court
      (try_begin),
        (eq, ":result", -1),
        (eq, ":faction_no", "fac_player_supporters_faction"),
        (is_between, "$g_player_court", walled_centers_begin, walled_centers_end),
        
        (store_faction_of_party, ":center_faction", "$g_player_court"),
        (try_begin),
          (eq, ":center_faction", "fac_player_supporters_faction"),
          (assign, ":result", "$g_player_court"),
        (else_try),
          (eq, "$cheat_mode", 1),
          (assign, reg4, ":center_faction"),
          (display_message, "@{!}script_lord_get_home_center: player court belongs to faction {reg4}"),
        (try_end),
      (try_end),
      
      #If lord has no walled center and any not walled village then assign faction capital
      (try_begin),
        (eq, ":result", -1),
        (faction_get_slot, ":faction_leader", ":faction_no", slot_faction_leader),
        (neq, ":troop_no", ":faction_leader"),
        (ge, ":faction_leader", 0), # Bugfix to filter invalid faction leader values.
        (call_script, "script_lord_get_home_center", ":faction_leader"),
        (is_between, reg0, walled_centers_begin, walled_centers_end),
        (assign, ":result", reg0),
      (try_end),
      
      #Any town/castle of the faction
      (try_begin),
        (eq, ":result", -1),
        (assign, ":end_val", walled_centers_end),
        (try_for_range, ":center_no", walled_centers_begin, ":end_val"),
          (store_faction_of_party, ":center_faction", ":center_no"),
          (eq, ":center_faction", ":faction_no"),
          (assign, ":result", ":center_no"),
          (assign, ":end_val", ":center_no"),	#break loop
        (try_end),
      (try_end),
      
      (assign, reg0, ":result"),
  ]),
  
  
  
  
  ("get_kingdom_lady_social_determinants", #Calradian society is rather patriarchal, at least among the upper classes
    [
      (store_script_param, ":kingdom_lady", 1),
      
      (store_faction_of_troop, ":faction_of_lady", ":kingdom_lady"),
      (assign, ":center", -1),
      (assign, ":closest_male_relative", -1),
      (assign, ":best_center_score", 0),
      
      (try_begin),
        (troop_slot_ge, ":kingdom_lady", slot_troop_spouse, 0),
        (troop_get_slot, ":closest_male_relative", ":kingdom_lady", slot_troop_spouse),
      (else_try),
        (troop_slot_ge, ":kingdom_lady", slot_troop_father, 0),
        (troop_get_slot, ":closest_male_relative", ":kingdom_lady", slot_troop_father),
      (else_try),
        (troop_slot_ge, ":kingdom_lady", slot_troop_guardian, 0),
        (troop_get_slot, ":closest_male_relative", ":kingdom_lady", slot_troop_guardian),
      (try_end),
      
      
      (try_begin), #if ongoing social event (maybe add if not besieged)
        (faction_slot_eq, ":faction_of_lady", slot_faction_ai_state, sfai_feast),
        (faction_get_slot, ":feast_center", ":faction_of_lady", slot_faction_ai_object),
        
        (gt, ":closest_male_relative", -1),
        (troop_get_slot, ":closest_male_party", ":closest_male_relative", slot_troop_leaded_party),
        (party_is_active, ":closest_male_party"),
        (party_get_attached_to, ":closest_male_cur_location", ":closest_male_party"),
        
        (eq, ":closest_male_cur_location", ":feast_center"),
        (is_between, ":feast_center", walled_centers_begin, walled_centers_end),
        
        (assign, ":center", ":feast_center"),
        
      (else_try),
        (troop_slot_eq, "trp_player", slot_troop_spouse, ":kingdom_lady"),
        (is_between, "$g_player_court", walled_centers_begin, walled_centers_end),
        (assign, ":center", "$g_player_court"),
      (else_try),
        (try_for_range, ":walled_center", walled_centers_begin, walled_centers_end),
          (store_faction_of_party, ":walled_center_faction", ":walled_center"),
          (this_or_next|eq, ":faction_of_lady", ":walled_center_faction"),
          (neg|is_between, ":faction_of_lady", kingdoms_begin, kingdoms_end), #lady married to a player without a faction
          
          (party_get_slot, ":castle_lord", ":walled_center", slot_town_lord),
          
          (gt, ":castle_lord", -1),
          
          (call_script, "script_troop_get_family_relation_to_troop", ":kingdom_lady", ":castle_lord"),
          
          (try_begin),
            (this_or_next|is_between, ":faction_of_lady", kingdoms_begin, kingdoms_end),
            (troop_slot_eq, "trp_player", slot_troop_spouse, ":kingdom_lady"),
            
            (faction_slot_eq, ":faction_of_lady", slot_faction_leader, ":castle_lord"),
            (val_max, reg0, 1),
          (try_end),
          
          (try_begin),
            (eq, "$cheat_mode", 2),
            (str_store_troop_name, s3, ":kingdom_lady"),
            (str_store_troop_name, s4, ":castle_lord"),
            (str_store_party_name, s5, ":walled_center"),
            (display_message, "str_checking_s3_at_s5_with_s11_relationship_with_s4_score_reg0"),
            (str_clear, s11),
          (try_end),
          
          (gt, reg0, ":best_center_score"),
          
          (assign, ":best_center_score", reg0),
          (assign, ":center", ":walled_center"),
          
          
        (try_end),
      (try_end),
      
      
      ##	###debug Detect Ladies issue.
      ##	(try_begin),
      ##		(eq,":closest_male_relative",-1),
      ##		(str_store_troop_name,s1,":kingdom_lady"),
      ##		(assign,reg1,":kingdom_lady"),
      ##		(try_begin),
      ##			(troop_slot_eq, "trp_player", slot_troop_spouse, ":kingdom_lady"),
      ##			(display_message,"@{!}Error! PLEASE REPORT!  - Lady nr.{reg1} ({s1}), player's wife, has no male relative!!!",0xFF0000),
      ##		(else_try),
      ##			(display_message,"@{!}Error! PLEASE REPORT!  - Lady nr.{reg1} ({s1}) has no male relative!!!",0xFF0000),
      ##		(try_end),
      ##	(try_end),
      ##	###debug end
      
      (assign, reg0, ":closest_male_relative"),
      (assign, reg1, ":center"),
      
      
  ]),
  
  
  #This is probably unnecessarily complicated, but can support a multi-generational mod
  ("age_troop_one_year",
    [
      (store_script_param, ":troop_no", 1),
      
      (troop_get_type, ":is_female", ":troop_no"),
      (val_mod, ":is_female", 2),
      
      (troop_get_slot, ":age", ":troop_no", slot_troop_age),
      (troop_get_slot, ":appearance", ":troop_no", slot_troop_age_appearance),
      
      (val_add, ":age", 1),
      (store_random_in_range, ":addition", 1, 5),
      
      (try_begin),
        (eq, ":is_female", 1),
        #		(val_add, ":addition", 2), #the women's age slider seems to produce less change than the male one - commented out: makes women look too old.
      (try_end),
      
      (val_add, ":appearance", ":addition"),
      (try_begin),
        (gt, ":age", 45),
        (store_attribute_level, ":strength", ":troop_no", ca_strength),
        (store_attribute_level, ":agility", ":troop_no", ca_agility),
        (store_random_in_range, ":random", 0, 50), #2% loss brings it down to about 36% by age 90, but of course can be counteracted by new level gain
        (try_begin),
          (lt, ":random", ":strength"),
          (troop_raise_attribute, ":troop_no", ca_strength, -1),
        (try_end),
        (try_begin),
          (lt, ":random", ":agility"),
          (troop_raise_attribute, ":troop_no", ca_agility, -1),
        (try_end),
      (try_end),
      
      (val_clamp, ":appearance", 1, 100),
      
      (troop_set_slot, ":troop_no", slot_troop_age, ":age"),
      (troop_set_slot, ":troop_no", slot_troop_age_appearance, ":appearance"),
      (troop_set_age, ":troop_no", ":appearance"),
  ]),
  
  
  ("add_lady_items",
    [
      (store_script_param, ":lady_no", 1),
      (troop_equip_items, ":lady_no"),
      
      (store_faction_of_troop, ":faction_no", ":lady_no"),
      
      (store_random_in_range, ":random", 0, 6),
      
      (try_begin), #assign clothes
        ##		(this_or_next|troop_slot_eq, ":lady_no", slot_lord_reputation_type, lrep_adventurous),
        ##			(troop_slot_eq, ":lady_no", slot_lord_reputation_type, lrep_ambitious),
        ##
        ##		(this_or_next|troop_slot_eq, ":lady_no", slot_lord_reputation_type, lrep_adventurous),
        ##			(lt, ":random", 2),
        ##
        ##		(neg|troop_slot_ge, ":lady_no", slot_troop_age, 40),
        ##		(try_begin),
        ##			(eq, ":faction_no", "fac_kingdom_2"),
        ##			(lt, ":random", 4),
        ##			(troop_add_item, ":lady_no", "itm_robe", 0),
        ##		(else_try),
        ##			(eq, ":faction_no", "fac_kingdom_3"),
        ##			(lt, ":random", 3),
        ##			(troop_add_item, ":lady_no", "itm_robe", 0),
        ##		(else_try),
        ##			(troop_add_item, ":lady_no", "itm_robe", 0),
        ##		(try_end),
        ##	(else_try),
        (this_or_next|is_between, ":faction_no", "fac_kingdom_1", "fac_kingdom_5"),
        (eq, ":faction_no", "fac_kingdom_8"),
        (try_begin),
          (lt, ":random", 2),
          (troop_add_item, ":lady_no", "itm_pict_long_tunic2", 0),
        (else_try),
          (lt, ":random", 4),
          (troop_add_item, ":lady_no", "itm_pict_long_tunic3", 0),
        (else_try),
          (troop_add_item, ":lady_no", "itm_pict_long_tunic4", 0),
        (try_end),
      (else_try),
        (is_between, ":faction_no", "fac_kingdom_5", "fac_kingdom_8"),
        (try_begin),
          (eq, ":random", 0),
          (troop_add_item, ":lady_no", "itm_woman_saxon8", 0),
        (else_try),
          (eq, ":random", 1),
          (troop_add_item, ":lady_no", "itm_woman_saxon9", 0),
        (else_try),
          (eq, ":random", 2),
          (troop_add_item, ":lady_no", "itm_woman_saxon1", 0),
          ##		(else_try),
          ##			(lt, ":random", 5),
          ##			(neg|troop_slot_eq, ":lady_no", slot_lord_reputation_type, lrep_conventional),
          ##			(neg|troop_slot_eq, ":lady_no", slot_lord_reputation_type, lrep_ambitious),
          ##			#(troop_add_item, ":lady_no", "itm_peasant_dress", 0),
          ##		(else_try),
          ##			(lt, ":random", 5),
          ##			(troop_add_item, ":lady_no", "itm_lady_dress_ruby", 0),
        (else_try),
          (troop_add_item, ":lady_no", "itm_woman_saxon2", 0),
        (try_end),
        
      (else_try),
        (is_between, ":faction_no", "fac_kingdom_9", "fac_kingdom_14"),
        (try_begin),
          (eq, ":random", 0),
          (troop_add_item, ":lady_no", "itm_richwoman_norse1", 0),
        (else_try),
          (eq, ":random", 1),
          (troop_add_item, ":lady_no", "itm_richwoman_norse2", 0),
        (else_try),
          (eq, ":random", 2),
          (troop_add_item, ":lady_no", "itm_richwoman_norse3", 0),
          ##		(else_try),
          ##			(lt, ":random", 5),
          ##			(neg|troop_slot_eq, ":lady_no", slot_lord_reputation_type, lrep_conventional),
          ##			(neg|troop_slot_eq, ":lady_no", slot_lord_reputation_type, lrep_ambitious),
          ##			#(troop_add_item, ":lady_no", "itm_peasant_dress", 0),
          ##		(else_try),
          ##			(lt, ":random", 5),
          ##			(troop_add_item, ":lady_no", "itm_lady_dress_ruby", 0),
        (else_try),
          (troop_add_item, ":lady_no", "itm_richwoman_norse4", 0),
        (try_end),
        
      (else_try),
        (try_begin),
          (eq, ":random", 0),
          (troop_add_item, ":lady_no", "itm_richwoman_norse1", 0),
        (else_try),
          (eq, ":random", 1),
          (troop_add_item, ":lady_no", "itm_richwoman_norse2", 0),
        (else_try),
          (eq, ":random", 2),
          (troop_add_item, ":lady_no", "itm_richwoman_norse3", 0),
        (else_try),
          (troop_add_item, ":lady_no", "itm_pict_long_tunic1", 0),
        (try_end),
      (try_end),
      (troop_equip_items, ":lady_no"),
      
      #also available:
      #itm_blue_dress
      #itm_court_dress
      
      #to add for khergits -- salwar/shalvar?
      #western tang costume (p105, china's golden age)
      #kipchak woman from russia book
      
      (try_begin), #assign headguear matched to item
        (this_or_next|troop_has_item_equipped, ":lady_no", "itm_richwoman_norse1"),
        (this_or_next|troop_has_item_equipped, ":lady_no", "itm_richwoman_norse2"),
        (troop_has_item_equipped, ":lady_no", "itm_richwoman_norse3"),
        
        #assign no headgear
      (else_try),
        (this_or_next|troop_slot_eq, ":lady_no", slot_lord_reputation_type, lrep_moralist),
        (this_or_next|troop_slot_eq, ":lady_no", slot_lord_reputation_type, lrep_conventional),
        (lt, ":random", 2),
        
        
        (try_begin),
          ##			#(troop_has_item_equipped, ":lady_no", "itm_khergit_lady_dress"),
          ##			(troop_add_item, ":lady_no", "itm_spangenhelm_17", 0),
          ##
          ##		(else_try),
          ##		#	(troop_has_item_equipped, ":lady_no", "itm_lady_dress_ruby"),
          (troop_add_item, ":lady_no", "itm_common_veil_b", 0),
          
          # (str_store_troop_name, s4, ":lady_no"),
          # (display_message, "@Giving ruby turret hat to {s4}"),
          ##		(else_try),
          ##			(troop_has_item_equipped, ":lady_no", "itm_lady_dress_blue"),
          ##			(troop_add_item, ":lady_no", "itm_phrygian13", 0),
          ##
          ##			(str_store_troop_name, s4, ":lady_no"),
          ###			(display_message, "@Giving blue turret hat to {s4}"),
          ##		(else_try),
          ##			(troop_has_item_equipped, ":lady_no", "itm_lady_dress_green"),
          ##			(troop_add_item, ":lady_no", "itm_phrygian12", 0),
          ##
          ##			(str_store_troop_name, s4, ":lady_no"),
          ###			(display_message, "@Giving green turret hat to {s4}"),
          ##		(else_try),
          ##			(troop_has_item_equipped, ":lady_no", "itm_woolen_dress"),
          ##			(troop_add_item, ":lady_no", "itm_wimple_with_veil", 0),
          ##
          ##			(str_store_troop_name, s4, ":lady_no"),
          ###			(display_message, "@Giving green-lined wimple to {s4}"),
          ##		(else_try),
          ##			(neq, ":faction_no", "fac_kingdom_3"),
          ##			(neq, ":faction_no", "fac_kingdom_6"),
          ##			(troop_add_item, ":lady_no", "itm_wimple_a", 0),
          ##
          ##			(str_store_troop_name, s4, ":lady_no"),
          ###			(display_message, "@Giving red-lined wimple to {s4}"),
          ##		(else_try),
          ##			(eq, ":faction_no", "fac_kingdom_6"),
          ####			(try_begin),
          ####				#(troop_has_item_equipped, ":lady_no", "itm_sarranid_lady_dress"),
          ####				#(troop_add_item, ":lady_no", "itm_sarranid_head_cloth", 0),
          ####			(else_try),
          ####				#(troop_add_item, ":lady_no", "itm_sarranid_head_cloth_b", 0),
          ####			(try_end),
        (try_end),
      (try_end),
      (troop_equip_items, ":lady_no"),
      
    ]
  ),
  
  # ("init_troop_age",
    # [
      # (store_script_param, ":troop_no", 1),
      # (store_script_param, ":age", 2), #minimum 20
      
      # (try_begin),
        # (gt, ":age", 20),
        # (troop_set_slot, ":troop_no", slot_troop_age, 20),
      # (else_try),
        # (troop_set_slot, ":troop_no", slot_troop_age, ":age"),
      # (try_end),
      
      # (store_sub, ":years_to_age", ":age", 20),
      # (troop_set_age, ":troop_no", 0),
      
      # (try_begin),
        # (gt, ":years_to_age", 0),
        # (try_for_range, ":unused", 0, ":years_to_age"),
          # (call_script, "script_age_troop_one_year", ":troop_no"),
        # (try_end),
      # (try_end),
      
  # ]),
  
  
  ("assign_troop_love_interests", #Called at the beginning, or whenever a lord is spurned
    [
      (store_script_param, ":cur_troop", 1),
      
      (store_faction_of_troop, ":troop_faction", ":cur_troop"),
      (try_for_range, ":unused", 0, 10),
        (store_random_in_range, ":cur_lady", "trp_kingdom_1_lady_1", kingdom_ladies_end),
        (troop_slot_eq, ":cur_lady", slot_troop_spouse, -1),
        (troop_slot_eq, ":cur_troop", slot_troop_spouse, -1),
        (store_faction_of_troop, ":lady_faction", ":cur_lady"),
        
        (assign, ":continue", 0),
        (try_begin),
          (eq, ":troop_faction", ":lady_faction"),
          (call_script, "script_troop_get_family_relation_to_troop", ":cur_troop", ":cur_lady"),
          (gt, reg0, 0),
          (assign, ":continue", 1),
        (try_end),
        (eq, ":continue", 0),
        
        (call_script, "script_troop_get_relation_with_troop", ":cur_troop", ":cur_lady"),
        (eq, reg0, 0), #do not develop love interest if already spurned or courted
        
        (neg|troop_slot_eq, ":cur_troop", slot_troop_love_interest_1, ":cur_lady"),
        (neg|troop_slot_eq, ":cur_troop", slot_troop_love_interest_2, ":cur_lady"),
        (neg|troop_slot_eq, ":cur_troop", slot_troop_love_interest_3, ":cur_lady"),
        (try_begin),
          (troop_slot_eq, ":cur_troop", slot_troop_love_interest_1, 0),
          (troop_set_slot, ":cur_troop", slot_troop_love_interest_1, ":cur_lady"),
        (else_try),
          (troop_slot_eq, ":cur_troop", slot_troop_love_interest_2, 0),
          (troop_set_slot, ":cur_troop", slot_troop_love_interest_2, ":cur_lady"),
        (else_try),
          (troop_slot_eq, ":cur_troop", slot_troop_love_interest_3, 0),
          (troop_set_slot, ":cur_troop", slot_troop_love_interest_3, ":cur_lady"),
        (try_end),
      (try_end),
  ]),
  
  ("faction_conclude_feast",
    [
      (store_script_param, ":faction_no", 1),
      (store_script_param, ":venue", 2),
      
      (str_store_faction_name, s3, ":faction_no"),
      (str_store_party_name, s4, ":venue"),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (display_message, "str_s3_feast_concludes_at_s4"),
      (try_end),
      
      (try_begin),
        (eq, ":faction_no", "fac_player_faction"),
        (assign, ":faction_no", "$players_kingdom"),
      (try_end),
      
      (party_set_slot, ":venue", slot_town_has_tournament, 0),
      
      #markspot
      
      (assign, ":nobility_in_faction", 0),
      (assign, ":nobility_in_attendance", 0),
      
      (try_for_range, ":troop_no", active_npcs_begin, kingdom_ladies_end),
        (store_faction_of_troop, ":troop_faction", ":troop_no"),
        (eq, ":faction_no", ":troop_faction"),
        
        (val_add, ":nobility_in_faction", 1),
        
        #CHECK -- is the troop there?
        (troop_slot_eq, ":troop_no", slot_troop_cur_center, ":venue"),
        (val_add, ":nobility_in_attendance", 1),
        
        #check for marriages
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_lady),
        (troop_get_slot, ":groom", ":troop_no", slot_troop_betrothed),
        (gt, ":groom", 0),
        
        (troop_get_slot, ":groom_party", ":groom", slot_troop_leaded_party),
        (party_is_active, ":groom_party"),
        (party_get_attached_to, ":groom_party_attached", ":groom_party"),
        (eq, ":groom_party_attached", ":venue"),
        
        (store_faction_of_troop, ":lady_faction", ":troop_no"),
        (store_faction_of_troop, ":groom_faction", ":groom"),
        
        (eq, ":groom_faction", ":lady_faction"),
        (eq, ":lady_faction", ":faction_no"),
        (store_current_hours, ":hours_since_betrothal"),
        (troop_get_slot, ":betrothal_time", ":troop_no", slot_troop_betrothal_time),
        (val_sub, ":hours_since_betrothal", ":betrothal_time"),
        (ge, ":hours_since_betrothal", 144), #6 days, should perhaps eventually be 29 days, or 696 yours
        
        (call_script, "script_get_kingdom_lady_social_determinants", ":troop_no"),
        (assign, ":wedding_venue", reg1),
        
        (eq, ":venue", ":wedding_venue"),
        (neq, ":troop_no", "trp_player"),
        (neq, ":groom", "trp_player"),
        
        (call_script, "script_courtship_event_bride_marry_groom", ":troop_no", ":groom", 0), #parameters from dialog
      (try_end),
      
      
      #ssss	(assign, ":placeholder_reminder_to_calculate_effect_for_player_feast", 1),
      
      
      
      (party_get_slot, ":feast_host", ":venue", slot_town_lord),
      (assign, ":quality_of_feast", 0),
      
      (try_begin),
        (check_quest_active, "qst_organize_feast"),
        (quest_slot_eq, "qst_organize_feast", slot_quest_target_center, ":venue"),
        (assign, ":feast_host", "trp_player"),
        
        (assign, ":total_guests", 400),
        
        (call_script, "script_succeed_quest", "qst_organize_feast"),
        (set_show_messages, 0),
        (call_script, "script_end_quest", "qst_organize_feast"),
        (set_show_messages, 1),
        
        (call_script, "script_internal_politics_rate_feast_to_s9", "trp_household_possessions", ":total_guests", "$players_kingdom", 1),
        (assign, ":quality_of_feast", reg0),
      (else_try),
        (assign, ":quality_of_feast", 60),
      (try_end),
      
      
      (try_begin),
        (ge, "$cheat_mode", 1),
        (str_store_troop_name, s4, ":feast_host"),
        (assign, reg4, ":quality_of_feast"),
        (display_message, "@{!}DEBUG - {s4}'s feast has rating of {reg4}"),
      (try_end),
      
      
      (try_begin),
        (ge, ":feast_host", 0),
        (store_div, ":renown_boost", ":quality_of_feast", 3),
        (call_script, "script_change_troop_renown", ":feast_host", ":renown_boost"),
        
        (try_for_range, ":troop_no", active_npcs_begin, active_npcs_end),
          (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
          (troop_get_slot, ":leaded_party", ":troop_no", slot_troop_leaded_party),
          (party_is_active, ":leaded_party"),
          (party_get_attached_to, ":leaded_party_attached", ":leaded_party"),
          (eq, ":leaded_party_attached", ":venue"),
          
          (assign, ":relation_booster", ":quality_of_feast"),
          (val_div, ":relation_booster", 20),
          
          (try_begin),
            (eq, ":feast_host", "trp_player"),
            (val_sub, ":relation_booster", 1),
            (val_max, ":relation_booster", 0),
          (try_end),
          (try_begin),
            (store_faction_of_troop, ":faction", ":troop_no"),
            (eq, ":faction", "fac_adventurers"),
            (call_script, "script_change_troop_renown", ":troop_no", 1),
            (str_store_troop_name, s24, ":troop_no"),
            (str_store_party_name, s25, ":leaded_party_attached"),
            (display_message, "@{s24} increased renown by attending a feast at {s25}", color_good_news),
          (try_end),
          (call_script, "script_troop_change_relation_with_troop", ":feast_host", ":troop_no", ":relation_booster"),
          (val_add, "$total_feast_changes", ":relation_booster"),
        (try_end),
      (try_end),
      
      
      (assign, reg3, ":nobility_in_attendance"),
      (assign, reg4, ":nobility_in_faction"),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (display_message, "str_attendance_reg3_nobles_out_of_reg4"),
      (try_end),
  ]),
  
  ("lady_evaluate_troop_as_suitor",
    [
      (store_script_param, ":lady", 1),
      (store_script_param, ":suitor", 2),
      
      (call_script, "script_troop_get_romantic_chemistry_with_troop", ":lady", ":suitor"),
      (assign, ":romantic_chemistry", reg0),
      
      (try_begin),
        (call_script, "script_cf_test_lord_incompatibility_to_s17", ":lady", ":suitor"),
      (try_end),
      
      (store_sub, ":personality_modifier", 0, reg0),
      (assign, reg2, ":personality_modifier"),
      
      (try_begin),
        (troop_get_slot, ":renown_modifier", ":suitor", slot_troop_renown),
        (val_div, ":renown_modifier", 20),
        (try_begin),
          (this_or_next|troop_slot_eq, ":lady", slot_lord_reputation_type, lrep_conventional),
          (troop_slot_eq, ":lady", slot_lord_reputation_type, lrep_ambitious),
          (val_mul, ":renown_modifier", 2),
          (val_sub, ":renown_modifier", 15),
        (try_end),
      (try_end),
      
      (store_add, ":final_score", ":renown_modifier", ":personality_modifier"),
      (val_add, ":final_score", ":romantic_chemistry"),
      (assign, reg0, ":final_score"),
  ]),
  
  ("courtship_event_troop_court_lady",
    [
      (store_script_param, ":suitor", 1),
      (store_script_param, ":lady", 2),
      
      
      #(try_begin),
      #(eq, "$cheat_mode", 1),
      #(str_store_troop_name, s4, ":suitor"),
      #(str_store_troop_name, s5, ":lady"),
      #(troop_get_slot, ":lady_location", ":lady", slot_troop_cur_center),
      #(str_store_party_name, s7, ":lady_location"),
      #(display_message, "str_s4_pursues_suit_with_s5_in_s7"),
      #(try_end),
      
      (troop_get_slot, ":previous_suitor", ":lady", slot_lady_last_suitor),
      (troop_set_slot, ":lady", slot_lady_last_suitor, ":suitor"), #can determine quarrels
      
      (try_begin),
        (eq, ":previous_suitor", "trp_player"),
        
        (troop_slot_ge, ":lady", slot_troop_met, 2),
        (call_script, "script_troop_get_relation_with_troop", ":suitor", "trp_player"), #add this to list of quarrels
        (assign, ":suitor_relation_w_player", reg0),
        
        (try_begin),
          (this_or_next|troop_slot_eq, ":suitor", slot_lord_reputation_type, lrep_selfrighteous),
          (this_or_next|troop_slot_eq, ":suitor", slot_lord_reputation_type, lrep_quarrelsome),
          (troop_slot_eq, ":suitor", slot_lord_reputation_type, lrep_debauched),
          (gt, ":suitor_relation_w_player", -20),
          (call_script, "script_add_log_entry", logent_lords_quarrel_over_woman, ":suitor", "trp_player", ":lady", 0),
        (else_try),
          (is_between, ":suitor_relation_w_player", -5, -25),
          (call_script, "script_add_log_entry", logent_lords_quarrel_over_woman, ":suitor", "trp_player", ":lady", 0),
        (try_end),
      (else_try),
        (neq, ":previous_suitor", "trp_player"), #not the player
        
        (neq, ":suitor", ":previous_suitor"),
        (ge, ":previous_suitor", active_npcs_begin),
        
        (call_script, "script_cf_test_lord_incompatibility_to_s17", ":suitor", ":previous_suitor"),
        (call_script, "script_add_log_entry", logent_lords_quarrel_over_woman, ":suitor", ":previous_suitor", ":lady", 0),
        
        (call_script, "script_troop_get_relation_with_troop", ":suitor", ":previous_suitor"), #add this to list of quarrels
        (ge, reg0, 0),
        (call_script, "script_troop_change_relation_with_troop", ":suitor", ":previous_suitor", -20),
        (val_add, "$total_courtship_quarrel_changes", -20),
      (else_try),	 #quarrelsome lords quarrel anyway
        (troop_slot_eq, ":suitor", slot_lord_reputation_type, lrep_quarrelsome),
        (neq, ":suitor", ":previous_suitor"),
        (ge, ":previous_suitor", active_npcs_begin),
        
        #		(neq, ":previous_suitor", "trp_player"),
        
        (call_script, "script_troop_get_relation_with_troop", ":suitor", ":previous_suitor"), #add this to list of quarrels
        (lt, reg0, 10),
        (call_script, "script_add_log_entry", logent_lords_quarrel_over_woman, ":suitor", ":previous_suitor", ":lady", 0),
        (ge, reg0, 0),
        (call_script, "script_troop_change_relation_with_troop", ":suitor", ":previous_suitor", -20),
        (val_add, "$total_courtship_quarrel_changes", -20),
        
      (try_end),
      
      
      #	(call_script, "script_troop_get_relation_with_troop", ":lady", ":suitor"),
      #	(assign, ":orig_relation", reg0),
      
      (call_script, "script_lady_evaluate_troop_as_suitor", ":lady", ":suitor"),
      
      (store_random_in_range, ":random", 5, 16),
      (store_div, ":relationship_change", reg0, ":random"),
      
      (call_script, "script_troop_get_relation_with_troop", ":lady", ":suitor"),
      (assign, ":orig_relation", reg0),
      
      (call_script, "script_troop_change_relation_with_troop", ":lady", ":suitor", ":relationship_change"),
      
      (call_script, "script_troop_get_relation_with_troop", ":lady", ":suitor"),
      (assign, ":lady_suitor_relation", reg0),
      
      (try_begin),
        (ge, ":lady_suitor_relation", 10),
        (lt, ":orig_relation", 10),
        (call_script, "script_add_log_entry", logent_lady_favors_suitor, ":lady", 0, ":suitor", 0),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "str_note__favor_event_logged"),
        (try_end),
        
      (else_try),
        (this_or_next|lt, ":lady_suitor_relation", -20),
        (ge, ":lady_suitor_relation", 20),
        
        (call_script, "script_get_kingdom_lady_social_determinants", ":lady"),
        (assign, ":guardian", reg0),
        (call_script, "script_troop_get_relation_with_troop", ":suitor", ":guardian"),
        (assign, ":suitor_guardian_relation", reg0),
        #things come to a head, one way or another
        
        (assign, ":highest_competitor_lady_score", -1),
        (assign, ":competitor_preferred_by_lady", -1),
        
        (assign, ":highest_competitor_guardian_score", ":suitor_guardian_relation"),
        (assign, ":competitor_preferred_by_guardian", -1),
        
        #log potential competitors
        (try_for_range, ":possible_competitor", lords_begin, lords_end),
          (neq, ":possible_competitor", ":suitor"),
          
          (this_or_next|troop_slot_eq, ":possible_competitor", slot_troop_love_interest_1, ":lady"),
          (this_or_next|troop_slot_eq, ":possible_competitor", slot_troop_love_interest_2, ":lady"),
          (troop_slot_eq, ":possible_competitor", slot_troop_love_interest_3, ":lady"),
          
          (try_begin),
            (call_script, "script_troop_get_relation_with_troop", ":possible_competitor", ":lady"),
            (gt, reg0, ":highest_competitor_lady_score"),
            (assign, ":competitor_preferred_by_lady", ":possible_competitor"),
            (assign, ":highest_competitor_lady_score", reg0),
          (try_end),
          
          (try_begin),
            (call_script, "script_troop_get_relation_with_troop", ":possible_competitor", ":guardian"),
            (gt, reg0, ":highest_competitor_guardian_score"),
            (assign, ":competitor_preferred_by_guardian", ":possible_competitor"),
            (assign, ":highest_competitor_guardian_score", reg0),
          (try_end),
        (try_end),
        
        #RESULTS
        #Guardian forces lady to be betrothed to suitor now
        (try_begin),
          (lt, ":lady_suitor_relation", -20),
          (this_or_next|troop_slot_eq, ":guardian", slot_lord_reputation_type, lrep_selfrighteous),
          (this_or_next|troop_slot_eq, ":guardian", slot_lord_reputation_type, lrep_debauched),
          (troop_slot_eq, ":guardian", slot_lord_reputation_type, lrep_quarrelsome),
          (eq, ":competitor_preferred_by_guardian", -1),
          
          (this_or_next|troop_slot_eq, ":suitor", slot_lord_reputation_type, lrep_selfrighteous),
          (this_or_next|troop_slot_eq, ":suitor", slot_lord_reputation_type, lrep_debauched),
          (troop_slot_eq, ":suitor", slot_lord_reputation_type, lrep_quarrelsome),
          
          (troop_slot_eq, ":suitor", slot_troop_betrothed, -1),
          (troop_slot_eq, ":lady", slot_troop_betrothed, -1),
          
          (call_script, "script_add_log_entry", logent_lady_betrothed_to_suitor_by_family, ":lady", 0, ":suitor", 0),
          (troop_set_slot, ":suitor", slot_troop_betrothed, ":lady"),
          (troop_set_slot, ":lady", slot_troop_betrothed, ":suitor"),
          (store_current_hours, ":hours"),
          (troop_set_slot, ":lady", slot_troop_betrothal_time, ":hours"),
          (troop_set_slot, ":suitor", slot_troop_betrothal_time, ":hours"),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (display_message, "str_result_lady_forced_to_agree_to_engagement"),
          (try_end),
          
          #Lady rejects the suitor
        (else_try),
          (lt, ":lady_suitor_relation", -20),
          
          (call_script, "script_add_log_entry", logent_lady_rejects_suitor, ":lady", 0, ":suitor", 0),
          (call_script, "script_courtship_event_lady_break_relation_with_suitor", ":lady", ":suitor"),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (display_message, "str_result_lady_rejects_suitor"),
          (try_end),
          
          #A happy engagement, with parental blessing
        (else_try),
          (gt, ":lady_suitor_relation", 20),
          (gt, ":suitor_guardian_relation", 0),
          (eq, ":competitor_preferred_by_lady", -1),
          
          (troop_slot_eq, ":suitor", slot_troop_betrothed, -1),
          (troop_slot_eq, ":lady", slot_troop_betrothed, -1),
          
          (call_script, "script_add_log_entry", logent_lady_betrothed_to_suitor_by_choice, ":lady", 0, ":suitor", 0),
          (troop_set_slot, ":suitor", slot_troop_betrothed, ":lady"),
          (troop_set_slot, ":lady", slot_troop_betrothed, ":suitor"),
          (store_current_hours, ":hours"),
          (troop_set_slot, ":lady", slot_troop_betrothal_time, ":hours"),
          (troop_set_slot, ":suitor", slot_troop_betrothal_time, ":hours"),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, s4, ":lady"),
            (str_store_troop_name, s5, ":suitor"),
            (display_message, "str_result_happy_engagement_between_s4_and_s5"),
          (try_end),
          
          #Lady elopes
        (else_try),
          (gt, ":lady_suitor_relation", 20),
          
          (eq, ":competitor_preferred_by_lady", -1),
          (this_or_next|troop_slot_eq, ":guardian", slot_lord_reputation_type, lrep_adventurous),
          (troop_slot_eq, ":guardian", slot_lord_reputation_type, lrep_ambitious),
          
          (troop_slot_eq, ":suitor", slot_troop_betrothed, -1),
          (troop_slot_eq, ":lady", slot_troop_betrothed, -1),
          
          #lady elopes
          (call_script, "script_courtship_event_bride_marry_groom", ":lady", ":suitor", 1),
          #add elopements to quarrel descriptions
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, s4, ":lady"),
            (str_store_troop_name, s5, ":suitor"),
            (display_message, "str_result_s4_elopes_with_s5"),
          (try_end),
          
          #Lady reluctantly agrees to marry under pressure from family
        (else_try),
          (troop_slot_eq, ":lady", slot_lord_reputation_type, lrep_conventional),
          (eq, ":competitor_preferred_by_guardian", -1),
          (gt, ":suitor_guardian_relation", 4),
          
          (store_random_in_range, ":random", 0, 5),
          (eq, ":random", 0),
          
          (troop_slot_eq, ":suitor", slot_troop_betrothed, -1),
          (troop_slot_eq, ":lady", slot_troop_betrothed, -1),
          
          (call_script, "script_add_log_entry", logent_lady_betrothed_to_suitor_by_pressure, ":lady", 0, ":suitor", 0),
          (troop_set_slot, ":suitor", slot_troop_betrothed, ":lady"),
          (troop_set_slot, ":lady", slot_troop_betrothed, ":suitor"),
          (store_current_hours, ":hours"),
          (troop_set_slot, ":lady", slot_troop_betrothal_time, ":hours"),
          (troop_set_slot, ":suitor", slot_troop_betrothal_time, ":hours"),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_troop_name, s4, ":lady"),
            (str_store_troop_name, s5, ":suitor"),
            (display_message, "str_result_s4_reluctantly_agrees_to_engagement_with_s5"),
          (try_end),
          
          #Stalemate -- make patience roll
        (else_try),
          (gt, ":lady_suitor_relation", 20),
          
          (store_random_in_range, reg3, 0, 3),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (display_message, "str_result_stalemate_patience_roll_=_reg3"),
          (try_end),
          
          (eq, reg3, 0),
          (call_script, "script_add_log_entry", logent_lady_rejected_by_suitor, ":lady", 0, ":suitor", 0),
          (call_script, "script_courtship_event_lady_break_relation_with_suitor", ":lady", ":suitor"),
        (try_end),
        
      (try_end),
      
  ]),
  
  
  
  ("courtship_event_lady_break_relation_with_suitor", #parameters from dialog
    [
      (store_script_param, ":lady", 1),
      (store_script_param, ":suitor", 2),
      
      (try_for_range, ":love_interest_slot", slot_troop_love_interest_1, slot_troop_love_interests_end),
        (troop_slot_eq, ":suitor", ":love_interest_slot", ":lady"),
        (troop_set_slot, ":suitor", ":love_interest_slot", 0),
      (try_end),
      (call_script, "script_assign_troop_love_interests", ":suitor"),
      
      (try_begin),
        (troop_slot_eq, ":lady", slot_troop_betrothed, ":suitor"),
        
        
        (troop_set_slot, ":lady", slot_troop_betrothed, -1),
        (troop_set_slot, ":suitor", slot_troop_betrothed, -1),
      (try_end),
      
      
  ]),
  
  
  ("courtship_event_bride_marry_groom", #parameters from dialog or scripts
    [
      (store_script_param, ":bride", 1),
      (store_script_param, ":groom", 2),
      (store_script_param, ":elopement", 3),
      
      (try_begin),
        (eq, ":bride", "trp_player"),
        (assign, ":venue", "$g_encountered_party"),
      (else_try),
        (troop_get_slot, ":venue", ":bride", slot_troop_cur_center),
      (try_end),
      
      (store_faction_of_troop, ":groom_faction", ":groom"),
      
      
      (try_begin),
        (eq, ":elopement", 0),
        (call_script, "script_add_log_entry", logent_lady_marries_suitor, ":bride", ":venue", ":groom", 0),
      (else_try),
        (call_script, "script_add_log_entry", logent_lady_elopes_with_lord, ":bride", ":venue", ":groom", 0),
      (try_end),
      
      (str_store_troop_name, s3, ":bride"),
      (str_store_troop_name, s4, ":groom"),
      (str_store_party_name, s5, ":venue"),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (display_message, "str_s3_marries_s4_at_s5"),
      (try_end),
      
      (troop_set_slot, ":bride", slot_troop_spouse, ":groom"),
      (troop_set_slot, ":groom", slot_troop_spouse, ":bride"),
      
      #Break groom's romantic relations
      (try_for_range, ":love_interest_slot", slot_troop_love_interest_1, slot_troop_love_interests_end),
        (troop_set_slot, ":groom", ":love_interest_slot", 0),
      (try_end),
      
      #Break bride's romantic relations
      (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
        (try_for_range, ":love_interest_slot", slot_troop_love_interest_1, slot_troop_love_interests_end),
          (troop_slot_eq, ":active_npc", ":love_interest_slot", ":bride"),
          (call_script, "script_courtship_event_lady_break_relation_with_suitor", ":bride", ":active_npc"),
        (try_end),
      (try_end),
      
      
      
      (troop_set_slot, ":bride", slot_troop_betrothed, -1),
      (troop_set_slot, ":groom", slot_troop_betrothed, -1),
      
      
      
      #change relations with family
      (try_for_range, ":family_member", lords_begin, lords_end),
        (call_script, "script_troop_get_family_relation_to_troop", ":bride", ":family_member"),
        (gt, reg0, 0),
        (store_div, ":family_relation_boost", reg0, 3),
        (try_begin),
          (eq, ":elopement", 1),
          (val_mul, ":family_relation_boost", -2),
        (try_end),
        (call_script, "script_troop_change_relation_with_troop", ":groom", ":bride", ":family_relation_boost"),
        (val_add, "$total_courtship_quarrel_changes", ":family_relation_boost"),
      (try_end),
      
      (try_begin),
        (this_or_next|eq, ":groom", "trp_player"),
        (eq, ":bride", "trp_player"),
        (call_script, "script_change_player_right_to_rule", "trp_player", 15),
      (try_end),
      
      
      (try_begin),
        (eq, ":groom", "trp_player"),
        (check_quest_active, "qst_wed_betrothed"),
        (call_script, "script_succeed_quest", "qst_wed_betrothed"),
        (set_show_messages, 0),
        (call_script, "script_end_quest", "qst_wed_betrothed"),
        (set_show_messages, 1),
      (try_end),
      
      
      (try_begin),
        (check_quest_active, "qst_visit_lady"),
        (quest_slot_eq, "qst_visit_lady", slot_quest_giver_troop, ":bride"),
        (call_script, "script_abort_quest", "qst_visit_lady", 0),
      (try_end),
      
      
      (try_begin),
        (eq, ":groom", "trp_player"),
        (check_quest_active, "qst_visit_lady"),
        (call_script, "script_abort_quest", "qst_visit_lady", 0),
      (try_end),
      (try_begin),
        (eq, ":groom", "trp_player"),
        (check_quest_active, "qst_duel_courtship_rival"),
        (call_script, "script_abort_quest", "qst_duel_courtship_rival", 0),
      (try_end),
      
      
      (try_begin),
        (eq, ":bride", "trp_player"),
        (call_script, "script_player_join_faction", ":groom_faction"),
        (assign, "$player_has_homage", 1),
      (else_try),
        (eq, ":groom", "trp_player"),
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (str_store_troop_name, s4, ":bride"),
          (display_message, "@{!} DEBUG - {s4} faction change in marriage case 5"),
        (try_end),
        (troop_set_faction, ":bride", "$players_kingdom"),
      (else_try),
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (str_store_troop_name, s4, ":bride"),
          (display_message, "@{!}DEBUG - {s4} faction changed by marriage, case 6"),
        (try_end),
        
        (troop_set_faction, ":bride", ":groom_faction"),
      (try_end),
      
      (try_begin),
        (this_or_next|eq, ":groom", "trp_player"),
        (eq, ":bride", "trp_player"),
        (unlock_achievement, ACHIEVEMENT_HAPPILY_EVER_AFTER),
        (try_begin),
          (eq, ":elopement", 1),
          (unlock_achievement, ACHIEVEMENT_HEART_BREAKER),
        (try_end),
      (try_end),
      
      
      
      # (try_begin),
        # (eq, 1, 0),	#VC-2456 disable wedding mission
        # (this_or_next|eq, ":groom", "trp_player"),
        # (eq, ":bride", "trp_player"),
        # #(eq, ":elopement", 0),
        # (call_script, "script_start_wedding_cutscene", ":groom", ":bride"),
      # (try_end),
  ]),
  
  
  #script_npc_decision_checklist_party_ai
  # DECISION CHECKLISTS (OCT 14)
  # I was thinking of trying to convert as much AI decision-making as possible to the checklist format
  # While outcomes are not as nuanced and varied as a random decision using weighted chances for each outcoms,
  # the checklist has the advantage of being much more transparent, both to developers and to players
  # The checklist can yield a string (standardized to s14) which explains the rationale for the decision
  # When the script yields a yes/no/maybe result, than that is standardized from -3 to +3
  # INPUT: troop_no
  # OUTPUT: none
  ("npc_decision_checklist_party_ai",
    [
      #this script can replace decide_kingdom_hero_ai and decide_kingdom_hero_ai_follow_or_not
      #However, it does not contain script_party_set_ai_state
      
      (store_script_param, ":troop_no", 1),
      (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
      #(party_get_slot, ":our_strength", ":party_no", slot_party_cached_strength),
      #(store_div, ":min_strength_behind", ":our_strength", 2),
      #(party_get_slot, ":our_follower_strength", ":party_no", slot_party_follower_strength),
      (try_begin),
        (troop_slot_eq, ":troop_no", slot_troop_current_mission, npc_mission_improve_relations),
        (troop_get_slot, ":mission_object", ":troop_no", slot_troop_mission_object),
        (troop_get_slot, ":mission_target", ":troop_no", slot_troop_mission_target),
        (try_begin),
          (is_between, ":mission_object", centers_begin, centers_end),
          (str_store_party_name, s33, ":mission_object"),
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":mission_object"),
          (eq, ":troop_no", "$g_talk_troop"),
          (str_store_string, s14, "@You asked me to come and meet you here at {s33}. So, here I am, waiting for you to show up."),
        (else_try),
          (is_between, ":mission_target", centers_begin, centers_end),
          (str_store_party_name, s33, ":mission_target"),
          (assign, ":action", spai_patrolling_around_center),
          (assign, ":object", ":mission_target"),
          (eq, ":troop_no", "$g_talk_troop"),
          (str_store_string, s14, "@You asked me to come and patrol around {s33}. So, here I am, looking for enemies."),
        (try_end),
      (else_try),
        (neg|troop_slot_ge, ":troop_no", slot_troop_current_mission, 1),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (assign, "$g_talk_troop", ":troop_no"),
        (try_end),
        
        (store_troop_faction, ":faction_no", ":troop_no"),
        
        (try_begin),
          (eq, ":troop_no", "$g_talk_troop"),
          (str_store_string, s15, "str__i_must_attend_to_this_matter_before_i_worry_about_the_affairs_of_the_realm"),
        (try_end),
        
        #find current center
        (party_get_attached_to, ":cur_center_no", ":party_no"),
        (try_begin),
          (lt, ":cur_center_no", 0),
          (party_get_cur_town, ":cur_center_no", ":party_no"),
        (try_end),
        (assign, ":besieger_party", -1),
        (try_begin),
          (neg|is_between, ":cur_center_no", centers_begin, centers_end),
          (assign, ":cur_center_no", -1),
        (else_try),
          (party_get_slot, ":besieger_party", ":cur_center_no", slot_center_is_besieged_by),
          (try_begin),
            (neg|party_is_active, ":besieger_party"),
            (assign, ":besieger_party", -1),
          (try_end),
        (try_end),
        
        #party_count
        (call_script, "script_party_count_fit_for_battle", ":party_no"),
        (assign, ":party_fit_for_battle", reg0),
        (call_script, "script_party_get_ideal_size", ":party_no"),
        (assign, ":ideal_size", reg0),
        (store_mul, ":party_strength_as_percentage_of_ideal", ":party_fit_for_battle", 100),
        (val_div, ":party_strength_as_percentage_of_ideal", ":ideal_size"),
        (try_begin),
          (faction_slot_eq, ":faction_no", slot_faction_num_towns, 0),
          (faction_slot_eq, ":faction_no", slot_faction_num_castles, 0),
          (assign, ":party_ratio_of_prisoners", 0), #do not let prisoners have an effect on ai calculation
        (else_try),
          # (party_get_num_prisoners, ":num_prisoners", ":party_no"),	MOTO scrimping for local var slots
          (party_get_num_prisoners, ":party_ratio_of_prisoners", ":party_no"),
          (val_mul, ":party_ratio_of_prisoners", 100),	#MOTO I believe they're going for 35% prisoners, not 35:1 prisoners (see below)
          (val_max, ":party_fit_for_battle", 1), #avoid division by zero error
          # (store_div, ":party_ratio_of_prisoners", ":num_prisoners", ":party_fit_for_battle"),	MOTO scrimping for local var slots
          (val_div, ":party_ratio_of_prisoners", ":party_fit_for_battle"),	#MOTO scrimping for local var slots
        (try_end),
        
        (assign, ":faction_is_at_war", 0),
        (try_for_range, reg0, kingdoms_begin, kingdoms_end),
          (faction_slot_eq, reg0, slot_faction_state, sfs_active),
          (store_relation, ":relation", ":faction_no", reg0),
          (lt, ":relation", 0),
          (assign, ":faction_is_at_war", 1),
        (try_end),
        
        (assign, ":operation_in_progress", 0),
        (try_begin),
          (this_or_next|party_slot_eq, ":party_no", slot_party_ai_state, spai_raiding_around_center),
          (party_slot_eq, ":party_no", slot_party_ai_state, spai_besieging_center),
          (party_get_slot, ":target_center", ":party_no", slot_party_ai_object),
          (is_between, ":target_center", centers_begin, centers_end),
          (store_faction_of_party, ":center_faction", ":target_center"),
          (store_relation, ":relation", ":faction_no", ":center_faction"),
          (lt, ":relation", 0),
          
          (store_distance_to_party_from_party, ":distance", ":party_no", ":target_center"),
          (lt, ":distance", 10),
          
          #MOTO keep marshal from getting stranded during siege
          (try_begin),
            (party_slot_eq, ":target_center", slot_village_state, svs_under_siege),
            (faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
            (neg|party_slot_ge, ":party_no", slot_party_follower_strength, 1000),	#about 50 troops
            (assign, ":marshal_stranded", 1),
          (else_try),
            (assign, ":marshal_stranded", 0),
          (try_end),
          
          (eq, ":marshal_stranded", 0),
          #MOTO end keep marshal from getting stranded during siege
          
          (this_or_next|party_slot_eq, ":target_center", slot_village_state, svs_under_siege),
          (this_or_next|party_slot_eq, ":target_center", slot_village_state, svs_normal),
          (party_slot_eq, ":target_center", slot_village_state, svs_being_raided),
          
          (assign, ":operation_in_progress", 1),
          
          #MOTO include operations under commander
        (else_try),
          (party_slot_eq, ":party_no", slot_party_ai_state, spai_accompanying_army),
          (party_get_slot, ":commander_party", ":party_no", slot_party_ai_object),
          (gt, ":commander_party", 0),
          (party_is_active, ":commander_party"),
          (party_get_slot, ":target_center", ":commander_party", slot_party_ai_object),
          (is_between, ":target_center", centers_begin, centers_end),
          (store_faction_of_party, ":center_faction", ":target_center"),
          (store_relation, ":relation", ":faction_no", ":center_faction"),
          (lt, ":relation", 0),
          (store_distance_to_party_from_party, ":distance", ":party_no", ":target_center"),
          (lt, ":distance", 10),
          (this_or_next|party_slot_eq, ":target_center", slot_village_state, svs_under_siege),
          (this_or_next|party_slot_eq, ":target_center", slot_village_state, svs_normal),
          (party_slot_eq, ":target_center", slot_village_state, svs_being_raided),
          
          (assign, ":operation_in_progress", 1),
          #MOTO end include operations under commander
        (try_end),
        
        (troop_get_slot, ":troop_reputation", ":troop_no", slot_lord_reputation_type),
        
        (party_get_slot, ":old_ai_state", ":party_no", slot_party_ai_state),
        (party_get_slot, ":old_ai_object", ":party_no", slot_party_ai_object),
        (party_get_slot, ":party_cached_strength", ":party_no", slot_party_cached_strength),
        
        #MOTO scrimping for local var slots
        (store_current_hours, ":hours_since_last_rest"),
        (party_get_slot, reg0, ":party_no", slot_party_last_in_any_center),
        (val_sub, ":hours_since_last_rest", reg0),
        
        (store_current_hours, ":hours_since_last_home"),
        (party_get_slot, reg0, ":party_no", slot_party_last_in_home_center),
        (val_sub, ":hours_since_last_home", reg0),
        (store_current_hours, ":hours_since_last_combat"),
        (party_get_slot, reg0, ":party_no", slot_party_last_in_combat),
        (val_sub, ":hours_since_last_combat", reg0),
        
        (store_current_hours, ":hours_since_last_courtship"),
        (party_get_slot, reg0, ":party_no", slot_party_leader_last_courted),
        (val_sub, ":hours_since_last_courtship", reg0),
        
        (troop_get_slot, reg0, ":troop_no", slot_troop_temp_decision_seed),
        (store_mod, ":aggressiveness", reg0, 73), #To derive the
        #MOTO end scrimping for local var slots
        (try_begin),
          (eq, ":troop_reputation", lrep_martial),
          (val_add, ":aggressiveness", 27),
        (else_try),
          (neq, ":troop_reputation", lrep_debauched),
          (neq, ":troop_reputation", lrep_quarrelsome),
          (val_add, ":aggressiveness", 14),
        (try_end),
        
        (try_begin),
          (gt, ":aggressiveness", ":hours_since_last_combat"),
          (val_add, ":aggressiveness", ":hours_since_last_combat"),
          (val_div, ":aggressiveness", 2),
        (try_end),
        
        (try_begin),
          (eq, "$cheat_mode", 1), #100
          (eq, ":troop_no", "$g_talk_troop"),
          (str_store_troop_name, s4, ":troop_no"),
          (assign, reg3, ":hours_since_last_rest"),
          (assign, reg4, ":hours_since_last_courtship"),
          (assign, reg5, ":hours_since_last_combat"),
          (assign, reg6, ":hours_since_last_home"),
          (assign, reg7, ":aggressiveness"),
          #(display_message, "@{!}{s4}: hours since rest {reg3}, courtship {reg4}, combat {reg5}, home {reg6}, aggressiveness {reg7}"),
        (try_end),
        
        ##I am inspecting an estate (use slot_center_npc_volunteer_troop_amount)
        
        (str_store_string, s17, "str_the_other_matter_took_precedence"),
        
        # (assign, ":do_only_collecting_rents", 0),	MOTO deprecated
        
        #Wait in current city (dangerous to travel with less (<=10) men)
        
        #NOTE : I added also this condition to very top of list. Because if this condition does not exists in top then a bug happens.
        #Bug is about alone wounded lords without any troop near him travels between cities, sometimes it want to return his home city
        #to collect reinforcements, sometimes it want to patrol ext, but his party is so weak even without anyone. So we sometimes see
        #(0/1) parties in map with only one wounded lord inside. Because after wars completely defeated lords spawn again in a walled center
        #in 48 hours periods (by codes in module_simple_trigers). He spawns with only wounded himself. Then he should wait in there for
        #a time to collect new men to his (0/1) party. If a lord is the only one in his party and if he is at any walled center already then he
        #should stay where he is. He should not travel to anywhere because of any reason. If he is the only one and he is wounded and
        #he is not in any walled center this means this situation happens because of one another bug, because any lord cannot be out of
        #walled centers with wounded himself only. So I am adding this condition below.
        
        #SUMMARY : If lord has not got enought troops (<10 || <10%) with himself and he is currently at a walled center he should not leave
        #his current center because of any reason.
        (try_begin),
          (ge, ":cur_center_no", 0),
          
          # (this_or_next|le, ":party_fit_for_battle", 10),
          # (le, ":party_strength_as_percentage_of_ideal", 30),
          (le, ":party_fit_for_battle", 10),	#MOTO percentage requirement too broad
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":cur_center_no"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_need_to_raise_some_men_before_attempting_anything_else"),
            (str_store_string, s16, "str_i_need_to_raise_some_men_before_attempting_anything_else"),
          (try_end),
          
          #Stand in a siege
        (else_try),
          (gt, ":besieger_party", -1),
          (ge, ":cur_center_no", 0),	#MOTO bug fix
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":cur_center_no"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_cannot_leave_this_fortress_now_as_it_is_under_siege"),
            (str_store_string, s16, "str_after_all_we_are_under_siege"),
          (try_end),
          
          #Continue retreat to walled center
        (else_try),
          (eq, ":old_ai_state", spai_retreating_to_center),
          (neg|party_is_in_any_town, ":party_no"),
          
          (ge, ":old_ai_object", 0),
          (party_is_active, ":old_ai_object"),
          
          (store_faction_of_party, ":center_faction", ":old_ai_object"),
          (eq, ":faction_no", ":center_faction"),
          
          (assign, ":action", spai_retreating_to_center),
          (assign, ":object", ":old_ai_object"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_we_are_not_strong_enough_to_face_the_enemy_out_in_the_open"),
            (str_store_string, s16, "str_i_should_probably_seek_shelter_behind_some_stout_walls"),
          (try_end),
          
          #Stand by in current center against enemies
        (else_try),
          (is_between, ":cur_center_no", walled_centers_begin, walled_centers_end),
          
          (party_get_slot, ":enemy_strength_nearby", ":cur_center_no", slot_center_sortie_enemy_strength),
          #MOTO stay if needed
          # (ge, ":enemy_strength_in_area", 50),	MOTO this is 2-3 troops
          (ge, ":enemy_strength_nearby", ":party_cached_strength"),
          (party_get_slot, ":center_strength", ":cur_center_no", slot_party_cached_strength),
          (ge, ":enemy_strength_nearby", ":center_strength"),
          #MOTO end stay if needed
          
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":cur_center_no"),
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_enemies_are_reported_to_be_nearby_and_we_should_stand_ready_to_either_man_the_walls_or_sortie_out_to_do_battle"),
            (str_store_string, s16, "str_the_enemy_is_nearby"),
          (try_end),
          
          #MOTO move here to inform marshal decisions
          #Get reinforcements
        (else_try),
          (assign, ":lowest_acceptable_strength_percentage", 30),
          (assign, ":distance_addition", 0),	#MOTO for testing in marshal levy section
          
          (call_script, "script_lord_get_home_center", ":troop_no"),
          (assign, ":center_to_visit", reg0),
          (is_between, ":center_to_visit", walled_centers_begin, walled_centers_end),
          (party_slot_eq, ":center_to_visit", slot_town_lord, ":troop_no"), #newly added
          
          #if troop is very close to its home center increase by 20%
          (assign, ":distance_addition", 0),
          (party_get_position, pos0, ":center_to_visit"),
          (party_get_position, pos1, ":party_no"),
          (get_distance_between_positions, ":distance", pos0, pos1),
          (try_begin),
            # (le, ":dist", 9000),	MOTO this is NOT very close
            # (store_div, ":distance_addition", ":dist", 600),
            (le, ":distance", 1500),
            (store_div, ":distance_addition", ":distance", 100),
            (store_sub, ":distance_addition", 15, ":distance_addition"),
          (else_try),
            (assign, ":distance_addition", 0),
          (try_end),
          (val_add, ":lowest_acceptable_strength_percentage", ":distance_addition"),
          
          #if there is no campaign for faction increase by 35%
          (assign, ":no_campaign_addition", 35),
          (try_begin),
            (neq, ":faction_no", "fac_adventurers"),
            (this_or_next|faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemy_army),
            (this_or_next|faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemies_around_center),
            (this_or_next|faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_raiding_village),
            (this_or_next|faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_center),
            (this_or_next|party_slot_eq, ":party_no", slot_party_ai_state, spai_besieging_center),
            (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_gathering_army),
            (assign, ":no_campaign_addition", 0),
            
            #If marshal is player itself and if there is a campaign then lower lowest_acceptable_strength_percentage by 10 instead of not changing it.
            #Because players become confused when they see very less participation from AI lords to their campaigns.
            (try_begin),
              (faction_slot_eq, ":faction_no", slot_faction_marshal, "trp_player"),
              (options_get_campaign_ai, ":reduce_campaign_ai"),
              (try_begin),
                (eq, ":reduce_campaign_ai", 0), #hard
                (assign, ":no_campaign_addition", 0),
              (else_try),
                (eq, ":reduce_campaign_ai", 1), #medium
                (assign, ":no_campaign_addition", -10),
              (else_try),
                (eq, ":reduce_campaign_ai", 2), #easy
                (assign, ":no_campaign_addition", -15),
              (try_end),
            (try_end),
          (try_end),
          (val_add, ":lowest_acceptable_strength_percentage", ":no_campaign_addition"),
          (val_max, ":lowest_acceptable_strength_percentage", 25),
          
          #max : 30%+15%+35% = 80% (happens when there is no campaign and player is near to its home center.)
          (lt, ":party_strength_as_percentage_of_ideal", ":lowest_acceptable_strength_percentage"),
          
          #MOTO don't interrupt marshal operation for this
          (this_or_next|eq, ":operation_in_progress", 0),
          (neg|faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
          (neg|party_slot_eq, ":party_no", slot_party_ai_state, spai_besieging_center),
          #MOTO end don't interrupt marshal operation for this
          
          (troop_get_slot, ":troop_wealth", ":troop_no", slot_troop_wealth),
          (assign, ":hiring_budget", ":troop_wealth"),	#MOTO hiring budget/reinforcement cost comparison from script_hire_men_to_kingdom_hero_party
          (val_mul, ":hiring_budget", 3),
          (val_div, ":hiring_budget", 4),
          
          (try_begin),
            (this_or_next|eq, ":faction_no", "fac_player_supporters_faction"),
            (eq, ":faction_no", "$players_kingdom"),
            (assign, ":reinforcement_cost", reinforcement_cost_moderate),
          (else_try),
            (options_get_campaign_ai, ":reduce_campaign_ai"),
            (try_begin),
              (eq, ":reduce_campaign_ai", 0), #hard
              (assign, ":reinforcement_cost", reinforcement_cost_hard),
            (else_try),
              (eq, ":reduce_campaign_ai", 2), #easy
              (assign, ":reinforcement_cost", reinforcement_cost_easy),
            (else_try),
              (assign, ":reinforcement_cost", reinforcement_cost_moderate),
            (try_end),
          (try_end),
          
          (try_begin),
            (ge, ":hiring_budget", ":reinforcement_cost"),
            
            (assign, ":action", spai_holding_center),
            (assign, ":object", ":center_to_visit"),
            
            (try_begin),
              (eq, ":troop_no", "$g_talk_troop"),
              (str_store_string, s14, "str_i_dont_have_enough_troops_and_i_need_to_get_some_more"),
              (str_store_string, s16, "str_i_am_running_low_on_troops"),
            (try_end),
            
          (else_try),	#MOTO copy and modify visiting estates from below to avoid needlessly skipping other state choices
            (assign, ":action", 0),	#MOTO set test for collection undertaken
            (assign, ":center_to_visit", -1),
            
            #MOTO chief collect rents only when it actually helps recruit
            # (assign, ":score_to_beat", 300), #at least 300 gold to pick up MOTO equals reinforcement_cost_hard
            (try_begin),
              (this_or_next|eq, ":faction_no", "fac_player_supporters_faction"),
              (eq, ":faction_no", "$players_kingdom"),
              (assign, ":score_to_beat", reinforcement_cost_moderate),
            (else_try),
              (options_get_campaign_ai, ":reduce_campaign_ai"),
              (try_begin),
                (eq, ":reduce_campaign_ai", 0), #hard
                (assign, ":score_to_beat", reinforcement_cost_hard),
              (else_try),
                (eq, ":reduce_campaign_ai", 2), #easy
                (assign, ":score_to_beat", reinforcement_cost_easy),
              (else_try),
                (assign, ":score_to_beat", reinforcement_cost_moderate),
              (try_end),
            (try_end),
            (val_mul, ":score_to_beat", 4),    # 4/3 "hiring budget" from script_hire_men_to_kingdom_hero_party
            (val_div, ":score_to_beat", 3),
            (val_div, ":score_to_beat", 2),    #parties that can't afford to recruit are willing to go get half of what they need
            #MOTO end collect rents only when it actually helps recruit
            
            (try_begin),
              (neq, ":faction_no", "fac_adventurers"),
              (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
              (ge, ":faction_marshal", 0),	#MOTO avoid bug
              
              (assign, reg17, 0),
              (try_begin),
                (party_slot_eq, ":party_no", slot_party_ai_state, spai_accompanying_army),
                # (party_slot_eq, ":party_no", slot_party_ai_object, ":faction_marshal"),	MOTO wrong
                (troop_get_slot, ":marshal_party", ":faction_marshal", slot_troop_leaded_party),
                (party_is_active, ":marshal_party"),
                (party_slot_eq, ":party_no", slot_party_ai_object, ":marshal_party"),
                #MOTO end wrong
                (assign, reg17, 1),
              (else_try),
                (party_slot_eq, ":party_no", slot_party_following_player, 1),
                (assign, reg17, 1),
              (try_end),
              (eq, reg17, 1),
              
              (try_begin),
                (neq, ":faction_marshal", "trp_player"),
                (neg|party_slot_eq, ":party_no", slot_party_following_player, 1),
                (val_add, ":score_to_beat", 125),
              (else_try),
                (val_add, ":score_to_beat", 250),
              (try_end),
            (try_end),
            
            (try_for_range, ":center_no", centers_begin, centers_end),
              (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
              
              (assign, reg17, 0),
              (try_begin),
                (is_between, ":center_no", villages_begin, villages_end),
                (party_slot_eq, ":center_no", slot_village_state, svs_normal),
                (assign, reg17, 1),
              (else_try),
                (party_slot_eq, ":center_no", slot_center_is_besieged_by, -1),
                (assign, reg17, 1),
              (try_end),
              (eq, reg17, 1),
              
              (party_get_slot, ":tariffs_available", ":center_no", slot_center_accumulated_tariffs),
              (party_get_slot, ":rents_available", ":center_no", slot_center_accumulated_rents),
              (store_add, ":money_available", ":rents_available", ":tariffs_available"),
              
              (gt, ":money_available", ":score_to_beat"),
              (assign, ":center_to_visit", ":center_no"),
              (assign, ":score_to_beat", ":money_available"),
            (try_end),
            
            (is_between, ":center_to_visit", centers_begin, centers_end),
            
            (try_begin),
              (is_between, ":center_to_visit", walled_centers_begin, walled_centers_end),
              (assign, ":action", spai_holding_center),
              (assign, ":object", ":center_to_visit"),
            (else_try),
              (assign, ":action", spai_visiting_village),
              (assign, ":object", ":center_to_visit"),
            (try_end),
            
            (try_begin),
              (eq, ":troop_no", "$g_talk_troop"),
              (str_store_string, s14, "str_i_need_to_inspect_my_properties_and_collect_my_dues"),
              (str_store_string, s16, "str_it_has_been_too_long_since_i_have_inspected_my_estates"),
            (try_end),
          (try_end),
          
          (neq, ":action", 0),	#catch failure of is_between above
          
        (else_try),	#MOTO special state - marshal levies troops
          (faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
          (store_sub, reg0, ":lowest_acceptable_strength_percentage", ":distance_addition"),	#MOTO ignore distance addition for this purpose
          (lt, ":party_strength_as_percentage_of_ideal", reg0),
          (eq, ":operation_in_progress", 0),
          
          (assign, ":center_to_visit", -1),
          (assign, ":troops_to_transfer", 25),	#don't go back until center recuits 25 more
          
          (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
            (store_faction_of_party, ":center_faction", ":center_no"),
            (eq, ":center_faction", ":faction_no"),
            
            (party_get_slot, ":center_max_garrison", ":center_no", slot_town_prosperity),
            (val_mul, ":center_max_garrison", 8),
            (val_add, ":center_max_garrison", 100),	#100..900, average 500
            (val_max, ":center_max_garrison", 250),
            (store_party_size_wo_prisoners, ":center_strength", ":center_no"),
            (store_sub, ":surplus_troops", ":center_strength", ":center_max_garrison"),
            (lt, ":troops_to_transfer", ":surplus_troops"),
            (assign, ":center_to_visit", ":center_no"),
            (assign, ":troops_to_transfer", ":surplus_troops"),
          (try_end),
          
          (ge, ":center_to_visit", 0),
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":center_to_visit"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_dont_have_enough_troops_and_i_need_to_get_some_more"),
            (str_store_string, s16, "str_i_am_running_low_on_troops"),
          (try_end),
          (try_begin),
            (party_get_slot, ":town_lord", ":object", slot_town_lord),
            (eq, ":town_lord", "trp_player"),
            (str_store_party_name, s13, ":object"),
            (tutorial_box, "@Marshal has chosen {s13} to levy troops.", "@Marshal Levying Troops"),
          (try_end),
          (try_begin),
            (party_is_in_town, ":party_no", ":center_to_visit"),
            (try_begin),
              (party_get_slot, ":town_lord", ":center_to_visit", slot_town_lord),
              (neq, ":town_lord", ":troop_no"),
              (try_begin),
                (eq, ":town_lord", "trp_player"),
                (assign, reg0, ":troops_to_transfer"),
                (str_store_party_name, s10, ":center_to_visit"),
                (display_message, "@The marshal is levying {reg0} troops from your center {s10}."),
                
              (else_try),
                (store_mul, ":attitude_adjustment", ":troops_to_transfer", 20),
                (store_party_size_wo_prisoners, ":center_strength", ":center_to_visit"),
                (val_div, ":attitude_adjustment", ":center_strength"),
                (val_add, ":attitude_adjustment", 1),
                (val_mul, ":attitude_adjustment", -1),
                (call_script, "script_troop_change_relation_with_troop", ":troop_no", ":town_lord", ":attitude_adjustment"),
              (try_end),
            (try_end),
            
            (assign, ":num_stacks", 0),
            (assign, ":cur_stack", 1),
            
            (try_for_range, reg0, 0, ":troops_to_transfer"),
              (try_begin),
                (ge, ":cur_stack", ":num_stacks"),
                (party_get_num_companion_stacks, ":num_stacks", ":center_to_visit"),
                (assign, ":cur_stack", 1),
              (try_end),
              
              (party_stack_get_troop_id, ":levy_troop", ":center_to_visit", ":cur_stack"),
              (party_stack_get_size, ":stack_size", ":center_to_visit", ":cur_stack"),
              
              (try_begin),
                (this_or_next|le, ":stack_size", 0),
                (is_between, ":levy_troop", active_npcs_begin, active_npcs_end),
                (val_add, ":troops_to_transfer", 1),	#not levying this guy!
              (else_try),
                (party_remove_members, ":center_to_visit", ":levy_troop", 1),
                (party_add_members, ":party_no", ":levy_troop", 1),
              (try_end),
              
              (val_add, ":cur_stack", 1),
            (try_end),
          (try_end),
          #MOTO end move here to inform marshal decisions
          
          #As the marshal, lead faction campaign
        (else_try),
          (faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
          
          #MOTO chief attempt to replace marshal if can't raise an army
          (try_begin),
            (eq, ":operation_in_progress", 0),
            (store_sub, reg0, ":lowest_acceptable_strength_percentage", ":distance_addition"),	#MOTO ignore distance addition for this purpose
            (lt, ":party_strength_as_percentage_of_ideal", reg0),
            (neg|faction_slot_ge, ":faction_no", slot_faction_political_issue, 1),	#not already done?
            (call_script, "script_change_troop_renown", ":troop_no", -1),
            (faction_set_slot, ":faction_no", slot_faction_political_issue, 1), #Appointment of marshal
            (store_current_hours, ":hours"),
            (val_max, ":hours", 0),
            (faction_set_slot, ":faction_no", slot_faction_political_issue_time, ":hours"),
          (try_end),
          #MOTO end attempt to replace marshal if can't raise an army
          
          #Appoint screening party MOTO move here so marshal group always has a screening party
          (try_begin),
            (assign, ":best_screening_party", -1),
            (assign, ":score_to_beat", 200),	#+- this amount
            
            (try_for_range, ":screen_leader", active_npcs_begin, active_npcs_end),
              # (store_faction_of_troop, ":screen_leader_faction", ":screen_leader"),
              # (eq, ":screen_leader_faction", ":faction_no"),	MOTO neglects allies
              
              (troop_get_slot, ":screening_party", ":screen_leader", slot_troop_leaded_party),
              (party_is_active, ":screening_party"),
              (party_slot_eq, ":screening_party", slot_party_ai_object, ":party_no"),
              (party_slot_eq, ":screening_party", slot_party_ai_state, spai_accompanying_army),
              
              (try_begin),
                (party_slot_eq, ":screening_party", slot_party_ai_substate, 1),	#MOTO use substate to represent screening properly
                (call_script, "script_party_set_ai_state", ":screening_party", spai_accompanying_army, ":party_no"),	#turn off screening in case not chosen again
              (try_end),
              
              (store_distance_to_party_from_party, ":distance", ":screening_party", ":party_no"),
              (lt, ":distance", 15),
              
              (try_begin),
                (ge, "$cheat_mode", 1),
                (str_store_party_name, s4, ":screening_party"),
                (display_message, "@{!}DEBUG -- {s4} close enough to screen for marshal"),
              (try_end),
              
              (store_party_size_wo_prisoners, ":screening_party_score", ":screening_party"),
              (val_sub, ":screening_party_score", 250),	#closest in size to this
              (val_abs, ":screening_party_score"),
              (lt, ":screening_party_score", ":score_to_beat"),
              
              #set party and score
              (assign, ":best_screening_party", ":screening_party"),
              (assign, ":score_to_beat", ":screening_party_score"),
            (try_end),
            
            (party_is_active, ":best_screening_party"),
            (call_script, "script_party_set_ai_state", ":best_screening_party", spai_screening_army, ":party_no"),
            (try_begin),
              (ge, "$cheat_mode", 1),
              (str_store_party_name, s4, ":best_screening_party"),
              (display_message, "@{!}DEBUG -- {s4} chosen as screen"),
            (try_end),
          (try_end),
          #end Appoint screening party MOTO move here so marshal group always has a screening party
          
          (str_clear, s15), #Does not say that overrides faction orders
          (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_gathering_army),
          
          # (party_set_ai_initiative, ":party_no", 10),	MOTO chief this is just overriden by script_party_set_ai_state called next
          
          #new ozan added - active gathering
          #this code will allow marshal to travel around cities while gathering army if currently collected are less than 60%.
          #By ratio increases travel distances become less. Travels will be only points around walled centers.
          (party_get_slot, ":old_ai_object", ":party_no", slot_party_ai_object),
          #MOTO chief prevent marshal from gathering at enemy centers!
          (try_begin),
            (is_between, ":old_ai_object", centers_begin, centers_end),
            (store_faction_of_party, ":center_faction", ":old_ai_object"),
            (eq, ":center_faction", ":faction_no"),
            (assign, ":travel_target", ":old_ai_object"),
          (else_try),
            (assign, ":travel_target", -1),
          (try_end),
          #MOTO end prevent marshal from gathering at enemy centers!
          
          (call_script, "script_find_center_to_defend", ":troop_no"),
          (assign, ":most_threatened_center", reg0),
          (assign, ":travel_target_new_assigned", 0),
          
          (try_begin),
            (lt, ":old_ai_object", 0),
            
            (store_random_in_range, ":random_value", 0, 8), #to eanble marshal to wait sometime during active gathering
            (this_or_next|eq, "$g_gathering_new_started", 1),
            (eq, ":random_value", 0),
            
            (assign, ":vassals_already_assembled", 0),
            (assign, ":total_vassals", 0),
            (try_for_range, ":lord", active_npcs_begin, active_npcs_end),
              (store_faction_of_troop, ":lord_faction", ":lord"),
              (eq, ":lord_faction", ":faction_no"),
              (troop_get_slot, ":led_party", ":lord", slot_troop_leaded_party),
              (party_is_active, ":led_party"),
              (val_add, ":total_vassals", 1),
              
              (party_slot_eq, ":led_party", slot_party_ai_state, spai_accompanying_army),
              (party_slot_eq, ":led_party", slot_party_ai_object, ":party_no"),
              
              (party_is_active, ":party_no"),
              (store_distance_to_party_from_party, ":distance", ":led_party", ":party_no"),
              (lt, ":distance", 15),
              (val_add, ":vassals_already_assembled", 1),
            (try_end),
            
            (assign, ":ratio_of_vassals_assembled", -1),
            (try_begin),
              (gt, ":total_vassals", 0),
              (store_mul, ":ratio_of_vassals_assembled", ":vassals_already_assembled", 100),
              (val_div, ":ratio_of_vassals_assembled", ":total_vassals"),
            (try_end),
            
            (try_begin),
              #if more than 35% of vassals already collected do not make any more active gathering, just hold and wait last vassals to participate.
              (le, ":ratio_of_vassals_assembled", 35),
              
              (assign, ":best_center_to_travel", ":most_threatened_center"),
              (try_begin),
                (eq, "$g_gathering_new_started", 1),
                (assign, ":minimum_distance", 100000),
                (try_for_range, ":center_no", centers_begin, centers_end),
                  (store_faction_of_party, ":center_faction", ":center_no"),
                  (eq, ":center_faction", ":faction_no"), #200
                  (try_begin),
                    (neq, ":center_no", ":most_threatened_center"),
                    (store_distance_to_party_from_party, ":distance", ":party_no", ":center_no"),
                    (lt, ":distance", ":minimum_distance"),
                    (assign, ":minimum_distance", ":distance"),
                    (assign, ":best_center_to_travel", ":center_no"),
                  (try_end),
                (try_end),
              (else_try),
                #active gathering
                (assign, ":max_travel_distance", 150),
                (try_begin),
                  (ge, ":ratio_of_vassals_assembled",15),
                  (store_sub, ":max_travel_distance", 35, ":ratio_of_vassals_assembled"),
                  (val_add, ":max_travel_distance", 5), #5..25
                  (val_mul, ":max_travel_distance", 6), #30..150
                (try_end),
                
                (try_begin),
                  (ge, ":most_threatened_center", 0),
                  (store_distance_to_party_from_party, reg12, ":party_no", ":most_threatened_center"),
                (else_try),
                  (assign, reg12, 0),
                (try_end),
                
                (assign, ":num_centers", 0),
                (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
                  (store_faction_of_party, ":center_faction", ":center_no"),
                  (eq, ":center_faction", ":faction_no"),
                  (try_begin),
                    #(ge, ":max_travel_distance", 0),
                    (store_distance_to_party_from_party, ":distance", ":party_no", ":center_no"),
                    
                    (try_begin),
                      (ge, ":most_threatened_center", 0),
                      (store_distance_to_party_from_party, reg13, ":center_no", ":most_threatened_center"),
                    (else_try),
                      (assign, reg13, 0),
                    (try_end),
                    
                    (store_sub, reg11, reg13, reg12),
                    
                    (this_or_next|ge, reg11, 40),
                    (this_or_next|ge, ":distance", ":max_travel_distance"),
                    (eq, ":center_no", ":most_threatened_center"),
                  (else_try),
                    #this center is a candidate so increase num_centers by one.
                    (val_add, ":num_centers", 1),
                  (try_end),
                (try_end),
                
                (try_begin),
                  (ge, ":num_centers", 0),
                  (store_random_in_range, ":random_center_no", 0, ":num_centers"),
                  (val_add, ":random_center_no", 1),
                  (assign, ":num_centers", 0),
                  (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
                    (store_faction_of_party, ":center_faction", ":center_no"),
                    (eq, ":center_faction", ":faction_no"),
                    (try_begin),
                      (neq, ":center_no", ":most_threatened_center"),
                      (store_distance_to_party_from_party, ":distance", ":party_no", ":center_no"),
                      (lt, ":distance", ":max_travel_distance"),
                      
                      (try_begin),
                        (ge, ":most_threatened_center", 0),
                        (store_distance_to_party_from_party, reg13, ":center_no", ":most_threatened_center"),
                      (else_try),
                        (assign, reg13, 0),
                      (try_end),
                      
                      (store_sub, reg11, reg13, reg12),
                      (lt, reg11, 40),
                      
                      (val_sub, ":random_center_no", 1),
                      (eq, ":random_center_no", 0),
                      (assign, ":best_center_to_travel", ":center_no"),
                    (try_end),
                  (try_end),
                (try_end),
              (try_end),
              
              (assign, ":travel_target", ":best_center_to_travel"),
              (assign, ":travel_target_new_assigned", 1),
            (try_end),
          (else_try),
            #if party has an ai object and they are close to that object while gathering army,
            #forget that ai object so they will select a new ai object next.
            (is_between, ":old_ai_object", centers_begin, centers_end),
            # (party_get_position, pos1, ":party_no"),	MOTO chief this is a very small distance
            # (party_get_position, pos2, ":old_ai_object"),
            # (get_distance_between_positions, ":dist", pos1, pos2),
            (store_distance_to_party_from_party, ":distance", ":party_no", ":old_ai_object"),
            (le, ":distance", 3),
            (assign, ":travel_target", -1),
          (try_end),
          #end ozan
          
          (try_begin),
            (eq, ":travel_target", -1),
            (assign, ":action", spai_undefined),
          (else_try),
            (assign, ":action", spai_visiting_village),
          (try_end),
          
          (assign, ":object", ":travel_target"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (try_begin),
              (eq, ":travel_target", -1),
              (str_store_string, s14, "str_as_the_marshal_i_am_assembling_the_army_of_the_realm"),
            (else_try),
              (try_begin),
                (eq, ":faction_no", "$players_kingdom"),
                (eq, ":travel_target_new_assigned", 1),
                (le, "$number_of_report_to_army_quest_notes", 13),
                (check_quest_active, "qst_report_to_army"),
                (str_store_party_name_link, s10, ":travel_target"),
                (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal), #300
                
                (str_store_troop_name_link, s11, ":faction_marshal"),
                (store_current_hours, ":hours"),
                (call_script, "script_game_get_date_text", 0, ":hours"),
                
                (str_store_string, s14, "str_as_the_marshal_i_am_assembling_the_army_of_the_realm_and_travel_to_lands_near_s10_to_inform_more_vassals"),
                (str_store_string, s14, "@({s1}) {s11}: {s14}"),
                (add_quest_note_from_sreg, "qst_report_to_army", "$number_of_report_to_army_quest_notes", s14, 0),
                (val_add, "$number_of_report_to_army_quest_notes", 1),
              (try_end),
              
              (assign, reg0, ":travel_target"),
              (str_store_party_name, s10, ":travel_target"),
              (str_store_string, s14, "str_as_the_marshal_i_am_assembling_the_army_of_the_realm_and_travel_to_lands_near_s10_to_inform_more_vassals"),
            (try_end),
            (str_store_string, s16, "str_i_intend_to_assemble_the_army_of_the_realm"),
          (try_end),
        (else_try),
          (faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
          (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_center),
          (faction_get_slot, ":faction_object", ":faction_no", slot_faction_ai_object),
          
          (assign, ":action", spai_besieging_center),
          (assign, ":object", ":faction_object"),
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_as_the_marshal_i_am_leading_the_siege"),
            (str_store_string, s16, "str_i_intend_to_begin_the_siege"),
          (try_end),
          
        (else_try),
          (faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
          (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_raiding_village),
          (faction_get_slot, ":faction_object", ":faction_no", slot_faction_ai_object),
          
          (assign, ":action", spai_raiding_around_center),
          (assign, ":object", ":faction_object"),
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_as_the_marshal_i_am_leading_our_raid"),
            (str_store_string, s16, "str_i_intend_to_start_our_raid"),
          (try_end),
          
        (else_try),
          (faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
          (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemies_around_center),
          (faction_get_slot, ":faction_object", ":faction_no", slot_faction_ai_object),
          (party_is_active, ":faction_object"),
          
          #moved (party_set_ai_initiative, ":party_no", 10), #new to avoid losing time of marshal with attacking unimportant targets while there is a threat in our centers.
          
          # MOTO move functionality to party_set_ai_state to generalize
          # (party_get_battle_opponent, ":besieger_party", ":faction_object"),
          
          # (try_begin),
          # (gt, ":besieger_party", 0),
          # (party_is_active, ":besieger_party"),
          
          # (assign, ":action", spai_engaging_army),
          # (assign, ":object", ":besieger_party"),
          # (try_begin),
          # (eq, ":troop_no", "$g_talk_troop"),
          # (str_store_string, s14, "str_as_the_marshal_i_am_leading_our_forces_to_engage_the_enemy_in_battle"),
          # (str_store_string, s16, "str_i_intend_to_lead_our_forces_out_to_engage_the_enemy"),
          # (try_end),
          # (else_try),
          # MOTO end move functionality to party_set_ai_state to generalize
          (assign, ":action", spai_patrolling_around_center),
          (assign, ":object", ":faction_object"),
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_as_the_marshal_i_am_leading_our_forces_in_search_of_the_enemy"),
            (str_store_string, s16, "str_i_intend_to_lead_our_forces_out_to_find_the_enemy"),
          (try_end),
          # (try_end), MOTO move functionality to party_set_ai_state to generalize
          
        (else_try),
          (faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
          (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemy_army),
          (faction_get_slot, ":faction_object", ":faction_no", slot_faction_ai_object),
          (party_is_active, ":faction_object"),
          
          (assign, ":action", spai_engaging_army),
          (assign, ":object", ":faction_object"),
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_as_the_marshal_i_am_leading_our_forces_to_engage_the_enemy_in_battle"),
            (str_store_string, s16, "str_i_intend_to_lead_our_forces_out_to_engage_the_enemy"),
          (try_end),
          
          
          #MOTO chief move up to inform marshal decisions
          #Get reinforcements
          # (else_try),
          # (assign, ":lowest_acceptable_strength_percentage", 30),
          
          # #if troop has enought gold then increase by 10%
          # #(troop_get_slot, ":cur_wealth", ":troop_no", slot_troop_wealth),
          # #(try_begin),
          # #  (ge, ":cur_wealth", 2000),
          # #  (assign, ":wealth_addition", 10),
          # #(else_try),
          # #  (store_div, ":wealth_addition", ":cur_wealth", 200),
          # #(try_end),
          # #(val_add, ":lowest_acceptable_strength_percentage", ":wealth_addition"),
          
          # (call_script, "script_lord_get_home_center", ":troop_no"),
          # (assign, ":home_center", reg0),
          # (gt, ":home_center", -1),
          # (party_slot_eq, ":home_center", slot_town_lord, ":troop_no"), #newly added
          
          # #if troop is very close to its home center increase by 20%
          # (assign, ":distance_addition", 0),
          # (party_get_position, pos0, ":home_center"),
          # (party_get_position, pos1, ":party_no"),
          # (get_distance_between_positions, ":dist", pos0, pos1),
          
          # (try_begin),
          # (le, ":dist", 9000),
          # (store_div, ":distance_addition", ":dist", 600),
          # (store_sub, ":distance_addition", 15, ":distance_addition"),
          # (else_try),
          # (assign, ":distance_addition", 0),
          # (try_end),
          # (val_add, ":lowest_acceptable_strength_percentage", ":distance_addition"),
          
          # #if there is no campaign for faction increase by 35%
          # (assign, ":no_campaign_addition", 35),
          # (try_begin),
          # (this_or_next|faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemy_army),
          # (this_or_next|faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemies_around_center),
          # (this_or_next|faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_raiding_village),
          # (this_or_next|faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_center),
          # (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_gathering_army),
          # (assign, ":no_campaign_addition", 0),
          
          # #If marshal is player itself and if there is a campaign then lower lowest_acceptable_strength_percentage by 10 instead of not changing it.
          # #Because players become confused when they see very less participation from AI lords to their campaigns.
          # (try_begin),
          # (faction_slot_eq, ":faction_no", slot_faction_marshal, "trp_player"),
          # (options_get_campaign_ai, ":reduce_campaign_ai"),
          # (try_begin),
          # (eq, ":reduce_campaign_ai", 0), #hard
          # (assign, ":no_campaign_addition", 0),
          # (else_try),
          # (eq, ":reduce_campaign_ai", 1), #medium
          # (assign, ":no_campaign_addition", -10),
          # (else_try),
          # (eq, ":reduce_campaign_ai", 2), #easy
          # (assign, ":no_campaign_addition", -15),
          # (try_end),
          # (try_end),
          # (try_end),
          # (val_add, ":lowest_acceptable_strength_percentage", ":no_campaign_addition"),
          # (val_max, ":lowest_acceptable_strength_percentage", 25),
          
          # #max : 30%+15%+35% = 80% (happens when there is no campaign and player is near to its home center.)
          # (lt, ":party_strength_as_percentage_of_ideal", ":lowest_acceptable_strength_percentage"),
          
          # #MOTO chief don't stick around if can't buy reinforcements
          # (troop_get_slot, ":troop_wealth", ":troop_no", slot_troop_wealth),
          # (assign, ":hiring_budget", ":troop_wealth"),	#MOTO hiring budget/reinforcement cost comparison from script_hire_men_to_kingdom_hero_party
          # (val_mul, ":hiring_budget", 3),
          # (val_div, ":hiring_budget", 4),
          
          # (options_get_campaign_ai, ":reduce_campaign_ai"),
          # (try_begin),
          # (eq, ":reduce_campaign_ai", 0), #hard
          # (assign, ":reinforcement_cost", reinforcement_cost_hard),
          # (else_try),
          # (eq, ":reduce_campaign_ai", 2), #easy
          # (assign, ":reinforcement_cost", reinforcement_cost_easy),
          # (else_try),
          # (assign, ":reinforcement_cost", reinforcement_cost_moderate),
          # (try_end),
          
          # (try_begin),
          # (lt, ":hiring_budget", ":reinforcement_cost"),
          # (assign, ":do_only_collecting_rents", 1),
          # (try_end),
          
          # (ge, ":hiring_budget", ":reinforcement_cost"),
          
          # # (try_begin),
          # # (store_div, ":lowest_acceptable_strength_percentage_div_3", ":lowest_acceptable_strength_percentage", 3),
          # # (ge, ":party_strength_as_percentage_of_ideal", ":lowest_acceptable_strength_percentage_div_3"),
          # # (troop_get_slot, ":troop_wealth", ":troop_no", slot_troop_wealth),
          # # (le, ":troop_wealth", 1800),
          # # (assign, ":do_only_collecting_rents", 1),
          # # (try_end),
          # #MOTO end don't stick around if can't buy reinforcements
          
          # (assign, ":action", spai_holding_center),
          # (assign, ":object", ":home_center"),
          
          # (try_begin),
          # (eq, ":troop_no", "$g_talk_troop"),
          # (str_store_string, s14, "str_i_dont_have_enough_troops_and_i_need_to_get_some_more"),
          
          # (str_store_string, s16, "str_i_am_running_low_on_troops"),
          # (try_end),
          
          # (eq, ":do_only_collecting_rents", 0),
          #MOTO chief end move up to inform marshal decisions
          
          #follow player orders
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO deprecated
          (party_slot_ge, ":party_no", slot_party_following_orders_of_troop, "trp_kingdom_heroes_including_player_begin"),
          
          (party_get_slot, ":orders_type", ":party_no", slot_party_orders_type),
          (party_get_slot, ":orders_object", ":party_no", slot_party_orders_object),
          (party_get_slot, ":orders_time", ":party_no", slot_party_orders_time),
          
          (ge, ":orders_object", 0),
          
          (store_current_hours, ":hours_since_orders_given"),
          (val_sub, ":hours_since_orders_given", ":orders_time"),
          
          (party_is_active, ":orders_object"),
          (party_get_slot, ":object_state", ":orders_object", slot_village_state),
          (store_faction_of_party, ":object_faction", ":orders_object"),
          (store_relation, ":relation_with_object", ":faction_no", ":object_faction"),
          
          (assign, ":orders_are_appropriate", 1),
          (try_begin),
            (gt, ":hours_since_orders_given", 48),
            (assign, ":orders_are_appropriate", 0),
          (else_try),
            (eq, ":orders_type", spai_raiding_around_center),
            (this_or_next|ge, ":relation_with_object", 0),
            (ge, ":object_state", 2),
            (assign, ":orders_are_appropriate", 0),
          (else_try),
            (eq, ":orders_type", spai_besieging_center),
            (ge, ":relation_with_object", 0),
            (assign, ":orders_are_appropriate", 0),
          (else_try),
            (this_or_next|eq, ":orders_type", spai_holding_center),
            (this_or_next|eq, ":orders_type", spai_retreating_to_center),
            (this_or_next|eq, ":orders_type", spai_accompanying_army),
            (eq, ":orders_type", spai_visiting_village),
            (le, ":relation_with_object", 0),
            (assign, ":orders_are_appropriate", 0),
          (try_end),
          
          (eq, ":orders_are_appropriate", 1),
          
          (assign, ":action", ":orders_type"),
          (assign, ":object", ":orders_object"),
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_we_are_following_your_direction"),
          (try_end),
          
          #Host of player wedding
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO deprecated
          (eq, ":operation_in_progress", 0),
          (check_quest_active, "qst_wed_betrothed"),
          (quest_slot_eq, "qst_wed_betrothed", slot_quest_giver_troop, ":troop_no"),
          (quest_get_slot, ":bride", "qst_wed_betrothed", slot_quest_target_troop),
          (call_script, "script_get_kingdom_lady_social_determinants", ":bride"),
          (assign, ":center_to_visit", reg1),
          (is_between, ":center_to_visit", walled_centers_begin, walled_centers_end),	#MOTO avoid bug
          
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":center_to_visit"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_need_to_make_preparations_for_your_wedding"),
            (str_store_string, s16, "str_after_all_i_need_to_make_preparations_for_your_wedding"),
          (try_end),
          
          #Bridegroom at player wedding
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO deprecated
          (eq, ":operation_in_progress", 0),
          (check_quest_active, "qst_wed_betrothed_female"),
          (quest_slot_eq, "qst_wed_betrothed_female", slot_quest_giver_troop, ":troop_no"),
          
          (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_feast),
          (faction_get_slot, ":center_to_visit", ":faction_no", slot_faction_ai_object),
          (is_between, ":center_to_visit", walled_centers_begin, walled_centers_end),	#MOTO avoid bug
          
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":center_to_visit"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_am_heading_to_the_site_of_our_wedding"), #500
            (str_store_string, s16, "str_after_all_we_are_soon_to_be_wed"),
          (try_end),
          
          #Host of other feast
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO deprecated
          (eq, ":operation_in_progress", 0),
          (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_feast),
          (faction_get_slot, ":center_to_visit", ":faction_no", slot_faction_ai_object),
          (is_between, ":center_to_visit", walled_centers_begin, walled_centers_end),	#MOTO avoid bug
          (party_slot_eq, ":center_to_visit", slot_town_lord, ":troop_no"),
          
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":center_to_visit"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_am_hosting_a_feast_there"),
            (str_store_string, s16, "str_i_have_a_feast_to_host"),
          (try_end),
          
          #I am the bridegroom at a feast
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO deprecated
          (eq, ":operation_in_progress", 0),
          (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_feast),
          (troop_get_slot, reg0, ":troop_no", slot_troop_betrothed),
          (is_between, reg0, kingdom_ladies_begin, kingdom_ladies_end),
          
          (faction_get_slot, ":center_to_visit", ":faction_no", slot_faction_ai_object),
          (is_between, ":center_to_visit", walled_centers_begin, walled_centers_end),	#MOTO avoid bug
          
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":center_to_visit"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_am_to_be_the_bridegroom_there"),
            (str_store_string, s16, "str_my_wedding_day_draws_near"),
          (try_end),
          
          #Drop off prisoners
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO deprecated
          (gt,  ":party_ratio_of_prisoners", 35),
          (eq, ":operation_in_progress", 0),
          
          (call_script, "script_lord_get_home_center", ":troop_no"),
          (assign, ":center_to_visit", reg0),
          
          (is_between, ":center_to_visit", walled_centers_begin, walled_centers_end),
          
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":center_to_visit"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_have_too_much_loot_and_too_many_prisoners_and_need_to_secure_them"),
            (str_store_string, s16, "str_i_should_think_of_dropping_off_some_of_my_prisoners"),
          (try_end),
          
          #Reinforce a weak center
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO deprecated
          (assign, ":center_to_visit", -1),
          (assign, ":score_to_beat", 100),
          (eq, ":operation_in_progress", 0),
          
          (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
            (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
            # (party_get_slot, ":center_strength", ":walled_center", slot_party_cached_strength),	MOTO base on number of troops
            (store_party_size_wo_prisoners, ":center_strength", ":center_no"),	#MOTO base on number of troops
            
            #MOTO add condition to limit size of prospective garrison
            #players complaining about huge armies sitting in a      center
            #garrison limit from levy above
            (party_get_slot, ":center_max_garrison",      ":center_no", slot_town_prosperity),
            (val_mul, ":center_max_garrison", 8),
            (val_add, ":center_max_garrison", 100),    #100..900,      average 500
            (store_add, reg0, ":center_strength", ":party_fit_for_battle"),
            (lt, reg0, ":center_max_garrison"),
            #MOTO end add condition to limit size of prospective      garrison
            
            (lt, ":center_strength", ":score_to_beat"),
            (assign, ":center_to_visit", ":center_no"),
            (assign, ":score_to_beat", ":center_strength"),
          (try_end),
          
          (gt, ":center_to_visit", -1),
          
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":center_to_visit"),
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_need_to_reinforce_it_as_it_is_poorly_garrisoned"),
            (str_store_string, s16, "str_there_is_a_hole_in_our_defenses"),
          (try_end),
          
          #Continue screening, if already doing so
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO deprecated
          #MOTO use substate to represent screening properly
          # (eq, ":old_ai_state", spai_screening_army),
          (eq, ":old_ai_state", spai_accompanying_army),
          (party_slot_eq, ":party_no", slot_party_ai_substate, 1),
          #MOTO end use substate to represent screening properly
          
          (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
          (ge, ":faction_marshal", 0),	#MOTO avoid bug
          (troop_get_slot, ":marshal_party", ":faction_marshal", slot_troop_leaded_party),
          (party_is_active, ":marshal_party"),
          
          (call_script, "script_npc_decision_checklist_troop_follow_or_not", ":troop_no"),
          (eq, reg0, 1),
          
          (assign, ":action", spai_screening_army),
          (assign, ":object", ":marshal_party"),
          (try_begin),
            (eq, "$g_talk_troop", ":troop_no"),
            (str_store_string, s14, "str_i_am_following_the_marshals_orders"),
            (str_store_string, s16, "str_the_marshal_has_given_me_this_command"),
          (try_end),
          
        (else_try), #special case for sfai_attacking_enemies_around_center for village raids
          (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemies_around_center),
          (is_between, ":faction_object", villages_begin, villages_end),
          
          (call_script, "script_npc_decision_checklist_troop_follow_or_not", ":troop_no"),
          (this_or_next|faction_slot_eq, ":faction_no",  slot_faction_marshal, -1),	#MOTO act if no marshal
          (eq, reg0, 1),
          
          (faction_get_slot, ":faction_object", ":faction_no", slot_faction_ai_object),
          (party_get_slot, ":raider_party", ":faction_object", slot_village_raided_by),
          (party_is_active, ":raider_party"),
          
          #think about adding one more condition here, what if raider army is so powerfull, again lords will go and engage enemy one by one?
          (party_get_slot, ":enemy_strength_nearby", ":faction_object", slot_center_sortie_enemy_strength),
          # (lt, ":enemy_strength_nearby", 4000),	MOTO compare to party's own strength
          (lt, ":enemy_strength_nearby", ":party_cached_strength"),	#MOTO compare to party's own strength
          #end think
          
          (assign, ":action", spai_engaging_army),
          (assign, ":object", ":raider_party"),
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_our_realm_needs_my_support_there_is_enemy_raiding_one_of_our_villages_which_is_not_to_far_from_here_i_am_going_there"),
            (str_store_string, s16, "str_the_marshal_has_issued_a_summons"),
          (try_end),
          
          #Follow the marshal's orders - if on the offensive, and the campaign has not lasted too long. Readiness is currently randomly set
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO deprecated
          # (call_script, "script_npc_decision_checklist_troop_follow_or_not", ":troop_no"),	MOTO check if marshal exists first
          # (eq, reg0, 1),
          
          (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
          (ge, ":faction_marshal", 0),	#MOTO avoid bug
          (troop_get_slot, ":marshal_party", ":faction_marshal", slot_troop_leaded_party),
          
          #MOTO check if marshal first
          (call_script, "script_npc_decision_checklist_troop_follow_or_not", ":troop_no"),
          (eq, reg0, 1),
          #MOTO end check if marshal first
          
          (assign, ":action", spai_accompanying_army),
          (assign, ":object", ":marshal_party"),
          
          (try_begin),
            (eq, "$g_talk_troop", ":troop_no"),
            (str_store_string, s14, "str_i_am_answering_the_marshals_summons"),
            (str_store_string, s16, "str_the_marshal_has_issued_a_summons"),
          (try_end),
          
          #Support a nearby ally who is on the offensive
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO deprecated
          (eq, ":faction_is_at_war", 1),
          
          (assign, ":party_to_support", -1),
          (try_for_range, ":allied_hero", active_npcs_begin, active_npcs_end),
            (troop_slot_eq, ":allied_hero", slot_troop_occupation, slto_kingdom_hero),
            (store_faction_of_troop, ":allied_hero_faction", ":allied_hero"),
            (eq, ":allied_hero_faction", ":faction_no"),
            (neq, ":allied_hero", ":troop_no"),
            
            (troop_get_slot, ":allied_hero_party", ":allied_hero", slot_troop_leaded_party),
            (gt, ":allied_hero_party", 1),
            (party_is_active, ":allied_hero_party"),
            
            
            (this_or_next|party_slot_eq, ":allied_hero_party", slot_party_ai_state, spai_raiding_around_center),
            (party_slot_eq, ":allied_hero_party", slot_party_ai_state, spai_besieging_center),
            
            (call_script, "script_troop_get_relation_with_troop", ":troop_no", ":allied_hero"),
            # (gt, reg0, 4),	MOTO make variable
            (store_div, ":relation", reg0, 4),	#MOTO make variable
            (gt, ":relation", 1),	#MOTO make variable
            
            (troop_get_slot, ":troop_renown", ":troop_no", slot_troop_renown),
            (troop_get_slot, ":ally_renown", ":allied_hero", slot_troop_renown),
            (le, ":troop_renown", ":ally_renown"), #Ally to support must have higher renown
            
            (store_distance_to_party_from_party, ":distance", ":party_no", ":allied_hero_party"),
            
            # (lt, ":distance", 5),	MOTO make variable
            (lt, ":distance", ":relation"),	#MOTO make variable
            
            (assign, ":party_to_support", ":allied_hero_party"),
          (try_end),
          (gt, ":party_to_support", 0),
          
          (assign, ":action", spai_accompanying_army),
          (assign, ":object", ":party_to_support"),
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (party_stack_get_troop_id, ":leader", ":object", 0),
            (str_store_troop_name, s10, ":leader"),
            
            (call_script, "script_troop_get_family_relation_to_troop", ":leader", "$g_talk_troop"),
            (try_begin),
              (eq, reg0, 0),
              (str_store_string, s11, "str_comradeinarms"),
            (try_end),
            (str_store_string, s14, "str_i_am_supporting_my_s11_s10"),
            (str_store_string, s16, "str_i_believe_that_one_of_my_comrades_is_in_need"),
          (try_end),
          #I have decided to attack a vulnerable fortress
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO deprecated
          (eq, ":faction_is_at_war", 1),
          (eq, ":operation_in_progress", 0),
          
          (assign, ":target_center", -1),
          (assign, ":score_to_beat", 50),
          
          (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
            (store_faction_of_party, ":center_faction", ":center_no"),
            (store_relation, ":relation", ":faction_no", ":center_faction"),
            (lt, ":relation", 0),
            
            (party_get_slot, ":center_strength", ":center_no", slot_party_cached_strength),
            (val_mul, ":center_strength", 3),
            (val_mul, ":center_strength", 2),
            
            (lt, ":center_strength", ":party_cached_strength"),
            # (lt, ":center_strength", 750),
            (lt, ":center_strength", 3000),	#MOTO this is about 100 troops (with defense bonus)
            
            (party_slot_eq, ":center_no", slot_village_state, svs_normal),
            (store_distance_to_party_from_party, ":distance", ":center_no", ":party_no"),
            (lt, ":distance", ":score_to_beat"),
            
            (assign, ":target_center", ":center_no"),
            (assign, ":score_to_beat", ":distance"),
          (try_end),
          
          (is_between, ":target_center", centers_begin, centers_end),
          
          (assign, ":action", spai_besieging_center),
          (assign, ":object", ":target_center"),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (str_store_faction_name, s20, ":faction_no"),
            (str_store_party_name, s21, ":object"),
            (display_message, "str_s20_decided_to_attack_s21"),
          (try_end),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_a_fortress_is_vulnerable"),
            (str_store_string, s16, "str_i_believe_that_the_enemy_may_be_vulnerable"),
          (try_end),
          
          #I am visiting an estate
        (else_try),
          (eq, ":operation_in_progress", 0),
          
          #MOTO collect rents only when it actually helps recruit
          # (assign, ":score_to_beat", 300),	#at least 300 gold to pick up MOTO equals reinforcement_cost_hard
          (try_begin),
            (this_or_next|eq, ":faction_no", "fac_player_supporters_faction"),
            (eq, ":faction_no", "$players_kingdom"),
            (assign, ":score_to_beat", reinforcement_cost_moderate),
          (else_try),
            (options_get_campaign_ai, ":reduce_campaign_ai"),
            (try_begin),
              (eq, ":reduce_campaign_ai", 0),	#hard
              (assign, ":score_to_beat", reinforcement_cost_hard),
            (else_try),
              (eq, ":reduce_campaign_ai", 2),	#easy
              (assign, ":score_to_beat", reinforcement_cost_easy),
            (else_try),
              (assign, ":score_to_beat", reinforcement_cost_moderate),
            (try_end),
          (try_end),
          (val_mul, ":score_to_beat", 4),	# 4/3 "hiring budget" from script_hire_men_to_kingdom_hero_party
          (val_div, ":score_to_beat", 3),
          (troop_get_slot, ":troop_wealth", ":troop_no", slot_troop_wealth),	#average troop wealth is 2000
          (try_begin),
            (lt, ":troop_wealth", ":score_to_beat"),	#parties that lack wealth...
            (val_div, ":score_to_beat", 2),	#...are willing to go get half of what they need
          (else_try),	#parties with wealth are not so eager to collect more
            (val_sub, ":troop_wealth", ":score_to_beat"),	#MOTO add 10% of amount OVER what is needed
            (val_div, ":troop_wealth", 10),	#average troop wealth 10% is is 200 MOTO 160 with hard campaign AI
            (val_add, ":score_to_beat", ":troop_wealth"),	#average score to beat is 500 MOTO 560 with hard campaign AI
          (try_end),
          #MOTO end collect rents only when it actually helps recruit
          
          (try_begin),
            (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
            (ge, ":faction_marshal", 0),	#MOTO avoid bug
            
            (assign, reg17, 0),
            (try_begin),
              (party_slot_eq, ":party_no", slot_party_ai_state, spai_accompanying_army),
              # (party_slot_eq, ":party_no", slot_party_ai_object, ":faction_marshal"),	MOTO wrong
              (troop_get_slot, ":marshal_party", ":faction_marshal", slot_troop_leaded_party),
              (party_is_active, ":marshal_party"),
              (party_slot_eq, ":party_no", slot_party_ai_object, ":marshal_party"),
              #MOTO end wrong
              (assign, reg17, 1),
            (else_try),
              (party_slot_eq, ":party_no", slot_party_following_player, 1),
              (assign, reg17, 1),
            (try_end),
            (eq, reg17, 1),
            
            (try_begin),
              (neq, ":faction_marshal", "trp_player"),
              (neg|party_slot_eq, ":party_no", slot_party_following_player, 1),
              (val_add, ":score_to_beat", 125),
            (else_try),
              (val_add, ":score_to_beat", 250),
            (try_end),
          (try_end),
          
          (assign, ":center_to_visit", -1),
          
          (try_for_range, ":center_no", centers_begin, centers_end),
            (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
            
            (assign, reg17, 0),
            (try_begin),
              (is_between, ":center_no", villages_begin, villages_end),
              (party_slot_eq, ":center_no", slot_village_state, svs_normal),
              (assign, reg17, 1),
            (else_try),
              (party_slot_eq, ":center_no", slot_center_is_besieged_by, -1),
              (assign, reg17, 1),
            (try_end),
            (eq, reg17, 1),
            
            (party_get_slot, ":tariffs_available", ":center_no", slot_center_accumulated_tariffs),
            (party_get_slot, ":rents_available", ":center_no", slot_center_accumulated_rents),
            (store_add, ":money_available", ":rents_available", ":tariffs_available"),
            
            (gt, ":money_available", ":score_to_beat"),
            (assign, ":center_to_visit", ":center_no"),
            (assign, ":score_to_beat", ":money_available"),
          (try_end),
          
          (is_between, ":center_to_visit", centers_begin, centers_end),
          
          (try_begin),
            (is_between, ":center_to_visit", walled_centers_begin, walled_centers_end),
            (assign, ":action", spai_holding_center),
            (assign, ":object", ":center_to_visit"),
          (else_try),
            (assign, ":action", spai_visiting_village),
            (assign, ":object", ":center_to_visit"),
          (try_end),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_need_to_inspect_my_properties_and_collect_my_dues"),
            (str_store_string, s16, "str_it_has_been_too_long_since_i_have_inspected_my_estates"),
          (try_end),
          
          #My men are weary, and I wish to return home
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (this_or_next|gt, ":hours_since_last_rest", 504), #Three weeks
          (lt, ":aggressiveness", 25),
          (gt, ":hours_since_last_rest", 168), #one week if aggressiveness < 25
          (eq, ":operation_in_progress", 0),
          
          (call_script, "script_lord_get_home_center", ":troop_no"),
          (assign, ":center_to_visit", reg0),
          
          (is_between, ":center_to_visit", walled_centers_begin, walled_centers_end),
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":center_to_visit"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_my_men_are_weary_so_we_are_returning_home"),
            (str_store_string, s16, "str_my_men_are_becoming_weary"),
          (try_end),
          
          #I have a score to settle with the enemy
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (this_or_next|gt, ":hours_since_last_combat", 12),
          (lt, ":hours_since_last_rest", 96),
          (eq, ":operation_in_progress", 0),
          (gt, ":party_fit_for_battle", 30),	#MOTO chief size requirement
          
          (eq, ":faction_is_at_war", 1),
          (this_or_next|eq, ":troop_reputation", lrep_debauched),
          (eq, ":troop_reputation", lrep_quarrelsome),
          
          (assign, ":target_center", -1),
          (assign, ":score_to_beat",-5), #based on relation
          
          (try_for_range, ":center_no", villages_begin, villages_end),
            (store_faction_of_party, ":center_faction", ":center_no"),
            (store_relation, ":relation", ":center_faction", ":faction_no"),
            (lt, ":relation", 0),
            
            (neg|party_slot_ge, ":center_no", slot_village_state, svs_looted),
            (party_get_slot, ":town_lord", ":center_no", slot_town_lord),
            (call_script, "script_troop_get_relation_with_troop", ":troop_no", ":town_lord"),
            (assign, ":village_score", reg0),
            
            (lt, ":village_score", ":score_to_beat"),
            (assign, ":score_to_beat", ":village_score"),
            (assign, ":target_center", ":center_no"),
          (try_end),
          
          (is_between, ":target_center", centers_begin, centers_end),
          (assign, ":action", spai_raiding_around_center),
          (assign, ":object", ":target_center"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_have_a_score_to_settle_with_the_lord_there"),
            (str_store_string, s16, "str_i_am_thinking_of_settling_an_old_score"),
          (try_end),
          
          #I need money, so I am raiding where the money is
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (eq, ":faction_is_at_war", 1),
          (eq, ":operation_in_progress", 0),
          (gt, ":party_fit_for_battle", 30),	#MOTO chief size requirement
          
          (this_or_next|gt, ":hours_since_last_combat", 12),
          (lt, ":hours_since_last_rest", 96),
          (gt, ":aggressiveness", 40),
          
          (this_or_next|eq, ":troop_reputation", lrep_debauched),
          (this_or_next|eq, ":troop_reputation", lrep_selfrighteous),
          (this_or_next|eq, ":troop_reputation", lrep_cunning),
          (eq, ":troop_reputation", lrep_quarrelsome),
          
          (troop_get_slot, ":troop_wealth", ":troop_no", slot_troop_wealth),
          (lt, ":troop_wealth", 500),
          
          (assign, ":score_to_beat", 0),
          (assign, ":target_center", -1),
          
          #MOTO count lord's fiefs
          (assign, ":num_centers", 0),
          # (try_for_range, ":possible_target", villages_begin, villages_end),
          (try_for_range, ":center_no", centers_begin, centers_end),
            (party_get_slot, ":town_lord", ":center_no", slot_town_lord),
            (try_begin),
              (eq, ":town_lord", ":troop_no"),
              (val_add, ":num_centers", 1),
            (try_end),
            (is_between, ":center_no", villages_begin, villages_end),
            #MOTO end count lord's fiefs
            (store_faction_of_party, ":center_faction", ":center_no"),
            (store_relation, ":relation", ":center_faction", ":faction_no"),
            (lt, ":relation", 0),
            
            (this_or_next|party_slot_eq, ":center_no", slot_village_state, svs_normal),
            (party_slot_eq, ":center_no", slot_village_state, svs_being_raided),
            
            (party_get_slot, reg17, ":center_no", slot_town_prosperity),
            (store_distance_to_party_from_party, ":distance", ":party_no", ":center_no"),
            (val_sub, reg17, ":distance"),
            
            (gt, reg17, ":score_to_beat"),
            (assign, ":score_to_beat", reg17),
            (assign, ":target_center", ":center_no"),
          (try_end),
          
          (gt, ":num_centers", 0),	#a fief yields at least 1200 per week, so such a lord would not need to raid
          (gt, ":target_center", -1),
          
          (assign, ":action", spai_raiding_around_center),
          (assign, ":object", ":target_center"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_am_short_of_money_and_i_hear_that_there_is_much_wealth_there"),
            (str_store_string, s16, "str_i_need_to_refill_my_purse_preferably_with_the_enemys_money"),
          (try_end),
          
          #Attacking wealthiest lands
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (eq, ":faction_is_at_war", 1),
          (eq, ":operation_in_progress", 0),
          (gt, ":aggressiveness", 65),
          (gt, ":party_fit_for_battle", 30),	#MOTO chief size requirement
          
          (assign, ":score_to_beat", 0),
          (assign, ":target_center", -1),
          
          (try_for_range, ":center_no", villages_begin, villages_end),
            (store_faction_of_party, ":center_faction", ":center_no"),
            (store_relation, ":relation", ":center_faction", ":faction_no"),
            (lt, ":relation", 0),
            (neg|party_slot_eq, ":center_no", slot_village_state, svs_looted),
            (party_get_slot, ":village_prosperity", ":center_no", slot_town_prosperity),
            # (val_mul, ":village_prosperity", 2),	MOTO too far
            
            (store_distance_to_party_from_party, ":distance", ":party_no", ":center_no"),
            (val_sub, ":village_prosperity", ":distance"),
            (gt, ":village_prosperity", ":score_to_beat"),
            
            (assign, ":score_to_beat", ":village_prosperity"),
            (assign, ":target_center", ":center_no"),
          (try_end),
          
          (gt, ":target_center", -1),
          
          (assign, ":action", spai_raiding_around_center),
          (assign, ":object", ":target_center"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_by_striking_at_the_enemys_richest_lands_perhaps_i_can_draw_them_out_to_battle"),
            (str_store_string, s16, "str_i_am_thinking_of_going_on_the_attack"),
          (try_end),
          
          #End the war
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (eq, ":troop_reputation", lrep_upstanding),
          (eq, ":faction_is_at_war", 1),
          (eq, ":operation_in_progress", 0),
          (gt, ":party_fit_for_battle", 30),	#MOTO chief size requirement
          
          (assign, ":faction_to_attack", -1),
          (try_for_range, ":possible_faction_to_attack", kingdoms_begin, kingdoms_end),
            (store_relation, ":relation", ":faction_no", ":possible_faction_to_attack"),
            (lt, ":relation", 0),
            (faction_slot_eq, ":possible_faction_to_attack", slot_faction_state, sfs_active),
            
            (store_add, ":slot", ":possible_faction_to_attack", slot_faction_war_damage_inflicted_on_factions_begin),
            (val_sub, ":slot", kingdoms_begin),
            (faction_get_slot, ":war_damage_inflicted", ":faction_no", ":slot"),
            
            (store_add, ":slot", ":faction_no", slot_faction_war_damage_inflicted_on_factions_begin),
            (val_sub, ":slot", kingdoms_begin),
            (faction_get_slot, ":war_damage_suffered", ":possible_faction_to_attack", ":slot"),
            
            (gt, ":war_damage_inflicted", 80),
            (lt, ":war_damage_inflicted", ":war_damage_suffered"),
            (assign, ":faction_to_attack", ":possible_faction_to_attack"),
          (try_end),
          
          (gt, ":faction_to_attack", -1),
          
          (assign, ":target_center", -1),
          (assign, ":score_to_beat", 50),
          
          (try_for_range, ":center_no", villages_begin, villages_end),
            (store_faction_of_party, ":center_faction", ":center_no"),
            (eq, ":center_faction", ":faction_to_attack"),
            (neg|party_slot_eq, ":center_no", slot_village_state, svs_looted),
            (store_distance_to_party_from_party, ":distance", ":party_no", ":center_no"),
            (lt, ":distance", ":score_to_beat"),
            
            (assign, ":score_to_beat", ":distance"),
            (assign, ":target_center", ":center_no"),
          (try_end),
          
          (gt, ":target_center", -1),
          
          (assign, ":action", spai_raiding_around_center),
          (assign, ":object", ":target_center"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_perhaps_if_i_strike_one_more_blow_we_may_end_this_war_on_our_terms_"),
            (str_store_string, s16, "str_we_may_be_able_to_bring_this_war_to_a_close_with_a_few_more_blows"),
          (try_end),
          
          #I have a feast to attend
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_feast),
          (faction_get_slot, ":center_to_visit", ":faction_no", slot_faction_ai_object),
          (party_get_slot, ":town_lord", ":center_to_visit", slot_town_lord),
          (is_between, ":center_to_visit", walled_centers_begin, walled_centers_end),	#MOTO avoid bug
          (eq, ":operation_in_progress", 0),
          
          (call_script, "script_troop_get_relation_with_troop", ":troop_no", ":town_lord"),
          # (assign, ":relation_with_host", reg0),
          
          (ge, reg0, 0),
          
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":center_to_visit"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_wish_to_attend_the_feast_there"),
            (str_store_string, s16, "str_there_is_a_feast_which_i_wish_to_attend"),
          (try_end),
          #A lady to court
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (neg|troop_slot_eq, "trp_player", slot_troop_betrothed, ":troop_no"),
          # (troop_slot_eq, ":troop_no", slot_troop_spouse, -1),
          (neg|is_between, ":troop_no", kings_begin, kings_end),
          (neg|is_between, ":troop_no", pretenders_begin, pretenders_end),
          
          
          (gt, ":hours_since_last_courtship", 72),
          (eq, ":operation_in_progress", 0),
          (troop_slot_eq, ":troop_no", slot_troop_spouse, -1),
          
          (assign, ":center_to_visit", -1),
          (assign, ":score_to_beat", 150),
          
          (try_for_range, ":slot", slot_troop_love_interest_1, slot_troop_love_interests_end),
            (troop_get_slot, ":love_interest", ":troop_no", ":slot"),
            (is_between, ":love_interest", kingdom_ladies_begin, kingdom_ladies_end),
            (troop_get_slot, ":center_no", ":love_interest", slot_troop_cur_center),
            (is_between, ":center_no", centers_begin, centers_end),
            (store_faction_of_party, ":center_faction", ":center_no"),
            # (eq, ":faction_no", ":center_faction"),
            (store_relation, ":relation", ":faction_no", ":center_faction"),
            (ge, ":relation", 0),
            
            (store_distance_to_party_from_party, ":distance", ":party_no", ":center_no"),
            
            (lt, ":distance", ":score_to_beat"),
            (assign, ":center_to_visit", ":center_no"),
            (assign, ":score_to_beat", ":distance"),
          (try_end),
          
          (gt, ":center_to_visit", -1),
          
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":center_to_visit"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_there_is_a_fair_lady_there_whom_i_wish_to_court"),
            (str_store_string, s16, "str_i_have_the_inclination_to_pay_court_to_a_fair_lady"),
          (try_end),
          
          #Patrolling an alarmed center
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (assign, ":target_center", -1),
          (assign, ":score_to_beat", 60),
          (eq, ":operation_in_progress", 0),
          (gt, ":aggressiveness", 40),
          
          (try_for_range, ":center_no", centers_begin, centers_end), #find closest center that has spotted enemies.
            (store_faction_of_party, ":center_faction", ":center_no"),
            (eq, ":center_faction", ":faction_no"),
            (party_slot_ge, ":center_no", slot_center_last_spotted_enemy, 0),
            
            #new - begin
            # (party_get_slot, ":sortie_strength", ":center_to_patrol", slot_center_sortie_strength),	MOTO use party_slot_ge below
            (party_get_slot, ":enemy_strength_mul_14_div_10", ":center_no", slot_center_sortie_enemy_strength),
            (val_mul, ":enemy_strength_mul_14_div_10", 14),
            (val_div, ":enemy_strength_mul_14_div_10", 10),
            # (party_get_slot, ":party_strength", ":party_no", slot_party_cached_strength),	MOTO use party_slot_ge below
            
            (this_or_next|neg|party_is_in_town, ":party_no", ":center_no"),
            # (gt, ":sortie_strength", ":enemy_strength_mul_14_div_10"),
            (party_slot_ge, ":center_no", slot_center_sortie_strength, ":enemy_strength_mul_14_div_10"),
            
            # (ge, ":party_strength", 100),	MOTO this is 5 troops
            (party_slot_ge, ":party_no", slot_party_cached_strength, 2000),	#MOTO this is around 100 troops
            #new - end
            
            (party_get_slot, ":town_lord", ":center_no", slot_town_lord),
            (call_script, "script_troop_get_relation_with_troop", ":town_lord", ":troop_no"),
            
            (this_or_next|eq, ":troop_reputation", lrep_upstanding),
            (gt, reg0, -5),
            
            (store_distance_to_party_from_party, ":distance", ":party_no", ":center_no"),
            (lt, ":distance", ":score_to_beat"),
            
            (assign, ":target_center", ":center_no"),
            (assign, ":score_to_beat", ":distance"),
          (try_end),
          
          (is_between, ":target_center", centers_begin, centers_end),
          
          (assign, ":action", spai_patrolling_around_center),
          (assign, ":object", ":target_center"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_we_have_heard_reports_that_the_enemy_is_in_the_area"),
            (str_store_string, s16, "str_i_have_heard_reports_of_enemy_incursions_into_our_territory"),
          (try_end),
          
          #Get reinforcements	MOTO copy here as low priority recruit without restriction
        (else_try),
          (gt, ":center_to_visit", -1),
          (party_slot_eq, ":center_to_visit", slot_town_lord, ":troop_no"), #newly added
          
          # (lt, ":party_strength_as_percentage_of_ideal", 100),	#MOTO recruit whenever can
          
          #MOTO don't interrupt marshal operation for this
          (this_or_next|eq, ":operation_in_progress", 0),
          (neg|faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
          #MOTO end don't interrupt marshal operation for this
          
          (troop_get_slot, ":troop_wealth", ":troop_no", slot_troop_wealth),
          (assign, ":hiring_budget", ":troop_wealth"),	#MOTO hiring budget/reinforcement cost comparison from script_hire_men_to_kingdom_hero_party
          (val_mul, ":hiring_budget", 3),
          (val_div, ":hiring_budget", 4),
          
          (try_begin),
            (this_or_next|eq, ":faction_no", "fac_player_supporters_faction"),
            (eq, ":faction_no", "$players_kingdom"),
            (assign, ":reinforcement_cost", reinforcement_cost_moderate),
          (else_try),
            (options_get_campaign_ai, ":reduce_campaign_ai"),
            (try_begin),
              (eq, ":reduce_campaign_ai", 0), #hard
              (assign, ":reinforcement_cost", reinforcement_cost_hard),
            (else_try),
              (eq, ":reduce_campaign_ai", 2), #easy
              (assign, ":reinforcement_cost", reinforcement_cost_easy),
            (else_try),
              (assign, ":reinforcement_cost", reinforcement_cost_moderate),
            (try_end),
          (try_end),
          
          (ge, ":hiring_budget", ":reinforcement_cost"),
          
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":center_to_visit"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_dont_have_enough_troops_and_i_need_to_get_some_more"),
            (str_store_string, s16, "str_i_am_running_low_on_troops"),
          (try_end),
          
          #Time in household
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (gt, ":hours_since_last_home", 168),
          (eq, ":operation_in_progress", 0),
          
          (call_script, "script_lord_get_home_center", ":troop_no"),
          (assign, ":center_to_visit", reg0),
          (is_between, ":center_to_visit", walled_centers_begin, walled_centers_end),
          
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":center_to_visit"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_need_to_spend_some_time_with_my_household"),
            (str_store_string, s16, "str_it_has_been_a_long_time_since_i_have_been_able_to_spend_time_with_my_household"),
          (try_end),
          
          #Patrolling the borders
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (eq, ":faction_is_at_war", 1),
          (gt, ":aggressiveness", 65),
          (eq, ":operation_in_progress", 0),
          
          (assign, ":target_center", -1),
          (assign, ":score_to_beat", 75),
          
          (try_for_range, ":center_no", villages_begin, villages_end),
            (store_faction_of_party, ":center_faction", ":center_no"),
            (store_relation, ":relation", ":center_faction", ":faction_no"),
            (lt, ":relation", 0),
            
            (store_distance_to_party_from_party, ":distance", ":center_no", ":party_no"),
            (lt, ":distance", ":score_to_beat"),
            
            (assign, ":score_to_beat", ":distance"),
            (assign, ":target_center", ":center_no"),
          (try_end),
          
          (is_between, ":target_center", villages_begin, villages_end),
          
          (assign, ":action", spai_patrolling_around_center),
          (assign, ":object", ":target_center"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_am_watching_the_borders"),
            (str_store_string, s16, "str_i_may_be_needed_to_watch_the_borders"),
          (try_end),
          
          #Visiting a friend - temporarily disabled
          # (else_try),
          # (eq, 1, 0),
          
          #Patrolling home
        (else_try),
          (eq, ":operation_in_progress", 0),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (assign, ":center_to_visit", -1),
          (assign, ":end_val", centers_end),
          (try_for_range, ":center_no", centers_begin, ":end_val"),
            (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),
            
            (store_faction_of_party, ":center_faction", ":center_no"),
            (try_begin),
              (eq, ":center_faction", ":faction_no"),
              (assign, ":center_to_visit", ":center_no"),
              (assign, ":end_val", ":center_no"),	#break loop
            (else_try),
              (eq, "$cheat_mode", 1),
              (assign, reg1, ":troop_no"),
              (assign, reg2, ":faction_no"),
              (assign, reg3, ":center_no"),
              (assign, reg4, ":center_faction"),
              (display_message, "@{!}script_npc_decision_checklist_party_ai: troop {reg1} of faction {reg2} lord of center {reg3} of faction {reg4}"),
            (try_end),
          (try_end),
          
          (is_between, ":center_to_visit", centers_begin, centers_end),
          (assign, ":action", spai_patrolling_around_center),
          (assign, ":object", ":center_to_visit"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_will_guard_the_areas_near_my_home"),
            (str_store_string, s16, "str_i_am_perhaps_needed_most_at_home"),
          (try_end),
          
          #Default end
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (eq, ":operation_in_progress", 0),
          
          (call_script, "script_lord_get_home_center", ":troop_no"),
          (assign, ":center_to_visit", reg0),
          (is_between, ":center_to_visit", walled_centers_begin, walled_centers_end),
          
          (assign, ":action", spai_holding_center),
          (assign, ":object", ":center_to_visit"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_cant_think_of_anything_better_to_do"),
          (try_end),
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (eq, ":operation_in_progress", 1),
          
          (party_get_slot, ":action", ":party_no", slot_party_ai_state),
          (party_get_slot, ":object", ":party_no", slot_party_ai_object),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_am_completing_what_i_have_already_begun"),
          (try_end),
        (else_try),
          # (eq, ":do_only_collecting_rents", 0),	MOTO chief since we are past "visit estates" section, there's no rent to collect
          (assign, ":action", spai_undefined),
          (assign, ":object", -1),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s14, "str_i_dont_even_have_a_home_to_which_to_return"),
          (try_end),
        (try_end),
        (assign, reg0, ":action"),
        (assign, reg1, ":object"),
      (try_end),
      
      (try_begin),
        (eq, "$cheat_mode", 2),
        (str_store_troop_name, s10, ":troop_no"),
        (display_message, "str_debug__s10_decides_s14_faction_ai_s15"),
      (try_end),
  ]),
  
  #script_npc_decision_checklist_troop_follow_or_not
  # INPUT: troop_no
  # OUTPUT: reg0
  (
    "npc_decision_checklist_troop_follow_or_not", [
      
      (store_script_param, ":troop_no", 1),
      (store_faction_of_troop, ":faction_no", ":troop_no"),
      (faction_get_slot, ":faction_ai_state", ":faction_no", slot_faction_ai_state),
      
      (troop_get_slot, ":troop_reputation", ":troop_no", slot_lord_reputation_type),
      (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
      
      (assign, ":result", 0),
      (try_begin),
        (eq, ":faction_marshal", -1),
        
        (try_begin),
          (eq, ":troop_no", "$g_talk_troop"),
          (str_store_string, s15, "str__i_am_acting_independently_because_no_marshal_is_appointed"),
        (try_end),
      (else_try),
        (troop_get_slot, ":faction_marshal_party", ":faction_marshal", slot_troop_leaded_party),
        (neg|party_is_active, ":faction_marshal_party"),
        
        #Not doing an offensive
        (try_begin),
          (eq, ":troop_no", "$g_talk_troop"),
          (str_store_string, s15, "str__i_am_acting_independently_because_our_marshal_is_currently_indisposed"),
        (try_end),
      (else_try),
        (neq, ":faction_ai_state", sfai_attacking_center),
        (neq, ":faction_ai_state", sfai_raiding_village),
        (neq, ":faction_ai_state", sfai_attacking_enemies_around_center),
        (neq, ":faction_ai_state", sfai_attacking_enemy_army),
        (neq, ":faction_ai_state", sfai_gathering_army),
        
        #Not doing an offensive
        (try_begin),
          (eq, ":troop_no", "$g_talk_troop"),
          (str_store_string, s15, "str__i_am_acting_independently_because_our_realm_is_currently_not_on_campaign"),
        (try_end),
      (else_try),
        (call_script, "script_troop_get_relation_with_troop", ":troop_no", ":faction_marshal"),
        (assign, ":relation_with_marshal", reg0),
        
        (try_begin),
          (le, ":relation_with_marshal", -10),
          (assign, ":acceptance_level", 10000),
        (else_try),
          (store_mul, ":acceptance_level", ":relation_with_marshal", -1000),
        (try_end),
        
        (val_add, ":acceptance_level", 1500),
        
        (options_get_campaign_ai, ":reduce_campaign_ai"), #mot chief
        (try_begin),
          (neq, ":faction_no", "$players_kingdom"),
          (try_begin),
            (eq, ":reduce_campaign_ai", 0), #hard
            (val_add, ":acceptance_level", -1250),
          (else_try),
            (eq, ":reduce_campaign_ai", 1), #moderate
          (else_try),
            (eq, ":reduce_campaign_ai", 2), #easy
            (val_add, ":acceptance_level", 1250),
          (try_end),
        (else_try),
          (faction_slot_eq, ":faction_no", slot_faction_marshal, "trp_player"),
          (try_begin),
            (eq, ":reduce_campaign_ai", 0), #hard/player's faction
            (val_add, ":acceptance_level", -1000),
          (else_try),
            (eq, ":reduce_campaign_ai", 1), #moderate/player's faction
            (val_add, ":acceptance_level", -1500),
          (else_try),
            (eq, ":reduce_campaign_ai", 2), #easy/player's faction
            (val_add, ":acceptance_level", -2000),
          (try_end),
        (try_end),
        
        (troop_get_slot, ":temp_ai_seed", ":troop_no", slot_troop_temp_decision_seed),
        
        (le, ":temp_ai_seed", ":acceptance_level"),
        
        #Very low opinion of marshal
        (try_begin),
          (eq, ":troop_no", "$g_talk_troop"),
          (str_store_string, s15, "str__i_am_not_accompanying_the_marshal_because_i_fear_that_he_may_lead_us_into_disaster"),
        (try_end),
        #Make nuanced, depending on personality type
      (else_try),
        # (troop_get_slot, ":marshal_controversy", ":faction_marshal", slot_faction_marshal),
        (troop_get_slot, ":marshal_controversy", ":faction_marshal", slot_troop_controversy),	#MOTO correction!
        
        (lt, ":relation_with_marshal", -5),#was 0
        (ge, ":marshal_controversy", 50),
        
        (try_begin),
          (eq, ":troop_no", "$g_talk_troop"),
          (str_store_string, s15, "str_i_am_not_accompanying_the_marshal_because_i_question_his_judgment"),
        (try_end),
      (else_try),
        # (troop_get_slot, ":marshal_controversy", ":faction_marshal", slot_faction_marshal),	MOTO wrong!
        (neg|faction_slot_eq, ":faction_no", slot_faction_leader, ":faction_marshal"),
        
        (lt, ":relation_with_marshal", 5),
        (ge, ":marshal_controversy", 80),
        
        (try_begin),
          (eq, ":troop_no", "$g_talk_troop"),
          (str_store_string, s15, "str_i_am_not_accompanying_the_marshal_because_will_be_reappointment"),
        (try_end),
      (else_try),
        #(lt, ":relation_with_marshal", 45),
        #(eq, ":faction_marshal", "trp_player"), #moved below as only effector. Search "think about this".
        
        (store_sub, ":relation_with_marshal_difference", 50, ":relation_with_marshal"),
        
        #for 50 relation with marshal ":acceptance_level" will be 0
        #for 20 relation with marshal ":acceptance_level" will be 2100
        #for 10 relation with marshal ":acceptance_level" will be 2800
        #for 0 relation with marshal ":acceptance_level" will be 3500
        #for -10 relation with marshal ":acceptance_level" will be 4200
        #average is about 2500
        (store_mul, ":acceptance_level", ":relation_with_marshal_difference", 70),
        
        (options_get_campaign_ai, ":reduce_campaign_ai"), # moto chief
        (try_begin),
          (neq, ":faction_no", "$players_kingdom"),
          
          (try_begin),
            (eq, ":reduce_campaign_ai", 0), #hard
            (val_add, ":acceptance_level", -1200),
          (else_try),
            (eq, ":reduce_campaign_ai", 1), #moderate
          (else_try),
            (eq, ":reduce_campaign_ai", 2), #easy
            (val_add, ":acceptance_level", 1200),
          (try_end),
        (else_try),
          (eq, ":faction_marshal", "trp_player"),
          
          (try_begin),
            (eq, ":reduce_campaign_ai", 0), #hard
            (val_add, ":acceptance_level", -1000),
          (else_try),
            (eq, ":reduce_campaign_ai", 1), #moderate
            (val_add, ":acceptance_level", -1500),
          (else_try),
            (eq, ":reduce_campaign_ai", 2), #easy
            (val_add, ":acceptance_level", -2000),
          (try_end),
        (try_end),
        
        (try_begin),
          (eq, ":troop_reputation", lrep_selfrighteous),
          (val_add, ":acceptance_level", 1500),
        (else_try),
          (this_or_next|eq, ":troop_reputation", lrep_martial),
          (this_or_next|eq, ":troop_reputation", lrep_roguish),
          (eq, ":troop_reputation", lrep_quarrelsome),
          (val_add, ":acceptance_level", 1000),
        (else_try),
          (eq, ":troop_reputation", lrep_cunning),
          (val_add, ":acceptance_level", 500),
        (else_try),
          (eq, ":troop_reputation", lrep_upstanding), #neutral
        (else_try),
          (this_or_next|eq, ":troop_reputation", lrep_benefactor), #helper
          (eq, ":troop_reputation", lrep_goodnatured),
          (val_add, ":acceptance_level", -500),
        (else_try),
          (eq, ":troop_reputation", lrep_custodian), #very helper
          (val_add, ":acceptance_level", -1000),
        (try_end),
        
        (try_begin),
          (troop_slot_eq, ":faction_marshal", slot_lord_reputation_type, lrep_quarrelsome),
          (val_add, ":acceptance_level", -750),
        (else_try),
          (this_or_next|troop_slot_eq, ":faction_marshal", slot_lord_reputation_type, lrep_martial),
          (troop_slot_eq, ":faction_marshal", slot_lord_reputation_type, lrep_upstanding),
          (val_add, ":acceptance_level", -250),
        (try_end),
        
        (val_add, ":acceptance_level", 2000),
        #average become 2500 + 2000 = 4500, (45% of lords will not join campaign because of this reason. (33% for hard, 57% for easy, 30% for marshal player))
        
        (troop_get_slot, ":temp_ai_seed", ":troop_no", slot_troop_temp_decision_seed),
        
        (le, ":temp_ai_seed", ":acceptance_level"),
        
        (try_begin),
          (eq, ":troop_no", "$g_talk_troop"),
          (str_store_string, s15, "str_i_am_not_accompanying_the_marshal_because_i_can_do_greater_deeds"),
        (try_end),
        
        #(try_begin),
        #  (ge, "$cheat_mode", 1),
        #  (assign, reg7, ":acceptance_level"),
        #  (assign, reg8, ":relation_with_marshal"),
        #  (display_message, "@{!}DEBUGS : acceptance level : {reg7}, relation with marshal : {reg8}"),
        #(try_end),
      (else_try),
        (store_current_hours, ":hours_since_last_faction_rest"),
        (faction_get_slot, ":last_rest_time", ":faction_no", slot_faction_ai_last_rest_time),
        (val_sub, ":hours_since_last_faction_rest", ":last_rest_time"),
        
        #nine days on average, marshal will usually end after 10 days
        #ozan changed, 360 hours (15 days) in average, marshal cannot end it during a siege attack/defence anymore.
        (assign, ":troop_campaign_limit", 360),
        (store_mul, ":marshal_relation_modifier", ":relation_with_marshal", 6), #ozan changed 4 to 6.
        (val_add, ":troop_campaign_limit", ":marshal_relation_modifier"),
        
        (try_begin),
          (eq, ":troop_reputation", lrep_upstanding),
          (val_mul, ":troop_campaign_limit", 4),
          (val_div, ":troop_campaign_limit", 3),
        (try_end),
        
        (str_store_troop_name, s16, ":faction_marshal"),
        
        (gt, ":hours_since_last_faction_rest", ":troop_campaign_limit"),
        
        #Too long a campaign
        (try_begin),
          (eq, ":troop_no", "$g_talk_troop"),
          (str_store_string, s15, "str__s16_has_kept_us_on_campaign_on_far_too_long_and_there_are_other_pressing_matters_to_which_i_must_attend"),
        (try_end),
        #Also make nuanced, depending on personality type
      (else_try),
        (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
        (neg|party_is_active, ":party_no"),
        #This string should not occur, as it will only happen if a lord is contemplating following the player
      (else_try),
        (troop_get_slot, ":marshal_party", ":faction_marshal", slot_troop_leaded_party),
        
        (assign, ":information_radius", 40),
        (try_begin),
          (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_gathering_army),
          (assign, ":information_radius", 50),
        (try_end),
        
        (options_get_campaign_ai, ":reduce_campaign_ai"),
        (try_begin),
          (neq, ":faction_no", "fac_player_supporters_faction"),
          (neq, ":faction_no", "$players_kingdom"),
          (try_begin),
            (eq, ":reduce_campaign_ai", 2), #easy
            (try_begin),
              (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_gathering_army),
              (val_add, ":information_radius", -10),
            (else_try),
              (val_add, ":information_radius", -8),
            (try_end),
          (else_try),
            (eq, ":reduce_campaign_ai", 1), #moderate
            (try_begin),
              (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_gathering_army),
              (val_add, ":information_radius", -5),
            (else_try),
              (val_add, ":information_radius", -4),
            (try_end),
          (try_end),
        (else_try),
          (try_begin),
            (eq, ":reduce_campaign_ai", 2), #easy
            (try_begin),
              (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_gathering_army),
              (val_add, ":information_radius", 25),
            (else_try),
              (val_add, ":information_radius", 20),
            (try_end),
          (else_try),
            (eq, ":reduce_campaign_ai", 1), #moderate
            (try_begin),
              (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_gathering_army),
              (val_add, ":information_radius", 15),
            (else_try),
              (val_add, ":information_radius", 12),
            (try_end),
          (else_try),
            (eq, ":reduce_campaign_ai", 0), #hard
            (try_begin),
              (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_gathering_army),
              (val_add, ":information_radius", 5),
            (else_try),
              (val_add, ":information_radius", 4),
            (try_end),
          (try_end),
        (try_end),
        
        (faction_get_slot, ":faction_object", ":faction_no", slot_faction_ai_object),
        (assign, reg17, 0),
        (try_begin),
          (try_begin),
            (neg|is_between, ":faction_object", villages_begin, villages_end),
            (assign, reg17, 1),
          (try_end),
          (try_begin),
            (neg|faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_attacking_enemies_around_center),
            (assign, reg17, 1),
          (try_end),
          (eq, reg17, 1),
          
          (store_distance_to_party_from_party, ":distance", ":marshal_party", ":party_no"),
          
          (gt, ":distance", ":information_radius"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s15, "str__i_am_not_participating_in_the_marshals_campaign_because_i_do_not_know_where_to_find_our_main_army"),
          (try_end),
        (else_try),
          (eq, reg17, 0),
          
          (assign, reg17, 1),
          (try_begin),
            #if we are already accompanying marshal forget below.
            (party_slot_eq, ":party_no", slot_party_ai_state, spai_accompanying_army),
            (party_slot_eq, ":party_no", slot_party_ai_object, ":marshal_party"),
            (assign, reg17, 0),
          (try_end),
          (eq, reg17, 1),
          
          #if faction ai is "attacking enemies around a center" is then do not find and compare distance to marshal, find and compare distance to "attacked village"
          (party_get_slot, ":enemy_strength_nearby", ":faction_object", slot_center_sortie_enemy_strength),
          
          (try_begin), #changes between 70..x (as ":enemy_strength_nearby" increases, ":information_radius" increases too.),
            (ge, ":enemy_strength_nearby", 4000),
            (val_sub, ":enemy_strength_nearby", 4000),
            (store_div, ":information_radius", ":enemy_strength_nearby", 200),
            (val_add, ":information_radius", 70),
          (else_try), #changes between 30..70
            (store_div, ":information_radius", ":enemy_strength_nearby", 100),
            (val_add, ":information_radius", 30),
          (try_end),
          
          (store_distance_to_party_from_party, ":distance", ":faction_object", ":party_no"),
          
          (gt, ":distance", ":information_radius"),
          
          (try_begin),
            (eq, ":troop_no", "$g_talk_troop"),
            (str_store_string, s15, "str__i_am_acting_independently_although_some_enemies_have_been_spotted_within_our_borders_they_havent_come_in_force_and_the_local_troops_should_be_able_to_dispatch_them"),
          (try_end),
        (try_end),
        
        (gt, ":distance", ":information_radius"),
      (else_try),
        (try_begin),
          (eq, ":troop_no", "$g_talk_troop"),
          (str_store_string, s15, "str__the_needs_of_the_realm_must_come_first"),
        (try_end),
        (assign, ":result", 1),
      (try_end),
      
      (assign, reg0, ":result"),
  ]),
  
  #script_find_total_prosperity_score
  # INPUT: center_no
  # OUTPUT: reg0 = total_prosperity_score
  (
    "find_total_prosperity_score",
    [
      (store_script_param, ":center_no", 1),
      
      (try_begin), #":total_prosperity_score" changes between 10..100
        (is_between, ":center_no", walled_centers_begin, walled_centers_end),
        
        (party_get_slot, ":center_prosperity", ":center_no", slot_town_prosperity),
        (store_add, ":center_prosperity_add_200_div_10", ":center_prosperity", 200),
        (val_div, ":center_prosperity_add_200_div_10", 10),
        (try_begin),
          (is_between, ":center_no", towns_begin, towns_end),
          (store_mul, ":this_center_score", ":center_prosperity_add_200_div_10", 15),
        (else_try),
          (store_mul, ":this_center_score", ":center_prosperity_add_200_div_10", 5),
        (try_end),
        (assign, ":total_prosperity_score", ":this_center_score"),
        
        (try_for_range_backwards, ":village_no", villages_begin, villages_end),
          (party_slot_eq, ":village_no", slot_village_bound_center, ":center_no"),
          
          (party_get_slot, ":village_prosperity", ":village_no", slot_town_prosperity),
          (store_add, ":village_prosperity_add_200_div_10", ":village_prosperity", 200),
          (val_div, ":village_prosperity_add_200_div_10", 10),
          (store_mul, ":this_village_score", ":village_prosperity_add_200_div_10", 5),
          
          (val_add, ":total_prosperity_score", ":this_village_score"),
        (try_end),
      (else_try),
        (party_get_slot, ":center_prosperity", ":center_no", slot_town_prosperity),
        (store_add, ":center_prosperity_add_200_div_10", ":center_prosperity", 200),
        (val_div, ":center_prosperity_add_200_div_10", 10),
        (store_mul, ":this_center_score", ":center_prosperity_add_200_div_10", 5),
        (assign, ":total_prosperity_score", ":this_center_score"),
      (try_end),
      (val_div, ":total_prosperity_score", 10),
      
      (assign, reg0, ":total_prosperity_score"),
  ]),
  
  #script_calculate_center_assailability_score
  # INPUT: faction_no
  # param1: faction_no
  # param2: all_vassals_included, (becomes 1 if we want to find attackable center if we collected 20% of vassals during gathering army phase)
  # OUTPUT:
  # reg0 = center_to_attack (-1 if none is logical)
  # reg1 = maximum_attack_score
  (
    "calculate_center_assailability_score",
    [
      (store_script_param, ":troop_no", 1),
      (store_script_param, ":potential_target", 2),
      (store_script_param, ":all_vassals_included", 3),
      
      (assign, ":target_score", -1),
      
      (store_faction_of_troop, ":faction_no", ":troop_no"),
      
      (store_current_hours, ":hours_since_last_offensive"),
      (faction_get_slot, ":last_offensive_time", ":faction_no", slot_faction_last_offensive_concluded),
      (val_sub, ":hours_since_last_offensive", ":last_offensive_time"),
      
      (store_div, ":last_offensive_time_score", ":hours_since_last_offensive", 12), #30..50 MOTO 30..100
      (val_add, ":last_offensive_time_score", 30),
      (val_min, ":last_offensive_time_score", 100),
      
      (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
      
      (assign, ":marshal_party", -1),
      (assign, ":marshal_strength", 0),
      #(assign, ":strength_of_nearby_friend", 0),
      
      (try_begin),
        (gt, ":faction_marshal", 0),
        (troop_get_slot, ":marshal_party", ":faction_marshal", slot_troop_leaded_party),
        (party_is_active, ":marshal_party"),
        (party_get_slot, ":marshal_strength", ":marshal_party", slot_party_cached_strength),
        #(eq, ":all_vassals_included", 0),
        (party_get_slot, ":strength_of_current_followers", ":marshal_party", slot_party_follower_strength),
        #(party_get_slot, ":strength_of_nearby_friend", ":marshal_party", slot_party_nearby_friend_strength),
      (try_end),
      
      #(try_begin),
      #  (eq, ":all_vassals_included", 0),
      #
      #  (try_begin),
      #    (gt, ":faction_marshal", 0),
      #    (troop_get_slot, ":marshal_party", ":faction_marshal", slot_troop_leaded_party),
      #    (party_is_active, ":marshal_party"),
      #    (party_get_slot, ":strength_of_potential_followers", ":marshal_party", slot_party_follower_strength),
      #  (try_end),
      #(else_try),
      #  (eq, ":all_vassals_included", 1),
      #
      #  (assign, ":strength_of_potential_followers", 0),
      #
      #  (try_for_parties, ":party_no"),
      #    (store_faction_of_party, ":party_faction", ":party_no"),
      #    (eq, ":party_faction", ":faction_no"),
      #    (neq, ":party_no", ":marshal_party"),
      #    (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
      #    (call_script, "script_party_calculate_strength", ":party_no", 0),
      #    (val_add, ":strength_of_potential_followers", reg0),
      #  (try_end),
      #
      #  (val_div, ":strength_of_potential_followers", 2), #Ozan - Think about this, will you divide strength_of_potential_followers to 3 or 2.5 or 2
      #(else_try),
      #  (assign, ":strength_of_potential_followers", 0),
      #(try_end),
      
      (faction_get_slot, ":last_attacked_center", ":faction_no", slot_faction_last_attacked_center),
      (faction_get_slot, ":last_attacked_hours", ":faction_no", slot_faction_last_attacked_hours),
      
      (try_begin),
        (store_current_hours, ":hours"),
        (store_add, ":last_attacked_hours_plus_24", ":last_attacked_hours", 24),
        (gt, ":hours", ":last_attacked_hours_plus_24"),
        (faction_set_slot, ":faction_no", slot_faction_last_attacked_center, 0),
        (assign, ":last_attacked_center", 0),
      (try_end),
      
      (try_begin),
        (this_or_next|eq, ":last_attacked_center", 0),
        (this_or_next|eq, ":last_attacked_center", ":potential_target"),
        (this_or_next|eq, "$g_do_not_skip_other_than_current_ai_object", 1),
        (neg|faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
        
        (party_is_active, ":potential_target"),
        (store_faction_of_party, ":potential_target_faction", ":potential_target"),
        
        (store_relation, ":relation", ":potential_target_faction", ":faction_no"),
        (lt, ":relation", 0),
        
        #attack if and only if we are already besieging that center or anybody do not making besiege.
        (assign, ":faction_of_besieger_party", -1),
        (try_begin),
          (is_between, ":potential_target", walled_centers_begin, walled_centers_end),
          (neg|party_slot_eq, ":potential_target", slot_center_is_besieged_by, -1),
          (party_get_slot, ":besieger_party", ":potential_target", slot_center_is_besieged_by),
          (party_is_active, ":besieger_party"),
          (store_faction_of_party, ":faction_of_besieger_party", ":besieger_party"),
        (try_end),
        
        (this_or_next|eq, ":faction_of_besieger_party", -1),
        (eq, ":faction_of_besieger_party", ":faction_no"),
        
        #attack if and only if this center is not a village or if it is village it should not be raided or looted
        (assign, ":village_is_looted_or_raided_already", 0),
        (try_begin),
          (is_between, ":potential_target", villages_begin, villages_end),
          (try_begin),
            (party_slot_eq, ":potential_target", slot_village_state, svs_being_raided),
            (party_get_slot, ":raider_party", ":potential_target", slot_village_raided_by),
            (party_is_active, ":raider_party"),
            
            (store_faction_of_party, ":raider_faction", ":raider_party"),
            (neq, ":raider_faction", ":faction_no"),
            (assign, ":raiding_by_one_other_faction", 1),
          (else_try),
            (assign, ":raiding_by_one_other_faction", 0),
          (try_end),
          (this_or_next|party_slot_eq, ":potential_target", slot_village_state, svs_looted),
          (eq, ":raiding_by_one_other_faction", 1),
          (assign, ":village_is_looted_or_raided_already", 1),
        (try_end),
        (eq, ":village_is_looted_or_raided_already", 0),
        
        #if ":potential_target" is faction object of some other faction which is enemy to owner of
        #":potential_target" then this target cannot be new target we are looking for.
        (assign, ":this_potantial_target_is_target_of_some_other_faction", 0),
        (try_for_range, ":cur_faction", kingdoms_begin, kingdoms_end),
          (is_between, ":cur_faction", "fac_kingdom_1", kingdoms_end), #Excluding player kingdom
          (neq, ":cur_faction", ":faction_no"),
          (faction_get_slot, ":faction_object", ":cur_faction", slot_faction_ai_object),
          (eq, ":faction_object", ":potential_target"),
          (store_relation, ":rel", ":potential_target_faction", ":cur_faction"),
          (lt, ":rel", 0),
          (assign, ":this_potantial_target_is_target_of_some_other_faction", 1),
        (try_end),
        (eq, ":this_potantial_target_is_target_of_some_other_faction", 0),
        
        (try_begin),
          (is_between, ":potential_target", walled_centers_begin, walled_centers_end),
          (party_get_slot, ":potential_target_inside_strength", ":potential_target", slot_party_cached_strength),
          (party_get_slot, ":potential_target_nearby_enemy_strength", ":potential_target", slot_party_nearby_friend_strength),
          (val_div, ":potential_target_nearby_enemy_strength", 2),
          (store_add, ":potential_target_strength", ":potential_target_inside_strength", ":potential_target_nearby_enemy_strength"),
          
          #(try_begin),
          #(eq, ":faction_no", "fac_kingdom_4"),
          #(assign, reg0, ":potential_target_inside_strength"),
          #(assign, reg1, ":potential_target_nearby_enemy_strength"),
          #(assign, reg2, ":marshal_strength"),
          #(assign, reg3, ":strength_of_potential_followers"),
          #(assign, reg4, ":strength_of_nearby_friend"),
          #(assign, reg6, ":marshal_party"),
          #(str_store_party_name, s8, ":potential_target"),
          #(eq, ":all_vassals_included", 0),
          #(display_message, "@{!}DEBUG : {s8}:{reg0}, neare {reg1}, our {reg2}, follow {reg3}, nearf {reg4}"),
          #(try_end),
          
          (val_mul, ":potential_target_strength", 4), #in walled centers defenders have advantage.
          (val_div, ":potential_target_strength", 3),
          
          #(store_add, ":army_strength", ":marshal_strength", ":strength_of_potential_followers"),
          (assign, ":army_strength", ":marshal_strength"),
          (val_add, ":army_strength", ":strength_of_current_followers"),
          (store_mul, ":power_ratio", ":army_strength", 100),
          
          #this ratio ":power_ratio" shows (our total army power) / (their total army power)
          (try_begin),
            (gt, ":potential_target_strength", 0),
            (val_div, ":power_ratio", ":potential_target_strength"),
          (else_try),
            (assign, ":power_ratio", 1000),
          (try_end),
        (else_try),
          # (party_get_slot, ":potential_target_nearby_enemy_strength", ":potential_target", slot_party_nearby_friend_strength),
          (assign, ":potential_target_strength", 1000),
          
          #(store_add, ":army_strength", ":marshal_strength", ":strength_of_potential_followers"),
          (assign, ":army_strength", ":marshal_strength"),
          (val_add, ":army_strength", ":strength_of_current_followers"),
          (store_mul, ":power_ratio", ":army_strength", 100),
          
          (try_begin),
            (gt, ":potential_target_strength", 0),
            (val_div, ":power_ratio", ":potential_target_strength"),
          (else_try),
            (assign, ":power_ratio", 1000),
          (try_end),
        (try_end),
        
        (ge, ":power_ratio", 120), #attack if and only if our army is at least 1.2 times powerfull
        (store_sub, ":power_ratio_sub_120", ":power_ratio", 120),
        
        (try_begin),
          (lt, ":power_ratio_sub_120", 100), #changes between 20..120
          (store_add, ":power_ratio_score", ":power_ratio_sub_120", 20),
        (else_try),
          (lt, ":power_ratio_sub_120", 200), #changes between 120..170
          (store_sub, ":power_ratio_score", ":power_ratio_sub_120", 100),
          (val_div, ":power_ratio_score", 2),
          (val_add, ":power_ratio_score", 120),
        (else_try),
          (lt, ":power_ratio_sub_120", 400), #changes between 170..210
          (store_sub, ":power_ratio_score", ":power_ratio_sub_120", 200),
          (val_div, ":power_ratio_score", 5),
          (val_add, ":power_ratio_score", 170),
        (else_try),
          (lt, ":power_ratio_sub_120", 800), #changes between 210..250
          (store_sub, ":power_ratio_score", ":power_ratio_sub_120", 400),
          (val_div, ":power_ratio_score", 10),
          (val_add, ":power_ratio_score", 210),
        (else_try),
          (assign, ":power_ratio_score", 250),
        (try_end),
        
        (assign, ":number_of_walled_centers", 0),
        (assign, ":total_distance", 0),
        (try_for_range, ":walled_center", walled_centers_begin, walled_centers_end),
          (store_faction_of_party, ":walled_center_faction", ":walled_center"),
          (eq, ":walled_center_faction", ":faction_no"),
          
          (store_distance_to_party_from_party, ":dist", ":walled_center", ":potential_target"),
          (val_add, ":total_distance", ":dist"),
          
          (val_add, ":number_of_walled_centers", 1),
        (try_end),
        
        (try_begin),
          (gt, ":number_of_walled_centers", 0),
          (store_div, ":average_distance", ":total_distance", ":number_of_walled_centers"),
          #(assign, reg0, ":average_distance"),
          #(str_store_faction_name, s7, ":faction_no"),
          #(str_store_party_name, s8, ":potential_target"),
          #(display_message, "@average distance for {s7} for {s8} is {reg0}"),
          
          (try_begin),
            (ge, ":marshal_party", 0),
            (party_is_active, ":marshal_party"),
            (store_distance_to_party_from_party, ":marshal_dist_to_potential_target", ":marshal_party", ":potential_target"),
          (else_try),
            (assign, ":marshal_dist_to_potential_target", 100),
          (try_end),
          
          (try_begin),
            #if currently main aim of our faction is attacking to an enemy center and that center is already besieged/raided by one of
            #our parties then divide marshal_dist_to_potential_target_div_x score for current center to "3/2" instead of "3" and this
            #result in decrease at distance_score, and also decrease some scores from power_ratio_score in order to avoid frequently
            #changes at main aimed target city of our faction during sieges.
            
            (faction_get_slot, ":current_ai_state", ":faction_no", slot_faction_ai_state),
            (eq, ":current_ai_state", sfai_attacking_center),
            (faction_get_slot, ":current_ai_object", ":faction_no", slot_faction_ai_object),
            
            (ge, ":current_ai_object", 0),
            (neq, ":current_ai_object", ":potential_target"),
            
            (try_begin),
              (ge, ":power_ratio_score", 300), #200 max
              (assign, ":power_ratio_score", 200),
            (else_try),
              (ge, ":power_ratio_score", 100), #100..200
              (val_sub, ":power_ratio_score", 100),
              (val_div, ":power_ratio_score", 2),
              (val_add, ":power_ratio_score", 100),
            (try_end),
            
            (try_begin),
              (faction_slot_eq, ":faction_no", slot_faction_marshal, ":troop_no"),
              (eq, "$g_do_not_skip_other_than_current_ai_object", 0),
              (assign, ":power_ratio_score", 0), #lets completely forget all other choices if we are already besieging one center.
            (try_end),
            
            (faction_set_slot, ":faction_no", slot_faction_last_attacked_center, ":current_ai_object"),
            (store_current_hours, ":hours"),
            (faction_set_slot, ":faction_no", slot_faction_last_attacked_hours, ":hours"),
            
            (eq, ":all_vassals_included", 0),
            
            (store_mul, ":marshal_dist_to_potential_target_div_x", ":marshal_dist_to_potential_target", 2),
            (val_div, ":marshal_dist_to_potential_target_div_x", 3),
          (else_try),
            (store_div, ":marshal_dist_to_potential_target_div_x", ":marshal_dist_to_potential_target", 3),
          (try_end),
          
          (store_add, ":total_distance", ":average_distance", ":marshal_dist_to_potential_target_div_x"), #in average ":total_distance" is about 150, min : 0, max : 1000
        (else_try),
          (assign, ":total_distance", 100),
        (try_end),
        
        (try_begin),
          #according to cautious troop distance is more important
          (this_or_next|neq, ":faction_no", "fac_kingdom_1"),
          (neq, ":faction_no", "fac_kingdom_2"),
          (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_upstanding),
          (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_debauched),
          (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_goodnatured),
          (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_cunning),
          
          #MOTO chief use hyperbolic and fit to kingdom size
          #Hyperbolic function drops off more sharply (emphasizes closeness more)
          #Fit to kingdom size means small kingdoms won't attack too far from their centers
          # (try_begin),
          # (lt, ":total_distance", 30), #very close (100p)
          # (assign, ":distance_score", 100),
          # (else_try),
          # (lt, ":total_distance", 80), #close (50p-100p)
          # (store_sub, ":distance_score", ":total_distance", 30),
          # (val_div, ":distance_score", 1),
          # (store_sub, ":distance_score", 100, ":distance_score"),
          # (else_try),
          # (lt, ":total_distance", 160), #far (10p-50p)
          # (store_sub, ":distance_score", ":total_distance", 80),
          # (val_div, ":distance_score", 2),
          # (store_sub, ":distance_score", 50, ":distance_score"),
          # (else_try),
          # (assign, ":distance_score", 10), #very far
          # (try_end),
          # (else_try),
          # #according to agressive troop distance is less important
          
          # (try_begin),
          # (lt, ":total_distance", 40), #very close (100p)
          # (assign, ":distance_score", 100),
          # (else_try),
          # (lt, ":total_distance", 140), #close (50p-100p)
          # (store_sub, ":distance_score", ":total_distance", 40),
          # (val_div, ":distance_score", 2),
          # (store_sub, ":distance_score", 100, ":distance_score"),
          # (else_try),
          # (lt, ":total_distance", 300), #far (10p-50p)
          # (store_sub, ":distance_score", ":total_distance", 140),
          # (val_div, ":distance_score", 4),
          # (store_sub, ":distance_score", 50, ":distance_score"),
          # (else_try),
          # (assign, ":distance_score", 10), #very far
          # (try_end),
          # (try_end),
          (assign, ":depth_of_incursion", 2*min_dist_neighb_of_neighb),
        (else_try),
          #according to agressive troop distance is less important
          (assign, ":depth_of_incursion", 4*min_dist_neighb_of_neighb),
        (try_end),
        
        (try_begin),
          (this_or_next|eq, ":faction_no", "fac_kingdom_1"),
          (eq, ":faction_no", "fac_kingdom_2"),
          (val_mul, ":depth_of_incursion", 4),	#practically the whole map for the aggressive Viking
        (try_end),
        
        #estimate kingdom "radius"
        (assign, reg0, ":number_of_walled_centers"),
        (convert_to_fixed_point, reg0),
        (store_sqrt, ":hyper_denominator", reg0),
        (convert_from_fixed_point, ":hyper_denominator"),
        (val_sub, ":hyper_denominator", 1),
        (val_mul, ":hyper_denominator", -22),	# min_dist_neighb_of_neighb / SQRT(2)
        
        #build rest of function
        (store_div, reg0, ":total_distance", 2),	#average marshal and average-walled-center distances
        (val_add, ":hyper_denominator", reg0),
        (val_div, ":hyper_denominator", ":depth_of_incursion"),
        (try_begin),
          (neq,":hyper_denominator",-1),
          (val_add, ":hyper_denominator", 1),
        (try_end),
        
        (store_div, ":distance_score", 180, ":hyper_denominator"),
        (val_sub, ":distance_score", 80),
        (val_max, ":distance_score", 1),
        #MOTO chief use hyperbolic and fit to kingdom size end
        
        (store_mul, ":target_score", ":distance_score", ":power_ratio_score"),
        (val_mul, ":target_score", ":last_offensive_time_score"),
        (val_div, ":target_score", 100), #target score is between 0..10000 generally here MOTO 6..25000
        
        (call_script, "script_find_total_prosperity_score", ":potential_target"),
        (assign, ":total_prosperity_score", reg0),	#MOTO 10..75
        
        #(try_begin), #new for increase attackability of villages by ai
        #(is_between, ":potential_target", villages_begin, villages_end),
        (val_mul, ":total_prosperity_score", 3),
        (val_div, ":total_prosperity_score", 2),
        #(try_end),
        
        (val_mul, ":target_score", ":total_prosperity_score"),	#MOTO 60..1875000
        
        #MOTO make discreet so can amplify importance of homelands
        # (try_begin), #if both that center was our (original center) and (ex center) than bonus is 1.2x
        # (party_slot_eq, ":potential_target", slot_center_ex_faction, ":faction_no"),
        # (party_slot_eq, ":potential_target", slot_center_original_faction, ":faction_no"),
        # (val_mul, ":target_score", 12),
        # (val_div, ":target_score", 10),
        # (else_try), #if either that center was our (original center) or (ex center) than bonus is 1.1x
        # (this_or_next|party_slot_eq, ":potential_target", slot_center_ex_faction, ":faction_no"),
        # (party_slot_eq, ":potential_target", slot_center_original_faction, ":faction_no"),
        # (val_mul, ":target_score", 11),
        # (val_div, ":target_score", 10),
        # (try_end),
        (try_begin),
          (is_between, ":potential_target", walled_centers_begin, walled_centers_end),
          (try_begin),
            (party_slot_eq, ":potential_target", slot_center_original_faction, ":faction_no"),
            (val_mul, ":target_score", 14),
            (val_div, ":target_score", 10),
          (try_end),
          (try_begin),
            (party_slot_eq, ":potential_target", slot_center_ex_faction, ":faction_no"),
            (val_mul, ":target_score", 11),
            (val_div, ":target_score", 10),
          (try_end),
        (try_end),
        #MOTO end make discreet so can amplify importance of homelands
        
        (val_div, ":target_score", 1000), #target score is between 0..1000 generally here MOTO 0..2887
        
        (try_begin),
          (eq, ":potential_target_faction", "fac_player_supporters_faction"),
          (options_get_campaign_ai, ":reduce_campaign_ai"),
          
          (assign, ":number_of_walled_centers_player_have", 0),
          (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
            (store_faction_of_party, ":center_faction", ":center_no"),
            (eq, ":center_faction", "fac_player_supporters_faction"),
            (val_add, ":number_of_walled_centers_player_have", 1),
          (try_end),
          
          (try_begin),
            (eq, ":reduce_campaign_ai", 2), #easy
            
            (try_begin),
              (le, ":number_of_walled_centers_player_have", 2),
              (assign, ":hardness_score", 0),
            (else_try),
              (eq, ":number_of_walled_centers_player_have", 3),
              (assign, ":hardness_score", 20),
            (else_try),
              (eq, ":number_of_walled_centers_player_have", 4),
              (assign, ":hardness_score", 40),
            (else_try),
              (eq, ":number_of_walled_centers_player_have", 5),
              (eq, ":number_of_walled_centers_player_have", 6),
              (assign, ":hardness_score", 55),
            (else_try),
              (eq, ":number_of_walled_centers_player_have", 7),
              (eq, ":number_of_walled_centers_player_have", 8),
              (eq, ":number_of_walled_centers_player_have", 9),
              (assign, ":hardness_score", 70),
            (else_try),
              (assign, ":hardness_score", 85),
            (try_end),
          (else_try),
            (eq, ":reduce_campaign_ai", 1), #medium
            
            (try_begin),
              (le, ":number_of_walled_centers_player_have", 1),
              (assign, ":hardness_score", 25),
            (else_try),
              (eq, ":number_of_walled_centers_player_have", 2),
              (assign, ":hardness_score", 45),
            (else_try),
              (eq, ":number_of_walled_centers_player_have", 3),
              (assign, ":hardness_score", 60),
            (else_try),
              (eq, ":number_of_walled_centers_player_have", 4),
              (eq, ":number_of_walled_centers_player_have", 5),
              (assign, ":hardness_score", 75),
            (else_try),
              (eq, ":number_of_walled_centers_player_have", 6),
              (eq, ":number_of_walled_centers_player_have", 7),
              (eq, ":number_of_walled_centers_player_have", 8),
              (assign, ":hardness_score", 85),
            (else_try),
              (assign, ":hardness_score", 92),
            (try_end),
          (else_try), #hard
            (assign, ":hardness_score", 100),
          (try_end),
          
          (val_mul, ":target_score", ":hardness_score"),
          (val_div, ":target_score", 100),
        (try_end),
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (eq, ":faction_no", "fac_kingdom_2"),
          (ge, ":target_score", -1),
          (assign, reg0, ":target_score"),
          (assign, reg7, ":total_prosperity_score"),
          (assign, reg8, ":power_ratio_score"),
          (assign, reg9, ":distance_score"),
          (assign, reg10, ":last_offensive_time_score"),
          (str_store_party_name, s8, ":potential_target"),
          #(eq, ":all_vassals_included", 0),
          (assign, reg11, ":all_vassals_included"),
          (display_message, "@{!}DEBUG : attack of {s8} is {reg0}({reg11}), prs:{reg7}, pow:{reg8}, dis:{reg9}, lst:{reg10}"),
        (try_end),
      (try_end),
      
      (assign, reg0, ":target_score"),
      (assign, reg1, ":power_ratio"),
      (assign, reg2, ":distance_score"),
      (assign, reg3, ":total_prosperity_score"),
  ]),
  
  #script_find_center_to_defend
  # INPUT:
  # param1: faction_no
  # OUTPUT:
  # reg0 = center_to_defend (-1 if none is logical)
  # reg1 = maximum_defend_score
  # reg3 = enemy_strength_near_most_threatened_center
  (
    "find_center_to_defend",
    [
      (store_script_param, ":troop_no", 1),
      
      (store_faction_of_troop, ":faction_no", ":troop_no"),
      
      (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
      (faction_get_slot, ":current_ai_state", ":faction_no", slot_faction_ai_state),
      (assign, ":marshal_party", -1),
      (try_begin),
        (gt, ":faction_marshal", 0),
        (troop_get_slot, ":marshal_party", ":faction_marshal", slot_troop_leaded_party),
      (try_end),
      
      (assign, ":most_threatened_center", -1),
      (assign, ":maximum_threat_score", 0),
      (assign, ":num_walled_centers", 0),	#MOTO count
      (assign, ":num_villages", 0),	#MOTO count
      (assign, ":num_idle_villages", 0),	#MOTO count
      (try_for_range, ":cur_center", centers_begin, centers_end),
        (store_faction_of_party, ":center_faction", ":cur_center"),
        (eq, ":center_faction", ":faction_no"),
        
        (party_get_slot, ":exact_enemy_strength", ":cur_center", slot_center_sortie_enemy_strength),
        #Distort this to account for questionable intelligence
        #(call_script, "script_reduce_exact_number_to_estimate", ":exact_enemy_strength"),
        #(assign, ":enemy_strength_nearby", reg0),
        (assign, ":enemy_strength_nearby", ":exact_enemy_strength"),
        
        (assign, ":threat_importance", 0),
        (try_begin),
          (is_between, ":cur_center", walled_centers_begin, walled_centers_end),
          (val_add, ":num_walled_centers", 1),	#MOTO count
          (party_slot_ge, ":cur_center", slot_center_is_besieged_by, 0),
          
          (call_script, "script_find_total_prosperity_score", ":cur_center"),
          (assign, ":total_prosperity_score", reg0),
          
          (party_get_slot, ":cur_center_strength", ":cur_center", slot_party_cached_strength),
          (val_mul, ":cur_center_strength", 4),
          (val_div, ":cur_center_strength", 3), #give 33% bonus to insiders because they are inside a castle
          
          #I removed below line and assigned ":cur_center_nearby_strength" to 0, because if not when defender army comes to help
          #threat become less because of high defence power but not yet enemy cleared.
          #(party_get_slot, ":cur_center_nearby_strength", ":cur_center", slot_party_nearby_friend_strength),
          (assign, ":cur_center_nearby_strength", 0),
          
          (val_add, ":cur_center_strength", ":cur_center_nearby_strength"), #add nearby friends and find ":cur_center_strength"
          
          (store_mul, ":power_ratio", ":enemy_strength_nearby", 100),
          (val_add, ":cur_center_strength", 1),
          (val_max, ":cur_center_strength", 1),
          (val_div, ":power_ratio", ":cur_center_strength"),
          
          (assign, ":player_is_attacking", 0),
          (party_get_slot, ":besieger_party", ":cur_center", slot_center_is_besieged_by),
          (try_begin),
            (party_is_active, ":besieger_party"),
            (try_begin),
              (eq, ":besieger_party", "p_main_party"),
              (assign, ":player_is_attacking", 1),
              #(display_message, "@{!}DEBUG : player is attacking a center (1)"),
            (else_try),
              (store_faction_of_party, ":besieger_faction", ":besieger_party"),
              (eq, ":besieger_faction", "fac_player_faction"),
              (assign, ":player_is_attacking", 1),
              #(display_message, "@{!}DEBUG : player is attacking a center (2)"),
            (else_try),
              (party_get_attached_to, ":player_is_attached_to", "p_main_party"),
              (ge, ":player_is_attached_to", 0),
              (eq, ":player_is_attached_to", ":besieger_party"),
              (assign, ":player_is_attacking", 1),
              #(display_message, "@{!}DEBUG : player is attacking a center (3)"),
            (try_end),
          (try_end),
          
          (try_begin),
            (eq, ":player_is_attacking", 0),
            
            (try_begin),
              (lt, ":power_ratio", 40), #changes between 1..1
              (assign, ":threat_importance", 1),
            (else_try),
              (lt, ":power_ratio", 80), #changes between 1..7
              (store_sub, ":threat_importance", ":power_ratio", 40),
              (val_div, ":threat_importance", 5),
              (val_add, ":threat_importance", 1), #1
            (else_try),
              (lt, ":power_ratio", 120), #changes between 7..17
              (store_sub, ":threat_importance", ":power_ratio", 80),
              (val_div, ":threat_importance", 4),
              (val_add, ":threat_importance", 7), #1 + 6
            (else_try),
              (lt, ":power_ratio", 200),
              (store_sub, ":threat_importance", ":power_ratio", 120),
              (val_div, ":threat_importance", 10),
              (val_add, ":threat_importance", 17), #1 + 6 + 10
            (else_try),
              (assign, ":threat_importance", 25),
            (try_end),
          (else_try),
            (try_begin),
              (lt, ":power_ratio", 200), #changes between 5..25
              (store_div, ":threat_importance", ":power_ratio", 10),    #MOTO correction (thanks MOTO:) (mexxico))
              (val_add, ":threat_importance", 6 ),
            (else_try),
              (assign, ":threat_importance", 25),
            (try_end),
          (try_end),
        (else_try),
          (is_between, ":cur_center", villages_begin, villages_end),
          #MOTO village defense rewrite
          # (party_slot_eq, ":cur_center", slot_village_state, svs_being_raided),
          (neg|party_slot_eq, ":cur_center", slot_village_state, svs_looted),
          
          (val_add, ":num_villages", 1),
          (try_begin),
            (neg|party_slot_eq, ":cur_center", slot_village_state, svs_normal),
            (val_add, ":num_idle_villages", 1),
          (try_end),
          
          (gt, ":enemy_strength_nearby", 0),
          
          (call_script, "script_find_total_prosperity_score", ":cur_center"),
          # (assign, ":power_ratio", 100), #useless
          (assign, ":total_prosperity_score", reg0),
          (assign, ":threat_importance", 10), #if faction village is looted they lose money for shorter time period. So importance is something low (6-8).
        (try_end),
        
        (gt, ":threat_importance", 0),
        
        # (try_begin),
        # (is_between, ":cur_center", walled_centers_begin, walled_centers_end),
        # (assign, ":enemy_strength_nearby_score", 120),
        (store_add, ":enemy_strength_nearby_score", ":enemy_strength_nearby", 20000),
        (val_div, ":enemy_strength_nearby_score", 200),
        
        (try_begin),
          (ge, ":marshal_party", 0),
          (party_is_active, ":marshal_party"),
          (store_distance_to_party_from_party, ":marshal_dist_to_cur_center", ":marshal_party", ":cur_center"),
        (else_try),
          (assign, ":marshal_dist_to_cur_center", 100),
        (try_end),
        
        (try_begin),
          #if currently our target is ride to break a siege then
          #divide marshal_distance for other center's to "2" instead of "4" and add some small more distance to avoid easily
          #changing mind during siege because of small score differences.
          
          #(faction_get_slot, ":current_ai_state", ":faction_no", slot_faction_ai_state),
          (eq, ":current_ai_state", sfai_attacking_enemies_around_center),
          (faction_get_slot, ":current_ai_object", ":faction_no", slot_faction_ai_object),
          (is_between, ":current_ai_object", walled_centers_begin, walled_centers_end),
          (neq, ":current_ai_object", ":cur_center"),
          (val_mul, ":marshal_dist_to_cur_center", 2),
          (val_add, ":marshal_dist_to_cur_center", 20),
        (try_end),
        
        (val_mul, ":marshal_dist_to_cur_center", 2), #standard multipication (1.5x) to adjust distance scoring same with formula at find_center_to_attack
        #(val_div, ":marshal_dist_to_cur_center", 2),
        
        (try_begin),
          (lt, ":marshal_dist_to_cur_center", 10), #very close (100p)
          (assign, ":distance_score", 100),
        (else_try),
          (lt, ":marshal_dist_to_cur_center", 110), #close (50p-100p)	MOTO change 160 to 110
          (store_sub, ":distance_score", ":marshal_dist_to_cur_center", 10),
          (val_div, ":distance_score", 2),	#MOTO change 3 to 2
          (store_sub, ":distance_score", 100, ":distance_score"),
        (else_try),
          (lt, ":marshal_dist_to_cur_center", 270), #far (10p-50p) MOTO change 360 to 270
          (store_sub, ":distance_score", ":marshal_dist_to_cur_center", 110),	#MOTO change 250 to 110
          (val_div, ":distance_score", 4),	#MOTO change 5 to 4
          (store_sub, ":distance_score", 50, ":distance_score"),
        (else_try),
          (assign, ":distance_score", 10), #very far
        (try_end),
        # (else_try),
        # (store_add, ":enemy_strength_nearby_score", ":enemy_strength_nearby", 20000),
        # (val_div, ":enemy_strength_nearby_score", 200),
        # (assign, ":distance_score", 70), #not related to marshal's position, because everybody is going same place (no gathering in most village raids)
        # (try_end),
        #MOTO end village defense rewrite
        
        (store_mul, ":threat_score", ":enemy_strength_nearby_score", ":total_prosperity_score"),
        (val_mul, ":threat_score", ":threat_importance"),
        (val_mul, ":threat_score", ":distance_score"),
        (val_div, ":threat_score", 10000),	#MOTO 1..2808
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (gt, ":threat_score", 0),
          (eq, ":faction_no", "fac_kingdom_6"),
          (assign, reg0, ":threat_score"),
          (str_store_party_name, s32, ":cur_center"),
          (assign, reg1,  ":total_prosperity_score"),
          (assign, reg2, ":enemy_strength_nearby_score"),
          (assign, reg3, ":threat_importance"),
          (assign, reg4, ":distance_score"),
          #(display_message, "@{!}DEBUG : defend of {s32} is {reg0}, prosperity:{reg1}, enemy nearby:{reg2}, threat importance:{reg3}, distance: {reg4}"),
        (try_end),
        
        (gt, ":threat_score", ":maximum_threat_score"),
        
        (assign, ":most_threatened_center", ":cur_center"),
        (assign, ":maximum_threat_score", ":threat_score"),
        (assign, ":enemy_strength_near_most_threatened_center", ":enemy_strength_nearby"),
      (try_end),
      
      (val_mul, ":maximum_threat_score", 3),
      (val_div, ":maximum_threat_score", 2),
      
      #MOTO importance decreases with more centers
      (val_mul, ":maximum_threat_score", 2),
      (try_begin),	#last walled center is all-important
        (is_between, ":most_threatened_center", walled_centers_begin, walled_centers_end),
        (val_div, ":maximum_threat_score", ":num_walled_centers"),
      (else_try),	#villages start becoming important after half of them have been trashed
        (gt, ":num_villages", 0),
        (val_mul, ":maximum_threat_score", ":num_idle_villages"),
        (val_div, ":maximum_threat_score", ":num_villages"),
      (try_end),
      #MOTO end importance decreases with more centers
      
      (assign, reg0, ":most_threatened_center"),
      (assign, reg1, ":maximum_threat_score"),
      (assign, reg2, ":enemy_strength_near_most_threatened_center"),
  ]),
  
  
  #script_npc_decision_checklist_peace_or_war
  (
    "npc_decision_checklist_peace_or_war",
    #this script is used to add a bit more color to diplomacy, particularly with regards to the player
    
    [
      (store_script_param, ":actor_faction", 1),
      (store_script_param, ":target_faction", 2),
      (store_script_param, ":envoy", 3),
      
      (assign, ":actor_strength", 1),	#MOTO avoid div by 0
      (assign, ":target_strength", 1),	#MOTO avoid div by 0
      (assign, ":actor_centers_held_by_target", 0),
      
      (assign, ":two_factions_share_border", 0),
      (assign, ":third_party_war", 0),
      (assign, ":num_third_party_wars", 0),
      (assign, ":num_target_third_party_wars", 0),	#MOTO chief assess weakness
      
      (assign, ":active_mutual_enemy", 0), #an active enemy with which the target is at war
      (assign, "$g_concession_demanded", 0),
      
      (store_relation, ":current_faction_relation", ":actor_faction", ":target_faction"),
      
      (try_begin),
        (eq, ":target_faction", "fac_player_supporters_faction"),
        (store_div, ":p_honor", "$player_honor", 2),#JuJu
        (val_clamp, ":p_honor", -50, 51),
        (assign, ":modified_honor_and_relation", ":p_honor"), #this can be affected by the emissary's skill
        (val_add, ":target_strength", 2), #for player party
      (else_try),
        (assign, ":modified_honor_and_relation", 0), #this can be affected by the emissary's skill
      (try_end),
      
      (faction_get_slot, ":actor_leader", ":actor_faction", slot_faction_leader),
      (faction_get_slot, ":target_leader", ":target_faction", slot_faction_leader),
      
      (call_script, "script_troop_get_relation_with_troop", ":actor_leader", ":target_leader"),
      
      (assign, ":relation_bonus", reg0),
      (val_min, ":relation_bonus", 10),
      (val_add, ":modified_honor_and_relation", ":relation_bonus"),
      # Torture adj
      (try_begin),
        (eq, ":target_faction", "fac_player_supporters_faction"),
        (troop_get_slot, ":tor", ":actor_leader", slot_troop_tortured),
        (gt, ":tor", 0),
        (val_div, ":tor", 2),
        (val_max, ":tor", 1),
        (val_sub, ":modified_honor_and_relation", ":tor"),
      (try_end),
      # End torture adj
      
      # MOTO clarify actors chief
      # (str_store_troop_name, s15, ":actor_leader"),
      # (str_store_troop_name, s16, ":target_leader"),
      (str_store_faction_name, s34, ":actor_faction"),
      (str_store_troop_name, s35, ":actor_leader"),
      (str_store_string, s15, "@{s35} of the {s34}"),
      
      (str_store_faction_name, s36, ":target_faction"),
      (str_store_troop_name, s37, ":target_leader"),
      (str_store_string, s16, "@{s37} of the {s36}"),
      # MOTO clarify actors end
      
      (assign, ":war_damage_suffered", 0),
      (assign, ":war_damage_inflicted", 0),
      
      (call_script, "script_diplomacy_faction_get_diplomatic_status_with_faction", ":actor_faction", ":target_faction"),
      (assign, ":war_peace_truce_status", reg0),
      (str_clear, s12),
      (try_begin),
        (eq, ":war_peace_truce_status", -2),
        (str_store_string, s12, "str_s15_is_at_war_with_s16_"),
        
        (store_add, ":war_damage_inflicted_slot", ":target_faction", slot_faction_war_damage_inflicted_on_factions_begin),
        (val_sub, ":war_damage_inflicted_slot", kingdoms_begin),
        (faction_get_slot, ":war_damage_inflicted", ":actor_faction", ":war_damage_inflicted_slot"),
        
        (store_add, ":war_damage_suffered_slot", ":actor_faction", slot_faction_war_damage_inflicted_on_factions_begin),
        (val_sub, ":war_damage_suffered_slot", kingdoms_begin),
        (faction_get_slot, ":war_damage_suffered", ":target_faction", ":war_damage_suffered_slot"),
        
        
      (else_try),
        #truce in effect
        (eq, ":war_peace_truce_status", 1),
        (str_store_string, s12, "str_in_the_short_term_s15_has_a_truce_with_s16_as_a_matter_of_general_policy_"),
      (else_try),
        #provocation noted
        (eq, ":war_peace_truce_status", -1),
        (str_store_string, s12, "str_in_the_short_term_s15_was_recently_provoked_by_s16_and_is_under_pressure_to_declare_war_as_a_matter_of_general_policy_"),
      (try_end),
      
      #MOTO chief cultural antipathy
      (faction_get_slot, ":kingdom_culture", ":actor_faction", slot_faction_culture),
      (faction_get_slot, ":recruit_type", ":kingdom_culture", slot_faction_tier_1_troop),
      (faction_get_slot, ":kingdom_culture", ":target_faction", slot_faction_culture),
      (faction_get_slot, ":recruit_type_2", ":kingdom_culture", slot_faction_tier_1_troop),
      (try_begin),
        (eq, ":recruit_type", ":recruit_type_2"),
        (assign, ":cultural_antipathy", 0),
      (else_try),
        (assign, ":cultural_antipathy", 1),
      (try_end),
      
      (store_relation, reg0, ":actor_faction", fac_pagans),
      (try_begin),
        (lt, reg0, 0),
        (store_relation, reg0, ":target_faction", fac_christians),
        (try_begin),
          (lt, reg0, 0),
          (val_max, ":cultural_antipathy", 1),
        (try_end),
      (try_end),
      
      (store_relation, reg0, ":actor_faction", fac_christians),
      (try_begin),
        (lt, reg0, 0),
        (store_relation, reg0, ":target_faction", fac_pagans),
        (try_begin),
          (lt, reg0, 0),
          (val_max, ":cultural_antipathy", 1),
        (try_end),
      (try_end),
      
      (assign, ":shift", ":cultural_antipathy"),
      
      (try_begin),
        (eq, ":war_peace_truce_status", -1),
        (val_max, ":shift", 1),	#attitude shaped by cultural antipathy or provocation shifts many results below
      (try_end),
      #MOTO chief cultural antipathy end
      
      #clear for dialog with lords
      (try_begin),
        (is_between, "$g_talk_troop", active_npcs_begin, active_npcs_end),
        (str_clear, s12),
      (try_end),
      
      (try_begin),
        (gt, ":envoy", -1),
        (store_skill_level, ":persuasion_x_2", "skl_persuasion", ":envoy"),
        (val_mul, ":persuasion_x_2", 2),
        (val_add, ":modified_honor_and_relation", ":persuasion_x_2"),
        #Juju
        (store_attribute_level, ":charisma", ":envoy", ca_charisma),
        (val_sub, ":charisma", 12),
        (try_begin),
          (gt, ":charisma", 0),
          (val_add, ":modified_honor_and_relation", ":charisma"),
        (try_end),
        #End Juju
        (try_begin),
          (eq, "$cheat_mode", 1),
          (assign, reg4, ":modified_honor_and_relation"),
          (str_store_troop_name, s4, ":envoy"),
          (display_message, "str_envoymodified_diplomacy_score_honor_plus_relation_plus_envoy_persuasion_=_reg4"),
        (try_end),
        
      (try_end),
      
      
      (try_for_range, ":kingdom_to_reset", kingdoms_begin, kingdoms_end),
        (faction_set_slot, ":kingdom_to_reset", slot_faction_temp_slot, 0),
      (try_end),
      
      (try_for_parties, ":party_no"),
        (assign, ":party_value", 0),
        (try_begin),
          (is_between, ":party_no", towns_begin, towns_end),
          (assign, ":party_value", 3),
        (else_try),
          (is_between, ":party_no", castles_begin, castles_end),
          (assign, ":party_value", 2),
        (else_try),
          (is_between, ":party_no", villages_begin, villages_end),
          (assign, ":party_value", 1),
        (else_try),
          (party_get_template_id, ":template", ":party_no"),
          (eq, ":template", "pt_kingdom_hero_party"),
          (assign, ":party_value", 2),
        (try_end),
        
        
        (store_faction_of_party, ":party_current_faction", ":party_no"),
        (party_get_slot, ":party_original_faction", ":party_no", slot_center_original_faction),
        (party_get_slot, ":party_ex_faction", ":party_no", slot_center_ex_faction),
        
        
        #total strengths
        (try_begin),
          (is_between, ":party_current_faction", kingdoms_begin, kingdoms_end),
          (faction_get_slot, ":faction_strength", ":party_current_faction", slot_faction_temp_slot),
          (val_add, ":faction_strength", ":party_value"),
          (faction_set_slot, ":party_current_faction", slot_faction_temp_slot, ":faction_strength"),
        (try_end),
        
        
        (try_begin),
          (eq, ":party_current_faction", ":target_faction"),
          (val_add, ":target_strength", ":party_value"),
          
          (try_begin),
            (this_or_next|eq, ":party_original_faction", ":actor_faction"),
            (eq, ":party_ex_faction", ":actor_faction"),
            (val_add, ":actor_centers_held_by_target", 1),
            (try_begin),
              (is_between, ":party_no", walled_centers_begin, walled_centers_end),
              (assign, "$g_concession_demanded", ":party_no"),
              (str_store_party_name, s18, "$g_concession_demanded"),
            (try_end),
          (try_end),
          
          # Could include two factions share border, but war is unlikely to break out in the first place unless there is a common border
          
          #			(try_begin),
          #				(is_between, ":party_no", walled_centers_begin, walled_centers_end),
          #				(try_for_range, ":other_center", walled_centers_begin, walled_centers_end),
          #					(assign, ":two_factions_share_border", 0),
          #					(store_faction_of_party, ":other_faction", ":other_center"),
          #					(eq, ":other_faction", ":actor_faction"),
          #					(store_distance_to_party_from_party, ":distance", ":party_no", ":other_center"),
          #					(le, ":distance", 15),
          #					(assign, ":two_factions_share_border", 1),
          #				(try_end),
          #			(try_end),
          
          #MOTO chief used anyway, so may as well include, but use slot previously set
          (store_add, ":slot", slot_faction_neighbors_begin, ":target_faction"),
          (val_sub, ":slot", kingdoms_begin),
          (faction_get_slot, ":two_factions_share_border", ":actor_faction", ":slot"),
          #MOTO chief used anyway, so may as well include, but use slot previously set end
          
        (else_try),
          (eq, ":party_current_faction", ":actor_faction"),
          (val_add, ":actor_strength", ":party_value"),
        (try_end),
      (try_end),
      
      #Total Calradia strength = 110 x 1 (villages,), 48? x 2 castles, 22 x 3 towns, 88 x 2 lord parties = 272 + 176 = 448
      (assign, ":strongest_kingdom", -1),
      (assign, ":score_to_beat", 60), #Maybe raise once it works
      (try_for_range, ":strongest_kingdom_candidate", kingdoms_begin, kingdoms_end),
        (faction_get_slot, ":candidate_strength", ":strongest_kingdom_candidate", slot_faction_temp_slot),
        (gt, ":candidate_strength", ":score_to_beat"),
        (assign, ":strongest_kingdom", ":strongest_kingdom_candidate"),
        (assign, ":score_to_beat", ":candidate_strength"),
      (try_end),
      
      
      (try_begin),
        (eq, "$cheat_mode", 2),
        (gt, ":strongest_kingdom", 1),
        (str_store_faction_name, s4, ":strongest_kingdom"),
        (assign, reg3, ":score_to_beat"),
        (display_message, "@{!}DEBUG - {s4} strongest kingdom with {reg3} strength"),
      (try_end),
      
      #MOTO avoid div-by-0 through init (above)
      # (assign, ":strength_ratio", 1),
      # (try_begin),
      # (gt, ":actor_strength", 0),
      (store_mul, ":strength_ratio", ":target_strength", 100),
      (val_div, ":strength_ratio", ":actor_strength"),
      # (try_end),
      #MOTO avoid div-by-0 through init (above) end
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (eq, ":target_faction", "fac_player_supporters_faction"),
        (assign, reg3, ":strength_ratio"),
        (str_store_faction_name, s4, ":actor_faction"),
        (display_message, "@{!}DEBUG: {s4} strength ratio {reg3}% versus player faction"),
      (try_end),
      
      (try_for_range, ":possible_mutual_enemy", kingdoms_begin, kingdoms_end),
        (neq, ":possible_mutual_enemy", ":target_faction"),
        (neq, ":possible_mutual_enemy", ":actor_faction"),
        (faction_slot_eq, ":possible_mutual_enemy", slot_faction_state, sfs_active),
        
        (store_relation, ":relation", ":possible_mutual_enemy", ":actor_faction"),
        #MOTO assess weakness chief
        (try_begin),
          (lt, ":relation", 0),
          (assign, ":third_party_war", ":possible_mutual_enemy"),
          (val_add, ":num_third_party_wars", 1),
        (try_end),
        
        (store_relation, ":relation", ":possible_mutual_enemy", ":target_faction"),
        (lt, ":relation", 0),
        (val_add, ":num_target_third_party_wars", 1),
        (eq, ":third_party_war", ":possible_mutual_enemy"),
        (assign, ":active_mutual_enemy", ":possible_mutual_enemy"),
        #MOTO assess weakness end
      (try_end),
      
      (store_current_hours, ":cur_hours"),
      (faction_get_slot, ":faction_ai_last_decisive_event", ":actor_faction", slot_faction_ai_last_decisive_event),
      (store_sub, ":hours_since_last_decisive_event", ":cur_hours", ":faction_ai_last_decisive_event"),
      
      (try_begin),
        (gt, "$supported_pretender", 0),
        (this_or_next|eq, "$supported_pretender", ":actor_leader"),
        (eq, "$supported_pretender", ":target_leader"),
        (this_or_next|eq, ":actor_faction", "$supported_pretender_old_faction"),
        (eq, ":target_faction", "$supported_pretender_old_faction"),
        
        (assign, ":result", -3),
        (troop_get_type, reg4, ":actor_leader"),
        (val_mod, reg4, 2),
        (assign, ":explainer_string", "str_s12s15_cannot_negotiate_with_s16_as_to_do_so_would_undermine_reg4herhis_own_claim_to_the_throne_this_civil_war_must_almost_certainly_end_with_the_defeat_of_one_side_or_another"),
      (else_try),
        (lt, ":modified_honor_and_relation", -20),
        (lt, ":strength_ratio", 125),
        (lt, ":war_damage_suffered", 400),
        (this_or_next|neq, ":war_peace_truce_status", -2),
        (lt, ":hours_since_last_decisive_event", 720),
        
        (eq, ":num_third_party_wars", 0),
        
        (assign, ":result", -3),
        (troop_get_type, reg4, ":actor_leader"),
        (val_mod, reg4, 2),
        (assign, ":explainer_string", "str_s12s15_considers_s16_to_be_dangerous_and_untrustworthy_and_shehe_wants_to_bring_s16_down"),
      (else_try),
        (gt, ":actor_centers_held_by_target", 0),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "@{!}Actor centers held by target noted"),
        (try_end),
        
        (lt, ":war_damage_suffered", 200),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "@{!}War damage under minimum"),
        (try_end),
        
        (lt, ":strength_ratio", 125),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "@{!}Strength ratio correct"),
        (try_end),
        
        (eq, ":num_third_party_wars", 0),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (display_message, "@{!}Third party wars"),
        (try_end),
        
        (assign, ":result", -2),
        (val_sub, ":result", ":shift"),	#MOTO add cultural antipathy, provocation factors
        (assign, ":explainer_string", "str_s12s15_is_anxious_to_reclaim_old_lands_such_as_s18_now_held_by_s16"),
      (else_try),
        (eq, ":war_peace_truce_status", -2),
        (lt, ":strength_ratio", 125),
        (le, ":num_third_party_wars", 1),
        (ge, ":war_damage_inflicted", 5),
        (this_or_next|neq, ":war_peace_truce_status", -2),
        (lt, ":hours_since_last_decisive_event", 720),
        
        (store_mul, ":war_damage_suffered_x_2", ":war_damage_suffered", 2),
        (gt, ":war_damage_inflicted", ":war_damage_suffered_x_2"),
        
        (assign, ":result", -2),
        (troop_get_type, reg4, ":actor_leader"),
        (val_mod, reg4, 2),
        (assign, ":explainer_string", "str_s12s15_feels_that_reg4shehe_is_winning_the_war_against_s16_and_sees_no_reason_not_to_continue"),
      (else_try),
        (le, ":war_peace_truce_status", -1),
        
        (this_or_next|eq, ":war_peace_truce_status", -1), #either a war is just beginning, or there is a provocation
        (le, ":war_damage_inflicted", 1),
        
        (lt, ":strength_ratio", 150),
        (eq, ":num_third_party_wars", 0),
        
        (faction_slot_ge, ":actor_faction", slot_faction_instability, 60),
        
        (assign, ":result", -1),
        (val_sub, ":result", ":cultural_antipathy"),	#MOTO add cultural antipathy factor
        (assign, ":explainer_string", "str_s12s15_faces_too_much_internal_discontent_to_feel_comfortable_ignoring_recent_provocations_by_s16s_subjects"),
      (else_try),
        (eq, ":war_peace_truce_status", -2),
        (lt, ":war_damage_inflicted", 100),
        (eq, ":num_third_party_wars", 1),
        
        (assign, ":result", -1),
        (troop_get_type, reg4, ":actor_leader"),
        (val_mod, reg4, 2),
        (assign, ":explainer_string", "str_s12even_though_reg4shehe_is_fighting_on_two_fronts_s15_is_inclined_to_continue_the_war_against_s16_for_a_little_while_longer_for_the_sake_of_honor"),
        
      (else_try),
        (eq, ":war_peace_truce_status", -2),
        (lt, ":war_damage_inflicted", 100),
        (eq, ":num_third_party_wars", 0),
        
        (assign, ":result", -1),
        (troop_get_type, reg4, ":actor_leader"),
        (val_mod, reg4, 2),
        (assign, ":explainer_string", "str_s12s15_feels_that_reg4shehe_must_pursue_the_war_against_s16_for_a_little_while_longer_for_the_sake_of_honor"),
      (else_try),
        (this_or_next|faction_slot_eq, ":actor_faction", slot_faction_ai_state, sfai_attacking_center),
        (this_or_next|faction_slot_eq, ":actor_faction", slot_faction_ai_state, sfai_raiding_village),
        (faction_slot_eq, ":actor_faction", slot_faction_ai_state, sfai_attacking_enemy_army),
        (faction_get_slot, ":offensive_object", ":actor_faction", slot_faction_ai_object),
        (party_is_active, ":offensive_object"),
        (store_faction_of_party, ":offensive_object_faction", ":offensive_object"),
        (eq, ":offensive_object_faction", ":target_faction"),
        (str_store_party_name, s17, ":offensive_object"),
        
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_s12s15_is_currently_on_the_offensive_against_s17_now_held_by_s16_and_reluctant_to_negotiate"),
        
        
      (else_try),
        #Attack strongest kingdom, if it is also at war
        (eq, ":strongest_kingdom", ":target_faction"),
        (eq, ":num_third_party_wars", 0),
        
        #Either not at war, or at war for two months
        (this_or_next|ge, ":war_peace_truce_status", -1),
        (lt, ":hours_since_last_decisive_event", 1440),
        
        #		(eq, ":two_factions_share_border", 0),
        
        (assign, ":at_least_one_other_faction_at_war_with_strongest", 0),
        (try_for_range, ":kingdom_to_check", kingdoms_begin, kingdoms_end),
          (neq, ":kingdom_to_check", ":actor_faction"),
          (neq, ":kingdom_to_check", ":target_faction"),
          (faction_slot_eq, ":kingdom_to_check", slot_faction_state, sfs_active),
          (store_relation, ":relation_of_factions", ":kingdom_to_check", ":target_faction"),
          (lt, ":relation_of_factions", 0),
          (assign, ":at_least_one_other_faction_at_war_with_strongest", 1),
        (try_end),
        (eq, ":at_least_one_other_faction_at_war_with_strongest", 1),
        
        
        (assign, ":result", -1),
        (val_sub, ":result", ":shift"),	#MOTO add cultural antipathy, provocation factors
        (assign, ":explainer_string", "str_s12s15_is_alarmed_by_the_growing_power_of_s16"),
        
        #bid to conquer all Calradia
      (else_try),
        (eq, ":num_third_party_wars", 0),
        (try_begin),
          (ge, "$cheat_mode", 1),
          (display_message, "@{!}DEBUG -- No third party wars for {s15}"),
        (try_end),
        (eq, ":actor_faction", ":strongest_kingdom"),
        #peace with no truce or provocation
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (display_message, "@{!}DEBUG -- {s15} is strongest kingdom"),
        (try_end),
        
        
        (faction_get_slot, ":actor_strength", ":actor_faction", slot_faction_temp_slot),
        (faction_get_slot, ":target_strength", ":target_faction", slot_faction_temp_slot),
        (store_sub, ":strength_difference", ":actor_strength", ":target_strength"),
        (ge, ":strength_difference", 30),
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (display_message, "@{!}DEBUG -- {s15} has 30 point advantage over {s16}"),
        (try_end),
        
        
        # (assign, ":nearby_center_found", 0),	MOTO simply use two_factions_share_border
        # (try_for_range, ":actor_faction_walled_center", walled_centers_begin, walled_centers_end),
        # (store_faction_of_party, ":walled_center_faction_1", ":actor_faction_walled_center"),
        # (eq, ":walled_center_faction_1", ":actor_faction"),
        # (try_for_range, ":target_faction_walled_center", walled_centers_begin, walled_centers_end),
        # (store_faction_of_party, ":walled_center_faction_2", ":target_faction_walled_center"),
        # (eq, ":walled_center_faction_2", ":target_faction"),
        # (store_distance_to_party_from_party, ":distance", ":target_faction_walled_center", ":actor_faction_walled_center"),
        # (lt, ":distance", 25),
        # (assign, ":nearby_center_found", 1),
        # (assign, ":last_center", ":actor_faction_center"),
        # (try_end),
        # (try_end),
        # (eq, ":nearby_center_found", 1),
        (eq, ":two_factions_share_border", 1),	#MOTO add border requirement
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (display_message, "@{!}DEBUG -- {s15} has proximity to {s16}"),
        (try_end),
        
        (assign, ":result", -1),
        (val_sub, ":result", ":shift"),	#MOTO add cultural antipathy, provocation factors
        (assign, ":explainer_string", "str_s12s15_declared_war_to_control_calradia"),
        
      (else_try),
        (lt, ":modified_honor_and_relation", -20),
        (lt, ":strength_ratio", 175),
        (assign, ":result", 0),
        (assign, ":explainer_string", "str_s12s15_distrusts_s16_and_fears_that_any_deals_struck_between_the_two_realms_will_not_be_kept"),
        
        
        #wishes to deal
      (else_try),
        (lt, ":current_faction_relation", 0),
        (ge, ":num_third_party_wars", 2),
        (assign, ":result", 3),
        (val_sub, ":result", ":shift"),	#MOTO add cultural antipathy, provocation factors
        
        (assign, ":explainer_string", "str_s12s15_is_at_war_on_too_many_fronts_and_eager_to_make_peace_with_s16"),
      (else_try),
        (gt, ":active_mutual_enemy", 0),
        (eq, ":actor_centers_held_by_target", 0),
        (this_or_next|ge, ":current_faction_relation", 0),
        (eq, ":two_factions_share_border", 0),
        # (eq, 1, 1),
        
        (assign, ":result", 3),
        (val_sub, ":result", ":shift"),	#MOTO add cultural antipathy, provocation factors
        (str_store_faction_name, s17, ":active_mutual_enemy"),
        (troop_get_type, reg4, ":actor_leader"),
        (val_mod, reg4, 2),
        (assign, ":explainer_string", "str_s12s15_seems_to_think_that_s16_and_reg4shehe_have_a_common_enemy_in_the_s17"),
        
      (else_try),
        (eq, ":war_peace_truce_status", -2),
        (ge, ":hours_since_last_decisive_event", 720),
        
        (troop_get_type, reg4, ":actor_leader"),
        (val_mod, reg4, 2),
        
        (assign, ":result", 2),
        (val_sub, ":result", ":shift"),	#MOTO add cultural antipathy, provocation factors
        (assign, ":explainer_string", "str_s12s15_feels_frustrated_by_reg4herhis_inability_to_strike_a_decisive_blow_against_s16"),
        
        
      (else_try),
        (lt, ":current_faction_relation", 0),
        (gt, ":war_damage_suffered", 100),
        
        (val_mul, ":war_damage_suffered_x_2", 2),
        (lt, ":war_damage_inflicted", ":war_damage_suffered_x_2"),
        
        (assign, ":result", 2),
        (val_sub, ":result", ":shift"),	#MOTO add cultural antipathy, provocation factors
        (assign, ":explainer_string", "str_s12s15_has_suffered_enough_in_the_war_with_s16_for_too_little_gain_and_is_ready_to_pursue_a_peace"),
        
      (else_try),
        (gt, ":third_party_war", 0),
        (ge, ":modified_honor_and_relation", 0),
        (lt, ":current_faction_relation", 0),
        (lt, ":shift", 1),	#MOTO no cultural antipathy, provocation
        
        (assign, ":result", 1),
        (str_store_faction_name, s17, ":third_party_war"),
        (assign, ":explainer_string", "str_s12s15_would_like_to_firm_up_a_truce_with_s16_to_respond_to_the_threat_from_the_s17"),
      (else_try),
        (gt, ":third_party_war", 0),
        (ge, ":modified_honor_and_relation", 0),
        (lt, ":shift", 1),	#MOTO no cultural antipathy, provocation
        
        (assign, ":result", 1),
        (str_store_faction_name, s17, ":third_party_war"),
        (assign, ":explainer_string", "str_s12s15_wishes_to_be_at_peace_with_s16_so_as_to_pursue_the_war_against_the_s17"),
      (else_try),
        (gt, ":strength_ratio", 175),
        (eq, ":two_factions_share_border", 1),
        
        (assign, ":result", 1),
        (assign, ":explainer_string", "str_s12s15_seems_to_be_intimidated_by_s16_and_would_like_to_avoid_hostilities"),
        
        #MOTO chief take advantage of a weak neighbor
      (else_try),
        (eq, ":two_factions_share_border", 1),
        (le, ":strength_ratio", 50),
        (eq, ":num_third_party_wars", 0),
        
        (try_begin),
          (le, ":strength_ratio", 25),
          (assign, ":result", -2),
        (else_try),
          (le, ":strength_ratio", 33),
          (assign, ":result", -1),
        (else_try),
          (assign, ":result", 0),
        (try_end),
        
        (val_sub, ":result", ":shift"),	#MOTO add cultural antipathy, provocation factors
        (troop_get_type, reg4, ":actor_leader"),
        (val_mod, reg4, 2),
        (assign, ":explainer_string", "str_s12s15_dominates_its_weaker_neighbor_s16"),
        #MOTO chief take advantage of a weak neighbor end
        
        #MOTO chief genocide
      (else_try),
        (eq, ":num_third_party_wars", 0),
        (eq, ":two_factions_share_border", 1),
        (lt, ":strength_ratio", 125),
        (eq, ":cultural_antipathy", 1),
        
        (try_begin),
          (eq, ":war_peace_truce_status", -1),	#provocation?
          (assign, ":result", -2),
        (else_try),
          (assign, ":result", -1),
        (try_end),
        (assign, ":explainer_string", "str_s12s15_acts_to_drive_the_people_of_s16_and_their_like_out_of_the_Isles"),
        #MOTO chief genocide end
        
      (else_try),
        (lt, ":current_faction_relation", 0),
        (lt, ":shift", 1),	#MOTO no cultural antipathy, provocation
        
        (assign, ":result", 1),
        (assign, ":explainer_string", "str_s12s15_has_no_particular_reason_to_continue_the_war_with_s16_and_would_probably_make_peace_if_given_the_opportunity"),
      (else_try),
        (lt, ":shift", 1),	#MOTO no cultural antipathy, provocation
        
        (assign, ":result", 1),
        (assign, ":explainer_string", "str_s12s15_seems_to_be_willing_to_improve_relations_with_s16"),
        
        #MOTO chief new default
      (else_try),
        (assign, ":result", 0),
        (assign, ":explainer_string", "str_s12s15_distrusts_s16_and_fears_that_any_deals_struck_between_the_two_realms_will_not_be_kept"),
      (try_end),
      #MOTO chief new default end
      
      (str_store_string, s14, ":explainer_string"),
      (assign, reg0, ":result"),
      (assign, reg1, ":explainer_string"),
      
  ]),
  
  ("npc_decision_checklist_male_guardian_assess_suitor", #parameters from dialog
    [
      (store_script_param, ":lord", 1),
      (store_script_param, ":suitor", 2),
      
      (troop_get_slot, ":lord_reputation", ":lord", slot_lord_reputation_type),
      (store_faction_of_troop, ":lord_faction", ":lord"),
      (troop_get_slot, ":lord_religion", ":lord", slot_troop_religion),
      (try_begin),
        (eq, ":suitor", "trp_player"),
        (assign, ":suitor_faction", "$players_kingdom"),
      (else_try),
        (store_faction_of_troop, ":suitor_faction", ":suitor"),
      (try_end),
      (store_relation, ":faction_relation_with_suitor", ":lord_faction", ":suitor_faction"),
      
      (call_script, "script_troop_get_relation_with_troop", ":lord", ":suitor"),
      (assign, ":lord_suitor_relation", reg0),
      
      (troop_get_slot, ":suitor_religion", ":suitor", slot_troop_religion),
      (troop_get_slot, ":suitor_renown", ":suitor", slot_troop_renown),
      
      (assign, ":competitor_found", -1),
      (try_begin),
        (eq, ":suitor", "trp_player"),
        (gt, "$marriage_candidate", 0),
        (try_for_range, ":competitor", lords_begin, lords_end),
          (store_faction_of_troop, ":competitor_faction", ":competitor"),
          (eq, ":competitor_faction", ":lord_faction"),
          (this_or_next|troop_slot_eq, ":competitor", slot_troop_love_interest_1, "$marriage_candidate"),
          (this_or_next|troop_slot_eq, ":competitor", slot_troop_love_interest_2, "$marriage_candidate"),
          (troop_slot_eq, ":competitor", slot_troop_love_interest_3, "$marriage_candidate"),
          (call_script, "script_troop_get_relation_with_troop", ":competitor", ":lord"),
          (gt, reg0, 5),
          (troop_slot_ge, ":competitor", slot_troop_renown, ":suitor_renown"),  #higher renown than player
          (assign, ":competitor_found", ":competitor"),
          (str_store_troop_name, s14, ":competitor"),
          (str_store_troop_name, s16, "$marriage_candidate"),
        (try_end),
      (try_end),
      
      #renown
      (try_begin),
        (neq, ":suitor_religion", ":lord_religion"),
        (assign, ":explainer_string", "str_no_suit_religion"),
        (assign, ":result", -3),
      (else_try),
        (le, ":suitor_renown", 200),
        (this_or_next|troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_quarrelsome),
        (this_or_next|troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_debauched),
        (troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_selfrighteous),
        (assign, ":explainer_string", "str_excuse_me_how_can_you_possibly_imagine_yourself_worthy_to_marry_into_our_family"),
        (assign, ":result", -3),
      (else_try),
        (le, ":suitor_renown", 150),
        (troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_goodnatured),
        
        (assign, ":explainer_string", "str_em_with_regard_to_her_ladyship_we_were_looking_specifically_for_a_groom_of_some_distinction_fight_hard_count_your_dinars_and_perhaps_some_day_in_the_future_we_may_speak_of_such_things_my_good_man"),
        (assign, ":result", -1),
      (else_try),
        (le, ":suitor_renown", 300),
        (neg|troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_goodnatured),
        (assign, ":explainer_string", "str_it_is_too_early_for_you_to_be_speaking_of_such_things_you_are_still_making_your_mark_in_the_world"),
        
        (assign, ":result", -1),
      (else_try),
        (le, ":suitor_renown", 200),
        
        (assign, ":explainer_string", "str_em_with_regard_to_her_ladyship_we_were_looking_specifically_for_a_groom_of_some_distinction"),
        (assign, ":result", -2),
        
      (else_try), #wrong faction
        (eq, ":suitor", "trp_player"),
        (neq, ":lord_faction", "$players_kingdom"),
        (str_store_faction_name, s4, ":lord_faction"),
        (this_or_next|eq, ":lord_reputation", lrep_quarrelsome),
        (this_or_next|eq, ":lord_reputation", lrep_martial),
        (this_or_next|eq, ":lord_reputation", lrep_selfrighteous),
        (eq, ":lord_reputation", lrep_debauched),
        (assign, ":explainer_string", "str_you_dont_serve_the_s4_so_id_say_no_one_day_we_may_be_at_war_and_i_prefer_not_to_have_to_kill_my_inlaws_if_at_all_possible"),
        (assign, ":result", -1),
      (else_try),
        (eq, ":suitor", "trp_player"),
        (neq, ":lord_faction", "$players_kingdom"),
        (neq, ":lord_reputation", lrep_goodnatured),
        (neq, ":lord_reputation", lrep_cunning),
        (assign, ":explainer_string", "str_as_you_are_not_a_vassal_of_the_s4_i_must_decline_your_request_the_twists_of_fate_may_mean_that_we_will_one_day_cross_swords_and_i_would_hope_not_to_make_a_widow_of_a_lady_whom_i_am_obligated_to_protect"),
        (assign, ":result", -1),
      (else_try),
        (eq, ":suitor", "trp_player"),
        (lt, ":faction_relation_with_suitor", 0),
        (assign, ":explainer_string", "str_as_you_are_not_a_vassal_of_the_s4_i_must_decline_your_request_the_twists_of_fate_may_mean_that_we_will_one_day_cross_swords_and_i_would_hope_not_to_make_a_widow_of_a_lady_whom_i_am_obligated_to_protect"),
        (assign, ":result", -1),
      (else_try),
        (eq, ":suitor", "trp_player"),
        (neq, "$player_has_homage", 1),
        (neg|faction_slot_eq, "fac_player_supporters_faction", slot_faction_state, sfs_active),
        (assign, ":explainer_string", "str_as_you_are_not_a_pledged_vassal_of_our_liege_with_the_right_to_hold_land_i_must_refuse_your_request_to_marry_into_our_family"),
        (assign, ":result", -1),
      (else_try),
        (gt, ":competitor_found", -1),
        (this_or_next|eq, ":lord_reputation", lrep_selfrighteous),
        (this_or_next|eq, ":lord_reputation", lrep_debauched),
        (this_or_next|eq, ":lord_reputation", lrep_martial),
        (eq, ":lord_reputation", lrep_quarrelsome),
        (assign, ":explainer_string",	"str_look_here_lad__the_young_s14_has_been_paying_court_to_s16_and_youll_have_to_admit__hes_a_finer_catch_for_her_than_you_so_lets_have_no_more_of_this_talk_shall_we"),
        (assign, ":result", -1),
      (else_try),
        (lt, ":lord_suitor_relation", -4),
        (assign, ":explainer_string", "str_i_do_not_care_for_you_sir_and_i_consider_it_my_duty_to_protect_the_ladies_of_my_household_from_undesirable_suitors"),
        (assign, ":result", -3),
      (else_try),
        (lt, ":lord_suitor_relation", 30),
        (assign, ":explainer_string",	"str_hmm_young_girls_may_easily_be_led_astray_so_out_of_a_sense_of_duty_to_the_ladies_of_my_household_i_think_i_would_like_to_get_to_know_you_a_bit_better_we_may_speak_of_this_at_a_later_date"),
        (assign, ":result", -1),
      (else_try),
        (store_random_in_range, ":rand", 0, 100),
        (try_begin),
          (eq, "$special1", 3),
          (val_sub, ":rand", 10),
          (val_max, ":rand", 0),
        (try_end),
        (try_begin),
          (lt, ":rand", 50),
          (assign, ":explainer_string", "str_you_may_indeed_make_a_fine_match_for_the_young_mistress"),
          (assign, ":result", 1),
        (else_try),
          (assign, ":explainer_string", "str_hmm_young_girls_may_easily_be_led_astray_so_out_of_a_sense_of_duty_to_the_ladies_of_my_household_i_think_i_would_like_to_get_to_know_you_a_bit_better_we_may_speak_of_this_at_a_later_date"),
          (assign, ":result", -1),
        (try_end),
      (try_end),
      
      (assign, reg0, ":result"),
      (assign, reg1, ":explainer_string"),
  ]),
  
  ("npc_decision_checklist_marry_female_pc", #
    [
      (store_script_param, ":npc", 1),
      
      
      (troop_get_slot, ":npc_reputation_type", ":npc", slot_lord_reputation_type),
      
      (call_script, "script_troop_get_romantic_chemistry_with_troop", ":npc", "trp_player"),
      (assign, ":romantic_chemistry", reg0),
      
      (call_script, "script_troop_get_relation_with_troop", ":npc", "trp_player"),
      (assign, ":relation_with_player", reg0),
      
      (assign, ":competitor", -1),
      (try_for_range, ":competitor_candidate", kingdom_ladies_begin, kingdom_ladies_end),
        (this_or_next|troop_slot_eq, ":npc", slot_troop_love_interest_1, ":competitor_candidate"),
        (this_or_next|troop_slot_eq, ":npc", slot_troop_love_interest_2, ":competitor_candidate"),
        (troop_slot_eq, ":npc", slot_troop_love_interest_3, ":competitor_candidate"),
        (call_script, "script_troop_get_relation_with_troop", ":npc", ":competitor"),
        (assign, ":competitor_relation", reg0),
        
        (gt, ":competitor_relation", ":relation_with_player"),
        (assign, ":competitor", ":competitor_candidate"),
      (try_end),
      
      (assign, ":player_possessions", 0),
      (try_for_range, ":center", centers_begin, centers_end),
        (troop_slot_eq, ":center", slot_town_lord, "trp_player"),
        (val_add, ":player_possessions", 1),
      (try_end),
      
      (assign, ":lord_agrees", 0),
      #reasons for refusal
      (try_begin),
        (troop_slot_ge, "trp_player", slot_troop_betrothed, active_npcs_begin),
        (neg|troop_slot_eq, "trp_player", slot_troop_betrothed, ":npc"),
        
        (str_store_string, s14, "str_my_lady_engaged_to_another"),
      (else_try),
        #bad relationship - minor
        (lt, ":relation_with_player", -3),
        (this_or_next|eq, ":npc_reputation_type", lrep_upstanding),
        (this_or_next|eq, ":npc_reputation_type", lrep_cunning),
        (eq, ":npc_reputation_type", lrep_goodnatured),
        
        (str_store_string, s14, "str_madame__given_our_relations_in_the_past_this_proposal_is_most_surprising_i_do_not_think_that_you_are_the_kind_of_woman_who_can_be_bent_to_a_hushands_will_and_i_would_prefer_not_to_have_our_married_life_be_a_source_of_constant_acrimony"),
        
      (else_try), #really bad relationship
        (lt, ":relation_with_player", -10),
        
        (this_or_next|eq, ":npc_reputation_type", lrep_quarrelsome),
        (this_or_next|eq, ":npc_reputation_type", lrep_debauched),
        (eq, ":npc_reputation_type", lrep_selfrighteous),
        
        (str_store_string, s14, "str_i_would_prefer_to_marry_a_proper_maiden_who_will_obey_her_husband_and_is_not_likely_to_split_his_head_with_a_sword"),
      (else_try),
        (lt, ":romantic_chemistry", 5),
        
        (str_store_string, s14, "str_my_lady_not_sufficient_chemistry"),
        
      (else_try), #would prefer someone more ladylike
        (this_or_next|eq, ":npc_reputation_type", lrep_upstanding),
        (eq, ":npc_reputation_type", lrep_martial),
        
        (str_store_string, s14, "str_my_lady_while_i_admire_your_valor_you_will_forgive_me_if_i_tell_you_that_a_woman_like_you_does_not_uphold_to_my_ideal_of_the_feminine_of_the_delicate_and_of_the_pure"),
      (else_try),
        (eq, ":npc_reputation_type", lrep_quarrelsome),
        (lt, ":romantic_chemistry", 15),
        
        (str_store_string, s14, "str_nah_i_want_a_woman_wholl_keep_quiet_and_do_what_shes_told_i_dont_think_thats_you"),
      (else_try), #no properties
        (this_or_next|eq, ":npc_reputation_type", lrep_selfrighteous),
        (eq, ":npc_reputation_type", lrep_debauched),
        
        (ge, ":romantic_chemistry", 10),
        (eq, ":player_possessions", 0),
        
        (str_store_string, s14, "str_my_lady_you_are_possessed_of_great_charms_but_no_properties_until_you_obtain_some_to_marry_you_would_be_an_act_of_ingratitude_towards_my_ancestors_and_my_lineage"),
        
      (else_try), #you're a nobody - I can do better
        (this_or_next|eq, ":npc_reputation_type", lrep_selfrighteous),
        (eq, ":npc_reputation_type", lrep_debauched),
        
        (eq, ":player_possessions", 0),
        
        (str_store_string, s14, "str_my_lady_you_are_a_woman_of_no_known_family_of_no_possessions__in_short_a_nobody_do_you_think_that_you_are_fit_to_marry_into_may_family"),
      (else_try), #just not that into you
        (lt, ":romantic_chemistry", 5),
        (lt, ":relation_with_player", 20),
        
        (neq, ":npc_reputation_type", lrep_debauched),
        (neq, ":npc_reputation_type", lrep_selfrighteous),
        
        (str_store_string, s14, "str_my_lady__forgive_me__the_quality_of_our_bond_is_not_of_the_sort_which_the_poets_tell_us_is_necessary_to_sustain_a_happy_marriage"),
        
      (else_try), #you're a liability, given your relation with the liege
        (eq, ":npc_reputation_type", lrep_cunning),
        (faction_get_slot, ":leader", slot_faction_leader, "$g_talk_troop_faction"),
        (str_store_troop_name, s4, ":leader"),
        (call_script, "script_troop_get_relation_with_troop", ":leader", "trp_player"),
        (lt, reg0, -10),
        
        (str_store_string, s14, "str_um_i_think_that_if_i_want_to_stay_on_s4s_good_side_id_best_not_marry_you"),
      (else_try),	#part of another faction
        (gt, "$players_kingdom", 0),
        (neq, "$players_kingdom", "$g_talk_troop_faction"),
        (faction_get_slot, ":leader", slot_faction_leader, "$g_talk_troop_faction"),
        (troop_get_type, reg4, ":leader"),
        (val_mod, reg4, 2),
        
        (str_store_string, s14, "str_you_serve_another_realm_i_dont_see_s4_granting_reg4herhis_blessing_to_our_union"),
      (else_try), #there's a competitor
        (gt, ":competitor", -1),
        (str_store_troop_name, s4, ":competitor"),
        
        (str_store_string, s14, "str_madame_my_heart_currently_belongs_to_s4"),
        
      (else_try),
        (lt, ":relation_with_player", 10),
        (assign, ":lord_agrees", 2),
        
        (str_store_string, s14, "str_my_lady_you_are_a_woman_of_great_spirit_and_bravery_possessed_of_beauty_grace_and_wit_i_shall_give_your_proposal_consideration"),
      (else_try),
        (assign, ":lord_agrees", 1),
        
        (str_store_string, s14, "str_my_lady_you_are_a_woman_of_great_spirit_and_bravery_possessed_of_beauty_grace_and_wit_i_would_be_most_honored_were_you_to_become_my_wife"),
      (try_end),
      
      
      (assign, reg0, ":lord_agrees"),
      
    ]
  ),
  
  
  #	(
  #	"npc_decision_checklist_king_chooses_lord_for_center",
  #	[
  #	(store_script_param, ":center", 1),
  
  #	(store_faction_of_party, ":faction", ":center"),
  #	(faction_get_slot, ":king", ":faction", slot_faction_leader),
  
  #	(assign, ":total_renown_in_faction"),
  #	(try_for_range, ":lord_iterator", active_npcs_including_player_begin, active_npcs_end),
  #		(assign, ":lord", ":lord_iterator"),
  #		(store_faction_of_troop, ":lord_faction", ":lord"),
  #		(try_begin),
  #			(eq, ":lord_iterator", "trp_kingdom_heroes_including_player_begin"),
  #			(assign, ":lord", "trp_player"),
  #			(assign, ":lord_faction", "$players_kingdom"),
  #		(try_end),
  #		(troop_get_slot, ":renown", ":lord", slot_troop_renown),
  #		(val_add, ":total_renown_in_faction", ":renown"),
  
  #		(troop_set_slot, ":lord", slot_troop_temp_slot, 0),
  #	(try_end),
  
  #	(assign, ":total_property_points_in_faction"),
  #	(try_for_range, ":village", villages_begin, villages_end),
  
  #	(try_end),
  
  
  
  #	(try_begin),
  
  #I needed it for myself
  
  #The one who captured it was suitably deserving
  
  #I had not sufficiently recognized Lord X for his service
  
  #	(try_end),
  
  
  #	]),
  
  
  
  ("courtship_poem_reactions", #parameters from dialog
    [
      (store_script_param, ":lady", 1),
      (store_script_param, ":poem", 2),
      
      (troop_get_slot, ":lady_reputation", ":lady", slot_lord_reputation_type),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (assign, reg4, ":poem"),
        (assign, reg5, ":lady_reputation"),
        (display_message, "str_poem_choice_reg4_lady_rep_reg5"),
      (try_end),
      
      (try_begin), #conventional ++, ambitious -, adventurous -
        (eq, ":poem", courtship_poem_tragic),
        (eq, ":lady_reputation", lrep_conventional),
        (str_store_string, s11, "str_ah__kais_and_layali__such_a_sad_tale_many_a_time_has_it_been_recounted_for_my_family_by_the_wandering_poets_who_come_to_our_home_and_it_has_never_failed_to_bring_tears_to_our_eyes"),
        (assign, ":result", 5),
      (else_try),
        (eq, ":poem", courtship_poem_tragic),
        (eq, ":lady_reputation", lrep_ambitious),
        (str_store_string, s11, "str_kais_and_layali_three_hundred_stanzas_of_pathetic_sniveling_if_you_ask_me_if_kais_wanted_to_escape_heartbreak_he_should_have_learned_to_live_within_his_station_and_not_yearn_for_what_he_cannot_have"),
        (assign, ":result", 0),
      (else_try),
        (eq, ":poem", courtship_poem_tragic),
        (eq, ":lady_reputation", lrep_otherworldly),
        (str_store_string, s11, "str_kais_and_layali_no_one_should_ever_have_written_such_a_sad_poem_if_it_was_the_destiny_of_kais_and_layali_to_be_together_than_their_love_should_have_conquered_all_obstacles"),
        (assign, ":result", 1),
      (else_try),
        (eq, ":poem", courtship_poem_tragic),
        #		moralizing and adventurous
        (str_store_string, s11, "str_ah_kais_and_layali_a_very_old_standby_but_moving_in_its_way"),
        (assign, ":result", 3),
        #Heroic
      (else_try), #adventurous ++, conventional -1, moralizing -1
        (eq, ":poem", courtship_poem_heroic),
        (eq, ":lady_reputation", lrep_adventurous),
        (str_store_string, s11, "str_the_saga_of_helgered_and_kara_such_happy_times_in_which_our_ancestors_lived_women_like_kara_could_venture_out_into_the_world_like_men_win_a_name_for_themselves_and_not_linger_in_their_husbands_shadow"),
        (assign, ":result", 5),
      (else_try), #adventurous ++, conventional -1, moralizing -1
        (eq, ":poem", courtship_poem_heroic),
        (eq, ":lady_reputation", lrep_ambitious),
        (str_store_string, s11, "str_ah_the_saga_of_helgered_and_kara_now_there_was_a_lady_who_knew_what_she_wanted_and_was_not_afraid_to_obtain_it"),
        (assign, ":result", 2),
      (else_try), #adventurous ++, conventional -1, moralizing -1
        (eq, ":poem", courtship_poem_heroic),
        (eq, ":lady_reputation", lrep_otherworldly),
        (str_store_string, s11, "str_the_saga_of_helgered_and_kara_a_terrible_tale__but_it_speaks_of_a_very_great_love_if_she_were_willing_to_make_war_on_her_own_family"),
        (assign, ":result", 2),
      (else_try), #adventurous ++, conventional -1, moralizing -1
        (eq, ":poem", courtship_poem_heroic),
        (eq, ":lady_reputation", lrep_moralist),
        (str_store_string, s11, "str_the_saga_of_helgered_and_kara_as_i_recall_kara_valued_her_own_base_passions_over_duty_to_her_family_that_she_made_war_on_her_own_father_i_have_no_time_for_a_poem_which_praises_such_a_woman"),
        (assign, ":result", 0),
      (else_try), #adventurous ++, conventional -1, moralizing -1
        (eq, ":poem", courtship_poem_heroic),
        (eq, ":lady_reputation", lrep_conventional),
        (str_store_string, s11, "str_the_saga_of_helgered_and_kara_how_could_a_woman_don_armor_and_carry_a_sword_how_could_a_man_love_so_ungentle_a_creature"),
        (assign, ":result", 0),
        #Comic
      (else_try), #ambitious ++, romantic -, moralizing 0
        (eq, ":poem", courtship_poem_comic),
        (eq, ":lady_reputation", lrep_otherworldly),
        (str_store_string, s11, "str_a_conversation_in_the_garden_i_cannot_understand_the_lady_in_that_poem_if_she_loves_the_man_why_does_she_tease_him_so"),
        (assign, ":result", 0),
      (else_try), #ambitious ++, romantic -, moralizing 0
        (eq, ":poem", courtship_poem_comic),
        (eq, ":lady_reputation", lrep_moralist),
        (str_store_string, s11, "str_a_conversation_in_the_garden_let_us_see__it_is_morally_unedifying_it_exalts_deception_it_ends_with_a_maiden_surrendering_to_her_base_passions_and_yet_i_cannot_help_but_find_it_charming_perhaps_because_it_tells_us_that_love_need_not_be_tragic_to_be_memorable"),
        (assign, ":result", 1),
      (else_try), #ambitious ++, romantic -, moralizing 0
        (eq, ":poem", courtship_poem_comic),
        (eq, ":lady_reputation", lrep_ambitious),
        (str_store_string, s11, "str_a_conversation_in_the_garden_now_that_is_a_tale_every_lady_should_know_by_heart_to_learn_the_subtleties_of_the_politics_she_must_practice"),
        (assign, ":result", 5),
      (else_try), #ambitious ++, romantic -, moralizing 0
        (eq, ":poem", courtship_poem_comic),
        #adventurous, conventional
        (str_store_string, s11, "str_a_conversation_in_the_garden_it_is_droll_i_suppose__although_there_is_nothing_there_that_truly_stirs_my_soul"),
        (assign, ":result", 3),
        
        #Allegoric
      (else_try), #moralizing ++, adventurous -, romantic -
        (eq, ":poem", courtship_poem_allegoric),
        (eq, ":lady_reputation", lrep_adventurous),
        (str_store_string, s11, "str_storming_the_fortress_of_love_ah_yes_the_lady_sits_within_doing_nothing_while_the_man_is_the_one_who_strives_and_achieves_i_have_enough_of_that_in_my_daily_life_why_listen_to_poems_about_it"),
        (assign, ":result", 0),
      (else_try), #moralizing ++, adventurous -, romantic -
        (eq, ":poem", courtship_poem_allegoric),
        (this_or_next|eq, ":lady_reputation", lrep_conventional),
        (eq, ":lady_reputation", lrep_moralist),
        (str_store_string, s11, "str_storming_the_fortress_of_love_ah_yes_an_uplifting_tribute_to_the_separate_virtues_of_man_and_woman"),
        (assign, ":result", 3),
      (else_try), #moralizing ++, adventurous -, romantic -
        (eq, ":poem", courtship_poem_allegoric),
        (eq, ":lady_reputation", lrep_otherworldly),
        (str_store_string, s11, "str_storming_the_fortress_of_love_ah_yes_but_although_it_is_a_fine_tale_of_virtues_it_speaks_nothing_of_passion"),
        (assign, ":result", 1),
      (else_try), #moralizing ++, adventurous -, romantic -
        (eq, ":poem", courtship_poem_allegoric),
        (eq, ":lady_reputation", lrep_ambitious),
        (str_store_string, s11, "str_storming_the_fortress_of_love_ah_a_sermon_dressed_up_as_a_love_poem_if_you_ask_me"),
        (assign, ":result", 1),
        
      (else_try), #romantic ++, moralizing 0, ambitious -
        (eq, ":poem", courtship_poem_mystic),
        (eq, ":lady_reputation", lrep_otherworldly),
        (str_store_string, s11, "str_a_hearts_desire_ah_such_a_beautiful_account_of_the_perfect_perfect_love_to_love_like_that_must_be_to_truly_know_rapture"),
        (assign, ":result", 4),
        
      (else_try), #romantic ++, moralizing 0, ambitious -
        (eq, ":poem", courtship_poem_mystic),
        (eq, ":lady_reputation", lrep_ambitious),
        (str_store_string, s11, "str_a_hearts_desire_silly_if_you_ask_me_if_the_poet_desires_a_lady_then_he_should_endeavor_to_win_her__and_not_dress_up_his_desire_with_a_pretense_of_piety"),
        (assign, ":result", 0),
        
      (else_try), #romantic ++, moralizing 0, ambitious -
        (eq, ":poem", courtship_poem_mystic),
        (eq, ":lady_reputation", lrep_moralist),
        (str_store_string, s11, "str_a_hearts_desire_hmm__it_is_an_interesting_exploration_of_earthly_and_divine_love_it_does_speak_of_the_spiritual_quest_which_brings_out_the_best_in_man_but_i_wonder_if_the_poet_has_not_confused_his_yearning_for_higher_things_with_his_baser_passions"),
        (assign, ":result", 2),
        
      (else_try), #romantic ++, moralizing 0, ambitious -
        (eq, ":poem", courtship_poem_mystic),
        (str_store_string, s11, "str_a_hearts_desire_oh_yes__it_is_very_worthy_and_philosophical_but_if_i_am_to_listen_to_a_bard_strum_a_lute_for_three_hours_i_personally_prefer_there_to_be_a_bit_of_a_story"),
        (assign, ":result", 1),
      (try_end),
      
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (assign, reg4, ":result"),
        (display_message, "str_result_reg4_string_s11"),
      (try_end),
      
      
      (assign, reg0, ":result"),
      
  ]),
  
  (
    "diplomacy_faction_get_diplomatic_status_with_faction",
    #result: -1 faction_1 has a casus belli against faction_2. 1, faction_1 has a truce with faction_2, -2, the two factions are at war 2 allianza
    [
      (store_script_param, ":actor_faction", 1),
      (store_script_param, ":target_faction", 2),
      
      (store_add, ":truce_slot", ":target_faction", slot_faction_truce_days_with_factions_begin),
      (store_add, ":provocation_slot", ":target_faction", slot_faction_provocation_days_with_factions_begin),
      (val_sub, ":truce_slot", kingdoms_begin),
      (val_sub, ":provocation_slot", kingdoms_begin),
      
      (assign, ":result", 0),
      (assign, ":duration", 0),
      (assign, ":result2", 0),
      (try_begin),
        (store_relation, ":relation", ":actor_faction", ":target_faction"),
        (lt, ":relation", 0),
        (assign, ":result", -2),
      (else_try),
        (faction_slot_ge, ":actor_faction", ":truce_slot", 1),
        (assign, ":result", 1),
        (faction_get_slot, ":duration", ":actor_faction", ":truce_slot"),
        (try_begin), #chief alliance
          (store_add, ":truce_slot2", ":actor_faction", slot_faction_truce_days_with_factions_begin),
          (val_sub, ":truce_slot2", kingdoms_begin),
          (faction_get_slot, ":truce_days", ":target_faction", ":truce_slot2"),
          (gt, ":truce_days", truce_time), #truce time = final alliance
          (assign, ":result2", 2), #alliance addon
        (try_end),
      (else_try),
        (faction_slot_ge, ":actor_faction", ":provocation_slot", 1),
        (assign, ":result", -1),
        (faction_get_slot, ":duration", ":actor_faction", ":provocation_slot"),
      (try_end),
      
      (assign, reg0, ":result"),
      (assign, reg1, ":duration"),
      (assign, reg2, ":result2"), #alliance
  ]),
  
  ("faction_follows_controversial_policy",
    [
      (store_script_param, ":faction_no", 1),
      (store_script_param, ":policy_type", 2),
      
      (faction_get_slot, ":faction_leader", ":faction_no", slot_faction_leader),
      
      (try_begin),
        (ge, "$cheat_mode", 1),
        (str_store_faction_name, s3, ":faction_no"),
        (display_message, "str_calculating_effect_for_policy_for_s3"),
        
        (val_add, "$number_of_controversial_policy_decisions", 1),
      (try_end),
      
      (try_begin),
        (eq, ":policy_type", logent_policy_ruler_attacks_without_provocation),
        (assign, ":hawk_relation_effect", 0),
        (assign, ":honorable_relation_effect", -2),
        (assign, ":honor_change", -1),
        
      (else_try),
        (eq, ":policy_type", logent_policy_ruler_ignores_provocation),
        (assign, ":hawk_relation_effect", -3),
        (assign, ":honorable_relation_effect", 0),
        (assign, ":honor_change", 0),
        
      (else_try),
        (eq, ":policy_type", logent_policy_ruler_declares_war_with_justification),
        (assign, ":hawk_relation_effect", 3),
        (assign, ":honorable_relation_effect", 1),
        (assign, ":honor_change", 0),
        
      (else_try),
        (eq, ":policy_type", logent_policy_ruler_breaks_truce),
        (assign, ":hawk_relation_effect", 0),
        (assign, ":honorable_relation_effect", -3),
        (assign, ":honor_change", -5),
        
      (else_try),
        (eq, ":policy_type", logent_policy_ruler_makes_peace_too_soon),
        (assign, ":hawk_relation_effect", -5),
        (assign, ":honorable_relation_effect", 0),
        (assign, ":honor_change", 0),
        
      (try_end),
      
      (try_begin),
        (eq, ":faction_leader", "trp_player"),
        (call_script, "script_change_player_honor", ":honor_change"),
      (try_end),
      
      (try_for_range, ":lord", active_npcs_begin, active_npcs_end),
        (troop_slot_eq, ":lord", slot_troop_occupation, slto_kingdom_hero),
        (store_faction_of_troop, ":lord_faction", ":lord"),
        (eq, ":lord_faction", ":faction_no"),
        (neq, ":lord", ":faction_leader"),
        
        (try_begin),
          (this_or_next|troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_martial),
          (this_or_next|troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_quarrelsome),
          (this_or_next|troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_selfrighteous),
          (this_or_next|troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_roguish),
          (troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_debauched),
          (call_script, "script_troop_change_relation_with_troop", ":faction_leader", ":lord", ":hawk_relation_effect"),
          (val_add, "$total_policy_dispute_changes", ":hawk_relation_effect"),
        (try_end),
        
        (try_begin),
          #			(this_or_next|troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_martial),
          (this_or_next|troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_goodnatured),
          #			(this_or_next|troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_selfrighteous),
          (this_or_next|troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_benefactor), #new for enfiefed commoners
          (this_or_next|troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_custodian), #new for enfiefed commoners
          (troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_upstanding),
          (call_script, "script_troop_change_relation_with_troop", ":faction_leader", ":lord", ":honorable_relation_effect"),
          (val_add, "$total_policy_dispute_changes", ":honorable_relation_effect"),
        (try_end),
        
      (try_end),
      
  ]),
  
  
  ("internal_politics_rate_feast_to_s9",
    [
      (store_script_param, ":householder", 1),
      (store_script_param, ":num_servings", 2),
      #	(store_script_param, ":faction", 3),
      (store_script_param, ":consume_items", 4),
      
      (val_max, ":num_servings", 1),
      
      (try_for_range, ":item", trade_goods_begin, trade_goods_end),
        (item_set_slot, ":item", slot_item_amount_available, 0), #had no "item"
      (try_end),
      
      (troop_get_inventory_capacity, ":capacity", ":householder"),
      (try_for_range, ":inventory_slot", 0, ":capacity"),
        (troop_get_inventory_slot, ":item", ":householder", ":inventory_slot"),
        (is_between, ":item", trade_goods_begin, trade_goods_end),
        (troop_inventory_slot_get_item_amount, ":slot_amount", ":householder", ":inventory_slot"),
        (item_get_slot, ":item_amount", ":item", slot_item_amount_available),
        (val_add, ":item_amount", ":slot_amount"),
        (item_set_slot, ":item", slot_item_amount_available, ":item_amount"),
      (try_end),
      #food
      (assign, ":food_amount", 0),
      (assign, ":food_variety", 0),
      
      (store_div, ":servings_div_by_12", ":num_servings", 12),
      (try_for_range, ":food_item", food_begin, food_end),
        (item_get_slot, ":food_in_slot", ":food_item", slot_item_amount_available),
        (val_add, ":food_amount", ":food_in_slot"),
        
        
        ##		(str_store_item_name, s4, ":food_item"),
        ##		(assign, reg3, ":food_in_slot"),
        ##		(assign, reg5, ":servings_div_by_12"),
        ##		(display_message, "str_reg3_units_of_s4_for_reg5_guests_and_retinue"),
        
        
        (ge, ":food_in_slot", ":servings_div_by_12"),
        (val_add, ":food_variety", 1),
      (try_end),
      
      (val_mul, ":food_amount", 100),
      (val_div, ":food_amount", ":num_servings"), #1 to 100 for each
      (val_min, ":food_amount", 100),
      
      (val_mul, ":food_variety", 100), #1 to 100 for each	#Was 85 before
      (val_div, ":food_variety", 10),
      (val_min, ":food_variety", 100),
      
      #drink
      (assign, ":drink_amount", 0),
      (assign, ":drink_variety", 0),
      #(store_div, ":servings_div_by_4", ":num_servings", 4),
      (try_for_range, ":drink_iterator", "itm_wine", "itm_smoked_fish"),
        (assign, ":drink_item", ":drink_iterator"),
        (item_get_slot, ":drink_in_slot", ":drink_item", slot_item_amount_available),
        
        (val_add, ":drink_amount", ":drink_in_slot"),
        
        #(ge, ":drink_in_slot", ":servings_div_by_4"),
        (ge, ":drink_in_slot", ":servings_div_by_12"),
        (val_add, ":drink_variety", 1),
      (try_end),
      
      #VC-2424
      (try_begin),
        (assign, ":drink_item", "itm_mead"),
        (item_get_slot, ":drink_in_slot", ":drink_item", slot_item_amount_available),
        (val_add, ":drink_amount", ":drink_in_slot"),
        #(ge, ":drink_in_slot", ":servings_div_by_4"),
        (ge, ":drink_in_slot", ":servings_div_by_12"),
        (val_add, ":drink_variety", 1),
      (try_end),
      #VC-2424 end
      
      (val_mul, ":drink_amount", 200), #amount needed is 50% of the number of guests
      (val_max, ":num_servings", 1),
      
      (val_div, ":drink_amount", ":num_servings"), #1 to 100 for each
      (val_min, ":drink_amount", 100),
      (val_mul, ":drink_variety", 34), #1 to 100 for each	#was 50 in native but we have 3 drinks in VC
      
      #(store_div, ":servings_div_by_4", ":num_servings", 4),
      
      #jewellery (was Spices in native)
      (item_get_slot, ":jewelry_amount", "itm_jewelry", slot_item_amount_available),	#eco note
      (store_mul, ":jewelry_percentage", ":jewelry_amount", 100),
      (val_max, ":servings_div_by_12", 1),
      (val_div, ":jewelry_amount", ":servings_div_by_12"),
      (val_min, ":jewelry_percentage", 100),
      (assign, reg3, ":jewelry_amount"),
      ##	(assign, reg5, ":servings_div_by_12"),
      ##	(assign, reg6, ":jewelry_percentage"),
      ##	(display_message, "str_reg3_units_of_spice_of_reg5_to_be_consumed"),
      
      #Fur (Was oil)
      (item_get_slot, ":furs_amount", "itm_vc_furs", slot_item_amount_available),	#eco note
      (store_mul, ":furs_percentage", ":furs_amount", 100),
      (val_max, ":servings_div_by_12", 1),
      (val_div, ":furs_amount", ":servings_div_by_12"),
      (val_min, ":furs_percentage", 100),
      (assign, reg3, ":furs_amount"),
      ##	(assign, reg5, ":servings_div_by_12"),
      ##	(assign, reg6, ":furs_percentage"),
      ##	(display_message, "str_reg3_units_of_oil_of_reg5_to_be_consumed"),
      
      (store_div, ":food_amount_string", ":food_amount", 20),
      (val_add, ":food_amount_string", "str_feast_description"),
      (str_store_string, s8, ":food_amount_string"),
      (str_store_string, s9, "str_of_food_which_must_come_before_everything_else_the_amount_is_s8"),
      
      (store_div, ":food_variety_string", ":food_variety", 20),
      (val_add, ":food_variety_string", "str_feast_description"),
      (str_store_string, s8, ":food_variety_string"),
      (str_store_string, s9, "str_s9_and_the_variety_is_s8_"),
      
      (store_div, ":drink_amount_string", ":drink_amount", 20),
      (val_add, ":drink_amount_string", "str_feast_description"),
      (str_store_string, s8, ":drink_amount_string"),
      (str_store_string, s9, "str_s9_of_drink_which_guests_will_expect_in_great_abundance_the_amount_is_s8"),
      
      (store_div, ":drink_variety_string", ":drink_variety", 20),
      (val_add, ":drink_variety_string", "str_feast_description"),
      (str_store_string, s8, ":drink_variety_string"),
      (str_store_string, s9, "str_s9_and_the_variety_is_s8_"),
      
      (store_div, ":jewelry_string", ":jewelry_percentage", 20),
      (val_add, ":jewelry_string", "str_feast_description"),
      (str_store_string, s8, ":jewelry_string"),
      (str_store_string, s9, "str_s9_of_jewellery_which_is_essential_to_demonstrate_x_the_amount_is_s8_"),
      
      (store_div, ":furs_string", ":furs_percentage", 20),
      (val_add, ":furs_string", "str_feast_description"),
      (str_store_string, s8, ":furs_string"),
      (str_store_string, s9, "str_s9_of_fur_which_we_shall_x_the_amount_is_s8"),
      
      (store_mul, ":food_amount_cap", ":food_amount", 8),
      (store_add, ":total", ":food_amount", ":food_variety"),
      (val_mul, ":total", 2), #x4
      (val_add, ":total", ":drink_variety"),
      (val_add, ":total", ":drink_amount"), #x6
      (val_add, ":total", ":jewelry_amount"), #x7
      (val_add, ":total", ":furs_amount"), #x8
      (val_min, ":total", ":food_amount_cap"),
      (val_div, ":total", 8),
      (val_clamp, ":total", 1, 101),
      (store_div, ":total_string", ":total", 20),
      (val_add, ":total_string", "str_feast_description"),
      (str_store_string, s8, ":total_string"),
      (str_store_string, s9, "str_s9_overall_our_table_will_be_considered_s8"),
      
      (assign, reg0, ":total"), #zero to 100
      
      
      
      (try_begin),
        (eq, ":consume_items", 1),
        
        (assign, ":num_of_servings_to_serve", ":num_servings"),
        (try_for_range, ":unused", 0, 1999),
          (gt, ":num_of_servings_to_serve", 0),
          
          (try_for_range, ":item", trade_goods_begin, trade_goods_end),
            (item_set_slot, ":item", slot_item_is_checked, 0),
          (try_end),
          
          (troop_get_inventory_capacity, ":inv_size", ":householder"),
          (try_for_range, ":i_slot", 0, ":inv_size"),
            (troop_get_inventory_slot, ":item", ":householder", ":i_slot"),
            (this_or_next|eq, ":item", "itm_jewelry"),		#eco note
            (this_or_next|eq, ":item", "itm_vc_furs"),
            (this_or_next|eq, ":item", "itm_wine"),
            (this_or_next|eq, ":item", "itm_ale"),
            (this_or_next|eq, ":item", "itm_mead"), #chief
            (is_between, ":item",  food_begin, food_end),
            (item_slot_eq, ":item", slot_item_is_checked, 0),
            (troop_inventory_slot_get_item_amount, ":cur_amount", ":householder", ":i_slot"),
            (gt, ":cur_amount", 0),
            
            (val_sub, ":cur_amount", 1),
            (troop_inventory_slot_set_item_amount, ":householder", ":i_slot", ":cur_amount"),
            (val_sub, ":num_of_servings_to_serve", 1),
            (item_set_slot, ":item", slot_item_is_checked, 1),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  
  ("faction_get_adjective_to_s10",
    [
      (store_script_param, ":faction_no", 1),
      
      (try_begin),
        (eq, ":faction_no", "fac_player_faction"),
        (assign, ":faction_no", "$players_kingdom"),
      (try_end),
      
      
      (try_begin),
        (eq, ":faction_no", "fac_player_supporters_faction"),
        (str_store_string, s10, "str_rebel"),
      (else_try),
        (this_or_next|eq, ":faction_no", "fac_outlaws"),
        (this_or_next|eq, ":faction_no", "fac_mountain_bandits"),
        (this_or_next|eq, ":faction_no", "fac_forest_bandits"),
        (eq, ":faction_no", "fac_deserters"),
        (str_store_string, s10, "str_bandit"),
      (else_try),
        (faction_get_slot, reg0, ":faction_no", slot_faction_culture),
        (str_store_faction_name, s10, reg0),
      (try_end),
  ]),
  
  ("setup_tavern_attacker",
    [
      (store_script_param, ":cur_entry", 1),
      
      (try_begin),
        (neg|troop_slot_eq, "trp_hired_assassin", slot_troop_cur_center, "$g_encountered_party"),
        (troop_slot_eq, "trp_belligerent_drunk", slot_troop_cur_center, "$g_encountered_party"),
        (set_visitor, ":cur_entry", "trp_belligerent_drunk"),
      (try_end),
      
      (try_begin),
        (troop_slot_eq, "trp_hired_assassin", slot_troop_cur_center, "$g_encountered_party"),
        (set_visitor, ":cur_entry", "trp_hired_assassin"),
      (try_end),
  ]),
  
  ("activate_tavern_attackers",
    [
      (set_party_battle_mode),
      (try_for_agents, ":cur_agent"),
        (agent_get_troop_id, ":cur_agent_troop", ":cur_agent"),
        (this_or_next|eq, ":cur_agent_troop", "trp_fugitive"),
        (this_or_next|eq, ":cur_agent_troop", "trp_belligerent_drunk"),
        (eq, ":cur_agent_troop", "trp_hired_assassin"),
        (agent_set_team, ":cur_agent", 1),
        (assign, "$g_main_attacker_agent", ":cur_agent"),
        (agent_ai_set_aggressiveness, ":cur_agent", 199),
        (team_give_order, 1, grc_everyone, mordr_charge),
      (try_end),
  ]),
  
  ("deactivate_tavern_attackers",
    [
      (finish_party_battle_mode),
      (try_for_agents, ":cur_agent"),
        (agent_get_troop_id, ":cur_agent_troop", ":cur_agent"),
        (this_or_next|eq, ":cur_agent_troop", "trp_fugitive"),
        (this_or_next|eq, ":cur_agent_troop", "trp_belligerent_drunk"),
        (eq, ":cur_agent_troop", "trp_hired_assassin"),
        (agent_set_team, ":cur_agent", 0),
        (agent_ai_set_aggressiveness, ":cur_agent", 0),
      (try_end),
  ]),
  
  ("activate_town_guard",
    [
      (set_party_battle_mode),
      #(get_player_agent_no, ":player_agent"),
      #(agent_get_team, ":player_team", ":player_agent"),
      
      (try_for_agents, ":cur_agent"),
        (agent_get_troop_id, ":troop_type", ":cur_agent"),
        (is_between, ":troop_type", "trp_norse_slave", "trp_looter"),
        (agent_set_team, ":cur_agent", 1),
        (agent_set_is_alarmed,":cur_agent", 1),
        #(team_give_order, 1, grc_everyone, mordr_charge), - for some reason, this freezes everyone if the player is not yet spawned
        #(try_begin),
        #	(eq, "$g_main_attacker_agent", 0),
        #	(assign, "$g_main_attacker_agent", ":cur_agent"),
        #(try_end),
      (else_try),
        (this_or_next|is_between, ":cur_agent", walkers_begin, walkers_end),
        (is_between, ":cur_agent", armor_merchants_begin, mayors_end),
        
        (agent_clear_scripted_mode, ":cur_agent"),
        (agent_set_team, ":cur_agent", 2),
      (try_end),
  ]),
  
  
  #this determines whether or not a lord is thrown into a dungeon by his captor, or is kept out on parole
  #Not currently used (ie, it always fails)
  ("cf_prisoner_offered_parole",
    [
      # (store_script_param, ":prisoner", 1),
      
      (eq, 1, 0), #disabled, this will always return false
      
      # (troop_get_slot, ":captor_party", ":prisoner", slot_troop_prisoner_of_party),
      # (party_is_active, ":captor_party"),
      # (is_between, ":captor_party", walled_centers_begin, walled_centers_end),
      # (party_get_slot, ":captor", ":captor_party", slot_town_lord),
      
      # (troop_get_slot, ":prisoner_rep", ":prisoner", slot_lord_reputation_type),
      # (troop_get_slot, ":captor_rep", ":captor", slot_lord_reputation_type),
      
      # (neq, ":prisoner_rep", lrep_debauched),
      # (neq, ":captor_rep", lrep_debauched),
      # (neq, ":captor_rep", lrep_quarrelsome),
      
      # #Prisoner is a noble, or lord is goodnatured
      # (this_or_next|eq, ":captor_rep", lrep_goodnatured),
      # (this_or_next|troop_slot_eq, ":prisoner", slot_troop_occupation, slto_kingdom_hero),
      # (troop_slot_eq, ":prisoner", slot_troop_occupation, slto_kingdom_lady),
      
      # (call_script, "script_troop_get_relation_with_troop", ":captor", ":prisoner"),
      # ##	(display_message, "str_relation_of_prisoner_with_captor_is_reg0"),
      # (ge, reg0, -10),
  ]),
  
  ("neutral_behavior_in_fight",
    [
      (get_player_agent_no, ":player_agent"),
      (agent_get_position, pos3, ":player_agent"),
      # (agent_get_team, ":player_team", ":player_agent"),
      
      (try_begin),
        (agent_is_active, "$g_main_attacker_agent"),
        (agent_get_team, ":attacker_team_no", "$g_main_attacker_agent"),
        (agent_get_position, pos5, "$g_main_attacker_agent"),
      (else_try),
        (assign, ":attacker_team_no", -1),
        (agent_get_position, pos5, ":player_agent"),
      (try_end),
      
      (set_fixed_point_multiplier, 100),
      
      (try_for_agents, ":agent"),
        (agent_get_team, ":other_team", ":agent"),
        (neq, ":other_team", ":attacker_team_no"),  #tavern brawl - team set in script_activate_tavern_attackers
        # (neq, ":other_team", ":player_team"), team normally not defined
        
        (agent_get_troop_id, ":troop_id", ":agent"),
        (neg|is_between, ":troop_id", "trp_norse_slave", "trp_caravan_master"),
        
        (agent_get_animation, ":anim", ":agent", 0),
        (try_begin),
          (is_between, ":anim", "anim_sitting", "anim_thrust_onehanded_overhead"),  #ambiance animations
          (agent_set_stand_animation,":agent","anim_stand_man"),
          (agent_ai_set_interact_with_player,":agent",1),
          (agent_set_slot,":agent",slot_agent_is_blocked,0),
          (try_begin),
            (is_between, ":anim", "anim_sitting", "anim_woodcutting_2"),  #sitting animations
            (agent_set_animation,":agent","anim_crouch_to_stand"),
          (try_end),
        (try_end),
        
        (agent_get_position, pos4, ":agent"),
        (assign, ":best_position_score", 0),
        (assign, ":best_position", -1),
        
        (try_begin),
          (neg|agent_slot_eq, ":agent", slot_agent_is_running_away, 0), #if agent is running away
          (agent_get_slot, ":target_entry_point_plus_one",  ":agent", slot_agent_is_running_away),
          (store_sub, ":target_entry_point", ":target_entry_point_plus_one", 1),
          (entry_point_get_position, pos6, ":target_entry_point"),
          (get_distance_between_positions, ":agent_distance_to_target", pos6, pos4),
          (lt, ":agent_distance_to_target", 100),
          (agent_set_slot, ":agent", slot_agent_is_running_away, 0),
        (try_end),
        
        (agent_slot_eq, ":agent", slot_agent_is_running_away, 0), #if agent is not already running away
        
        (try_begin), #stand in place
          (get_distance_between_positions, ":distance", pos4, pos5),
          (get_distance_between_positions, ":distance_to_player", pos4, pos3),
          
          (val_min, ":distance", ":distance_to_player"),
          
          (this_or_next|gt, ":distance", 700), #7 meters away from main belligerents
          (main_hero_fallen),
          
          (agent_set_scripted_destination, ":agent", pos4),
        (else_try), #get out of the way
          (try_for_range, ":target_entry_point", 0, 64),
            (neg|entry_point_is_auto_generated, ":target_entry_point"),
            (entry_point_get_position, pos6, ":target_entry_point"),
            (get_distance_between_positions, ":agent_distance_to_target", pos6, pos4),
            (get_distance_between_positions, ":player_distance_to_target", pos6, pos3),
            (store_sub, ":position_score", ":player_distance_to_target", ":agent_distance_to_target"),
            (ge, ":position_score", 0),
            (try_begin),
              (ge, ":agent_distance_to_target", 2000),
              (store_sub, ":extra_distance", ":agent_distance_to_target", 2000),
              (val_min, ":extra_distance", 1000),
              (val_min, ":agent_distance_to_target", 2000), #if more than 10 meters assume it is 10 meters far while calculating best run away target
              (val_sub, ":agent_distance_to_target", ":extra_distance"),
            (try_end),
            (val_mul, ":position_score", ":agent_distance_to_target"),
            (try_begin),
              (ge, ":position_score", ":best_position_score"),
              (assign, ":best_position_score", ":position_score"),
              (assign, ":best_position", ":target_entry_point"),
            (try_end),
          (try_end),
          
          (try_begin),
            (ge, ":best_position", 0),
            (entry_point_get_position, pos6, ":best_position"),
            (agent_set_speed_limit, ":agent", 10),
            (agent_set_scripted_destination, ":agent", pos6),
            (store_add, ":best_position_plus_one", ":best_position", 1),
            (agent_set_slot, ":agent", slot_agent_is_running_away, ":best_position_plus_one"),
          (try_end),
        (try_end),
      (try_end),
  ]),
  
  ("party_inflict_attrition", #parameters from dialog
    [
      (store_script_param, ":party", 1),
      (store_script_param, ":attrition_rate", 2),
      #	(store_script_param, ":attrition_type", 3), #1 = desertion, 2 = sickness
      
      (party_clear, "p_temp_casualties"),
      
      (party_get_num_companion_stacks, ":num_stacks", ":party"),
      
      #add to temp casualties
      (try_for_range, ":stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":troop_type", ":party", ":stack"),
        (neg|troop_is_hero, ":troop_type"),
        (party_stack_get_size, ":size", ":party", ":stack"),
        (store_mul, ":casualties_x_100", ":attrition_rate", ":size"),
        (store_div, ":casualties", ":casualties_x_100", 100),
        (party_add_members, "p_temp_casualties", ":troop_type", ":casualties"),
        
        (store_mul, ":subtractor", ":casualties", 100),
        (store_sub, ":chance_of_additional_casualty", ":casualties_x_100", ":subtractor"),
        
        (try_begin),
          (gt, ":chance_of_additional_casualty", 0),
          (store_random_in_range, ":random", 0, 100),
          (lt, ":random", ":chance_of_additional_casualty"),
          (party_add_members, "p_temp_casualties", ":troop_type", ":casualties"),
        (try_end),
        
        #		(try_begin),
        #			(eq, "$cheat_mode", 1),
        #			(str_store_party_name, s7, ":party"),
        #           		...
        #		(try_end),
      (try_end),
      
      #take temp casualties from main party
      (party_get_num_companion_stacks, ":num_stacks", "p_temp_casualties"),
      
      #add to temp casualties
      (try_for_range, ":stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":troop_type", "p_temp_casualties", ":stack"),
        (party_stack_get_size, ":size", "p_temp_casualties", ":stack"),
        (party_remove_members, ":party", ":troop_type", ":size"),
        
        (eq, "$cheat_mode", 1),
        (assign, reg3, ":size"),
        (str_store_troop_name, s4, ":troop_type"),
        (str_store_party_name, s5, ":party"),
        #		(display_message, "str_s5_suffers_attrition_reg3_x_s4"),
        (str_store_string, s65, "str_s5_suffers_attrition_reg3_x_s4"),
        (display_message, "str_s65"),
        (try_begin),
          (eq, "$debug_message_in_queue", 0),
          (call_script, "script_add_notification_menu", "mnu_debug_alert_from_s65", 0, 0),
          (assign, "$debug_message_in_queue", 1),
        (try_end),
      (try_end),
      
  ]),
  
  
  
  
  ("add_rumor_string_to_troop_notes", #parameters from dialog
    [
      (store_script_param, ":object_1", 1),
      (store_script_param, ":object_2", 2),
      (store_script_param, ":string", 3),
      
      (str_store_troop_name, s10, "$g_talk_troop"),
      (str_store_string_reg, s11, ":string"),
      
      (store_current_hours, ":hours"),
      (call_script, "script_game_get_date_text", 0, ":hours"),
      
      (str_store_string, s5, "str_s10_said_on_s1_s11__"),
      
      (try_begin),
        (is_between, ":object_1", active_npcs_begin, kingdom_ladies_end),
        (troop_get_slot, ":current_rumor_note", ":object_1", slot_troop_current_rumor),
        (val_add, ":current_rumor_note", 1),
        (try_begin),
          (neg|is_between, ":current_rumor_note", 3, 16),
          (assign, ":current_rumor_note", 3),
        (try_end),
        (troop_set_slot, ":object_1", slot_troop_current_rumor, ":current_rumor_note"),
        
        (add_troop_note_from_sreg, ":object_1", ":current_rumor_note", s5, 0), #troop, note slot, string, show
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (str_store_troop_name, s3, ":object_1"),
          (assign, reg4, ":current_rumor_note"),
          (display_message, "str_rumor_note_to_s3s_slot_reg4_s5"),
        (try_end),
      (try_end),
      
      (try_begin),
        (is_between, ":object_2", active_npcs_begin, kingdom_ladies_end),
        (troop_get_slot, ":current_rumor_note", ":object_2", slot_troop_current_rumor),
        (val_add, ":current_rumor_note", 1),
        (try_begin),
          (neg|is_between, ":current_rumor_note", 3, 16),
          (assign, ":current_rumor_note", 3),
        (try_end),
        (troop_set_slot, ":object_2", slot_troop_current_rumor, ":current_rumor_note"),
        
        (add_troop_note_from_sreg, ":object_2", ":current_rumor_note", s5, 0), #troop, note slot, string, show
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (str_store_troop_name, s3, ":object_2"),
          (assign, reg4, ":current_rumor_note"),
          (display_message, "str_rumor_note_to_s3s_slot_reg4_s5"),
        (try_end),
      (try_end),
  ]),
  
  ("center_get_goods_availability",
    [
      (store_script_param, ":center_no", 1),
      
      (str_store_party_name, s4, ":center_no"),
      
      (assign, ":hardship_index", 0),
      (try_for_range, ":cur_good", trade_goods_begin, trade_goods_end),
        #Must have consumption of at least 4 to be relevant
        #This prevents perishables and raw materials from having a major impact
        (try_begin),
          (is_between, ":center_no", villages_begin, villages_end),
          (item_get_slot, ":consumer_consumption", ":cur_good", slot_item_rural_demand),
        (else_try),
          (item_get_slot, ":consumer_consumption", ":cur_good", slot_item_urban_demand),
        (try_end),
        (gt, ":consumer_consumption", 2),
        
        (store_div, ":max_impact", ":consumer_consumption", 4), #was 4, dropped 3 again 4 now
        
        #High-demand items like grain tend to have much more dramatic price differentiation, so they yield substantially higher results than low-demand items
        
        (store_sub, ":cur_good_price_slot", ":cur_good", trade_goods_begin),
        (val_add, ":cur_good_price_slot", slot_town_trade_good_prices_begin),
        (party_get_slot, ":price", ":center_no", ":cur_good_price_slot"),
        
        (store_sub, ":price_differential", ":price", 1000),
        (gt, ":price_differential", 200), #was 100
        
        (val_div, ":price_differential", 200),
        (val_min, ":price_differential", ":max_impact"),
        
        (val_add, ":hardship_index", ":price_differential"),
      (try_end),
      
      (assign, reg0, ":hardship_index"),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (display_message, "@{!}DEBUG -- hardship index for {s4} = {reg0}"),
      (try_end),
  ]),
  
  ("lord_find_alternative_faction", #Also, make it so that lords will try to keep at least one center unassigned
  #despite name, might return current faction
    [
      (store_script_param, ":troop_no", 1),
      (store_script_param, ":must_leave", 2),
      
      (store_faction_of_troop, ":cur_faction", ":troop_no"),
      
      (assign, ":new_faction", -1),
      (assign, ":score_to_beat", -5),
      
      (store_random_in_range, ":advantegous_faction_change_time", 0, 10000),
      (try_begin),
        (this_or_next|le, "$g_advantegous_faction", 0),
        (eq, ":advantegous_faction_change_time", 0),
        (store_random_in_range, "$g_advantegous_faction", kingdoms_begin, kingdoms_end),
      (try_end),
      
      (try_begin),
        (neq, ":must_leave", 0),
        (assign, ":refusing_faction", ":cur_faction"),
      (else_try),
        (assign, ":refusing_faction", kingdoms_end),
      (try_end),
      
      (try_begin),
        (troop_slot_eq, ":troop_no", slot_troop_player_banned, 1),
        (assign, ":refused_faction", "fac_player_supporters_faction"),
      (else_try),
        (assign, ":refused_faction", kingdoms_end),
      (try_end),
      
      (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
        (neq, ":faction_no", ":refusing_faction"),
        (neq, ":faction_no", ":refused_faction"),
        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        
        ##		#MOTO chief neighbors
        ##		#add this to prevent factions from gaining fiefs (and starting wars) at far end of map
        ##		(store_add, ":slot", slot_faction_neighbors_begin, ":faction_no"),
        ##		(val_sub, ":slot", kingdoms_begin),
        ##		(this_or_next|faction_slot_eq, ":cur_faction", ":slot", 1),
        ##		(eq, ":faction_no", ":cur_faction"),
        ##		#MOTO chief neighbors end
        
        (assign, ":number_of_fiefs", 0),
        (try_for_range, ":center_no", centers_begin, centers_end),
          (store_faction_of_party, ":center_faction", ":center_no"),
          (eq, ":center_faction", ":faction_no"),
          (val_add, ":number_of_fiefs", 1),
          # (party_slot_eq, ":center_no", slot_town_lord, ":troop_no"),  current faction not a consideration; either takes or loses fiefs
          # (val_add, ":number_of_fiefs", 1), #keep lord churn down by favoring current faction
        (try_end),
        
        (gt, ":number_of_fiefs", 0),  #VC-3915 auto-defects if no fiefs (see trigger), so senseless to continue
        
        (assign, ":number_of_lords", 0),
        (try_for_range, ":troop_id", original_kingdom_heroes_begin, active_npcs_end),
          (store_troop_faction, ":faction_of_troop", ":troop_id"),
          (eq, ":faction_of_troop", ":faction_no"),
          (val_add, ":number_of_lords", 1),
        (try_end),
        (val_max, ":number_of_lords", 1),  #avoid div by 0
        
        (store_mul, ":faction_score", ":number_of_fiefs", 100), #score of 100 means there's a fief for every lord
        (val_div, ":faction_score", ":number_of_lords"),
        
        (faction_get_slot, ":liege", ":faction_no", slot_faction_leader),
        (call_script, "script_troop_get_relation_with_troop", ":troop_no", ":liege"),
        (store_add, ":multiplier", reg0, 100),  #0-200
        
        (try_begin),
          (eq, ":faction_no", "$g_advantegous_faction"),
          (val_mul, ":multiplier", 3),
          (val_div, ":multiplier", 2),
        (try_end),
        
        (troop_get_slot, ":orig_faction", ":troop_no", slot_troop_original_faction),
        
        (try_begin),
          (eq, ":faction_no", ":orig_faction"),
          (val_mul, ":multiplier", 3),  #there's no place like home
          (val_div, ":multiplier", 2),
        (try_end),
        
        (faction_get_slot, ":troop_culture", ":orig_faction", slot_faction_culture),
        (faction_get_slot, ":faction_culture", ":faction_no", slot_faction_culture),
        
        (try_begin),
          (eq, ":faction_culture", ":troop_culture"),
          (val_mul, ":multiplier", 3),
          (val_div, ":multiplier", 2),
        (try_end),
        
        (try_begin),
          (eq, ":faction_no", "$players_kingdom"),
          (store_add, ":subplier", "$player_right_to_rule", 50),    #50-150
          (val_mul, ":multiplier", ":subplier"),
          (val_div, ":multiplier", 100),
        (try_end),
        
        (val_mul, ":faction_score", ":multiplier"),
        (gt, ":faction_score", ":score_to_beat"),
        
        (assign, ":score_to_beat", ":faction_score"),
        (assign, ":new_faction", ":faction_no"),
      (try_end),
      
      (assign, reg0, ":new_faction"),
  ]),
  
  ("set_up_duel_with_troop", #now the setup is handled through the menu
    [
      (store_script_param, "$g_duel_troop", 1),
      (assign, "$g_start_arena_fight_at_nearest_town", 1),
      (unlock_achievement, ACHIEVEMENT_PUGNACIOUS_D),
      (jump_to_menu, "mnu_arena_duel_fight"),
      (finish_mission),
      
  ]),
  
  ("deduct_casualties_from_garrison", #after a battle in a center, deducts any casualties from "$g_encountered_party"
    [
      ##(display_message, "str_totalling_casualties_caused_during_mission"),
      
      (try_for_agents, ":agent"),
        (agent_get_troop_id, ":troop_type", ":agent"),
        (is_between, ":troop_type", regular_troops_begin, regular_troops_end),
        
        (neg|agent_is_alive, ":agent"),
        
        (try_begin), #if troop not present, search for another type which is
          (store_troop_count_companions, ":number", ":troop_type", "$g_encountered_party"),
          (eq, ":number", 0),
          (assign, ":troop_type", 0),
          (try_for_range, ":new_tier", slot_faction_tier_1_troop, slot_faction_tier_5_troop),
            (faction_get_slot, ":troop_type", "$g_encountered_party_faction", ":new_tier"),
            (faction_get_slot, ":new_troop_type", "$g_encountered_party_faction", ":new_tier"),
            (store_troop_count_companions, ":number", ":new_troop_type", "$g_encountered_party"),
            (gt, ":number", 0),
            (assign, ":troop_type", ":new_troop_type"),
          (try_end),
        (try_end),
        
        (gt, ":troop_type", 0),
        
        (party_remove_members, "$g_encountered_party", ":troop_type", 1),
        (str_store_troop_name, s4, ":troop_type"),
        (str_store_party_name, s5, "$g_encountered_party"),
      (try_end),
  ]),
  
  ("npc_decision_checklist_take_stand_on_issue",
    #Called from dialogs, and from simple_triggers
    
    #This a very inefficient checklist, and if I did it again, I would score for each troop. That way the troop could answer "why not" to an individual lord
    [
      (store_script_param, ":troop_no", 1),
      (store_faction_of_troop, ":troop_faction", ":troop_no"),
      
      (assign, ":result", -1),
      (faction_get_slot, ":faction_issue", ":troop_faction", slot_faction_political_issue),
      
      (assign, ":player_declines_honor", 0),
      (try_begin),
        (is_between, ":faction_issue", centers_begin, centers_end),
        (gt, "$g_dont_give_fief_to_player_days", 1),
        (assign, ":player_declines_honor", 1),
      (else_try),
        (gt, "$g_dont_give_marshalship_to_player_days", 1),
        (assign, ":player_declines_honor", 1),
      (try_end),
      
      
      (assign, ":total_faction_renown", 0),
      (troop_set_slot, "trp_player", slot_troop_temp_slot, 0),
      (try_begin),
        (eq, "$players_kingdom", ":troop_faction"),
        (eq, "$player_has_homage", 1),
        (troop_get_slot, ":total_faction_renown", "trp_player", slot_troop_renown),
      (try_end),
      
      (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
        (troop_set_slot, ":active_npc", slot_troop_temp_slot, 0), #reset to zero
        
        (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
        (eq, ":active_npc_faction", ":troop_faction"),
        (troop_slot_eq, ":active_npc", slot_troop_occupation, slto_kingdom_hero),
        
        (troop_get_slot, ":renown", ":active_npc", slot_troop_renown),
        (val_add, ":total_faction_renown", ":renown"),
      (try_end),
      
      
      (assign, ":total_faction_center_value", 0),
      (try_for_range, ":center", centers_begin, centers_end),
        (store_faction_of_party, ":center_faction", ":center"),
        (eq, ":center_faction", ":troop_faction"),
        
        (assign, ":center_value", 1),
        (try_begin),
          (is_between, ":center", towns_begin, towns_end),
          (assign, ":center_value", 2),
        (try_end),
        
        (val_add, ":total_faction_center_value", ":center_value"),
        
        (party_get_slot, ":town_lord", ":center", slot_town_lord),
        (gt, ":town_lord", -1),
        
        (troop_get_slot, ":temp_slot", ":town_lord", slot_troop_temp_slot),
        (val_add, ":temp_slot", ":center_value"),
        (troop_set_slot, ":town_lord", slot_troop_temp_slot, ":temp_slot"),
      (try_end),
      (val_max, ":total_faction_center_value", 1),
      
      (store_div, ":average_renown_per_center_point", ":total_faction_renown", ":total_faction_center_value"),
      
      
      (try_begin),
        (is_between, ":faction_issue", centers_begin, centers_end),
        #NOTE -- The algorithms here might seem a bit repetitive, but are designed that way to create internal cliques among the lords in a faction.
        
        
        
        (try_begin),#If the center is a village, and a lord has no fief, choose him
          (neg|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_debauched),
          (neg|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_selfrighteous),
          (neg|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_quarrelsome),
          
          (is_between, ":faction_issue", villages_begin, villages_end),
          (assign, ":favorite_lord_without_center", -1),
          (assign, ":score_to_beat", -1),
          
          
          (try_begin),
            (eq, "$players_kingdom", ":troop_faction"),
            (eq, "$player_has_homage", 1),
            (eq, ":player_declines_honor", 0),
            
            (troop_slot_eq, "trp_player", slot_troop_temp_slot, 0),
            (call_script, "script_troop_get_relation_with_troop", "trp_player", ":troop_no"),
            (assign, ":relation", reg0),
            
            (gt, ":relation", ":score_to_beat"),
            (neg|troop_slot_ge, "trp_player", slot_troop_controversy, 75),
            (assign, ":favorite_lord_without_center", "trp_player"),
            (assign, ":score_to_beat", ":relation"),
          (try_end),
          (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
            (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
            (eq, ":active_npc_faction", ":troop_faction"),
            (troop_slot_eq, ":active_npc", slot_troop_occupation, slto_kingdom_hero),
            
            (troop_slot_eq, ":active_npc", slot_troop_temp_slot, 0),
            (try_begin),
              (eq, ":active_npc", ":troop_no"),
              (assign, ":relation", 50),
            (else_try),
              (call_script, "script_troop_get_relation_with_troop", ":active_npc", ":troop_no"),
              (assign, ":relation", reg0),
            (try_end),
            (neg|troop_slot_ge, ":active_npc", slot_troop_controversy, 75),
            
            (gt, ":relation", ":score_to_beat"),
            (assign, ":favorite_lord_without_center", ":active_npc"),
            (assign, ":score_to_beat", ":relation"),
          (try_end),
          
          (gt, ":favorite_lord_without_center", -1),
          (assign, ":result", ":favorite_lord_without_center"),
          (assign, ":result_explainer", "str_political_explanation_lord_lacks_center"),
          
        (else_try),	#taken by troop
          (is_between, ":faction_issue", walled_centers_begin, walled_centers_end),
          (neg|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_debauched),
          (neg|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_selfrighteous),
          (neg|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_cunning),
          
          (party_get_slot, ":last_taken_by_troop", ":faction_issue", slot_center_last_taken_by_troop),
          (try_begin),
            (try_begin),
              (neq, ":troop_faction", "$players_kingdom"),
              (assign, ":last_taken_by_troop", -1),
            (else_try),
              (eq, "$player_has_homage", 0),
              (assign, ":last_taken_by_troop", -1),
            (else_try),
              (eq, ":faction_issue", "$g_castle_requested_by_player"),
              (assign, ":last_taken_by_troop", "trp_player"),
            (else_try),
              (eq, ":faction_issue", "$g_castle_requested_for_troop"),
              (assign, ":last_taken_by_troop", "trp_player"),
            (else_try), #ie, the fellow who took it is no longer in the faction
              (gt, ":last_taken_by_troop", -1),
              (store_faction_of_troop, ":last_take_by_troop_faction", ":last_taken_by_troop"),
              (neq, ":last_take_by_troop_faction", ":troop_faction"),
              (assign, ":last_taken_by_troop", -1),
            (try_end),
          (try_end),
          (gt, ":last_taken_by_troop", -1),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (gt, ":last_taken_by_troop", -1),
            (str_store_troop_name, s3, ":last_taken_by_troop"),
            (display_message, "@{!}Castle taken by {s3}"),
          (try_end),
          
          
          (call_script, "script_troop_get_relation_with_troop", ":troop_no", ":last_taken_by_troop"),
          (ge, reg0, 0),
          
          (neg|troop_slot_ge, ":last_taken_by_troop", slot_troop_controversy, 25),
          
          (troop_get_slot, ":renown", ":last_taken_by_troop", slot_troop_renown),
          (troop_get_slot, ":center_points", ":last_taken_by_troop", slot_troop_temp_slot),
          (val_max, ":center_points", 1),
          (store_div, ":renown_divided_by_center_points", ":renown", ":center_points"),
          (val_mul, ":renown_divided_by_center_points", 6), #was five
          (val_div, ":renown_divided_by_center_points", 4),
          
          (ge, ":renown_divided_by_center_points", ":average_renown_per_center_point"),
          
          
          (assign, ":result", ":last_taken_by_troop"),
          (assign, ":result_explainer", "str_political_explanation_lord_took_center"),
          
          
          #Check self, immediate family
          #This is done instead of a single weighted score to create cliques -- groups of NPCs who support one another
        (else_try),
          (assign, ":most_deserving_close_friend", -1),
          (assign, ":score_to_beat", ":average_renown_per_center_point"),
          (val_div, ":score_to_beat", 3),
          (val_mul, ":score_to_beat", 2),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (assign, reg3, ":score_to_beat"),
            (display_message, "@{!}Two-thirds average_renown = {reg3}"),
          (try_end),
          
          (try_begin),
            (eq, "$players_kingdom", ":troop_faction"),
            (eq, "$player_has_homage", 1),
            (eq, ":player_declines_honor", 0),
            
            (call_script, "script_troop_get_relation_with_troop", "trp_player", ":troop_no"),
            (assign, ":relation", reg0),
            (ge, ":relation", 20),
            (neg|troop_slot_ge, "trp_player", slot_troop_controversy, 50),
            
            (troop_get_slot, ":renown", "trp_player", slot_troop_renown),
            (troop_get_slot, ":center_points", "trp_player", slot_troop_temp_slot),
            (val_max, ":center_points", 1),
            (store_div, ":renown_divided_by_center_points", ":renown", ":center_points"),
            
            
            (assign, ":most_deserving_close_friend", "trp_player"),
            (assign, ":score_to_beat", ":renown_divided_by_center_points"),
          (try_end),
          (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
            (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
            (eq, ":active_npc_faction", ":troop_faction"),
            (troop_slot_eq, ":active_npc", slot_troop_occupation, slto_kingdom_hero),
            (neg|faction_slot_eq, ":troop_faction", slot_faction_leader, ":active_npc"), #avoid naming king as a good friend
            
            (call_script, "script_troop_get_relation_with_troop", ":active_npc", ":troop_no"),
            (assign, ":relation", reg0),
            
            (this_or_next|eq, ":active_npc", ":troop_no"),
            (ge, ":relation", 20),
            (neg|troop_slot_ge, ":active_npc", slot_troop_controversy, 50),
            
            (troop_get_slot, ":renown", ":active_npc", slot_troop_renown),
            (troop_get_slot, ":center_points", ":active_npc", slot_troop_temp_slot),
            (val_max, ":center_points", 1),
            (store_div, ":renown_divided_by_center_points", ":renown", ":center_points"),
            
            
            (try_begin),
              (eq, "$cheat_mode", 1),
              (str_store_troop_name, s10, ":active_npc"),
              (assign, reg3, ":renown_divided_by_center_points"),
              (display_message, "@{!}DEBUG -- Colleague test: score for {s10} = {reg3}"),
            (try_end),
            
            
            (gt, ":renown_divided_by_center_points", ":score_to_beat"),
            
            (assign, ":most_deserving_close_friend", ":active_npc"),
            (assign, ":score_to_beat", ":renown_divided_by_center_points"),
          (try_end),
          
          (gt, ":most_deserving_close_friend", -1),
          
          
          (assign, ":result", ":most_deserving_close_friend"),
          (assign, ":result_explainer", "str_political_explanation_most_deserving_friend"),
          
          
          
        (else_try),
          #Most deserving in entire faction, minus those with no relation
          (neg|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_debauched),
          (neg|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_selfrighteous),
          (neg|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_quarrelsome),
          
          (assign, ":most_deserving_in_faction", -1),
          (assign, ":score_to_beat", 0),
          
          (try_begin),
            (eq, "$players_kingdom", ":troop_faction"),
            (eq, "$player_has_homage", 1),
            (eq, ":player_declines_honor", 0),
            
            (call_script, "script_troop_get_relation_with_troop", "trp_player", ":troop_no"),
            (assign, ":relation", reg0),
            (ge, ":relation", 0),
            (troop_get_slot, ":renown", "trp_player", slot_troop_renown),
            (troop_get_slot, ":center_points", "trp_player", slot_troop_temp_slot),
            (neg|troop_slot_ge, "trp_player", slot_troop_controversy, 25),
            
            (val_max, ":center_points", 1),
            (store_div, ":renown_divided_by_center_points", ":renown", ":center_points"),
            
            (assign, ":most_deserving_in_faction", "trp_player"),
            (assign, ":score_to_beat", ":renown_divided_by_center_points"),
          (try_end),
          (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
            (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
            (eq, ":active_npc_faction", ":troop_faction"),
            (troop_slot_eq, ":active_npc", slot_troop_occupation, slto_kingdom_hero),
            
            (call_script, "script_troop_get_relation_with_troop", ":active_npc", ":troop_no"),
            (assign, ":relation", reg0),
            (this_or_next|eq, ":active_npc", ":troop_no"),
            (ge, ":relation", 0),
            (neg|troop_slot_ge, ":active_npc", slot_troop_controversy, 25),
            
            (troop_get_slot, ":renown", ":active_npc", slot_troop_renown),
            (troop_get_slot, ":center_points", ":active_npc", slot_troop_temp_slot),
            (val_max, ":center_points", 1),
            
            (store_div, ":renown_divided_by_center_points", ":renown", ":center_points"),
            (gt, ":renown_divided_by_center_points", ":score_to_beat"),
            
            (try_begin),
              (eq, "$cheat_mode", 1),
              (str_store_string, s10, ":active_npc"),
              (assign, reg3, ":renown_divided_by_center_points"),
              (display_message, "@{!}DEBUG -- Open test: score for {s10} = {reg3}"),
            (try_end),
            
            
            (assign, ":most_deserving_in_faction", ":active_npc"),
            (assign, ":score_to_beat", ":renown_divided_by_center_points"),
          (try_end),
          
          
          (gt, ":most_deserving_in_faction", -1),
          (assign, ":result", ":most_deserving_in_faction"),
          (assign, ":result_explainer", "str_political_explanation_most_deserving_in_faction"),
          
        (else_try),
          (assign, ":result", ":troop_no"),
          (assign, ":result_explainer", "str_political_explanation_self"),
        (try_end),
        
        
      (else_try),
        (eq, ":faction_issue", 1),
        
        (assign, ":relationship_threshhold", 15),
        (try_begin),
          (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_upstanding),
          (assign, ":relationship_threshhold", 5),
        (else_try),
          (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_debauched),
          (assign, ":relationship_threshhold", 25),
        (try_end),
        
        #For marshals, score marshals according to renown divided by controversy - first for friends and family, then for everyone
        (assign, ":marshal_candidate", -1),
        (assign, ":score_to_beat", 0),
        (try_begin),
          (eq, "$players_kingdom", ":troop_faction"),
          (eq, "$player_has_homage", 1),
          (eq, "$g_player_is_captive", 0),
          (eq, ":player_declines_honor", 0),
          
          
          (call_script, "script_troop_get_relation_with_troop", "trp_player", ":troop_no"),
          (ge, reg0, ":relationship_threshhold"),
          (party_is_active, "p_main_party"),	#MOTO check for test below
          (assign, ":marshal_candidate", "trp_player"),
          (troop_get_slot, ":renown", "trp_player", slot_troop_renown),
          #MOTO chief avoid electing small parties for marshal
          (store_party_size_wo_prisoners, ":party_size", "p_main_party"),
          (val_add, ":renown", ":party_size"),
          (val_add, ":renown", ":party_size"),
          #MOTO end avoid electing small parties for marshal
          (troop_get_slot, ":controversy_divisor", "trp_player", slot_troop_controversy),
          (val_add, ":controversy_divisor", 50),
          (store_div, ":score_to_beat", ":renown", ":controversy_divisor"),
        (try_end),
        
        (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
          (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
          (eq, ":active_npc_faction", ":troop_faction"),
          (troop_slot_eq, ":active_npc", slot_troop_occupation, slto_kingdom_hero),
          (troop_slot_eq, ":active_npc", slot_troop_prisoner_of_party, -1),
          
          (neg|faction_slot_eq, ":troop_faction", slot_faction_leader, ":active_npc"),
          
          (call_script, "script_troop_get_relation_with_troop", ":active_npc", ":troop_no"),
          (assign, ":relation", reg0),
          (this_or_next|eq, ":active_npc", ":troop_no"),
          (ge, ":relation", ":relationship_threshhold"),
          
          (troop_get_slot, ":renown", ":active_npc", slot_troop_renown),
          #MOTO chief avoid electing small parties for marshal
          (troop_get_slot, ":active_party", ":active_npc", slot_troop_leaded_party),
          (party_is_active, ":active_party"),
          (store_party_size_wo_prisoners, ":party_size", ":active_party"),
          (val_add, ":renown", ":party_size"),
          (val_add, ":renown", ":party_size"),
          #MOTO end avoid electing small parties for marshal
          (troop_get_slot, ":controversy_divisor", ":active_npc", slot_troop_controversy),
          (val_add, ":controversy_divisor", 50),
          (store_div, ":score", ":renown", ":controversy_divisor"),
          
          (gt, ":score", ":score_to_beat"),
          
          (assign, ":marshal_candidate", ":active_npc"),
          (assign, ":score_to_beat", ":score"),
          
        (try_end),
        
        (assign, ":result", ":marshal_candidate"),
        (assign, ":result_explainer", "str_political_explanation_marshal"),
      (try_end),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (gt, ":result", -1),
        (str_store_troop_name, s8, ":troop_no"),
        (str_store_troop_name, s9, ":result"),
        (str_store_string, s10, ":result_explainer"),
        (display_message, "@{!}DEBUG -- {s8} backs {s9}:{s10}"),
      (try_end),
      
      (assign, reg0, ":result"),
      (assign, reg1, ":result_explainer"),
      
  ]),
  
  
  ("process_player_enterprise",
    #reg0: Profit per cycle
    [
      (store_script_param, ":item_type", 1),
      (store_script_param, ":center", 2),
      
      (item_get_slot, ":price_of_labor", ":item_type", slot_item_overhead_per_run),
      
      (item_get_slot, ":base_price", ":item_type", slot_item_base_price),
      (store_sub, ":cur_good_price_slot", ":item_type", trade_goods_begin),
      (val_add, ":cur_good_price_slot", slot_town_trade_good_prices_begin),
      (party_get_slot, ":cur_price_modifier", ":center", ":cur_good_price_slot"),
      
      (try_begin),
        (is_between, ":center", towns_begin, towns_end),
        (assign, ":number_of_villages", 0),
        (assign, ":village_mod", 0),
        (try_for_range, ":village_no", villages_begin, villages_end),
          (this_or_next|party_slot_eq, ":village_no", slot_village_bound_center, ":center"),
          (party_slot_eq, ":village_no", slot_village_market_town, ":center"),
          (party_get_slot, ":price_at_village", ":village_no", ":cur_good_price_slot"),
          (val_add, ":village_mod", ":price_at_village"),
          (val_add, ":number_of_villages", 1),
        (try_end),
        (val_div, ":village_mod", ":number_of_villages"),
        (val_add, ":cur_price_modifier",  ":village_mod"),
        (val_div,":cur_price_modifier", 2),
      (try_end),
      
      (store_mul, ":final_price_for_single_produced_good", ":base_price", ":cur_price_modifier"),
      (val_div, ":final_price_for_single_produced_good", 1000),
      (item_get_slot, ":number_of_outputs_produced", ":item_type", slot_item_output_per_run),
      (store_mul, ":final_price_for_total_produced_goods", ":number_of_outputs_produced", ":final_price_for_single_produced_good"),
      
      (item_get_slot, ":primary_raw_material", ":item_type", slot_item_primary_raw_material),
      (item_get_slot, ":base_price", ":primary_raw_material", slot_item_base_price),
      (store_sub, ":cur_good_price_slot", ":primary_raw_material", trade_goods_begin),
      (val_add, ":cur_good_price_slot", slot_town_trade_good_prices_begin),
      (party_get_slot, ":cur_price_modifier", ":center", ":cur_good_price_slot"),
      
      (try_begin),
        (is_between, ":center", towns_begin, towns_end),
        (assign, ":number_of_villages", 0),
        (assign, ":village_mod", 0),
        (try_for_range, ":village_no", villages_begin, villages_end),
          (this_or_next|party_slot_eq, ":village_no", slot_village_bound_center, ":center"),
          (party_slot_eq, ":village_no", slot_village_market_town, ":center"),
          (party_get_slot, ":price_at_village", ":village_no", ":cur_good_price_slot"),
          (val_add, ":village_mod", ":price_at_village"),
          (val_add, ":number_of_villages", 1),
        (try_end),
        (val_div, ":village_mod", ":number_of_villages"),
        (val_div,":village_mod", 4),
        (val_add, ":cur_price_modifier", ":village_mod"),
      (try_end),
      
      (store_mul, ":final_price_for_single_input", ":base_price", ":cur_price_modifier"),
      (val_div, ":final_price_for_single_input", 1000),
      (item_get_slot, ":number_of_inputs_required", ":item_type", slot_item_input_number),
      (try_begin),
        (lt, ":number_of_inputs_required", 0),
        (store_div, ":final_price_for_total_inputs", ":final_price_for_single_input", 2),
      (else_try),
        (store_mul, ":final_price_for_total_inputs", ":final_price_for_single_input", ":number_of_inputs_required"),
      (try_end),
      
      (try_begin),
        (item_slot_ge, ":item_type", slot_item_secondary_raw_material, 1),
        (item_get_slot, ":secondary_raw_material", ":item_type", slot_item_secondary_raw_material),
        (item_get_slot, ":base_price", ":secondary_raw_material", slot_item_base_price),
        (store_sub, ":cur_good_price_slot", ":secondary_raw_material", trade_goods_begin),
        (val_add, ":cur_good_price_slot", slot_town_trade_good_prices_begin),
        (party_get_slot, ":cur_price_modifier", ":center", ":cur_good_price_slot"),
        (store_mul, ":final_price_for_secondary_input", ":base_price", ":cur_price_modifier"),
        (val_div, ":final_price_for_secondary_input", 1000),
        
        (try_begin),
          (lt, ":number_of_inputs_required", 0),
          (val_div, ":final_price_for_secondary_input", 2),
        (else_try),
          (val_mul, ":final_price_for_secondary_input", ":number_of_inputs_required"),
        (try_end),
        
      (else_try),
        (assign, ":final_price_for_secondary_input", 0),
      (try_end),
      
      (store_sub, ":profit_per_cycle", ":final_price_for_total_produced_goods", ":final_price_for_total_inputs"),
      (val_sub, ":profit_per_cycle", ":price_of_labor"),
      (val_sub, ":profit_per_cycle", ":final_price_for_secondary_input"),
      
      (assign, reg0, ":profit_per_cycle"),
      (assign, reg1, ":final_price_for_total_produced_goods"),
      (assign, reg2, ":final_price_for_total_inputs"),
      (assign, reg3, ":price_of_labor"),
      (assign, reg4, ":final_price_for_single_produced_good"),
      (assign, reg5, ":final_price_for_single_input"),
      (assign, reg10, ":final_price_for_secondary_input"),
  ]),
  
  # script_replace_scene_items_with_spawn_items_before_ms
  # Input: none
  # Output: none
  ("replace_scene_items_with_spawn_items_before_ms",
    [
      (try_for_range, ":item_no", all_items_begin, all_items_end),
        (scene_item_get_num_instances, ":num_instances", ":item_no"),
        (item_set_slot, ":item_no", slot_item_num_positions, 0),
        (assign, ":num_positions", 0),
        (try_for_range, ":cur_instance", 0, ":num_instances"),
          (scene_item_get_instance, ":scene_item", ":item_no", ":cur_instance"),
          (prop_instance_get_position, "$g_position_to_use_for_replacing_scene_items", ":scene_item"),
          (store_add, ":cur_slot", slot_item_positions_begin, ":num_positions"),
          (item_set_slot, ":item_no", ":cur_slot", "$g_position_to_use_for_replacing_scene_items"),
          (val_add, ":num_positions", 1),
          (val_add, "$g_position_to_use_for_replacing_scene_items", 1),
          (item_set_slot, ":item_no", slot_item_num_positions, ":num_positions"),
        (try_end),
        (replace_scene_items_with_scene_props, ":item_no", "spr_empty"),
      (try_end),
  ]),
  
  # script_replace_scene_items_with_spawn_items_after_ms
  # Input: none
  # Output: none
  ("replace_scene_items_with_spawn_items_after_ms",
    [
      (try_for_range, ":item_no", all_items_begin, all_items_end),
        (item_get_slot,  ":num_positions", ":item_no", slot_item_num_positions),
        (try_for_range, ":cur_position", 0, ":num_positions"),
          (store_add, ":cur_slot", slot_item_positions_begin, ":cur_position"),
          (item_get_slot, ":pos_no", ":item_no", ":cur_slot"),
          (set_spawn_position, ":pos_no"),
          (spawn_item, ":item_no", 0),
        (try_end),
      (try_end),
  ]),
  
  # script_cf_is_melee_weapon_for_tutorial
  # Input: arg1 = item_no
  # Output: none (can fail)
  ("cf_is_melee_weapon_for_tutorial",
    [
      (store_script_param, ":item_no", 1),
      (assign, ":result", 0),
      (try_begin),
        (this_or_next|eq, ":item_no", "itm_quarter_staff"),
        (eq, ":item_no", "itm_practice_sword"),
        (assign, ":result", 1),
      (try_end),
      (eq, ":result", 1),
  ]),
  
  # script_iterate_pointer_arrow
  # Input: none
  # Output: none
  ("iterate_pointer_arrow",
    [
      (store_mission_timer_a_msec, ":cur_time"),
      (try_begin),
        (assign, ":up_down", ":cur_time"),
        (assign, ":turn_around", ":cur_time"),
        (val_mod, ":up_down", 1080),
        (val_div, ":up_down", 3),
        (scene_prop_get_instance, ":prop_instance", "spr_pointer_arrow", 0),
        (prop_instance_get_position, pos0, ":prop_instance"),
        (position_set_z_to_ground_level, pos0),
        (position_move_z, pos0, "$g_pointer_arrow_height_adder", 1),
        (set_fixed_point_multiplier, 100),
        (val_mul, ":up_down", 100),
        (store_sin, ":up_down_sin", ":up_down"),
        (position_move_z, pos0, ":up_down_sin", 1),
        (position_move_z, pos0, 100, 1),
        (val_mod, ":turn_around", 2880),
        (val_div, ":turn_around", 8),
        (init_position, pos1),
        (position_rotate_z, pos1, ":turn_around"),
        (position_copy_rotation, pos0, pos1),
        (prop_instance_set_position, ":prop_instance", pos0),
      (try_end),
  ]),
  
  ("find_center_to_attack_alt", #moto chief cambia entero
    [
      (store_script_param, ":troop_no", 1),
      (store_script_param, ":attack_by_faction", 2),
      (store_script_param, ":all_vassals_included", 3),
      
      (assign, ":result", -1),
      (assign, ":score_to_beat", 0),
      
      #MOTO of walled centers, consider only the closest
      (try_for_range, ":troop_faction_no", kingdoms_begin, kingdoms_end),
        (faction_set_slot, ":troop_faction_no", slot_faction_temp_slot, 0),
      (try_end),
      
      #find troop's kingdom's center
      (store_faction_of_troop, ":troop_faction_no", ":troop_no"),
      (assign, ":number_of_walled_centers", 0),
      (assign, ":avg_pos_x", 0),
      (assign, ":avg_pos_y", 0),
      
      (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
        (store_faction_of_party, ":walled_center_faction", ":center_no"),
        (eq, ":walled_center_faction", ":troop_faction_no"),
        
        (party_get_position, pos34, ":center_no"),
        (position_get_x, reg0, pos34),
        (val_add, ":avg_pos_x", reg0),
        (position_get_y, reg0, pos34),
        (val_add, ":avg_pos_y", reg0),
        (val_add, ":number_of_walled_centers", 1),
      (try_end),
      
      (try_begin),
        (gt, ":number_of_walled_centers", 0),
        (val_div, ":avg_pos_x", ":number_of_walled_centers"),
        (val_div, ":avg_pos_y", ":number_of_walled_centers"),
        (init_position, pos34),	#position used only by battle AI.
        (position_set_x, pos34, ":avg_pos_x"),
        (position_set_y, pos34, ":avg_pos_y"),
      (try_end),
      
      #store closest walled center of every faction
      (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
        (store_faction_of_party, ":walled_center_faction", ":center_no"),
        (faction_get_slot, ":cur_closest_center", ":walled_center_faction", slot_faction_temp_slot),
        
        (try_begin),
          (eq, ":cur_closest_center", 0),
          (faction_set_slot, ":walled_center_faction", slot_faction_temp_slot, ":center_no"),
        (else_try),
          (party_get_position, pos36, ":center_no"), #moto chief
          (get_distance_between_positions, ":test_distance", pos34, pos36),
          (party_get_position, pos37, ":cur_closest_center"),
          (get_distance_between_positions, ":cur_distance", pos34, pos37),
          (lt, ":test_distance", ":cur_distance"),
          (faction_set_slot, ":walled_center_faction", slot_faction_temp_slot, ":center_no"),
        (try_end),
      (try_end),
      
      #NOW evaluate targets
      # (try_for_range, ":center_no", centers_begin, centers_end),
      (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),	#MOTO leave raiding to individual lords
        (store_faction_of_party, ":center_faction", ":center_no"),
        (neq, ":center_faction", ":troop_faction_no"),
        
        (this_or_next|party_slot_eq, ":center_no", slot_center_ex_faction, ":troop_faction_no"),    #always consider      former belongings
        (this_or_next|party_slot_eq, ":center_no", slot_center_original_faction, ":troop_faction_no"),
        (this_or_next|faction_slot_eq, ":center_faction", slot_faction_temp_slot, ":center_no"),	#this center is the closest walled center OR
        (neg|is_between, ":center_no", walled_centers_begin, walled_centers_end),	#is a village
        #MOTO end of walled centers, consider only the closest
        (call_script, "script_npc_decision_checklist_evaluate_enemy_center_for_attack",	":troop_no", ":center_no", ":attack_by_faction", ":all_vassals_included"),
        (assign, ":score", reg0),
        
        (gt, ":score", ":score_to_beat"),
        
        (assign, ":result", ":center_no"),
        (assign, ":score_to_beat", ":score"),
      (try_end),
      
      (assign, reg0, ":result"),
      (assign, reg1, ":score_to_beat"),
  ]),
  
  ("npc_decision_checklist_evaluate_enemy_center_for_attack",
    [
      #NOTES -- LAST OFFENSIVE TIME SCORE IS NOT USED
      
      (store_script_param, ":troop_no", 1),
      (store_script_param, ":potential_target", 2),
      (store_script_param, ":attack_by_faction", 3),
      (store_script_param, ":all_vassals_included", 4),
      
      (assign, ":result", -1),
      (assign, ":explainer_string", -1),
      #(assign, ":reason_is_obvious", 0),
      (assign, ":power_ratio", 0),
      #(assign, ":hours_since_last_recce", -1),
      
      #(assign, ":value_of_target", 0),
      #(assign, ":difficulty_of_capture", 0),
      (store_faction_of_troop, ":faction_no", ":troop_no"),
      
      (try_begin),
        (eq, ":attack_by_faction", 1),
        (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
        (ge, ":faction_marshal", 0), #STEVE ADDITION TO AVOID MESSAGE SPAM
        (troop_get_slot, ":party_no", ":faction_marshal", slot_troop_leaded_party),
      (else_try),
        (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
      (try_end),
      
      (assign, "$g_use_current_ai_object_as_s8", 0),
      
      #THE FIRST BATCH OF DISQUALIFYING CONDITIONS DO NOT REQUIRE THE ATTACKING PARTY TO HAVE CURRENT INTELLIGENCE ON THE TARGET
      (try_begin),
        (neg|party_is_active, ":party_no"),
        
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_party_not_active"),
        #(assign, ":reason_is_obvious", 1),
      (else_try),
        (store_faction_of_party, ":potential_target_faction", ":potential_target"),
        (store_relation, ":relation", ":potential_target_faction", ":faction_no"),
        (ge, ":relation", 0),
        
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_is_friendly"),
        #(assign, ":reason_is_obvious", 1),
      (else_try),
        (is_between, ":potential_target", walled_centers_begin, walled_centers_end),
        (assign, ":faction_of_besieger_party", -1),
        (try_begin),
          (neg|party_slot_eq, ":potential_target", slot_center_is_besieged_by, -1),
          (party_get_slot, ":besieger_party", ":potential_target", slot_center_is_besieged_by),
          (party_is_active, ":besieger_party"),
          (store_faction_of_party, ":faction_of_besieger_party", ":besieger_party"),
        (try_end),
        
        (neq, ":faction_of_besieger_party", -1),
        (neq, ":faction_of_besieger_party", ":faction_no"),
        
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_is_already_besieged"),
        #(assign, ":reason_is_obvious", 1),
      (else_try),
        (is_between, ":potential_target", villages_begin, villages_end),
        (assign, ":village_is_looted_or_raided_already", 0),
        (try_begin),
          (party_slot_eq, ":potential_target", slot_village_state, svs_being_raided),
          (party_get_slot, ":raider_party", ":potential_target", slot_village_raided_by),
          (party_is_active, ":raider_party"),
          (store_faction_of_party, ":raider_faction", ":raider_party"),
          (neq, ":raider_faction", ":faction_no"),
          (assign, ":raiding_by_one_other_faction", 1),
        (else_try),
          (assign, ":raiding_by_one_other_faction", 0),
        (try_end),
        
        (try_begin),
          (this_or_next|party_slot_eq, ":potential_target", slot_village_state, svs_looted),
          (eq, ":raiding_by_one_other_faction", 1),
          (assign, ":village_is_looted_or_raided_already", 1),
        (try_end),
        
        (eq, ":village_is_looted_or_raided_already", 1),
        
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_is_looted_or_raided_already"),
        #(assign, ":reason_is_obvious", 1),
      (else_try),
        (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_upstanding),
        (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_goodnatured),
        (troop_slot_eq, ":troop_no", slot_troop_religion, 1),#only christian lords
        (is_between, ":potential_target", villages_begin, villages_end),
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_marshal_does_not_want_to_attack_innocents"),
      (else_try),
        (assign, ":distance_from_our_closest_walled_center", 1000),
        (try_for_range, ":cur_center", walled_centers_begin, walled_centers_end),
          (store_faction_of_party, ":cur_center_faction", ":cur_center"),
          (eq, ":cur_center_faction", ":faction_no"),
          (store_distance_to_party_from_party, ":distance_from_cur_center", ":cur_center", ":potential_target"),
          (lt, ":distance_from_cur_center", ":distance_from_our_closest_walled_center"),
          (assign, ":distance_from_our_closest_walled_center", ":distance_from_cur_center"),
        (try_end),
        #JuJu70 - modified to account for Denmark and Norway
        #	  (try_begin),
        (gt, ":faction_no", "fac_kingdom_2"),
        (gt, ":distance_from_our_closest_walled_center", 75),
        (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_upstanding),
        (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_debauched),
        (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_goodnatured),
        (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_cunning),
        
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_far_away_our_cautious_marshal_does_not_wish_to_reconnoiter"),
        #RECONNOITERING BEGINS HERE - VALUE WILL BE TEN OR LESS
      (else_try),
        (gt, ":faction_no", "fac_kingdom_2"),
        (gt, ":distance_from_our_closest_walled_center", 90),
        
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_far_away_even_for_our_aggressive_marshal_to_reconnoiter"),
        #(assign, ":reason_is_obvious", 1),
      (else_try),
        (lt, ":faction_no", "fac_kingdom_3"),
        (gt, ":distance_from_our_closest_walled_center", 250),
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_far_away_even_for_our_aggressive_marshal_to_reconnoiter"),
      (else_try),
        (gt, ":faction_no", "fac_kingdom_2"),
        (is_between, ":potential_target", walled_centers_begin, walled_centers_end),
        (neg|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_martial),
        (neg|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_quarrelsome),
        (neg|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_selfrighteous),
        
        (assign, ":close_center_found", 0),
        (try_for_range, ":friendly_walled_center", walled_centers_begin, walled_centers_end),
          (eq, ":close_center_found", 0),
          (store_faction_of_party, ":friendly_walled_center_faction", ":friendly_walled_center"),
          (eq, ":friendly_walled_center_faction", ":faction_no"),
          (store_distance_to_party_from_party, ":distance_from_walled_center", ":potential_target", ":friendly_walled_center"),
          (lt, ":distance_from_walled_center", 60),
          (assign, ":close_center_found", 1),
        (try_end),
        (eq, ":close_center_found", 0),
        
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_is_indefensible"),
      (else_try),
        # Towns excluded in storyline
        (eq, "$campaign_type", camp_storyline), #only in storyline
        (this_or_next|eq,":potential_target", "p_town_1"),
        (this_or_next|eq,":potential_target", "p_town_4"),
        (this_or_next|eq,":potential_target", "p_town_10"),
        (eq,":potential_target", "p_town_16"),
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_even_aggressive_marshal_believes_center_too_difficult_to_capture"),
        # End of exclusion
        #	  (try_end),
        #(else_try),
        #For now it is removed as Armagan's decision, we can add this option in later patchs. I and Armagan accept it has good potential. But this system needs also
        #scouting quests and scouting AI added together. If we only add this then we limit AI very much, it can attack only very few of centers, this damages
        #variability of game and surprise attacks of AI. Player can predict where AI will attack and he can full garnisons of only this center.
        #We can add asking travellers about how good defended center X by paying 100 denars for example to equalize situations of AI and human player.
        #But these needs much work and detailed AI tests so Armagan decided to skip this for now.
        
        #(store_sub, ":faction_recce_slot", ":faction_no", kingdoms_begin),
        #(val_add, ":faction_recce_slot", slot_center_last_reconnoitered_by_faction_time),
        #(party_get_slot, ":last_recce_time", ":potential_target", ":faction_recce_slot"),
        #(store_current_hours, ":hours_since_last_recce"),
        #(val_sub, ":hours_since_last_recce", ":last_recce_time"),
        
        #(this_or_next|eq, ":last_recce_time", 0),
        #(gt, ":hours_since_last_recce", 96), #Information is presumed to be accurate for four days
        
        #(store_sub, ":150_minus_distance_div_by_10", 150, ":distance_from_party"),
        #(val_div, ":150_minus_distance_div_by_10", 10),
        
        #(assign, ":result", ":150_minus_distance_div_by_10"),
        #(assign, ":explainer_string", "str_center_has_not_been_scouted"),
        #DECISIONS BASED ON ENEMY STRENGTH BEGIN HERE
      (else_try),
        (party_get_slot, ":party_strength", ":party_no", slot_party_cached_strength),
        (party_get_slot, ":follower_strength", ":party_no", slot_party_follower_strength),
        (party_get_slot, ":strength_of_nearby_friend", ":party_no", slot_party_nearby_friend_strength),
        
        (store_add, ":total_strength", ":party_strength", ":follower_strength"),
        (val_add, ":total_strength", ":strength_of_nearby_friend"),
        
        #(party_get_slot, ":potential_target_nearby_enemy_exact_strength", ":potential_target", slot_party_nearby_friend_strength),
        #(assign, ":potential_target_nearby_enemy_strength", ":potential_target_nearby_enemy_exact_strength"),
        (try_begin),
          (is_between, ":potential_target", villages_begin, villages_end),
          (assign, ":enemy_strength", 10),
        (else_try),
          (party_get_slot, ":enemy_strength", ":potential_target", slot_party_cached_strength),
          (party_get_slot, ":enemy_strength_nearby", ":potential_target", slot_party_nearby_friend_strength),
          (val_add, ":enemy_strength", ":enemy_strength_nearby"),
        (try_end),
        (val_max, ":enemy_strength", 1),
        
        (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_martial),
        (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_quarrelsome),
        (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_selfrighteous),
        
        (store_mul, ":power_ratio", ":total_strength", 100),
        (val_div, ":power_ratio", ":enemy_strength"),
        (lt, ":power_ratio", 150),
        
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_protected_by_enemy_army_aggressive"),
      (else_try),
        (ge, ":enemy_strength", ":total_strength"), #if enemy is powerful
        
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_protected_by_enemy_army_cautious"),
      (else_try),
        (store_mul, ":power_ratio", ":total_strength", 100),
        (val_div, ":power_ratio", ":enemy_strength"),
        (lt, ":power_ratio", 185),
        
        (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_upstanding),
        (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_debauched),
        (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_goodnatured),
        (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_cunning),
        
        #equations here
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_cautious_marshal_believes_center_too_difficult_to_capture"),
      (else_try),
        (lt, ":power_ratio", 140), #it was 140
        
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_even_aggressive_marshal_believes_center_too_difficult_to_capture"),
        #To Steve - I moved below two if statement here from upper places, to enable in answering different different answers even
        #if we are close to an unlooted enemy village. For example now it can say "center X" is too far too while our army is
        #looting a village because of its closeness.
      (else_try),
        #if the party has already started the siege
        (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_raiding_village),
        (faction_get_slot, ":current_object", ":faction_no", slot_faction_ai_object),
        (is_between, ":current_object", villages_begin, villages_end),
        (neq, ":potential_target", ":current_object"),
        (party_slot_eq, ":current_object", slot_village_state, svs_under_siege),
        
        (store_current_hours, ":hours_since_siege_began"),
        (party_get_slot, ":hour_that_siege_began", ":current_object", slot_center_siege_begin_hours),
        (val_sub, ":hours_since_siege_began", ":hour_that_siege_began"),
        (gt, ":hours_since_siege_began", 4),
        
        (call_script, "script_npc_decision_checklist_evaluate_enemy_center_for_attack", ":troop_no", ":current_object", ":attack_by_faction", 0),
        (gt, reg0, -1),
        
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_we_have_already_committed_too_much_time_to_our_present_siege_to_move_elsewhere"),
      (else_try),
        #If the party is close to an unlooted village
        (faction_slot_eq, ":faction_no", slot_faction_ai_state, sfai_raiding_village),
        (faction_get_slot, ":current_object", ":faction_no", slot_faction_ai_object),
        (neq, ":potential_target", ":current_object"),
        (is_between, ":current_object", villages_begin, villages_end),
        (store_distance_to_party_from_party, ":distance_to_cur_object", ":party_no", ":current_object"),
        (lt, ":distance_to_cur_object", 10),
        
        (call_script, "script_npc_decision_checklist_evaluate_enemy_center_for_attack", ":troop_no", ":current_object", ":attack_by_faction", 0),
        (gt, reg0, -1),
        
        (assign, "$g_use_current_ai_object_as_s8", 1),
        
        (assign, ":result", -1),
        (assign, ":explainer_string", "str_center_we_are_already_here_we_should_at_least_loot_the_village"),
        #DECISION TO ATTACK IS HERE
        #(else_try),
        #To Steve - I removed below lines, as here decided. We will use pre-function to evaluate assailability scores for centers rather than below lines to make AI
        #selecting better targets. If you want to make some marshals to select not-best options I can add that option into script_calculate_center_assailability_score,
        #for that we can need seed values for each center and for each lord, so we can add these seed values to create variability, clever marshals have seeds with less
        #standard deviation and less values and less-clever marshals have bigger seeds. Then probability of some lords to disagree marshal increases because their seed
        #values will be different from marshal's. If Steve wants it from me to implement I can add this.
        
        #(try_begin),
        #  (is_between, ":potential_target", villages_begin, villages_end),
        #  (party_get_slot, ":score", ":potential_target", slot_town_prosperity),
        #  (val_add, ":score", 50), #average 100
        #(else_try),
        #  (is_between, ":potential_target", castles_begin, castles_end),
        #  (assign, ":score", ":power_ratio"), #ie, at least 140
        #(else_try),
        #  (party_get_slot, ":score", ":potential_target", slot_town_prosperity),
        #  (val_add, ":score", 75),
        #  (val_mul, ":score", ":power_ratio"),
        #  (val_div, ":score", 100), #ie, at least about 200
        #(try_end),
        #
        #(val_sub, ":score", ":distance_from_party"),
        #(lt, ":score", -1),
        
        #(assign, ":result", -1),
        #(assign, ":explainer_string", "str_center_value_outweighed_by_difficulty_of_capture"),
      (else_try),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (eq, ":faction_no", "fac_kingdom_1"),
          (store_faction_of_party, ":potential_target_faction", ":potential_target"),
          (store_relation, ":relation", ":potential_target_faction", ":faction_no"),
          (lt, ":relation", 0),
        (try_end),
        
        (call_script, "script_calculate_center_assailability_score", ":troop_no", ":potential_target", ":all_vassals_included"),
        (assign, ":score", reg0),
        (assign, ":power_ratio", reg1),
        #(assign, ":distance_score", reg2),
        
        (assign, ":result", ":score"),
        
        (try_begin),
          (le, ":power_ratio", 100),
          (try_begin),
            (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_upstanding),
            (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_debauched),
            (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_goodnatured),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_cunning),
            (assign, ":explainer_string", "str_center_cautious_marshal_believes_center_too_difficult_to_capture"),
          (else_try),
            (assign, ":explainer_string", "str_center_even_aggressive_marshal_believes_center_too_difficult_to_capture"),
          (try_end),
        (else_try),
          (le, ":power_ratio", 150),
          
          (try_begin),
            (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_upstanding),
            (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_debauched),
            (this_or_next|troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_goodnatured),
            (troop_slot_eq, ":troop_no", slot_lord_reputation_type, lrep_cunning),
            (assign, ":explainer_string", "str_center_protected_by_enemy_army_cautious"),
          (else_try),
            (assign, ":explainer_string", "str_center_protected_by_enemy_army_aggressive"),
          (try_end),
        (else_try),
          (try_begin),
            (le, ":score", "$g_faction_object_score"),
            (assign, ":explainer_string", "str_center_value_outweighed_by_difficulty_of_capture"),
          (else_try),
            #To Steve, does not this sentence needs to explain why we are not attacking that city?
            #This sentence says it justifies, so why we are not attacking?
            (assign, ":explainer_string", "str_center_value_justifies_the_difficulty_of_capture"),
          (try_end),
        (try_end),
      (try_end),
      
      (assign, reg0, ":result"),
      (assign, reg1, ":explainer_string"),
      (assign, reg2, ":power_ratio"),
  ]),
  
  (
    "npc_decision_checklist_faction_ai_alt", #This is called from within decide_faction_ai, or from
    [
      (store_script_param, ":troop_no", 1),
      
      (store_faction_of_troop, ":faction_no", ":troop_no"),
      
      (str_store_troop_name, s4, ":troop_no"),
      (str_store_faction_name, s33, ":faction_no"),
      (try_begin),
        (eq, "$cheat_mode", 1),
        (display_message, "@{!}DEBUG -- {s4} produces a faction strategy for {s33}"),
      (try_end),
      
      #INFORMATIONS COLLECTING STEP 0: Here we obtain general information about current faction like how much parties that faction has, which lord is the marshal, current ai state and current ai target object
      #(faction_get_slot, ":faction_strength", ":faction_no", slot_faction_number_of_parties),
      (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
      (faction_get_slot, ":current_ai_state", ":faction_no", slot_faction_ai_state),
      (faction_get_slot, ":current_ai_object", ":faction_no", slot_faction_ai_object),
      
      (assign, ":marshal_party", -1),
      (assign, ":marshal_party_strength", 0),
      
      (try_begin),
        (gt, ":faction_marshal", 0),
        (troop_get_slot, ":marshal_party", ":faction_marshal", slot_troop_leaded_party),
        (party_is_active, ":marshal_party"),
        (party_get_slot, ":marshal_party_itself_strength", ":marshal_party", slot_party_cached_strength),
        (party_get_slot, ":marshal_party_follower_strength", ":marshal_party", slot_party_follower_strength),
        (store_add, ":marshal_party_strength", ":marshal_party_itself_strength", ":marshal_party_follower_strength"),
      (try_end),
      
      #INFORMATIONS COLLECTING STEP 1: Here we are learning how much hours past from last offensive situation/feast concluded/current state started
      (store_current_hours, ":hours_since_last_offensive"),
      (faction_get_slot, ":last_offensive_time", ":faction_no", slot_faction_last_offensive_concluded),
      (val_sub, ":hours_since_last_offensive", ":last_offensive_time"),
      
      (store_current_hours, ":hours_since_last_feast_start"),
      (faction_get_slot, ":last_feast_time", ":faction_no", slot_faction_last_feast_start_time),
      (val_sub, ":hours_since_last_feast_start", ":last_feast_time"),
      
      (store_current_hours, ":hours_at_current_state"),
      (faction_get_slot, ":current_state_started", ":faction_no", slot_faction_ai_current_state_started),
      (val_sub, ":hours_at_current_state", ":current_state_started"),
      
      (store_current_hours, ":hours_since_last_faction_rest"),
      (faction_get_slot, ":last_rest_time", ":faction_no", slot_faction_ai_last_rest_time),
      (val_sub, ":hours_since_last_faction_rest", ":last_rest_time"),
      
      (try_begin), #calculating ":last_offensive_time_score", this will be used in #11 and #12
        (ge, ":hours_since_last_offensive", 1080), #more than 45 days (100p)
        (assign, ":last_offensive_time_score", 100),
      (else_try),
        (ge, ":hours_since_last_offensive", 480), #more than 20 days (65p..99p)
        (store_sub, ":last_offensive_time_score", ":hours_since_last_offensive", 480),
        (val_div, ":last_offensive_time_score", 20),
        (val_add, ":last_offensive_time_score", 64),
      (else_try),
        (ge, ":hours_since_last_offensive", 240), #more than 10 days (41p..64p)
        (store_sub, ":last_offensive_time_score", ":hours_since_last_offensive", 240),
        (val_div, ":last_offensive_time_score", 10),
        (val_add, ":last_offensive_time_score", 40),
      (else_try), #less than 10 days (0p..40p)
        (store_div, ":last_offensive_time_score", ":hours_since_last_offensive", 6), #0..40
      (try_end),
      
      #INFORMATION COLLECTING STEP 3: Here we are finding the most threatened center
      (call_script, "script_find_center_to_defend", ":troop_no"),
      (assign, ":most_threatened_center", reg0),
      (assign, ":threat_danger_level", reg1),
      (assign, ":enemy_strength_near_most_threatened_center", reg2), #NOTE! This will be off by as much as 50%
      
      #INFORMATION COLLECTING STEP 4: Here we are finding number of vassals who are already following the marshal, and the assigned vassal ratio of current faction.
      (assign, ":vassals_already_assembled", 0),
      (assign, ":total_vassals", 0),
      (try_for_range, ":lord", active_npcs_begin, active_npcs_end),
        (store_faction_of_troop, ":lord_faction", ":lord"),
        (eq, ":lord_faction", ":faction_no"),
        (troop_get_slot, ":led_party", ":lord", slot_troop_leaded_party),
        (party_is_active, ":led_party"),
        (val_add, ":total_vassals", 1),
        
        (party_slot_eq, ":led_party", slot_party_ai_state, spai_accompanying_army),
        (party_slot_eq, ":led_party", slot_party_ai_object, ":marshal_party"),
        
        (party_is_active, ":marshal_party"),
        (store_distance_to_party_from_party, ":distance_to_marshal", ":led_party", ":marshal_party"),
        (lt, ":distance_to_marshal", 15),
        (val_add, ":vassals_already_assembled", 1),
      (try_end),
      (assign, ":ratio_of_vassals_assembled", -1),
      (try_begin),
        (gt, ":total_vassals", 0),
        (store_mul, ":ratio_of_vassals_assembled", ":vassals_already_assembled", 100),
        (val_div, ":ratio_of_vassals_assembled", ":total_vassals"),
      (try_end),
      
      #50% of vassals means that the campaign hour limit is ten days
      (store_mul, ":campaign_hour_limit", ":ratio_of_vassals_assembled", 3),
      (val_add, ":campaign_hour_limit", 90),
      
      #To Steve - I understand your concern about some marshals will gather army and some will not be able to find any valueable center to attack after gathering,
      #and these marshals will be questioned by other marshals ext. This is ok but if we search for a target without adding all other vassals what if
      #AI cannot find any target for long time because of its low power ratio if enemy cities are equal defended? Do not forget if we do not count other vassals in
      #faction while making target search we can only add marshal army's power and vassals around him. And if there is any threat in our centers even it is smaller,
      #its threat_danger_level will be more than target_value_level if marshal new started gathering for ofensive. Because we only assume marshal and around vassals
      #will join attack. And in our scenarios currently there are less vassals are around him. So power ratio will be low and any small threat will be enought to stop
      #an offensive. Then when players finds out this they periodically will take under siege to enemy's any center and they will be saved from any kind of newly started
      #offensive they will be faced. So we have to calculate both attack levels and select highest one to compare with threat level. Please do not change this part.
      
      (try_begin),
        (ge, ":faction_marshal", 0),
        (ge, ":marshal_party", 0),
        (party_is_active, ":marshal_party"),
        
        (call_script, "script_party_count_fit_for_battle", ":marshal_party"),
        (assign, ":number_of_fit_soldiers_in_marshal_party", reg0),
        (ge, ":number_of_fit_soldiers_in_marshal_party", 40),
        
        (call_script, "script_find_center_to_attack_alt", ":troop_no", 1, 0),
        (assign, ":center_to_attack_all_vassals_included", reg0),
        (assign, ":target_value_level_all_vassals_included", reg1),
        
        (call_script, "script_find_center_to_attack_alt", ":troop_no", 1, 1),
        (assign, ":center_to_attack_only_marshal_and_followers", reg0),
        (assign, ":target_value_level_only_marshal_and_followers", reg1),
      (else_try),
        (assign, ":target_value_level_all_vassals_included", 0),
        (assign, ":target_value_level_only_marshal_and_followers", 0),
        (assign, ":center_to_attack_all_vassals_included", -1),
        (assign, ":center_to_attack_only_marshal_and_followers", -1),
      (try_end),
      
      (try_begin),
        (ge, ":target_value_level_all_vassals_included", ":center_to_attack_only_marshal_and_followers"),
        (assign, ":center_to_attack", ":center_to_attack_all_vassals_included"),
        (assign, ":target_value_level", ":target_value_level_all_vassals_included"),
      (else_try),
        (assign, ":center_to_attack", ":center_to_attack_only_marshal_and_followers"),
        (assign, ":target_value_level", ":target_value_level_only_marshal_and_followers"),
      (try_end),
      
      # (try_begin),	MOTO chief not sure this is needed
      # (eq, ":current_ai_state", sfai_attacking_center),
      # (val_mul, ":target_value_level", 3),
      # (val_div, ":target_value_level", 2),
      # (try_end),
      
      (try_begin),
        (eq, "$cheat_mode", 1),
        (try_begin),
          (is_between, ":center_to_attack", centers_begin, centers_end),
          (str_store_party_name, s4, ":center_to_attack"),
          (display_message, "@{!}Best offensive target {s4} has value level of {reg1}"),
        (else_try),
          (display_message, "@{!}No center found to attack"),
        (try_end),
        
        (try_begin),
          (is_between, ":most_threatened_center", centers_begin, centers_end),
          (str_store_party_name, s4, ":most_threatened_center"),
          (assign, reg1, ":threat_danger_level"),
          (display_message, "@{!}Best threat of {s4} has value level of {reg1}"),
        (else_try),
          (display_message, "@{!}No center found to defend"),
        (try_end),
      (try_end),
      
      (assign, "$g_target_after_gathering", -1),
      
      (store_current_hours, ":hours"),
      (try_begin),
        (ge, ":target_value_level", ":threat_danger_level"),
        (faction_set_slot, ":faction_no", slot_faction_last_safe_hours, ":hours"),
      (try_end),
      (faction_get_slot, ":last_safe_hours", ":faction_no", slot_faction_last_safe_hours),
      (try_begin),
        (eq, ":last_safe_hours", 0),
        (faction_set_slot, ":faction_no", slot_faction_last_safe_hours, ":hours"),
      (try_end),
      (faction_get_slot, ":last_safe_hours", ":faction_no", slot_faction_last_safe_hours),
      (store_sub, ":hours_since_days_defensive_started", ":hours", ":last_safe_hours"),
      (str_store_faction_name, s7, ":faction_no"),
      
      (assign, ":at_peace_with_everyone", 1),
      (try_for_range, ":faction_at_war", kingdoms_begin, kingdoms_end),
        (store_relation, ":relation", ":faction_no", ":faction_at_war"),
        (lt, ":relation", 0),
        (assign, ":at_peace_with_everyone", 0),
      (try_end),
      
      
      #INFORMATIONS ARE COLLECTED, NOW CHECK ALL POSSIBLE ACTIONS AND DECIDE WHAT TO DO	NEXT
      #Player marshal
      (try_begin), # a special case to end long-running feasts
        (eq, ":troop_no", "trp_player"),
        
        (eq, ":current_ai_state", sfai_feast),
        (ge, ":hours_at_current_state", 72),
        
        (assign, ":action", sfai_default),
        (assign, ":object", -1),
        
        #Normally you are not supposed to set permanent values in this state, but this is a special case to end player-called feasts
        (assign, "$player_marshal_ai_state", sfai_default),
        (assign, "$player_marshal_ai_object", -1),
      (else_try), #another special state, to make player-called feasts last for a while when the player is the leader of the faction, but not the marshal
        (eq, "$players_kingdom", "fac_player_supporters_faction"),
        (faction_slot_eq, "$players_kingdom", slot_faction_leader, "trp_player"),
        (neq, ":troop_no", "trp_player"),
        
        (eq, ":current_ai_state", sfai_feast),
        (le, ":hours_at_current_state", 48),
        
        (party_slot_eq, ":current_ai_object", slot_town_lord, "trp_player"),
        (store_faction_of_party, ":current_ai_object_faction", ":current_ai_object"),
        (eq, ":current_ai_object_faction", "$players_kingdom"),
        
        (assign, ":action", sfai_feast),
        (assign, ":object", ":current_ai_object"),
        
        
      (else_try), #this is the main player marshal state
        (eq, ":troop_no", "trp_player"),
        
        (str_clear, s14),
        (assign, ":action", "$player_marshal_ai_state"),
        (assign, ":object", "$player_marshal_ai_object"),
        
        #1-RESTING IF NEEDED
        #If not currently attacking a besieging a center and vassals did not rest for long time, let them rest.
        #If we do not take this part to toppest level, tired vassals already did not accept any order, so that
        #faction cannot do anything already. So first let vassals rest if they need. Thats why it should be toppest.
      (else_try),
        (neq, ":current_ai_state", sfai_default),
        (neq, ":current_ai_state", sfai_feast),
        (party_is_active, ":marshal_party"),
        
        (party_slot_eq, ":marshal_party", slot_party_ai_state, spai_retreating_to_center),
        
        (assign, ":action", sfai_default),
        (assign, ":object", -1),
        (str_store_string, s14, "str_the_enemy_temporarily_has_the_field"),
        
      (else_try),
        (neq, ":current_ai_state", sfai_feast),
        
        (assign, ":currently_besieging", 0),
        (try_begin),
          (eq, ":current_ai_state", sfai_attacking_center),
          (is_between, ":current_ai_object", walled_centers_begin, walled_centers_end),
          (party_get_slot, ":besieger_party", ":current_ai_object", slot_center_is_besieged_by),
          (party_is_active, ":besieger_party"),
          (store_faction_of_party, ":besieger_faction", ":besieger_party"),
          (eq, ":besieger_faction", ":faction_no"),
          (assign, ":currently_besieging", 1),
        (try_end),
        
        (assign, ":currently_defending_center", 0),
        (try_begin),
          (eq, ":current_ai_state", sfai_attacking_enemies_around_center),
          (gt, ":marshal_party", 0),
          (party_is_active, ":marshal_party"),
          
          (assign, ":besieged_center", -1),
          (try_begin),
            (party_slot_eq, ":marshal_party", slot_party_ai_state, spai_holding_center), #if commander is holding a center
            (party_get_slot, ":marshal_object", ":marshal_party", slot_party_ai_object), #get commander's ai object (center they are holding)
            (ge, ":marshal_object", 0), #if commander has an object MOTO chief needed
            (party_get_battle_opponent, ":besieger_enemy", ":marshal_object"), #get this object's battle opponent
            (ge, ":besieger_enemy", 0),
            (assign, ":besieged_center", ":marshal_object"),
          (else_try),
            (party_slot_eq, ":marshal_party", slot_party_ai_state, spai_engaging_army), #if commander is engaging an army
            (party_get_slot, ":marshal_object", ":marshal_party", slot_party_ai_object), #get commander's ai object (army which they engaded)
            # (ge, ":marshal_object", 0), #if commander has an object MOTO chief not needed (see next line)
            (neg|is_between, ":marshal_object", centers_begin, centers_end), #if this object is not a center, so it is a party
            (party_is_active, ":marshal_object"),
            (party_get_battle_opponent, ":besieged_center", ":marshal_object"), #get this object's battle opponent
          (try_end),
          
          (eq, ":besieged_center", ":current_ai_object"),
          (assign, ":currently_defending_center", 1),
        (try_end),
        
        (eq, ":currently_besieging", 0),
        (eq, ":currently_defending_center", 0),
        (ge, ":hours_since_last_faction_rest", 1240),
        
        (assign, ":action", sfai_default),
        (assign, ":object", -1),
        (str_store_string, s14, "str_the_vassals_are_tired_we_let_them_rest_for_some_time"),
        
        #2-DEFENSIVE ACTIONS : GATHERING ARMY FOR DEFENDING
      (else_try),
        (party_is_active, ":marshal_party"),
        (eq, ":at_peace_with_everyone", 0),
        
        (is_between, ":most_threatened_center", centers_begin, centers_end),
        (this_or_next|eq, ":current_ai_state", sfai_default),    #MOTO not going to attack anyway
        (this_or_next|eq, ":current_ai_state", sfai_feast),    #MOTO not going to attack anyway (THIS is the emergency to stop feast)
        (gt, ":threat_danger_level", ":target_value_level"),
        
        (assign, ":continue_gathering", 0),
        (assign, ":start_gathering", 0),
        
        (try_begin),
          (is_between, ":most_threatened_center", villages_begin, villages_end),
          
          (assign, ":continue_gathering", 0),
        (else_try),
          (try_begin),
            (lt, ":hours_since_days_defensive_started", 3),
            (assign, ":multiplier", 150),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 6),
            (assign, ":multiplier", 140),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 9),
            (assign, ":multiplier", 132),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 12),
            (assign, ":multiplier", 124),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 15),
            (assign, ":multiplier", 118),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 18),
            (assign, ":multiplier", 114),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 21),
            (assign, ":multiplier", 110),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 24),
            (assign, ":multiplier", 106),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 27),
            (assign, ":multiplier", 102),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 31),
            (assign, ":multiplier", 98),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 34),
            (assign, ":multiplier", 94),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 37),
            (assign, ":multiplier", 90),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 40),
            (assign, ":multiplier", 86),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 43),
            (assign, ":multiplier", 82),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 46),
            (assign, ":multiplier", 79),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 49),
            (assign, ":multiplier", 76),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 52),
            (assign, ":multiplier", 73),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 56),
            (assign, ":multiplier", 70),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 60),
            (assign, ":multiplier", 68),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 66),
            (assign, ":multiplier", 66),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 72),
            (assign, ":multiplier", 64),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 80),
            (assign, ":multiplier", 62),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 90),
            (assign, ":multiplier", 60),
          (else_try),
            (lt, ":hours_since_days_defensive_started", 100),
            (assign, ":multiplier", 58),
          (else_try),
            (assign, ":multiplier", 56),
          (try_end),
          
          (store_mul, ":enemy_strength_multiplied", ":enemy_strength_near_most_threatened_center", ":multiplier"),
          (val_div, ":enemy_strength_multiplied", 100),
          
          (try_begin),
            (lt, ":marshal_party_strength", ":enemy_strength_multiplied"),
            (assign, ":continue_gathering", 1),
          (try_end),
        (else_try),
          (eq, ":current_ai_state", sfai_attacking_enemies_around_center),
          (neq, ":most_threatened_center", ":current_ai_object"),
          
          (assign, ":marshal_is_already_defending_a_center", 0),
          (try_begin),
            (gt, ":marshal_party", 0),
            (party_is_active, ":marshal_party"),
            
            (assign, ":besieged_center", -1),
            (try_begin),
              (party_slot_eq, ":marshal_party", slot_party_ai_state, spai_holding_center), #if commander is holding a center
              (party_get_slot, ":marshal_object", ":marshal_party", slot_party_ai_object), #get commander's ai object (center they are holding)
              (party_get_battle_opponent, ":besieger_enemy", ":marshal_object"), #get this object's battle opponent
              (ge, ":besieger_enemy", 0),
              (assign, ":besieged_center", ":marshal_object"),
            (else_try),
              (party_slot_eq, ":marshal_party", slot_party_ai_state, spai_engaging_army), #if commander is engaging an army
              (party_get_slot, ":marshal_object", ":marshal_party", slot_party_ai_object), #get commander's ai object (army which they engaded)
              (ge, ":marshal_object", 0), #if commander has an object
              (neg|is_between, ":marshal_object", centers_begin, centers_end), #if this object is not a center, so it is a party
              (party_is_active, ":marshal_object"),
              (party_get_battle_opponent, ":besieged_center", ":marshal_object"), #get this object's battle opponent
            (try_end),
            
            (eq, ":besieged_center", ":current_ai_object"),
            
            (assign, ":marshal_is_already_defending_a_center", 1),
          (try_end),
          
          (eq, ":marshal_is_already_defending_a_center", 0),
          
          (store_mul, ":enemy_strength_multiplied", ":enemy_strength_near_most_threatened_center", 80),
          (val_div, ":enemy_strength_multiplied", 100),
          (lt, ":marshal_party_strength", ":enemy_strength_multiplied"),
          
          (this_or_next|is_between, ":most_threatened_center", walled_centers_begin, walled_centers_end),
          (neq, ":faction_no", "$players_kingdom"),
          
          (assign, ":start_gathering", 1),
        (try_end),
        
        (this_or_next|eq, ":continue_gathering", 1),
        (eq, ":start_gathering", 1),
        
        (assign, ":action", sfai_gathering_army),
        (assign, ":object", -1),
        (str_store_party_name, s21, ":most_threatened_center"),
        (str_store_string, s14, "str_we_should_prepare_to_defend_s21_but_we_should_gather_our_forces_until_we_are_strong_enough_to_engage_them"),
        
        (try_begin),
          (eq, ":faction_no", "$players_kingdom"),
          (assign, "$g_gathering_reason", ":most_threatened_center"),
        (try_end),
        
        #3-DEFENSIVE ACTIONS : RIDE TO BREAK ENEMY SIEGE / DEFEAT ENEMIES NEAR OUR CENTER
      (else_try),
        # (party_is_active, ":marshal_party"),	MOTO chief faction lords should defend even if they have no marshal
        (is_between, ":most_threatened_center", walled_centers_begin, walled_centers_end),
        (this_or_next|eq, ":current_ai_state", sfai_default),    #MOTO not going to attack anyway
        (this_or_next|eq, ":current_ai_state", sfai_feast),    #MOTO not going to attack anyway (THIS is the emergency to stop feast)
        (ge, ":threat_danger_level", ":target_value_level"),
        (party_slot_ge, ":most_threatened_center", slot_center_is_besieged_by, 0),
        
        (assign, ":action", sfai_attacking_enemies_around_center),
        (assign, ":object", ":most_threatened_center"),
        
        (str_store_party_name, s21, ":most_threatened_center"),
        (str_store_string, s14, "str_we_should_ride_to_break_the_siege_of_s21"),
        
        #3b - DEFEAT ENEMIES NEAR CENTER - similar to above, but a different string
      (else_try),
        # (party_is_active, ":marshal_party"),	MOTO chief faction lords should defend even if they have no marshal
        (this_or_next|eq, ":current_ai_state", sfai_default),    #MOTO not going to attack anyway
        (this_or_next|eq, ":current_ai_state", sfai_feast),    #MOTO not going to attack anyway (THIS is the emergency to stop feast)
        (ge, ":threat_danger_level", ":target_value_level"),
        (is_between, ":most_threatened_center", villages_begin, villages_end),
        
        (assign, ":action", sfai_attacking_enemies_around_center),
        (assign, ":object", ":most_threatened_center"),
        (str_store_party_name, s21, ":most_threatened_center"),
        (str_store_string, s14, "str_we_should_ride_to_defeat_the_enemy_gathered_near_s21"),
        
        #4-DEMOBILIZATION
        #Let vassals attend their own business
      (else_try),
        (this_or_next|eq, ":current_ai_state", sfai_gathering_army),
        (this_or_next|eq, ":current_ai_state", sfai_attacking_center),
        (eq, ":current_ai_state", sfai_raiding_village),
        
        (ge, ":hours_since_last_faction_rest", ":campaign_hour_limit"), #Effected by ratio of vassals
        (ge, ":hours_at_current_state", 24),
        
        #Ozan : I am adding some codes here because sometimes armies demobilize during last seconds of an important event like taking a castle, ext.
        (assign, ":there_is_an_important_situation", 0),
        (try_begin), #do not demobilize during taking a castle/town (fighting in the castle)
          (is_between, ":current_ai_object", walled_centers_begin, walled_centers_end),
          (party_get_battle_opponent, ":besieger_party", ":current_ai_object"),
          (party_is_active, ":besieger_party"),
          (store_faction_of_party, ":besieger_faction", ":besieger_party"),
          (this_or_next|eq, ":besieger_faction", ":faction_no"),
          (eq, ":besieger_faction", "fac_player_faction"),
          (assign, ":there_is_an_important_situation", 1),
        (else_try), #do not demobilize during besieging a siege (holding around castle)
          (is_between, ":current_ai_object", walled_centers_begin, walled_centers_end),
          (party_get_slot, ":besieger_party", ":current_ai_object", slot_center_is_besieged_by),
          (party_is_active, ":besieger_party"),
          (store_faction_of_party, ":besieger_faction", ":besieger_party"),
          (this_or_next|eq, ":besieger_faction", ":faction_no"),
          (eq, ":besieger_faction", "fac_player_faction"),
          (assign, ":there_is_an_important_situation", 1),
        (else_try), #do not demobilize during raiding a village (holding around village)
          (is_between, ":current_ai_object", centers_begin, centers_end),
          (neg|is_between, ":current_ai_object", walled_centers_begin, walled_centers_end),
          (party_slot_eq, ":current_ai_object", slot_village_state, svs_being_raided),
          (assign, ":there_is_an_important_situation", 1),
        (try_end),
        
        (eq, ":there_is_an_important_situation", 0),
        #end addition ozan
        
        (assign, reg7, ":hours_since_last_faction_rest"),
        (assign, reg8, ":campaign_hour_limit"),
        
        (str_store_string, s14, "str_this_offensive_needs_to_wind_down_soon_so_the_vassals_can_attend_to_their_own_business"),
        (assign, ":action", sfai_default),
        (assign, ":object", -1),
        
        #6-GATHERING BECAUSE OF NO REASON
        #Start to gather the army
      (else_try),
        (party_is_active, ":marshal_party"),
        (eq, ":at_peace_with_everyone", 0),
        
        
        (eq, ":current_ai_state", sfai_default),
        (ge, ":hours_since_last_offensive", 60),
        (lt, ":hours_since_last_faction_rest", 120),
        
        #There should not be a center as a precondition for attack
        #Otherwise, we are unlikely to have a situation in which the army gathers, but does nothing -- which is important to have for role-playing purposes
        
        (assign, ":action", sfai_gathering_army),
        (assign, ":object", -1),
        (str_store_string, s14, "str_it_is_time_to_go_on_the_offensive_and_we_must_first_assemble_the_army"),
        
        (try_begin),
          (eq, ":faction_no", "$players_kingdom"),
          (assign, "$g_gathering_reason", -1),
        (try_end),
        
        #7-OFFENSIVE ACTIONS : CONTINUE GATHERING
      (else_try),
        (party_is_active, ":marshal_party"),
        (eq, ":current_ai_state", sfai_gathering_army),
        (eq, ":at_peace_with_everyone", 0),
        
        (lt, ":hours_at_current_state", 54), #gather army for 54 hours
        
        (lt, ":ratio_of_vassals_assembled", 12),
        
        (str_store_string, s14, "str_we_must_continue_to_gather_the_army_before_we_ride_forth_on_an_offensive_operation"),
        (assign, ":action", sfai_gathering_army),
        (assign, ":object", -1),
        
        #7-OFFENSIVE ACTIONS PART 2 : CONTINUE GATHERING
      (else_try),
        (assign, ":minimum_possible_attackable_target_value_level", 50),
        (eq, ":at_peace_with_everyone", 0),
        
        (try_begin), #agressive marshal
          (troop_get_slot, ":reputation", ":troop_no", slot_lord_reputation_type),
          (this_or_next|eq, ":reputation", lrep_martial),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_selfrighteous),
          (val_mul, ":minimum_possible_attackable_target_value_level", 9),
          (val_div, ":minimum_possible_attackable_target_value_level", 10),
        (try_end),
        
        (party_is_active, ":marshal_party"),
        (eq, ":current_ai_state", sfai_gathering_army),
        
        (try_begin),
          (lt, ":hours_at_current_state", 6),
          (assign, ":minimum_needed_target_value_level", 1500),
        (else_try),
          (lt, ":hours_at_current_state", 10),
          (assign, ":minimum_needed_target_value_level", 1000),
        (else_try),
          (lt, ":hours_at_current_state", 14),
          (assign, ":minimum_needed_target_value_level", 720),
        (else_try),
          (lt, ":hours_at_current_state", 18),
          (assign, ":minimum_needed_target_value_level", 480),
        (else_try),
          (lt, ":hours_at_current_state", 22),
          (assign, ":minimum_needed_target_value_level", 360),
        (else_try),
          (lt, ":hours_at_current_state", 26),
          (assign, ":minimum_needed_target_value_level", 240),
        (else_try),
          (lt, ":hours_at_current_state", 30),
          (assign, ":minimum_needed_target_value_level", 180),
        (else_try),
          (lt, ":hours_at_current_state", 34),
          (assign, ":minimum_needed_target_value_level", 120),
        (else_try),
          (lt, ":hours_at_current_state", 38),
          (assign, ":minimum_needed_target_value_level", 100),
        (else_try),
          (lt, ":hours_at_current_state", 42),
          (assign, ":minimum_needed_target_value_level", 80),
        (else_try),
          (lt, ":hours_at_current_state", 46),
          (assign, ":minimum_needed_target_value_level", 65),
        (else_try),
          (lt, ":hours_at_current_state", 50),
          (assign, ":minimum_needed_target_value_level", 55),
        (else_try),
          (assign, ":minimum_needed_target_value_level", ":minimum_possible_attackable_target_value_level"),
        (try_end),
        
        (try_begin), #agressive marshal
          (troop_get_slot, ":reputation", ":troop_no", slot_lord_reputation_type),
          (this_or_next|eq, ":reputation", lrep_martial),
          (this_or_next|eq, ":reputation", lrep_quarrelsome),
          (eq, ":reputation", lrep_selfrighteous),
          (val_mul, ":minimum_needed_target_value_level", 9),
          (val_div, ":minimum_needed_target_value_level", 10),
        (try_end),
        
        (le, ":target_value_level", ":minimum_needed_target_value_level"),
        (le, ":hours_at_current_state", 54),
        
        (str_store_string, s14, "str_we_have_assembled_some_vassals"),
        (assign, ":action", sfai_gathering_army),
        (assign, ":object", -1),
        
        #8-ATTACK AN ENEMY CENTER case 1, reconnaissance against walled center
        #(else_try),
        #(party_is_active, ":marshal_party"),
        #(neq, ":current_ai_state", sfai_default),
        #(neq, ":current_ai_state", sfai_feast),
        #(is_between, ":center_to_attack", walled_centers_begin, walled_centers_end),
        
        #(store_sub, ":faction_recce_slot", ":faction_no", kingdoms_begin),
        #(val_add, ":faction_recce_slot", slot_center_last_reconnoitered_by_faction_time),
        #(store_current_hours, ":hours_since_last_recon"),
        #(party_get_slot, ":last_recon_time", ":center_to_attack", ":faction_recce_slot"),
        #(val_sub, ":hours_since_last_recon", ":last_recon_time"),
        #(this_or_next|eq, ":last_recon_time", 0),
        #(gt, ":hours_since_last_recon", 96),
        
        #(assign, ":action", sfai_attacking_center),
        #(assign, ":object", ":center_to_attack"),
        #(str_store_string, s14, "str_we_are_conducting_recce"),
        
        #8-ATTACK AN ENEMY CENTER case 2, reconnaissance against village
        #(else_try),
        #(party_is_active, ":marshal_party"),
        #(neq, ":current_ai_state", sfai_default),
        #(neq, ":current_ai_state", sfai_feast),
        #(is_between, ":center_to_attack", villages_begin, villages_end),
        
        #(store_sub, ":faction_recce_slot", ":faction_no", kingdoms_begin),
        #(val_add, ":faction_recce_slot", slot_center_last_reconnoitered_by_faction_time),
        #(store_current_hours, ":hours_since_last_recon"),
        #(party_get_slot, ":last_recon_time", ":center_to_attack", ":faction_recce_slot"),
        #(val_sub, ":hours_since_last_recon", ":last_recon_time"),
        #(this_or_next|eq, ":last_recon_time", 0),
        #(gt, ":hours_since_last_recon", 96),
        
        
        #(assign, ":action", sfai_raiding_village),
        #(assign, ":object", ":center_to_attack"),
        #(str_store_string, s14, "str_we_are_conducting_recce"),
      (else_try),
        (party_is_active, ":marshal_party"),
        (neq, ":current_ai_state", sfai_default),
        (neq, ":current_ai_state", sfai_feast),
        
        (assign, ":center_to_attack", ":center_to_attack_only_marshal_and_followers"),
        
        (is_between, ":center_to_attack", walled_centers_begin, walled_centers_end),
        
        (ge, ":target_value_level", ":minimum_possible_attackable_target_value_level"),
        
        (assign, ":action", sfai_attacking_center),
        (assign, ":object", ":center_to_attack"),
        (str_store_string, s14, "str_we_believe_the_fortress_will_be_worth_the_effort_to_take_it"),
      (else_try),
        (party_is_active, ":marshal_party"),
        (neq, ":current_ai_state", sfai_default),
        (neq, ":current_ai_state", sfai_feast),
        
        (assign, ":center_to_attack", ":center_to_attack_only_marshal_and_followers"),
        
        (is_between, ":center_to_attack", villages_begin, villages_end),
        
        (ge, ":target_value_level", ":minimum_possible_attackable_target_value_level"),
        
        (assign, ":action", sfai_raiding_village),
        (assign, ":object", ":center_to_attack"),
        (str_store_string, s14, "str_we_shall_leave_a_fiery_trail_through_the_heart_of_the_enemys_lands_targeting_the_wealthy_settlements_if_we_can"),
        
        #9 -- DISBAND THE ARMY
      (else_try),
        (eq, ":current_ai_state", sfai_gathering_army),
        
        (str_store_string, s14, "str_the_army_will_be_disbanded_because_we_have_been_waiting_too_long_without_a_target"),
        
        (assign, ":action", sfai_default),
        (assign, ":object", -1),
        #OFFENSIVE OPERATIONS END
        
        #FEAST-RELATED OPERATIONS BEGIN
        #10-CONCLUDE CURRENT FEAST
      (else_try),
        (eq, ":current_ai_state", sfai_feast),
        (gt, ":hours_at_current_state", 72),
        
        (assign, ":action", sfai_default),
        (assign, ":object", -1),
        (str_store_string, s14, "str_it_is_time_for_the_feast_to_conclude"),
        
        #11-CONTINE FEAST UNLESS THERE IS AN EMERGENCY
      (else_try),
        (eq, ":current_ai_state", sfai_feast),
        (le, ":hours_at_current_state", 72),
        
        (assign, ":action", sfai_feast),
        (assign, ":object", ":current_ai_object"),
        (str_store_string, s14, "str_we_should_continue_the_feast_unless_there_is_an_emergency"),
        
        #12-HOLD A FEAST BECAUSE THE PLAYER WANTS TO ORGANIZE ONE
      (else_try),
        (check_quest_active, "qst_organize_feast"),
        (eq, "$players_kingdom", ":faction_no"),
        (neq,":current_ai_state",sfai_feast),
        (quest_get_slot, ":target_center", "qst_organize_feast", slot_quest_target_center),
        
        (assign, ":action", sfai_feast),
        (assign, ":object", ":target_center"),
        (str_store_string, s14, "str_you_had_wished_to_hold_a_feast"),
        
        #13-HOLD A FEAST BECAUSE FEMALE PLAYER SCHEDULED TO GET MARRIED
      (else_try),
        (check_quest_active, "qst_wed_betrothed_female"),
        
        (quest_get_slot, ":groom", "qst_wed_betrothed_female", slot_quest_giver_troop),
        (troop_slot_eq, ":groom", slot_troop_prisoner_of_party, -1),
        
        (store_faction_of_troop, ":groom_faction", ":groom"),
        (eq, ":groom_faction", ":faction_no"),
        
        (faction_get_slot, ":faction_leader", ":groom_faction", slot_faction_leader),
        
        (assign, ":location_feast", -1),
        (try_for_range, ":possible_location", walled_centers_begin, walled_centers_end),
          (eq, ":location_feast", -1),
          (party_slot_eq, ":possible_location", slot_town_lord, ":groom"),
          (party_slot_ge, ":possible_location", slot_center_is_besieged_by, 0),
          (assign, ":location_feast", ":possible_location"),
        (try_end),
        
        (try_for_range, ":possible_location", walled_centers_begin, walled_centers_end),
          (eq, ":location_feast", -1),
          (party_slot_eq, ":possible_location", slot_town_lord, ":faction_leader"),
          (party_slot_ge, ":possible_location", slot_center_is_besieged_by, 0),
          (assign, ":location_feast", ":possible_location"),
        (try_end),
        
        (is_between, ":location_feast", walled_centers_begin, walled_centers_end),
        
        (assign, ":action", sfai_feast),
        (assign, ":object", ":location_feast"),
        (str_store_string, s14, "str_your_wedding_day_approaches_my_lady"),
        
        #14-HOLD A FEAST BECAUSE A MALE CHARACTER WANTS TO GET MARRIED
      (else_try),
        (check_quest_active, "qst_wed_betrothed"),
        (neg|quest_slot_ge, "qst_wed_betrothed", slot_quest_expiration_days, 362),
        
        (quest_get_slot, ":bride", "qst_wed_betrothed", slot_quest_target_troop),
        (call_script, "script_get_kingdom_lady_social_determinants", ":bride"),
        (assign, ":feast_host", reg0),
        (store_faction_of_troop, ":feast_host_faction", ":feast_host"),
        (eq, ":feast_host_faction", ":faction_no"),
        
        (troop_slot_eq, ":feast_host", slot_troop_prisoner_of_party, -1),
        (assign, ":wedding_venue", reg1),
        
        (is_between, ":wedding_venue", centers_begin, centers_end),
        (party_slot_eq, ":wedding_venue", slot_center_is_besieged_by, -1),
        
        (assign, ":action", sfai_feast),
        (assign, ":object", ":wedding_venue"),
        (str_store_string, s14, "str_your_wedding_day_approaches"),
        
        #15-HOLD A FEAST BECAUSE AN NPC WANTS TO GET MARRIED
      (else_try),
        (ge, ":hours_since_last_feast_start", 192), #If at least eight days past last feast start time
        
        (assign, ":location_feast", -1),
        
        (try_for_range, ":kingdom_lady", kingdom_ladies_begin, kingdom_ladies_end),
          (troop_get_slot, ":groom", ":kingdom_lady", slot_troop_betrothed),
          (gt, ":groom", 0), #not the player
          
          (store_faction_of_troop, ":lady_faction", ":kingdom_lady"),
          (store_faction_of_troop, ":groom_faction", ":groom"),
          
          (try_begin), #The groom checks if he wants to continue or break off relations. This causes actions, rather than just returns a value, so it probably should be moved elsewhere
            (troop_slot_ge, ":groom", slot_troop_prisoner_of_party, 0),
          (else_try),
            (neq, ":groom_faction", ":lady_faction"),
            (neq, ":groom_faction", "fac_player_faction"),
            (call_script, "script_courtship_event_lady_break_relation_with_suitor", ":kingdom_lady", ":groom"),
          (else_try),
            (eq, ":lady_faction", ":faction_no"),
            (store_current_hours, ":hours_since_betrothal"),
            (troop_get_slot, ":betrothal_time", ":kingdom_lady", slot_troop_betrothal_time),
            (val_sub, ":hours_since_betrothal", ":betrothal_time"),
            (ge, ":hours_since_betrothal", 719), #30 days
            
            (call_script, "script_get_kingdom_lady_social_determinants", ":kingdom_lady"),
            (assign, ":wedding_venue", reg1),
            
            (assign, ":location_feast", ":wedding_venue"),
            (assign, ":final_bride", ":kingdom_lady"),
            (assign, ":final_groom", ":groom"),
          (try_end),
        (try_end),
        
        (ge, ":location_feast", centers_begin),
        
        (assign, ":action", sfai_feast),
        (assign, ":object", ":location_feast"),
        
        (str_store_troop_name, s22, ":final_bride"),
        (str_store_troop_name, s23, ":final_groom"),
        (str_store_string, s14, "str_s22_and_s23_wish_to_marry"),
        
        #16-HOLD A FEAST ANYWAY
      (else_try),
        (eq, ":current_ai_state", sfai_default),
        (gt, ":hours_since_last_feast_start", 240), #If at least 10 days past after last feast. (added by ozan)
        
        (assign, ":location_high_score", 0),
        (assign, ":location_feast", -1),
        
        (try_for_range, ":location", walled_centers_begin, walled_centers_end),
          (store_faction_of_party, ":location_faction", ":location"),
          (eq, ":location_faction", ":faction_no"),
          
          (try_begin),
            (neg|party_slot_eq, ":location", slot_village_state, svs_under_siege),
            (party_get_slot, ":location_lord", ":location", slot_town_lord),
            (is_between, ":location_lord", active_npcs_begin, active_npcs_end),
            (troop_get_slot, ":location_score", ":location_lord", slot_troop_renown),
            (store_random_in_range, ":random", 0, 1000), #will probably be king or senior lord
            (val_add, ":location_score", ":random"),
            (gt, ":location_score", ":location_high_score"),
            (assign, ":location_high_score", ":location_score"),
            (assign, ":location_feast", ":location"),
          (else_try), #do not start new feasts if any place is under siege or being raided
            (this_or_next|party_slot_eq, ":location", slot_village_state, svs_under_siege),
            (party_slot_eq, ":location", slot_village_state, svs_being_raided),
            (assign, ":location_high_score", 9999),
            (assign, ":location_feast", -1),
          (try_end),
        (try_end),
        
        (is_between, ":location_feast", walled_centers_begin, walled_centers_end),
        (party_get_slot, ":feast_host", ":location_feast", slot_town_lord),
        (troop_slot_eq, ":feast_host", slot_troop_prisoner_of_party, -1),
        
        (assign, ":action", sfai_feast),
        (assign, ":object", ":location_feast"),
        (str_store_string, s14, "str_it_has_been_a_long_time_since_the_lords_of_the_realm_gathered_for_a_feast"),
        
        #17-DO NOTHING
      (else_try),
        (neq, ":current_ai_state", sfai_default),
        
        (assign, ":action", sfai_default),
        (assign, ":object", -1),
        (str_store_string, s14, "str_the_circumstances_which_led_to_this_decision_no_longer_apply_so_we_should_stop_and_reconsider_shortly"),
        
        #18-DO NOTHING
      (else_try),
        (eq, ":current_ai_state", sfai_default),
        
        (eq, ":at_peace_with_everyone", 1),
        
        (assign, ":action", sfai_default),
        (assign, ":object", -1),
        (str_store_string, s14, "str_we_are_currently_at_peace"),
      (else_try),
        (eq, ":current_ai_state", sfai_default),
        (faction_slot_eq, ":faction_no", slot_faction_marshal, -1),
        (assign, ":action", sfai_default),
        (assign, ":object", -1),
        (str_store_string, s14, "str_we_are_waiting_for_selection_of_marshal"),
        
      (else_try),
        (eq, ":current_ai_state", sfai_default),
        
        (assign, ":action", sfai_default),
        (assign, ":object", -1),
        (str_store_string, s14, "str_the_vassals_still_need_time_to_attend_to_their_own_business"),
      (try_end),
      
      (assign, reg0, ":action"),
      (assign, reg1, ":object"),
  ]),
  
  # (
    # "faction_last_reconnoitered_center", #This is called from within decide_faction_ai, or from
    # [
      # (store_script_param, ":faction_no", 1),
      # (store_script_param, ":center_no", 2),
      
      # (store_sub, ":faction_recce_slot", ":faction_no", kingdoms_begin),
      # (val_add, ":faction_recce_slot", slot_center_last_reconnoitered_by_faction_time),
      # (store_current_hours, ":hours_since_last_recon"),
      # (party_get_slot, ":last_recon_time", ":center_no", ":faction_recce_slot"),
      
      # (try_begin),
        # (lt, ":last_recon_time", 1),
        # (assign, ":hours_since_last_recon", 1000),
      # (else_try),
        # (val_sub, ":hours_since_last_recon", ":last_recon_time"),
      # (try_end),
      
      # (assign, reg0, ":hours_since_last_recon"),
      # (assign, reg1, ":last_recon_time"),
  # ]),
  
  # (
    # "reduce_exact_number_to_estimate",
    # #This is used to simulate limited intelligence
    # #It is roughly analogous to the descriptive strings which the player will receive from alarms
    # #Information is presumed to be accurate for four days
    # #This is obviously cheating for the AI, as the AI will have exact info for four days, and no info at all after that.
    # #It would be fairly easy to log the strength at a center when it is scouted, if we want, but I have not done that at this point,
    # #The AI also has a hive mind -- ie, each party knows what its allies are thinking. In this, AI factions have an advantage over the player
    # #It would be a simple matter to create a set of arrays in which each party's knowledge is individually updated, but that would also take up a lot of data space
    
    # [
      # (store_script_param, ":exact_number", 1),
      
      # (try_begin),
        # (lt, ":exact_number", 500),
        # (assign, ":estimate", 0),
      # (else_try),
        # (lt, ":exact_number", 1000),
        # (assign, ":estimate", 750),
      # (else_try),
        # (lt, ":exact_number", 2000),
        # (assign, ":estimate", 1500),
      # (else_try),
        # (lt, ":exact_number", 4000),
        # (assign, ":estimate", 3000),
      # (else_try),
        # (lt, ":exact_number", 8000),
        # (assign, ":estimate", 6000),
      # (else_try),
        # (lt, ":exact_number", 16000),
        # (assign, ":estimate", 12000),
      # (else_try),
        # (assign, ":estimate", 24000),
      # (try_end),
      
      # (assign, reg0, ":estimate"),
  # ]),
  
  #script_calculate_castle_prosperities_by_using_its_villages
  #input: castle id
  (
    "calculate_castle_prosperities_by_using_its_villages", #This is called from within decide_faction_ai, or from
    [
      (store_script_param_1, ":cur_castle"),
      (store_script_param_2, ":is_init"),
      
      (party_get_slot, ":old_prosperity", ":cur_castle", slot_town_prosperity),
      (assign, ":total_villages", 0),
      
      # (try_for_range, ":cur_castle", castles_begin, castles_end),
      # (party_set_slot, ":cur_castle", slot_party_temp_slot_1, 0),
      (party_set_slot, ":cur_castle", slot_town_prosperity, 0),
      # (try_end),
      
      (try_for_range, ":cur_village", villages_begin, villages_end),
        (party_get_slot, ":bound_center", ":cur_village", slot_village_bound_center),
        (eq, ":bound_center", ":cur_castle"),
        
        # (party_get_slot, reg0, ":bound_center", slot_party_temp_slot_1),
        # (val_add, reg0, 1),
        # (party_set_slot, ":bound_center", slot_party_temp_slot_1, reg0),
        (val_add, ":total_villages", 1),
        
        (party_get_slot, ":village_prosperity", ":cur_village", slot_town_prosperity),
        (party_get_slot, reg0, ":bound_center", slot_town_prosperity),
        (val_add, reg0, ":village_prosperity"),
        (party_set_slot, ":bound_center", slot_town_prosperity, reg0),
      (try_end),
      
      # (try_for_range, ":cur_castle", castles_begin, castles_end),
      # (party_get_slot, ":total_villages", ":cur_castle", slot_party_temp_slot_1),
      
      (try_begin),
        (eq, ":total_villages", 0),
        
        (try_begin),
          (eq, ":is_init", 1),
          (assign, ":old_prosperity", 50),
        (else_try),
          (gt, ":old_prosperity", 50),
          (val_sub, ":old_prosperity", 1),
        (else_try),
          (lt, ":old_prosperity", 50),
          (val_add, ":old_prosperity", 1),
        (try_end),
        
        (party_set_slot, ":cur_castle", slot_town_prosperity, ":old_prosperity"),
        
      (else_try),
        (party_get_slot, ":castle_prosperity", ":cur_castle", slot_town_prosperity),
        (val_div, ":castle_prosperity", ":total_villages"),
        
        (try_begin),
          (eq, ":is_init", 1),
          (assign, ":old_prosperity", ":castle_prosperity"),
        (else_try),
          (gt, ":old_prosperity", ":castle_prosperity"),
          (val_sub, ":old_prosperity", 1),
        (else_try),
          (lt, ":old_prosperity", ":castle_prosperity"),
          (val_add, ":old_prosperity", 1),
        (try_end),
        
        (party_set_slot, ":cur_castle", slot_town_prosperity, ":old_prosperity"),
      (try_end),
      # Brewery
      (try_begin),
        (party_slot_eq, ":cur_castle", slot_center_has_brewery, 1),
        (party_get_slot, ":prosperity", ":cur_castle", slot_town_prosperity),
        (store_random_in_range, ":rand", 0,100),
        (lt, ":rand", 50),
        (store_random_in_range, ":boost", 0,2),
        (gt, ":boost", 0),
        (val_add, ":prosperity", ":boost"),
        (party_set_slot, ":cur_castle", slot_town_prosperity, ":prosperity"),
        (party_slot_eq, ":cur_castle", slot_town_lord, "trp_player"),
        (str_store_party_name_link, s1, ":cur_castle"),
        (display_message, "@The brewery in {s1} adds to its prosperity."),
      (try_end),
      # (try_end),
  ]),
  
  #script_initialize_tavern_variables
  (
    "initialize_tavern_variables",
    [
      (assign, "$g_main_attacker_agent", 0),
      (assign, "$g_attacker_drawn_weapon", 0),
      (assign, "$g_start_belligerent_drunk_fight", 0),
      (assign, "$g_start_hired_assassin_fight", 0),
      (assign, "$g_belligerent_drunk_leaving", 0),
  ]),
  
  #script_prepare_town_to_fight
  (
    "prepare_town_to_fight",
    [
      (str_store_party_name_link, s9, "$g_starting_town"),
      (str_store_string, s2, "str_save_town_from_bandits"),
      (call_script, "script_start_quest", "qst_save_town_from_bandits", "$g_talk_troop"),
      
      (assign, "$g_mt_mode", tcm_default),
      (store_faction_of_party, ":town_faction", "$current_town"),
      (faction_get_slot, ":tier_2_troop", ":town_faction", slot_faction_tier_3_troop),
      (faction_get_slot, ":tier_3_troop", ":town_faction", slot_faction_tier_3_troop),
      (faction_get_slot, ":tier_4_troop", ":town_faction", slot_faction_tier_4_troop),
      
      (party_get_slot, ":town_scene", "$current_town", slot_town_center),
      (modify_visitors_at_site, ":town_scene"),
      (reset_visitors),
      
      #people spawned at #32, #33, #34, #35, #36, #37, #38 and #39 are town walkers.
      (try_begin),
        #(eq, "$town_nighttime", 0),
        (try_for_range, ":walker_no", 0, num_town_walkers),
          (store_add, ":troop_slot", slot_center_walker_0_troop, ":walker_no"),
          (party_get_slot, ":walker_troop_id", "$current_town", ":troop_slot"),
          (gt, ":walker_troop_id", 0),
          (store_add, ":entry_no", town_walker_entries_start, ":walker_no"),
          (set_visitor, ":entry_no", ":walker_troop_id"),
        (try_end),
      (try_end),
      
      #guards will be spawned at #25, #26 and #27
      (set_visitors, 25, ":tier_2_troop", 1),
      (set_visitors, 26, ":tier_3_troop", 1),
      (set_visitors, 27, ":tier_4_troop", 1),
      
      (set_visitors, 10, "trp_looter", 1),
      (set_visitors, 11, "trp_bandit", 1),
      (set_visitors, 12, "trp_looter", 1),
      
      (store_faction_of_party, ":starting_town_faction", "$g_starting_town"),
      (try_begin),
        (eq, ":starting_town_faction", "fac_kingdom_1"),
        (assign, ":troop_of_merchant", "trp_swadian_merchant"),
        #(assign, ":troop_of_bandit", "trp_forest_bandit"),
      (else_try),
        (eq, ":starting_town_faction", "fac_kingdom_2"),
        (assign, ":troop_of_merchant", "trp_vaegir_merchant"),
        #(assign, ":troop_of_bandit", "trp_mountain_bandit"),
      (else_try),
        (eq, ":starting_town_faction", "fac_kingdom_3"),
        (assign, ":troop_of_merchant", "trp_khergit_merchant"),
        #(assign, ":troop_of_bandit", "trp_steppe_bandit"),
      (else_try),
        (eq, ":starting_town_faction", "fac_kingdom_4"),
        (assign, ":troop_of_merchant", "trp_nord_merchant"),
        #(assign, ":troop_of_bandit", "trp_sea_raider"),
      (else_try),
        (eq, ":starting_town_faction", "fac_kingdom_5"),
        (assign, ":troop_of_merchant", "trp_rhodok_merchant"),
        #(assign, ":troop_of_bandit", "trp_mountain_bandit"),
      (else_try),
        #  (eq, ":starting_town_faction", "fac_kingdom_6"),
        (assign, ":troop_of_merchant", "trp_sarranid_merchant"),
        #(assign, ":troop_of_bandit", "trp_desert_bandit"),
      (try_end),
      (str_store_troop_name, s10, ":troop_of_merchant"),
      
      (set_visitors, 24, "trp_looter", 1),
      (set_visitors, 2, "trp_looter", 2),
      (set_visitors, 4, "trp_looter", 1),
      (set_visitors, 5, "trp_looter", 2),
      (set_visitors, 6, "trp_looter", 1),
      (set_visitors, 7, "trp_looter", 1),
      
      (set_visitors, 3, ":troop_of_merchant", 1),
      
      (set_jump_mission,"mt_town_fight"),
      (jump_to_scene, ":town_scene"),
      (change_screen_mission),
  ]),
  
  (
    "change_player_right_to_rule",
    [
      (store_script_param_1, ":right_to_rule_dif"),
      (val_add, "$player_right_to_rule", ":right_to_rule_dif"),
      (val_clamp, "$player_right_to_rule", 0, 100),
      (try_begin),
        (gt, ":right_to_rule_dif", 0),
        (display_message, "@You gain right to rule."),
      (else_try),
        (lt, ":right_to_rule_dif", 0),
        (display_message, "@You lose right to rule."),
      (try_end),
  ]),
  
  ("indict_lord_for_treason",#originally included in simple_triggers. Needed to be moved here to allow player to indict
    [
      (store_script_param, ":troop_no", 1),
      (store_script_param, ":faction", 2),
      
      (try_begin),
        (eq, ":faction", "fac_player_supporters_faction"),
        (troop_set_slot, ":troop_no", slot_troop_player_banned, 1),
      (try_end),
      
      (troop_get_type, reg4, ":troop_no"),
      (val_mod, reg4, 2),
      
      (try_for_range, ":center", centers_begin, centers_end), #transfer properties to liege
        (party_slot_eq, ":center", slot_town_lord, ":troop_no"),
        (party_set_slot, ":center", slot_town_lord, stl_unassigned),
      (try_end),
      
      (faction_get_slot, ":faction_leader", ":faction", slot_faction_leader),
      (call_script, "script_troop_get_relation_with_troop", ":troop_no", ":faction_leader"),
      (assign, ":liege_to_lord_relation", reg0),
      (store_sub, ":base_relation_modifier", -150, ":liege_to_lord_relation"),
      (val_div, ":base_relation_modifier", 40),#-1 at -100, -2 at -70, -3 at -30,etc.
      (val_min, ":base_relation_modifier", -1),
      
      #Indictments, cont: Influence relations
      (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end), #this effects all lords in all factions
        (store_faction_of_troop, ":active_npc_faction", ":active_npc"),
        (eq, ":faction", ":active_npc_faction"),
        
        (call_script, "script_troop_get_family_relation_to_troop", ":troop_no", ":active_npc"),
        (assign, ":family_relation", reg0),
        
        (assign, ":relation_modifier", ":base_relation_modifier"),
        (try_begin),
          (gt, ":family_relation", 1),
          (store_div, ":family_multiplier", reg0, 3),
          (val_sub, ":relation_modifier", ":family_multiplier"),
        (try_end),
        
        (lt, ":relation_modifier", 0),
        
        (call_script, "script_troop_change_relation_with_troop", ":faction_leader", ":active_npc", ":relation_modifier"),
        (val_add, "$total_indictment_changes", ":relation_modifier"),
        (try_begin),
          (eq, "$cheat_mode", 1),
          (str_store_troop_name, s17, ":active_npc"),
          (str_store_troop_name, s18, ":faction_leader"),
          
          (assign, reg3, ":relation_modifier"),
          (display_message, "str_trial_influences_s17s_relation_with_s18_by_reg3"),
        (try_end),
      (try_end),
      
      #Indictments, cont: Check for other factions
      (try_begin),
        (eq, ":troop_no", "trp_player"),
        (assign, ":new_faction", 0), #kicked out of faction
        (call_script, "script_player_leave_faction", 1),
        
      (else_try),
        (call_script, "script_lord_find_alternative_faction", ":troop_no", 1),
        
        (is_between, reg0, kingdoms_begin, kingdoms_end),
        (assign, ":new_faction", reg0),
        
        (try_begin),
          (ge, "$cheat_mode", 1),
          (str_store_troop_name, s4, ":troop_no"),
          (display_message, "@{!}DEBUG - {s4} faction changed in indictment"),
        (try_end),
        
        (call_script, "script_change_troop_faction", ":troop_no", ":new_faction"),
        
        (try_begin), #new-begin
          (neq, ":new_faction", "fac_player_supporters_faction"),
          (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_inactive),
          (troop_set_slot, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
        (try_end), #new-end
        
        (str_store_faction_name, s10, ":new_faction"),
        (str_store_string, s11, "str_with_the_s10"),
        
      (else_try),
        (assign, ":new_faction", "fac_outlaws"),
        (call_script, "script_change_troop_faction", ":troop_no", "fac_outlaws"),
        (str_store_string, s11, "str_outside_calradia"),
      (try_end),
      
      #Indictments, cont: Set up string
      (try_begin),
        (eq, ":troop_no", "trp_player"),
        (str_store_string, s9, "str_you_have_been_indicted_for_treason_to_s7_your_properties_have_been_confiscated_and_you_would_be_well_advised_to_flee_for_your_life"),
      (else_try),
        (str_store_troop_name, s4, ":troop_no"),
        (str_store_faction_name, s5, ":faction"),
        (str_store_troop_name, s6, ":faction_leader"),
        
        (troop_get_type, reg4, ":troop_no"),
        (val_mod, reg4, 2),
        (str_store_string, s9, "str_by_order_of_s6_s4_of_the_s5_has_been_indicted_for_treason_the_lord_has_been_stripped_of_all_reg4herhis_properties_and_has_fled_for_reg4herhis_life_he_is_rumored_to_have_gone_into_exile_s11"),
      (try_end),
      (display_message, "@{!}{s9}"),
      
      #Indictments, cont: Remove party
      (try_begin),
        (troop_get_slot, ":led_party", ":troop_no", slot_troop_leaded_party),
        (party_is_active, ":led_party"),
        (neq, ":led_party", "p_main_party"),
        (remove_party, ":led_party"),
        (troop_set_slot, ":troop_no", slot_troop_leaded_party, -1),
      (try_end),
      
      (try_begin),
        (eq, ":troop_no", "trp_player"),
        (call_script, "script_add_notification_menu", "mnu_notification_treason_indictment", ":troop_no", ":faction"),
        
      (else_try),
        (eq, "$cheat_mode", 1),
        (this_or_next|eq, ":faction", "$players_kingdom"),
        (eq, ":new_faction", "$players_kingdom"),
        (call_script, "script_add_notification_menu", "mnu_notification_treason_indictment", ":troop_no", ":faction"),
      (try_end),
  ]),
  
  
  # script_give_center_to_faction_while_maintaining_lord
  # Input: arg1 = center_no, arg2 = faction
  ("give_center_to_faction_while_maintaining_lord",
    [
      (store_script_param_1, ":center_no"),
      (store_script_param_2, ":faction_no"),
      
      (store_faction_of_party, ":old_faction", ":center_no"),
      (party_set_slot, ":center_no", slot_center_ex_faction, ":old_faction"),
      (party_set_faction, ":center_no", ":faction_no"),
      
      (try_begin),
        (party_slot_eq, ":center_no", slot_party_type, spt_village),
        
        # (party_get_slot, ":farmer_party", ":center_no", slot_village_farmer_party),
        # (try_begin),
          # (gt, ":farmer_party", 0),
          # (party_is_active, ":farmer_party"),
          # (party_set_faction, ":farmer_party", ":faction_no"),
        # (try_end),
        
        # (try_begin),
          (party_slot_eq, ":center_no", slot_village_state, svs_being_raided),
          (call_script, "script_village_set_state", ":center_no", svs_normal),
        # (try_end),
      (try_end),
      
      #update all attached parties VC-3829
      (try_for_parties, ":cur_party"),
        (party_get_slot, ":cur_center", ":cur_party", slot_party_home_center),
        (eq, ":cur_center", ":center_no"),
        (party_set_faction, ":cur_party", ":faction_no"),
      (try_end),
      
      (call_script, "script_update_faction_notes", ":faction_no"),
      (call_script, "script_update_faction_notes", ":old_faction"),
      (call_script, "script_update_center_notes", ":center_no"),
      
      (party_get_slot, ":current_lord", ":center_no", slot_town_lord),
      (try_for_range, ":other_center", centers_begin, centers_end),
        (party_slot_eq, ":other_center", slot_village_bound_center, ":center_no"),
        (call_script, "script_give_center_to_faction_while_maintaining_lord", ":other_center", ":faction_no"),
        
        (party_get_slot, ":old_town_lord", ":other_center", slot_town_lord),
        (try_begin),
          (neq, ":old_town_lord", ":current_lord"),
          (party_set_slot, ":other_center", slot_town_lord, stl_unassigned),
          (party_set_banner_icon, ":center_no", 0), #Removing banner
          
          (ge, ":old_town_lord", 0),
          (call_script, "script_update_troop_notes", ":old_town_lord"),
        (try_end),
      (try_end),
  ]),
  
  # script_check_concilio_calradi_achievement
  ("check_concilio_calradi_achievement",
    [
      (try_begin),
        (eq, "$players_kingdom", "fac_player_supporters_faction"),
        (faction_get_slot, ":player_faction_king", "fac_player_supporters_faction", slot_faction_leader),
        (eq, ":player_faction_king", "trp_player"),
        (assign, ":number_of_vassals", 0),
        (try_for_range, ":cur_troop", active_npcs_begin, active_npcs_end),
          (troop_slot_eq, ":cur_troop", slot_troop_occupation, slto_kingdom_hero),
          (store_faction_of_troop, ":cur_faction", ":cur_troop"),
          (eq, ":cur_faction", "fac_player_supporters_faction"),
          (val_add, ":number_of_vassals", 1),
        (try_end),
        (ge, ":number_of_vassals", 3),
        (unlock_achievement, ACHIEVEMENT_CONCILIO_CALRADI),
      (try_end),
  ]),
  
  # script_refresh_center_inventories
  ("refresh_center_inventories",
    [
      (set_merchandise_modifier_quality,150),
      (reset_item_probabilities,100),
      
      # Add trade goods to merchant inventories
      (try_for_range,":cur_center",towns_begin, towns_end),
        (party_get_slot,":cur_merchant",":cur_center",slot_town_merchant),
        (reset_item_probabilities,100),
        (assign, ":total_production", 0),
        (try_for_range, ":cur_goods", trade_goods_begin, trade_goods_end),
          (call_script, "script_center_get_production", ":cur_center", ":cur_goods"),
          (assign, ":cur_production", reg0),
          
          (try_for_range, ":cur_village", villages_begin, villages_end),
            (party_slot_eq, ":cur_village", slot_village_bound_center, ":cur_center"),
            (call_script, "script_center_get_production", ":cur_village", ":cur_goods"),
            (val_div, reg0, 3),
            (val_add, ":cur_production", reg0),
          (try_end),
          
          (val_max, ":cur_production", 1),
          (val_mul, ":cur_production", 4),
          
          (val_add, ":total_production", ":cur_production"),
        (try_end),
        
        (party_get_slot, ":town_prosperity", ":cur_center", slot_town_prosperity),
        (assign, ":number_of_items_in_town", 25),
        
        (try_begin), #1.0x - 2.0x (50 - 100 prosperity)
          (ge, ":town_prosperity", 50),
          (store_sub, ":ratio", ":town_prosperity", 50),
          (val_mul, ":ratio", 2),
          (val_add, ":ratio", 100),
          (val_mul, ":number_of_items_in_town", ":ratio"),
          (val_div, ":number_of_items_in_town", 100),
        (else_try), #0.5x - 1.0x (0 - 50 prosperity)
          (store_sub, ":ratio", ":town_prosperity", 50),
          (val_add, ":ratio", 100),
          (val_mul, ":number_of_items_in_town", ":ratio"),
          (val_div, ":number_of_items_in_town", 100),
        (try_end),
        
        (val_clamp, ":number_of_items_in_town", 10, 40),
        
        (try_begin),
          (is_between, ":cur_center", castles_begin, castles_end),
          (val_div, ":number_of_items_in_town", 2),
        (try_end),
        
        (try_for_range, ":cur_goods", trade_goods_begin, trade_goods_end),
          (call_script, "script_center_get_production", ":cur_center", ":cur_goods"),
          (assign, ":cur_production", reg0),
          
          (try_for_range, ":cur_village", villages_begin, villages_end),
            (party_slot_eq, ":cur_village", slot_village_bound_center, ":cur_center"),
            (call_script, "script_center_get_production", ":cur_village", ":cur_goods"),
            (val_div, reg0, 3),
            (val_add, ":cur_production", reg0),
          (try_end),
          
          (val_max, ":cur_production", 1),
          (val_mul, ":cur_production", 4),
          
          (val_mul, ":cur_production", ":number_of_items_in_town"),
          (val_mul, ":cur_production", 100),
          (val_div, ":cur_production", ":total_production"),
          (set_item_probability_in_merchandise, ":cur_goods", ":cur_production"),
        (try_end),
        
        (troop_clear_inventory, ":cur_merchant"),
        (troop_add_merchandise, ":cur_merchant", itp_type_goods, ":number_of_items_in_town"),
        
        (troop_ensure_inventory_space, ":cur_merchant", 20),
        (troop_sort_inventory, ":cur_merchant"),
        (store_troop_gold, ":cur_gold",":cur_merchant"),
        (lt,":cur_gold",1500),
        (store_random_in_range,":new_gold",1000,4000),
        (call_script, "script_troop_add_gold", ":cur_merchant", ":new_gold"),
      (try_end),
  ]),
  
  # script_refresh_center_armories
  ("refresh_center_armories",
    [
      (reset_item_probabilities, 100),
      (set_merchandise_modifier_quality, 150),
      (try_for_range, ":cur_merchant", armor_merchants_begin, armor_merchants_end),
        (try_begin),
          (neq, ":cur_merchant", "trp_town_30_armorer"),
          (store_sub, ":cur_town", ":cur_merchant", armor_merchants_begin),
          (val_add, ":cur_town", towns_begin),
          (party_get_slot, ":cur_faction", ":cur_town", slot_center_original_faction),
          
        #player lair: find an NPC kingdom of the same culture
        (else_try),
          (faction_get_slot, ":cur_culture", "fac_player_faction",  slot_faction_culture),
          (assign, ":cur_faction", npc_kingdoms_begin),
          (try_for_range_backwards, ":kingdom", npc_kingdoms_begin, npc_kingdoms_end),
            (faction_slot_eq, ":kingdom",  slot_faction_culture, ":cur_culture"),
            (assign, ":cur_faction", ":kingdom"),
          (try_end),
        (try_end),
        
        (troop_clear_inventory, ":cur_merchant"),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_body_armor, 16),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_head_armor, 16),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_foot_armor, 8),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_hand_armor, 4),
        (troop_ensure_inventory_space, ":cur_merchant", merchant_inventory_space),
        (troop_sort_inventory, ":cur_merchant"),
        (store_troop_gold, reg6, ":cur_merchant"),
        (lt, reg6, 1000),
        (store_random_in_range, ":new_gold", 1000,4000),
        (call_script, "script_troop_add_gold", ":cur_merchant", ":new_gold"),
      (end_try),
      (troop_clear_inventory, "trp_town_30_armorer"),
      (troop_add_merchandise_with_faction, "trp_town_30_armorer", ":cur_faction", itp_type_body_armor, 16),
      (troop_add_merchandise_with_faction, "trp_town_30_armorer", ":cur_faction", itp_type_head_armor, 16),
      (troop_add_merchandise_with_faction, "trp_town_30_armorer", ":cur_faction", itp_type_foot_armor, 8),
      (troop_add_merchandise_with_faction, "trp_town_30_armorer", ":cur_faction", itp_type_hand_armor, 4),
      (troop_ensure_inventory_space, "trp_town_30_armorer", merchant_inventory_space),
      (troop_sort_inventory, "trp_town_30_armorer"),
      (store_troop_gold, reg6, "trp_town_30_armorer"),
      (try_begin),
        (lt, reg6, 1000),
        (store_random_in_range, ":new_gold", 1000,2500),
        (call_script, "script_troop_add_gold", "trp_town_30_armorer", ":new_gold"),
      (try_end),
  ]),
  
  # script_refresh_center_weaponsmiths
  ("refresh_center_weaponsmiths",
    [
      (reset_item_probabilities, 100),
      (set_merchandise_modifier_quality, 150),
      (try_for_range, ":cur_merchant", weapon_merchants_begin, weapon_merchants_end),
        (try_begin),
          (neq, ":cur_merchant", "trp_town_30_weaponsmith"),
          (store_sub, ":cur_town", ":cur_merchant", weapon_merchants_begin),
          (val_add, ":cur_town", towns_begin),
          (party_get_slot, ":cur_faction", ":cur_town", slot_center_original_faction),
          
        #player lair: find an NPC kingdom of the same culture
        (else_try),
          (faction_get_slot, ":cur_culture", "fac_player_faction",  slot_faction_culture),
          (assign, ":cur_faction", npc_kingdoms_begin),
          (try_for_range_backwards, ":kingdom", npc_kingdoms_begin, npc_kingdoms_end),
            (faction_slot_eq, ":kingdom",  slot_faction_culture, ":cur_culture"),
            (assign, ":cur_faction", ":kingdom"),
          (try_end),
        (try_end),
        
        (troop_clear_inventory, ":cur_merchant"),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_one_handed_wpn, 5),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_two_handed_wpn, 5),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_polearm, 5),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_shield, 6),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_bow, 4),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_crossbow, 3),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_thrown, 5),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_arrows, 2),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_bolts, 2),
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_pistol,1), #chief sling
        (troop_add_merchandise_with_faction, ":cur_merchant", ":cur_faction", itp_type_bullets,1), #chief sling rocks
        (troop_ensure_inventory_space, ":cur_merchant", merchant_inventory_space),
        (troop_sort_inventory, ":cur_merchant"),
        (store_troop_gold, reg6, ":cur_merchant"),
        (lt, reg6, 1000),
        (store_random_in_range, ":new_gold", 1000,4000),
        (call_script, "script_troop_add_gold", ":cur_merchant", ":new_gold"),
      (try_end),
      (troop_clear_inventory, "trp_town_30_weaponsmith"),
      (troop_add_merchandise, "trp_town_30_weaponsmith",itp_type_one_handed_wpn, 5),
      (troop_add_merchandise, "trp_town_30_weaponsmith",itp_type_two_handed_wpn, 2),
      (troop_add_merchandise, "trp_town_30_weaponsmith",itp_type_polearm, 6),
      (troop_add_merchandise, "trp_town_30_weaponsmith",itp_type_shield, 2),
      (troop_add_merchandise, "trp_town_30_weaponsmith",itp_type_bow, 3),
      (troop_add_merchandise, "trp_town_30_weaponsmith",itp_type_arrows, 2),
      (troop_add_merchandise, "trp_town_30_weaponsmith",itp_type_pistol, 2),
      (troop_add_merchandise, "trp_town_30_weaponsmith",itp_type_thrown, 5),
      (troop_add_merchandise, "trp_town_30_weaponsmith",itp_type_bullets, 1),
      (troop_sort_inventory, "trp_town_30_weaponsmith"),
      (store_troop_gold, reg6, "trp_town_30_weaponsmith"),
      (try_begin),
        (lt, reg6, 1000),
        (store_random_in_range, ":new_gold", 1000,2500),
        (call_script, "script_troop_add_gold", "trp_town_30_weaponsmith", ":new_gold"),
      (try_end),
  ]),
  
  # script_refresh_center_stables
  ("refresh_center_stables",
    [
      (reset_item_probabilities, 100),
      (set_merchandise_modifier_quality, 150),
      (try_for_range, ":cur_merchant", horse_merchants_begin, horse_merchants_end),
        (troop_clear_inventory, ":cur_merchant"),
        (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, ":cur_gold", ":cur_merchant"),
        (lt, ":cur_gold", 600),
        (store_random_in_range, ":new_gold", 1000,4000),
        (call_script, "script_troop_add_gold", ":cur_merchant", ":new_gold"),
      (try_end),
  ]),
]




vc_scripts = [
  
  # script_update_savegame_to_content_update
  # This script is updating old savegames to the contentupdate (VC-2279)
  ("update_savegame_to_content_update",
    [(try_begin),
        # CONDITIONS
        (neq, "$game_started_with_content_update", 1),
        (neq, "$game_updated_to_content_update", 1),
        
        # DEBUG LOG
        (str_clear, s1),
        (troop_set_plural_name, "trp_pseudo_troop_01", s1),#economy
        (troop_set_plural_name, "trp_pseudo_troop_02", s1),#player army
        (troop_set_plural_name, "trp_pseudo_troop_03", s1),#bandits
        (store_random_in_range, "$savegame_id", 1, 999999),
        (assign,"$num_selled_goods_in_week", 0),
        (assign, "$selled_goods_in_week_for", 0),
        (assign, "$num_selled_items_in_week", 0),
        (assign, "$selled_items_in_week_for", 0),
        (assign, "$num_buyed_goods_in_week", 0),
        (assign, "$buyed_goods_in_week_for", 0),
        (assign, "$num_buyed_items_in_week", 0),
        (assign, "$buyed_items_in_week_for", 0),
        (assign, "$g_sgfix", 0),
        (assign, "$arboleda_conrey", 0),
        (assign, "$continue_storyline", 0),
        
        # For VC-1881
        (call_script, "script_initialize_item_info"),
        (call_script, "script_initialize_economic_information"),
        #VC-2674
        (try_for_range, ":troop_no", active_npcs_begin, active_npcs_end),
          (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_inactive),
          (troop_get_slot, ":prisoner_of_party", ":troop_no", slot_troop_prisoner_of_party),
          (gt,":prisoner_of_party", -1),
          (store_faction_of_troop, ":troop_faction", ":troop_no"),
          (try_begin),
            (eq, ":prisoner_of_party", "p_main_party"),
            (party_remove_prisoners, "p_main_party", ":troop_no", 1),
            (remove_troops_from_prisoners,  ":troop_no", 1),
            (troop_set_slot, ":troop_no", slot_troop_prisoner_of_party, -1),
          (else_try),
            (is_between, ":prisoner_of_party", walled_centers_begin, walled_centers_end),
            (store_faction_of_party, ":prison_faction", ":prisoner_of_party"),
            (eq, ":prison_faction", ":troop_faction"),
            (party_remove_prisoners, ":prisoner_of_party", ":troop_no",1),
            (remove_troops_from_prisoners,  ":troop_no", 1),
            (troop_set_slot, ":troop_no", slot_troop_prisoner_of_party, -1),
            (call_script, "script_remove_troop_from_prison", ":troop_no"),
          (try_end),
        (try_end),
        #End VC-2674
        # VC-2353
        (try_for_parties, ":party"),
          (party_get_template_id, ":party_template", ":party"),
          (eq, ":party_template", "pt_routed_warriors"),
          (party_set_flags, ":party", pf_auto_remove_in_town, 1),
          # (party_is_in_any_town, ":party"),
          #(remove_party, ":party"),
        (try_end),
        # Initialize npcs for messangers
        (try_begin),
          (try_for_range, ":npc", active_npcs_begin, active_npcs_end),
            (troop_set_slot, ":npc", slot_troop_mission_object, -1),
            (troop_set_slot, ":npc", slot_troop_message_sent, 0),
          (try_end),
        (try_end),
        # Resetting 0 foodstores
        (try_for_range, ":town", walled_centers_begin, walled_centers_end),
          (party_get_slot, ":store", ":town", slot_party_food_store),
          (lt, ":store", 400),
          (party_slot_eq, ":town", slot_center_is_besieged_by, -1),
          (party_slot_eq,":town",slot_center_blockaded,0),
          (party_set_slot, ":town", slot_party_food_store,500),
        (try_end),
        # Fixing negative rents
        (try_for_range, ":center", centers_begin, centers_end),
          (party_get_slot, ":rents", ":center", slot_center_accumulated_rents),
          (lt, ":rents", 0),
          (party_set_slot, ":center", slot_center_accumulated_rents, 0),
        (try_end),
        # adding jetty system for old save games (jettys are the ports for
        # islands without port town)
        (try_begin),
          (party_slot_eq, "p_jetty_1", slot_party_port_party, 0),
          (call_script, "script_add_jetty_system"),
        (try_end),
        
        #updating village elders tunics
        (try_for_range, ":troop", "trp_village_1_elder", "trp_monje_mercader"),
          (troop_get_inventory_slot,":cur_item1",":troop",ek_body),
          
          (neg | is_between, ":cur_item1", "itm_long_tunic1", "itm_btunic_1"),
          (neg | is_between, ":cur_item1", "itm_btunic_3", "itm_hoodtunic_08"),
          (neg | is_between, ":cur_item1", "itm_bl_tunic02", "itm_briton_tunic1"),
          (neg | is_between, ":cur_item1", "itm_briton_tunic3", "itm_picts_tunic_09"),
          (neg | is_between, ":cur_item1", "itm_picts_hoodtunic_03", "itm_britontunic_1woman"),
          (try_begin),
            (neq, ":cur_item1", -1),
            (troop_remove_item, ":troop", ":cur_item1"),
          (try_end),
          (store_random_in_range, ":tunic", "itm_btunic_15", "itm_hoodtunic_08"),
          (troop_add_item, ":troop", ":tunic", 0),
          (troop_set_auto_equip, ":troop", 1),
          (troop_equip_items, ":troop"),
          #(display_message,"@Updating dresses for npcs.", 0xFF0000),
        (try_end),
        (try_for_range, ":troop", "trp_ransom_broker_1", "trp_quastuosa_1"),
          (troop_get_inventory_slot,":cur_item1",":troop",ek_body),
          (neg | is_between, ":cur_item1", "itm_long_tunic1", "itm_btunic_1"),
          (neg | is_between, ":cur_item1", "itm_btunic_3", "itm_hoodtunic_08"),
          (neg | is_between, ":cur_item1", "itm_bl_tunic02", "itm_briton_tunic1"),
          (neg | is_between, ":cur_item1", "itm_briton_tunic3", "itm_picts_tunic_09"),
          (neg | is_between, ":cur_item1", "itm_picts_hoodtunic_03", "itm_britontunic_1woman"),
          (try_begin),
          (neq, ":cur_item1", -1),				(troop_remove_item, ":troop", ":cur_item1"),			(try_end),
          (store_random_in_range, ":tunic", "itm_btunic_15", "itm_hoodtunic_08"),
          (troop_add_item, ":troop", ":tunic", 0),
          (troop_set_auto_equip, ":troop", 1),
          (troop_equip_items, ":troop"),
          #(display_message,"@Updating dresses for npcs.", 0xFF0000),
        (try_end),
        #VC-2781
        (assign, ":tries", 1),
        (try_for_range, ":unused", 0, ":tries"),
          (store_character_level, ":level", "trp_sword_sister"),
          (lt, ":level", 26),
          (add_xp_to_troop, 50000, "trp_sword_sister"),
          (val_add, ":tries", 1),
        (try_end),
        ###updating training skill
        ####    #player
        ##	       (try_begin),
        ##                  (store_skill_level, ":training_skill", "skl_trainer",
        ##                  "trp_player"),
        ##                    (gt, ":training_skill", 5),
        ##                  (call_script, "script_player_set_skill_level",
        ##                  "skl_trainer", 5),
        ##	      (try_end),
        ##   #companions
        ##      (try_for_range, ":troop_no", companions_begin, companions_end),
        ##             (store_skill_level, ":training_skillc", "skl_trainer",
        ##             ":troop_no"),
        ##             (gt, ":training_skillc", 5),
        ##             (assign, ":new_skill", 5),
        ##             (val_sub, ":new_skill", ":training_skillc"),
        ##             (troop_raise_skill, ":troop_no","skl_trainer",":new_skill"),
        ##	(try_end),
        ##
        ##    ####
        #Specials
        (assign, "$special1", 0),
        (try_for_range, ":lady",active_npcs_begin, kingdom_ladies_end),
          (troop_set_slot, ":lady", slot_troop_temp, 0),
        (try_end),
        
        # VC-1280 Caer Dyf ferry fixing for old save games
        (set_fixed_point_multiplier, 100),
        (party_get_slot, ":cur_port", "p_ferry_4a", slot_party_port_party),
        (position_set_x, pos1, -5871),
        (position_set_y, pos1, -16927),
        (party_set_position, ":cur_port", pos1),
        (party_get_slot, ":cur_port", "p_ferry_4b", slot_party_port_party),
        (position_set_x, pos1, -5370),
        (position_set_y, pos1, -16727),
        (party_set_position, ":cur_port", pos1),
        
        # VC-2362
        (set_fixed_point_multiplier, 100),
        (position_set_x, pos1, -1600),
        (position_set_y, pos1,  7800),
        (party_set_position, "p_firth_clyde_spawn_point", pos1),
        
        # VC-2377 SET GOAL presentation
        (quest_set_slot, "qst_vc_menu", slot_set_goal_type, goal_empty),
        (assign, "$goal_type", goal_empty),
        (assign, "$set_goal_new_game_trigger", 0),
        (quest_set_slot, "qst_vc_menu", slot_set_goal_completed_raider, goal_not_complete),
        (quest_set_slot, "qst_vc_menu", slot_set_goal_completed_warrior, goal_not_complete),
        (quest_set_slot, "qst_vc_menu", slot_set_goal_completed_lord, goal_not_complete),
        (quest_set_slot, "qst_vc_menu", slot_set_goal_completed_king, goal_not_complete),
        (quest_set_slot, "qst_vc_menu", slot_set_goal_completed_expand, goal_not_complete),
        
        # Fixing slot_party_on_water of some parties
        # (try_for_parties, ":party_no"),
        # (party_get_template_id, ":party_template", ":party_no"),
        # (this_or_next|is_between, ":party_template", "pt_steppe_bandit_lair",
        # "pt_bandit_lair_templates_end"),
        # (is_between, ":party_no", spawn_points_begin, spawn_points_end),
        # (party_get_current_terrain, ":terrain_type", ":party_no"),
        # (str_store_party_name, s1, ":party_no"),
        # (try_begin),
        # (this_or_next|eq, ":terrain_type", rt_water),
        # (this_or_next|eq, ":terrain_type", rt_river),
        # (eq, ":terrain_type", rt_bridge),
        # (neg|party_slot_eq, ":party_no", slot_party_on_water, 1),
        # (party_set_slot, ":party_no", slot_party_on_water, 1),
        # # (display_message, "@{!}INFO: I had to correct slot_party_on_water
        # to 1 for {s1}"),
        # (else_try),
        # (neq, ":terrain_type", rt_water),
        # (neq, ":terrain_type", rt_river),
        # (neq, ":terrain_type", rt_bridge),
        # (neg|party_slot_eq, ":party_no", slot_party_on_water, 0),
        # (party_set_slot, ":party_no", slot_party_on_water, 0),
        # # (display_message, "@{!}INFO: I had to correct slot_party_on_water
        # to 0 for {s1}"),
        # (end_try),
        # (end_try),
        
        #variables
        (assign, "$g_spouse_embarazada", 0), #childs
        (assign,"$g_nino_varon",0),
        (assign,"$g_nina_chica",0),
        # END
        (assign, "$game_updated_to_content_update", 1),
        (display_message, "@Content update applied to the save game."),
      (end_try),]),
  
  # script_agent_perform_warcry
  # Component: Agent action in mission (edit of dedals shield_taunt_trigger)
  ("agent_perform_warcry",
    [(store_script_param, ":agent", 1),
      (try_begin),
        (agent_get_troop_id, ":troop_id", ":agent"),
        (troop_get_type, ":is_female", ":troop_id"),
        (val_mod, ":is_female", 2),
        (eq, ":is_female", 1),
        (agent_play_sound,":agent","snd_woman_yell"),
      (else_try),
        (agent_play_sound,":agent","snd_man_victory"),
      (try_end),
      (try_begin),
        (agent_get_horse,":horse",":agent"),
        (eq,":horse",-1),
        (agent_set_animation, ":agent", "anim_cheer"),
      (try_end),]),
  
  # script_agent_perform_horn
  # Component: Agent action in mission
  ("agent_perform_horn",
    [(store_script_param, ":agent", 1),
      (try_begin),
        (agent_is_alive, ":agent"),
        (agent_is_active, ":agent"),
        (agent_has_item_equipped, ":agent", "itm_horn"),
        (agent_set_wielded_item, ":agent", "itm_horn"),
        (agent_play_sound,":agent","snd_horn"),
        (agent_get_horse,":horse",":agent"),
        (try_begin),
          (agent_is_active,":horse"),
          (agent_set_animation,":agent","anim_horn",1),
        (else_try),
          (agent_set_animation,":agent","anim_horn",0),
        (try_end),
      (try_end),]),
  
  # script_agent_perform_shield_taunt
  # Component: Agent action in mission(edit of dedals shield_taunt_trigger)
  ("agent_perform_shield_taunt",
    [(store_script_param, ":agent", 1),
      (try_begin),
        (agent_is_active,":agent"),
        (agent_is_alive,":agent"),
        (agent_get_wielded_item,":item",":agent",1),
        (gt,":item",0),#has shield
        (agent_get_wielded_item,":item",":agent",0),
        (gt,":item",0),#has weapon
        (item_get_type,":item_type",":item"),
        (this_or_next | eq,":item_type",itp_type_polearm),
        (eq,":item_type",itp_type_one_handed_wpn),
        (agent_get_horse,":horse",":agent"),
        (eq,":horse",-1),#not on horse
        (agent_set_animation, ":agent", "anim_shield_taunt", 1),
        (agent_play_sound, ":agent", "snd_shield_taunt"),
      (try_end),]),
  
  # script_berserkermode_fortroop_on
  ("berserkermode_fortroop_on", #enter in berserker mode
    [(store_script_param, ":agent", 1),
      (try_begin),
        (agent_is_active,":agent"),
        (agent_is_alive,":agent"),
        (agent_get_horse,":horse",":agent"),
        (eq,":horse",-1),#not on horse
        (agent_set_animation, ":agent", "anim_berserker_trance", 1),
        (agent_play_sound,":agent","snd_berserker_trance"),###WARCRY SOUND
        (agent_set_damage_modifier,":agent",125),
        (call_script, "script_advanced_agent_set_speed_modifier",":agent",110),
        (agent_set_accuracy_modifier,":agent",50),
        (agent_set_reload_speed_modifier,":agent",60),
        (agent_set_slot,":agent",slot_agent_berserk_modeon,1),
        (agent_set_slot,":agent",slot_agent_berserk_cooldown,60),
        (agent_get_troop_id, ":berserker", ":agent"),
        
        (get_player_agent_no, ":player_agent"),
        (agent_get_team, ":team", ":player_agent"),
        (agent_get_team, ":team2", ":agent"),
        (try_begin),
          (eq,":team",":team2"),
          (str_clear, s1),
          (str_store_troop_name, s1, ":berserker"),
          (display_message,"@{s1} has entered trance!",0x000000),
        (try_end),
      (try_end),]),
  
  # script_berserk_cooldown_modeai_tigger
  ("berserk_cooldown_modeai_tigger", [(store_script_param, ":agent", 1),
      (agent_get_slot,":cooldown",":agent",slot_agent_berserk_cooldown),
      (val_max,":cooldown",0),
      (try_begin),
        (gt,":cooldown",0),
        (val_sub,":cooldown",10),
        (agent_set_slot,":agent",slot_agent_berserk_cooldown,":cooldown"),
        (agent_play_sound,":agent","snd_berserker_trance"),###WARCRY SOUND
        #   (agent_set_animation, ":agent", "anim_berserker_trance", 1),
        
        (try_begin), #actions
          (le,":cooldown",55),#take initiative
          (ge,":cooldown",45),#take initiative
          #negative consequences, could attack to
          #everybody
          (store_random_in_range, ":rand", 1, 100),
          (try_begin),
            (ge, ":rand", 96),
            (neg | party_slot_eq, "p_main_party", slot_party_on_water, 1), #no charge in sea battle or coastal assault
            (store_current_scene,":current_scene"),(neq,":current_scene","scn_morrigan_lair"),
            (get_player_agent_no, ":player_agent"),
            (agent_get_team, ":team", ":player_agent"),
            (agent_get_team, ":team2", ":agent"),
            (agent_get_troop_id, ":berserker", ":agent"),
            (try_begin),
              (eq,":team",":team2"),
              (str_clear, s1),
              (str_store_troop_name, s1, ":berserker"),
              (display_message,"@{s1} has become uncontrollable!",0x000000),
            (try_end),
            
            (agent_set_team, ":agent", 6), #team 6 non used in battles
            #   (agent_set_division, ":agent", 2),
            (team_set_relation, 6, 1, -1),  #Teams
            (team_set_relation, 6, 2, -1),
            (team_set_relation, 6, 3, -1),
            (team_set_relation, 6, 4, -1),
            (team_set_relation, 6, 5, -1),
            (team_set_relation, 6, 7, -1),
            
            (agent_ai_set_aggressiveness, ":agent", 199),
            (agent_force_rethink, ":agent"),
            (team_give_order, 6, grc_everyone, mordr_charge),
            ##		            (else_try),
            ##                                (ge, ":rand", 15),
            ##                                  (neg|party_slot_eq, "p_main_party",
            ##                                  slot_party_on_water, 1), #no charge in sea
            ##                                  battle or coastal assault
            ##                                 # (agent_set_division, ":agent", 8),
            ##                        	 (get_player_agent_no, ":player_agent"),
            ##		                 (agent_get_team, ":team", ":player_agent"),
            ##                                 (agent_get_team, ":agenteam", ":agent"),
            ##		                 (agent_get_troop_id, ":berserker", ":agent"),
            ##		                    (try_begin),
            ##			               (eq,":team",":agenteam"),
            ##                                       (str_clear, s1),
            ##		                       (str_store_troop_name, s1, ":berserker"),
            ##			               (display_message,"@{s1} has charged alone!",0x000000),
            ##                                     (try_end),
            ##
            ##		                    (try_begin),
            ##                                        (eq,":team",0),
            ##                                        (agent_set_team, ":agent", 3), #
            ##		                    (else_try),
            ##                                        (eq,":team",1),
            ##                                        (agent_set_team, ":agent", 2), #
            ##		                    (else_try),
            ##                                        (agent_set_team, ":agent", 3), #
            ##                                     (try_end),
            ##
            ##                                  (agent_get_team, ":agenteam", ":agent"),
            ##          				#(team_set_relation, 6, ":team", 1), #Teams
            ##                                  (agent_ai_set_aggressiveness, ":agent",
            ##                                  199),
            ##				  (set_show_messages, 0),
            ##                                        (agent_force_rethink, ":agent"),
            ##                                  (team_give_order, ":agenteam",
            ##                                  grc_everyone, mordr_charge),
            ##				  (set_show_messages, 1),
          (else_try),
            (agent_ai_set_aggressiveness, ":agent", 199),
          (try_end),
        (try_end),
        (try_begin),
          (le,":cooldown",25),#is tired
          (ge,":cooldown",15),#is tired
          (agent_set_damage_modifier,":agent",60),
          (call_script, "script_advanced_agent_set_speed_modifier",":agent",70),
          (agent_get_troop_id, ":berserker", ":agent"),
          
          (get_player_agent_no, ":player_agent"),
          (agent_get_team, ":team", ":player_agent"),
          (agent_get_team, ":team2", ":agent"),
          (try_begin),
            (eq,":team",":team2"),
            (str_clear, s1),
            (str_store_troop_name, s1, ":berserker"),
            (display_message,"@{s1} is getting exhausted after the rage.",0xff3333),
          (try_end),
        (try_end),
      (else_try),
        (eq,":cooldown",0),#back to normal
        (agent_set_damage_modifier,":agent",100),
        (call_script, "script_advanced_agent_set_speed_modifier",":agent",100),
        (agent_set_accuracy_modifier,":agent",100),
        (agent_set_reload_speed_modifier,":agent",100),
        (agent_set_slot,":agent",slot_agent_berserk_modeon,0), #berserker off
        (agent_get_troop_id, ":berserker", ":agent"),
        
        (get_player_agent_no, ":player_agent"),
        (agent_get_team, ":team", ":player_agent"),
        (agent_get_team, ":team2", ":agent"),
        (try_begin),
          (eq,":team",":team2"),
          (str_clear, s1),
          (str_store_troop_name, s1, ":berserker"),
          (display_message,"@{s1} back to normal!",0xff3333),
        (try_end),
        
      (try_end),]),
  
  # script_cf_is_hide_party
  # Component: Helper (story)
  ("cf_is_hide_party",
    [(store_script_param, ":party_no", 1),
      
      (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
      (party_is_active, ":party_no"),
      (party_stack_get_troop_id, ":party_leader", ":party_no", 0),
      (troop_is_hero, ":party_leader"),
      
      (this_or_next | eq, ":party_leader", "trp_kingdom_5_lord"),
      (this_or_next | eq, ":party_leader", "trp_kingdom_8_lord"),
      (this_or_next | is_between, ":party_leader", "trp_knight_5_1", "trp_knight_5_6"),
      (this_or_next | eq, ":party_leader", "trp_knight_8_4"),
      (this_or_next | eq, ":party_leader", "trp_knight_8_7"),
      (is_between, ":party_leader", "trp_knight_8_11", "trp_knight_9_1"),	#(knight_8_15	#trp_kingdom_8
  ]),
  
  # script_calculate_season_light
  # Component: Seasons
  ("calculate_season_light",
    [(set_fixed_point_multiplier, 100),
      (position_get_x, reg1, pos1),	# r
      (position_get_y, reg2, pos1), # g
      (position_get_z, reg3, pos1),	# b
      #(display_message, "@DISTANCE: r:{reg1}, g:{reg2}, b:{reg3}"),
      
      (try_begin),
        (eq, "$shader_season", shader_spring),
        (val_mul, reg1, 100),
        (val_mul, reg2, 107),
        (val_mul, reg3, 100),
      (else_try),
        (eq, "$shader_season", shader_summer),
        (val_mul, reg1, 145),
        (val_mul, reg2, 145),
        (val_mul, reg3, 130),
      (else_try),
        (eq, "$shader_season", shader_autumn),
        (val_mul, reg1, 85),
        (val_mul, reg2, 70),
        (val_mul, reg3, 70),
      (else_try),
        (eq, "$shader_season", shader_winter),
        (val_mul, reg1, 40),
        (val_mul, reg2, 40),
        (val_mul, reg3, 55),
      (else_try),
        (display_message, "@{!}ERROR in script_calculate_season_light", color_bad_news),
      (end_try),
      (val_div, reg1, 100),
      (val_div, reg2, 100),
      (val_div, reg3, 100),
      #(display_message, "@NEW DIST: r:{reg1}, g:{reg2}, b:{reg3}"),
  ]),
  
  # script_give_village_to_center
  # Component: Helper
  ("give_village_to_center",
    [(store_script_param, ":village", 1),
      (store_script_param, ":center", 2),
      (party_set_slot, ":village", slot_village_bound_center, ":center"),
      (store_faction_of_party, ":town_faction", ":center"),
      (call_script, "script_give_center_to_faction_aux", ":village", ":town_faction"),]),
  
  # script_cf_agent_is_in_parrying_animation
  # Component: Weapon break
  ("cf_agent_is_in_parrying_animation",
    [(store_script_param, ":agent", 1),
      (agent_get_animation, ":animation", ":agent", 1),
      
      (this_or_next | is_between, ":animation", "anim_defend_forward_staff_parry_1",	"anim_defend_up_staff"),
      (this_or_next | is_between, ":animation", "anim_defend_up_staff_parry_1",		"anim_defend_right_staff"),
      (this_or_next | is_between, ":animation", "anim_defend_right_staff_parry_1",	"anim_defend_left_staff"),
      (this_or_next | is_between, ":animation", "anim_defend_left_staff_parry_1",		"anim_strike_head_left"),
      (this_or_next | is_between, ":animation", "anim_defend_forward_greatsword_parry_1", "anim_defend_up_twohanded"),
      (this_or_next | is_between, ":animation", "anim_defend_up_twohanded_parry_1",	"anim_defend_right_twohanded"),
      (this_or_next | is_between, ":animation", "anim_defend_right_twohanded_parry_1","anim_defend_left_twohanded"),
      (this_or_next | is_between, ":animation", "anim_defend_left_twohanded_parry_1", "anim_defend_forward_onehanded"),
      (this_or_next | is_between, ":animation", "anim_defend_forward_onehanded_parry_1", "anim_defend_up_onehanded"),
      (this_or_next | is_between, ":animation", "anim_defend_up_onehanded_parry_1",	"anim_defend_right_onehanded"),
      (this_or_next | is_between, ":animation", "anim_defend_right_onehanded_parry_1","anim_defend_left_onehanded"),
      (is_between, ":animation", "anim_defend_left_onehanded_parry_1",	"anim_defend_forward_staff"),]),
  
  # script_change_party_icon_loot_state
  # Component: Map icons
  ("change_party_icon_loot_state",
    [(store_script_param, ":party", 1),
      (store_script_param, ":looted", 2),
      (party_get_icon, ":icon", ":party"),
      
      (try_begin),
        (eq, ":looted", 1),
        (is_between, ":icon", "icon_town_port", "icon_town_port_burnt"),
        #( is_between, ":icon", "icon_town_port_snow", "icon_town_port_snow_burnt"),
        ## because we have no dedicated snow icons anymore
        (val_add, ":icon", 6),
      (else_try),
        (eq, ":looted", 0),
        (is_between, ":icon", "icon_town_port_burnt", "icon_town_port_snow"),
        #( is_between, ":icon", "icon_town_port_snow_burnt",
        #"icon_village_deserted_a"), # because we have no dedicated snow icons
        #anymore
        (val_add, ":icon", -6),
      (end_try),
      (party_set_icon, ":party", ":icon"),]),
  
  # script_spawn_chimney_smoke
  # Component: Map icons
  ("spawn_chimney_smoke",
    [(store_script_param, ":town", 1),
      (store_script_param, ":x", 2),
      (store_script_param, ":y", 3),
      (party_get_icon, ":icon", ":town"),
      (try_begin),
        (this_or_next | eq, ":icon", "icon_town_port"),
        (eq, ":icon", "icon_fort1"),
        #(store_mul, ":icon_size_multiplier", settlement_scale_reduced, 100),
        (assign, ":icon_size_multiplier", 100),
      (else_try),
        #(store_mul, ":icon_size_multiplier", settlement_scale, 100),
        (assign, ":icon_size_multiplier", 133),
      (end_try),
      (spawn_around_party, ":town", "pt_chimney_smoke"),
      (assign, ":curr_smoke", reg0),
      (copy_position, pos1, pos2),
      (store_mul, ":value", ":x", ":icon_size_multiplier"),
      (val_div, ":value", 100),
      (position_move_x, pos1, ":value"),
      (store_mul, ":value", ":y", ":icon_size_multiplier"),
      (val_div, ":value", 100),
      (position_move_y, pos1, ":value"),
      (party_set_position, ":curr_smoke", pos1),]),
  
  # script_refill_animal_parties
  # Component: Hunting
  ("refill_animal_parties",
    [(store_num_parties_of_template, reg22, "$pin_party_template"),
      (try_begin),
        (lt,reg22, "$pin_limit"),
        (store_random_in_range, ":village", villages_begin, villages_end),
        (set_spawn_radius, 7),
        (spawn_around_party, ":village", "$pin_party_template"),
        (assign, ":new_party", reg0),
        (party_set_slot, ":new_party", slot_party_home_center, ":village"),
        (party_set_slot, ":new_party", slot_party_ai_state, spai_patrolling_around_center),
        (party_set_ai_patrol_radius, ":new_party", 2),
        (party_set_slot, ":new_party", slot_party_ai_object, ":village"),
        (party_set_ai_behavior, ":new_party", ai_bhvr_patrol_location),
        (party_set_ai_object, ":new_party", ":village"),
        #(party_set_flags, ":new_party", pf_default_behavior, 0),
        # (str_store_party_name, s1, ":village"),
        # (display_message, "@Animals created at {s1}."),
        (call_script,"script_refill_animal_parties"),	# to really fill it
      (try_end),]),
  
  
  # script_check_village_menu_conditions
  # Component: Scenic menu
  ("check_village_menu_conditions",
    [(try_for_range, ":menu_slot", slot_quest_menu_begin, slot_quest_menu_end),
        (quest_set_slot, "qst_vc_menu", ":menu_slot", 0),
      (end_try),
      (assign, "$curr_menu_slot", 30),
      ###
      (try_begin),
        # ("village_manage",#1
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (neg | party_slot_eq, "$current_town", slot_village_state, svs_looted),
        (neg | party_slot_eq, "$current_town", slot_village_state, svs_being_raided),
        (neg | party_slot_ge, "$current_town", slot_village_infested_by_bandits, 1),
        (party_slot_eq, "$current_town", slot_town_lord, "trp_player"),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Manage this village."),			#vc_menu
      (end_try),
      ###
      (try_begin),
        # ("recruit_volunteers",#2
        (val_add, "$curr_menu_slot", 1),
        (party_slot_eq,"$current_town", slot_party_levy_on, 0),		#vc_menu
        (call_script, "script_cf_village_recruit_volunteers_cond"),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Recruit volunteers."),			#vc_menu
      (end_try),
      ###
      (try_begin),
        # ("village_center",#3
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (neg | party_slot_eq, "$current_town", slot_village_state, svs_looted),
        (neg | party_slot_eq, "$current_town", slot_village_state, svs_being_raided),
        (neg | party_slot_ge, "$current_town", slot_village_infested_by_bandits, 1),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Go to the village center."),	#vc_menu
      (end_try),
      ###
      ###
      (try_begin),
        # ("village_elder",#3.1
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (neg | party_slot_eq, "$current_town", slot_village_state, svs_looted),
        (neg | party_slot_eq, "$current_town", slot_village_state, svs_being_raided),
        (neg | party_slot_ge, "$current_town", slot_village_infested_by_bandits, 1),
        # (neg | is_currently_night), player can visit leader at night in scene
        (party_slot_eq,"$current_town", slot_party_levy_on, 0),
        (neg | check_quest_active, "qst_blank_quest_7"), #!
        (party_get_slot, ":leader", "$current_town", slot_town_elder),
        (troop_get_slot, ":met", ":leader", slot_troop_met),
        (ge, ":met", 1),
        
        (assign, ":block", 0),#VC-3051
        (try_begin),
          (eq, "$campaign_type", camp_storyline),
          (eq, "$current_town", "p_village_150"),
          (assign, ":block", 1),
        (end_try),
        (eq, ":block", 0),
        
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Meet the leader."),			#vc_menu
      (end_try),
      ###
      ###
      (try_begin),
        # ("village_buy_food",#4
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (party_slot_eq, "$current_town", slot_village_state, 0),
        (neg | party_slot_ge, "$current_town", slot_village_infested_by_bandits, 1),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Buy supplies."),				#vc_menu
      (end_try),
      ###
      (try_begin),
        # ("village_attack_bandits",#5
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (party_slot_ge, "$current_town", slot_village_infested_by_bandits, 1),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Attack the bandits."),			#vc_menu
      (end_try),
      ###
      (try_begin),
        
        # ("village_wait",#6
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (party_slot_eq, "$current_town", slot_center_has_manor, 1),
        (party_slot_eq, "$current_town", slot_town_lord, "trp_player"),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Wait here for some time."),		#vc_menu
      (end_try),
      ###
      (try_begin),
        # ("collect_taxes_qst",#7
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (party_slot_eq, "$current_town", slot_village_state, 0),
        (neg | party_slot_ge, "$current_town", slot_village_infested_by_bandits, 1),
        (check_quest_active, "qst_collect_taxes"),
        (quest_get_slot, ":quest_giver_troop", "qst_collect_taxes", slot_quest_giver_troop),
        (quest_slot_eq, "qst_collect_taxes", slot_quest_target_center, "$current_town"),
        (neg | quest_slot_eq, "qst_collect_taxes", slot_quest_current_state, 4),
        (str_store_troop_name, s1, ":quest_giver_troop"),
        (quest_get_slot, reg5, "qst_collect_taxes", slot_quest_current_state),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@{reg5?Continue collecting taxes:Collect taxes}."),			#vc_menu
      (end_try),
      ###
      (try_begin),
        # ("train_peasants_against_bandits_qst",#8
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (party_slot_eq, "$current_town", slot_village_state, 0),
        (check_quest_active, "qst_train_peasants_against_bandits"),
        (neg | check_quest_concluded, "qst_train_peasants_against_bandits"),
        (quest_slot_eq, "qst_train_peasants_against_bandits", slot_quest_target_center, "$current_town"),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Train the peasants."),			#vc_menu
      (end_try),
      ###
      (try_begin),
        #("village_hostile_action", #9
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (party_slot_eq, "$current_town", slot_village_state, 0),
        (neg | party_slot_ge, "$current_town", slot_village_infested_by_bandits, 1),
        #(neq, "$players_kingdom", "$g_encountered_party_faction"), #outcommented
        #for VC-3396
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Take an action."),		#vc_menu
      (end_try),
      ###
      (try_begin),
        #("village_reports",#10
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (eq, "$cheat_mode", 1),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),				#vc_menu
        (str_store_string, "$curr_menu_slot", "@{!}CHEAT! Show reports."),		#vc_menu
      (end_try),
      ###
      (try_begin),
        #("village_leave",#11
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),				#vc_menu
        (str_store_string, "$curr_menu_slot", "@Leave..."),					#vc_menu
      (end_try),]),
  
  # script_execute_village_menu_cosequence
  # Component: Scenic menu
  # Input: ":menu_number"
  ("execute_village_menu_cosequence",
    [(store_script_param, ":menu_number", 1),
      (assign, "$curr_menu_slot", 30),
      
      (try_begin),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ###1
        (assign, "$g_next_menu", "mnu_village"),
        (jump_to_menu, "mnu_center_manage"),
        ###
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ###2
        (try_begin), #chief add for recruit need to permission
          (neg | party_slot_eq, "$current_town", slot_town_lord, "trp_player"),
          (party_slot_eq, "$current_town", recruit_permission_need, 1), #reclutamiento
          
          #Permission necesary first time
          (try_begin),
            (call_script, "script_cf_enter_center_location_bandit_check"),
          (else_try),
            (party_get_slot, ":conversation_troop", "$current_town", slot_town_elder),
            (call_script, "script_setup_troop_meeting", ":conversation_troop", -1),
          (try_end),
          ################################
          
          (display_message, "@You need to obtain permission from the leader in this village to recruit here."),
        (else_try),
          (call_script, "script_cf_enter_center_location_bandit_check"),
        (else_try),
          (party_slot_eq,"$current_town", slot_party_levy_on, 0),
          (jump_to_menu, "mnu_recruit_volunteers"),
        (try_end),
        ###
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ###3
        (try_begin),
          (call_script, "script_cf_enter_center_location_bandit_check"),
        (else_try),
          (neg | party_slot_eq, "$current_town", slot_village_state, svs_being_raided),	# for vc menu if village is raided
          (party_get_slot, ":village_scene", "$current_town", slot_castle_exterior),
          (modify_visitors_at_site,":village_scene"),
          (reset_visitors),
          (try_begin),
            (party_slot_eq, "$current_town", slot_party_levy_on, 0),
            (party_get_slot, ":village_elder_troop", "$current_town",slot_town_elder),
            (set_visitor, 11, ":village_elder_troop"),
          (try_end),
          (call_script, "script_init_town_walkers"),
          (try_begin),
            (check_quest_active, "qst_hunt_down_fugitive"),
            (neg | is_currently_night),
            (quest_slot_eq, "qst_hunt_down_fugitive", slot_quest_target_center, "$current_town"),
            (neg | check_quest_succeeded, "qst_hunt_down_fugitive"),
            (neg | check_quest_failed, "qst_hunt_down_fugitive"),
            (set_visitor, 45, "trp_fugitive"),
          (try_end),
          (try_begin),
            (check_quest_active, "qst_blank_quest_10"),
            (neg | is_currently_night),
            (quest_slot_eq, "qst_blank_quest_10", slot_quest_target_center, "$current_town"),
            #			 (store_faction_of_party, ":faction_no","$current_town"),
            (neg | check_quest_succeeded, "qst_blank_quest_10"),
            (neg | check_quest_failed, "qst_blank_quest_10"),
            (quest_get_slot, ":target_troop", "qst_blank_quest_10", slot_quest_target_troop),
            #			 (faction_get_slot, ":target_troop", ":faction_no",
            #			 slot_faction_deserter_troop),
            (store_random_in_range, ":rand", 1,4),
            (try_begin),
              (eq, ":rand", 1),
              (assign, ":pos",32),
            (else_try),
              (eq, ":rand", 2),
              (assign, ":pos", 34),
            (else_try),
              (eq, ":rand", 3),
              (assign, ":pos", 33),
            (try_end),
            #			  (shuffle_range, 1, 4),
            (set_visitor, ":pos", ":target_troop"),
          (try_end),
          (try_begin),
            (check_quest_active, "qst_blank_quest_14"),
            (neg | is_currently_night),
            (quest_slot_eq, "qst_blank_quest_14", slot_quest_target_center, "$current_town"),
            (neg | check_quest_succeeded, "qst_blank_quest_14"),
            (neg | check_quest_failed, "qst_blank_quest_14"),
            (set_visitor, 10, "trp_mercenaries_end"),
          (try_end),
          #		(try_end),
          (try_begin), #chief anade visitors for doccinga mainquest
            (eq, "$current_town", "p_village_150"),
            (eq, "$campaign_type", camp_storyline),
            #(check_quest_active,"qst_collect_men"),
            (neg | check_quest_finished,"qst_doccinga_assault"),
            (neg | is_currently_night),
            (set_visitor,20,"trp_tutorial_fighter_1"),
            (set_visitor,21,"trp_tutorial_fighter_2"),
            (set_visitor,22,"trp_tutorial_fighter_3"),
            (set_visitor,23,"trp_tutorial_fighter_4"),
            (set_visitor,40,"trp_tutorial_master_archer"),
            (set_visitor,41,"trp_tutorial_archer_1"),
            (set_visitor,42,"trp_tutorial_archer_1"),
            (set_visitor,60,"trp_tutorial_master_horseman"),
            (set_visitor,61,"trp_tutorial_rider_1"),
            (set_visitor,62,"trp_tutorial_rider_1"),
            (set_visitor,63,"trp_tutorial_rider_2"),
            (set_visitor,64,"trp_tutorial_rider_2"),
          (try_end),
          
          (try_begin), #chief anade mt for doccinga at war mainquest
            (eq, "$current_town", "p_village_150"),
            (eq, "$vc_menu_active", 0), 					# no tutorial etc.  if menu is active
            #(check_quest_active,"qst_collect_men"),
            (eq, "$campaign_type", camp_storyline),
            (neg | check_quest_finished,"qst_doccinga_assault"),
            (neg | is_currently_night),
            (assign, "$g_tutorial_entered", 1),
            (set_jump_mission,"mt_village_doccinga"),
          (else_try),
            (set_jump_mission,"mt_village_center"),
            #VC-2404
            (try_begin),
              (call_script, "script_cf_player_use_second_outfit"),#is using second outfit?
              (try_for_range, ":entry", 0, 2),
                (call_script, "script_init_second_outfit", "mt_village_center", ":entry"),
              (try_end),
              (mission_tpl_entry_set_override_flags, "mt_village_center", 0, af_override_outfit_1),
              (mission_tpl_entry_set_override_flags, "mt_village_center", 1, af_override_outfit_1 | af_override_horse),
            (try_end),
          (try_end),
          
          #VC-2242
          (try_begin),
            (eq, "$town_entered", 0),
            (assign, "$town_entered", 1),
            (set_jump_entry, 0),
          (else_try),
            (set_jump_entry, 1),
          (try_end),
          
          (jump_to_scene,":village_scene"),
          (change_screen_mission),
        (try_end),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##3.1 (village_leader)
        (party_slot_eq,"$current_town", slot_party_levy_on, 0),
        (party_get_slot, ":village_scene", "$current_town", slot_castle_exterior),
        (party_get_slot, ":leader", "$current_town", slot_town_elder),
        (modify_visitors_at_site,":village_scene"),
        (reset_visitors),
        (set_visitor,0,"trp_player"),
        (set_visitor,11,":leader"),
        (set_jump_mission,"mt_conversation_encounter"),
        (jump_to_scene,":village_scene"),
        (change_screen_map_conversation, ":leader"),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ###4
        (try_begin),
          (call_script, "script_cf_enter_center_location_bandit_check"),
        (else_try),
          (party_get_slot, ":merchant_troop", "$current_town", slot_town_elder),
          
          #(try_for_range, ":cur_goods", trade_goods_begin, trade_goods_end),
          #(store_sub, ":cur_good_price_slot", ":cur_goods", trade_goods_begin),
          #(val_add, ":cur_good_price_slot", slot_town_trade_good_prices_begin),
          #(party_get_slot, ":cur_price", "$current_town", ":cur_good_price_slot"),
          #(call_script, "script_center_get_production", "$current_town",
          #":cur_goods"),
          #(assign, reg13, reg0),
          #(call_script, "script_center_get_consumption", "$current_town",
          #":cur_goods"),
          #(str_store_party_name, s1, "$current_town"),
          #(str_store_item_name, s2, ":cur_goods"),
          #(assign, reg16, ":cur_price"),
          #(display_log_message, "@{!}DEBUG:{s1}-{s2}, prd: {reg13}, con: {reg0},
          #raw: {reg1}, cns: {reg2}, fee: {reg16}"),
          #(try_end),
          (troop_set_auto_equip, ":merchant_troop", 0),
          (change_screen_trade, ":merchant_troop"),
        (try_end),
        ###
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ###
        (party_get_slot, ":bandit_troop", "$current_town", slot_village_infested_by_bandits),
        (party_get_slot, ":scene_to_use", "$current_town", slot_castle_exterior),
        (modify_visitors_at_site,":scene_to_use"),
        (reset_visitors),
        (set_visitors, 0, ":bandit_troop", "$qst_eliminate_bandits_infesting_village_num_bandits"),
        (set_visitors, 2, "trp_farmer", "$qst_eliminate_bandits_infesting_village_num_villagers"),
        (set_party_battle_mode),
        (set_battle_advantage, 0),
        (assign, "$g_battle_result", 0),
        (set_jump_mission,"mt_village_attack_bandits"),
        (jump_to_scene, ":scene_to_use"),
        (assign, "$g_next_menu", "mnu_village_infest_bandits_result"),
        (jump_to_menu, "mnu_battle_debrief"),
        (assign, "$g_mt_mode", vba_normal),
        (change_screen_mission),
        ###
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ###
        (assign,"$auto_enter_town","$current_town"),
        (assign, "$g_last_rest_center", "$current_town"),
        (try_begin),
          (party_is_active, "p_main_party"),
          (party_get_current_terrain, ":cur_terrain", "p_main_party"),
          (try_begin),
            (eq, ":cur_terrain", rt_desert),
            (unlock_achievement, ACHIEVEMENT_SARRANIDIAN_NIGHTS),
          (try_end),
        (try_end),
        (rest_for_hours_interactive, 24 * 7, 5, 1), #rest while attackable
        (change_screen_return),
        ###
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ###
        (jump_to_menu, "mnu_collect_taxes"),
        ###
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ###
        (jump_to_menu, "mnu_train_peasants_against_bandits"),
        ###
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##		(assign, ":continue", 0), #why this is here?  It break menu in frisia
        ##		villages.
        ##		(try_begin),
        ##			(eq, "$campaign_type", camp_storyline),
        ##			(store_faction_of_party, ":faction", "$current_town"),
        ##			(eq, ":faction", "fac_kingdom_4"),
        ##			(assign, ":continue", 1),
        ##		(try_end),
        ##		(eq, ":continue", 0),
        (jump_to_menu,"mnu_village_hostile_action"),
        ###
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ###
        (jump_to_menu,"mnu_center_reports"),
        ###
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ###
        #(change_screen_return,0),
        (stop_all_sounds, 0),	#VC-2095
        (assign, "$g_encountered_party", -1), #VC-3853 disable second outfit for skill modifier update
        (change_screen_map), # fixing VC-491
        ###
      (end_try),]),
  
  # script_check_town_menu_conditions
  # Component: Scenic menu
  ("check_town_menu_conditions",
    [(try_for_range, ":menu_slot", slot_quest_menu_begin, slot_quest_menu_end),
        (quest_set_slot, "qst_vc_menu", ":menu_slot", 0),
      (end_try),
      (assign, "$curr_menu_slot", 30),
      ###
      (try_begin),
        # ("castle_castle",#1
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        
        (party_slot_eq,"$current_town",slot_party_type, spt_castle),
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (eq, "$sneaked_into_town", 0),
        
        (str_clear, s1),
        (try_begin),
          (store_faction_of_party, ":center_faction", "$current_town"),
          (faction_slot_eq, ":center_faction", slot_faction_ai_state, sfai_feast),
          (faction_slot_eq, ":center_faction", slot_faction_ai_object, "$current_town"),
          #(str_store_string, s1, "str__join_the_feast"),
          (str_store_string, s1, "@ (Symbel)"),
        (try_end),
        
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "str_go_to_the_lords_hall_s1"),	#vc_menu
      (end_try),
      ###
      (try_begin),
        # ("join_tournament",#2
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (neg | is_currently_night),
        (party_slot_ge, "$current_town", slot_town_has_tournament, 1),
        
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Join the competitions."),			#vc_menu
      (end_try),
      ###
      (try_begin),
        # ("town_castle",#3
        
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (party_slot_eq,"$current_town",slot_party_type, spt_town),
        (eq,"$entry_to_town_forbidden",0),
        (str_clear, s1),
        (try_begin),
          (store_faction_of_party, ":center_faction", "$current_town"),
          (faction_slot_eq, ":center_faction", slot_faction_ai_state, sfai_feast),
          (faction_slot_eq, ":center_faction", slot_faction_ai_object, "$current_town"),
          #(str_store_string, s1, "str__join_the_feast"),
          (str_store_string, s1, "@ (Symbel)"),
        (try_end),
        
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),				#vc_menu
        (str_store_string, "$curr_menu_slot", "str_go_to_the_lords_hall_s1"),		#vc_menu
      (end_try),
      ###
      (try_begin),
        # ("town_center",#4
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (party_slot_eq, "$current_town", slot_party_type, spt_town),
        (this_or_next | eq,"$entry_to_town_forbidden",0),
        (eq, "$sneaked_into_town",1),
        
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Stroll through town."),			#vc_menu
      (end_try),
      ###
      (try_begin),
        # ("town_mayor",#4.1
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        # (neg | is_currently_night), player can visit leader at night in scene
        (party_slot_eq, "$current_town", slot_party_type, spt_town),
        (this_or_next | eq,"$entry_to_town_forbidden",0),
        (eq, "$sneaked_into_town",1),
        (party_get_slot, ":mayor", "$current_town", slot_town_elder),
        (troop_get_slot, ":met", ":mayor", slot_troop_met),
        (ge, ":met", 1),
        
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Meet the mayor."),			#vc_menu
      (end_try),
      ###
      (try_begin),
        # ("town_tavern",#5
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (party_slot_eq,"$current_town",slot_party_type, spt_town),
        (this_or_next | eq,"$entry_to_town_forbidden",0),
        (eq, "$sneaked_into_town",1),
        #          (party_get_slot, ":scene", "$current_town", slot_town_tavern),
        #          (scene_slot_eq, ":scene", slot_scene_visited, 1), #check if scene
        #          has been visited before to allow entry from menu.  Otherwise scene
        #          will only be accessible from the town center.
        
        #new for
        (try_begin),
          (party_get_slot, ":mercenary_troop", "$current_town", slot_center_mercenary_troop_type),
          (party_get_slot, ":mercenary_amount", "$current_town", slot_center_mercenary_troop_amount),
          (gt, ":mercenary_troop", 0),
          (gt, ":mercenary_amount", 0),
          (str_store_string, s1, "@Mead hall (hire men)."),
        (else_try),
          (str_store_string, s1, "@Visit the mead hall."),
        (end_try),
        
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),				#vc_menu
        (str_store_string, "$curr_menu_slot", s1),			#vc_menu
      (end_try),
      ###
      (try_begin),
        #      ("town_smithy",[
        #          (eq,"$entry_to_town_forbidden",0),
        #          (eq,"$town_nighttime",0),
        #          ],
        #       "Visit the smithy.",
        #       [
        #           (set_jump_mission,"mt_town_default"),
        #           (jump_to_scene,"$pout_scn_smithy"),
        #           (change_screen_mission,0),
        #        ]),
        #	  (end_try),
        #	  ###
        #	  (try_begin),
        
        # ("town_merchant",#6 #NEW: NOW THIS IS MERCANERY MNO!!!
        
        ##	    (val_add, "$curr_menu_slot", 1), #vc_menu
        ##
        ##		(party_slot_eq,"$current_town",slot_party_type, spt_town),
        ##		(this_or_next|eq,"$entry_to_town_forbidden",0),
        ##		(eq, "$sneaked_into_town",1),
        ##		(party_get_slot, ":mercenary_troop", "$current_town",
        ##		slot_center_mercenary_troop_type),
        ##		(party_get_slot, ":mercenary_amount", "$current_town",
        ##		slot_center_mercenary_troop_amount),
        ##		(gt, ":mercenary_troop", 0),
        ##		(gt, ":mercenary_amount", 0),
        ##
        ##		(quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1), #vc_menu
        ##		(str_store_string, "$curr_menu_slot", "@Hire leaderless men."), #vc_menu
        #########################
        # ("town_merchant",#6
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        # (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        # (party_slot_eq,"$current_town",slot_party_type, spt_town),
        # (eq, 1, 0),
        # (eq,"$town_nighttime",0),
        # (this_or_next | eq,"$entry_to_town_forbidden",0),
        # (eq, "$sneaked_into_town",1),
        #           (party_get_slot, ":scene", "$current_town", slot_town_store),
        #           (scene_slot_eq, ":scene", slot_scene_visited, 1), #check if scene
        #           has been visited before to allow entry from menu.  Otherwise scene
        #           will only be accessible from the town center.
        
        # (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),				#vc_menu
        # (str_store_string, "$curr_menu_slot", "@Speak with the merchant."),	#vc_menu
        ########################
      (end_try),
      ###
      (try_begin),
        # ("town_arena",#7
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        
        (party_slot_eq,"$current_town",slot_party_type, spt_town),
        (eq, "$sneaked_into_town", 0),
        #           (party_get_slot, ":scene", "$current_town", slot_town_arena),
        #           (scene_slot_eq, ":scene", slot_scene_visited, 1), #check if scene
        #           has been visited before to allow entry from menu.  Otherwise scene
        #           will only be accessible from the town center.
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Visit the training grounds."),	#vc_menu
        
      (end_try),
      ###
      # (try_begin),
        # ("town_dungeon",#8
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        # (eq, 1, 0),
        
        # (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        # (str_store_string, "$curr_menu_slot", "@Never: Enter the prison."),	#vc_menu
        
      # (end_try),
      ###
      (try_begin),
        #("castle_inspect", #9
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        
        (party_slot_eq,"$current_town",slot_party_type, spt_castle),
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Tour the grounds."),			#vc_menu (because we dont have castles)
        
      (end_try),
      ###
      (try_begin),
        #("town_enterprise",#10
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        
        
        (party_slot_eq,"$current_town",slot_party_type, spt_town),
        (party_get_slot, ":item_produced", "$current_town", slot_center_player_enterprise),
        (gt, ":item_produced", 1),
        (eq,"$entry_to_town_forbidden",0),
        (call_script, "script_get_enterprise_name", ":item_produced"),
        (str_store_string, s3, reg0),
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Visit your farmstead."),				#vc_menu
      (end_try),
      ###
      (try_begin),
        #("visit_lady",#11
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        
        (neg | troop_slot_ge, "trp_player", slot_troop_spouse, kingdom_ladies_begin),
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (assign, "$love_interest_in_town", 0),
        (assign, "$love_interest_in_town_2", 0),
        (assign, "$love_interest_in_town_3", 0),
        (assign, "$love_interest_in_town_4", 0),
        (assign, "$love_interest_in_town_5", 0),
        (assign, "$love_interest_in_town_6", 0),
        (assign, "$love_interest_in_town_7", 0),
        (assign, "$love_interest_in_town_8", 0),
        
        (try_for_range, ":lady_no", kingdom_ladies_begin, kingdom_ladies_end),
          (troop_slot_eq, ":lady_no", slot_troop_cur_center, "$current_town"),
          (call_script, "script_get_kingdom_lady_social_determinants", ":lady_no"),
          (assign, ":lady_guardian", reg0),
          
          (troop_slot_eq, ":lady_no", slot_troop_spouse, -1),
          (ge, ":lady_guardian", 0), #not sure when this would not be the case
          
          #must have spoken to either father or lady
          (this_or_next | troop_slot_ge, ":lady_no", slot_troop_met, 2),
          (troop_slot_eq, ":lady_guardian", slot_lord_granted_courtship_permission, 1),
          
          (neg | troop_slot_eq, ":lady_no", slot_troop_met, 4),
          
          #must have approached father
          #		(this_or_next|troop_slot_eq, ":lady_guardian",
          #		slot_lord_granted_courtship_permission, 1),
          #			(troop_slot_eq, ":lady_guardian", slot_lord_granted_courtship_permission,
          #			-1),
          
          (try_begin),
            (eq, "$love_interest_in_town", 0),
            (assign, "$love_interest_in_town", ":lady_no"),
          (else_try),
            (eq, "$love_interest_in_town_2", 0),
            (assign, "$love_interest_in_town_2", ":lady_no"),
          (else_try),
            (eq, "$love_interest_in_town_3", 0),
            (assign, "$love_interest_in_town_3", ":lady_no"),
          (else_try),
            (eq, "$love_interest_in_town_4", 0),
            (assign, "$love_interest_in_town_4", ":lady_no"),
          (else_try),
            (eq, "$love_interest_in_town_5", 0),
            (assign, "$love_interest_in_town_5", ":lady_no"),
          (else_try),
            (eq, "$love_interest_in_town_6", 0),
            (assign, "$love_interest_in_town_6", ":lady_no"),
          (else_try),
            (eq, "$love_interest_in_town_7", 0),
            (assign, "$love_interest_in_town_7", ":lady_no"),
          (else_try),
            (eq, "$love_interest_in_town_8", 0),
            (assign, "$love_interest_in_town_8", ":lady_no"),
          (try_end),
        (try_end),
        (assign, ":continue", 0),
        (try_begin),
          (check_quest_active,"qst_blank_quest_16"),
          (quest_slot_eq, "qst_blank_quest_16", slot_quest_giver_troop, ":lady_no"),
          (neg | troop_slot_eq, ":lady_guardian", slot_lord_granted_courtship_permission, 1),
          (assign, ":continue", 1),
        (else_try),
          (check_quest_active,"qst_blank_quest_17"),
          (quest_slot_eq, "qst_blank_quest_17", slot_quest_giver_troop, ":lady_no"),
          (neg | troop_slot_eq, ":lady_guardian", slot_lord_granted_courtship_permission, 1),
          (assign, ":continue", 1),
        (try_end),
        (this_or_next | gt, "$love_interest_in_town", 0),
        (eq, ":continue", 1),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),						#vc_menu
        (str_store_string, "$curr_menu_slot", "@Attempt to visit a lady."),			#vc_menu
      (end_try),
      ###
      (try_begin),
        #("trade_with_merchants",#12
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (party_slot_eq,"$current_town",slot_party_type, spt_town),
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Trade with locals."),				#vc_menu
        
      (end_try),
      ###
      (try_begin),
        #("travel",#13
        (val_add, "$curr_menu_slot", 1),
        (party_slot_eq,"$current_town",slot_town_port, 1),
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),
        (str_store_string, "$curr_menu_slot", "@Travel to another Port."),
      (end_try),
      
      # Outfit presentation currently cancelled
      
      # (try_begin),
      # #("outfit_party", #13
      # #requirements copy mno_trade_with_merchants Moto chief
      # (val_add, "$curr_menu_slot", 1), #vc_menu
      # (eq, 1, 0), #to disable menu
      # (party_slot_eq,"$current_town",slot_party_type, spt_town),
      # (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1), #vc_menu
      # (str_store_string, "$curr_menu_slot", "@Outfit your party."), #vc_menu
      
      # (end_try),
      # ###
      (try_begin),
        #("walled_center_manage",#14
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        
        (neg | party_slot_eq, "$current_town", slot_village_state, svs_under_siege),
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (party_slot_eq, "$current_town", slot_town_lord, "trp_player"),
        (assign, reg0, 1),
        (try_begin),
          (party_slot_eq, "$current_town", slot_party_type, spt_castle),
          (assign, reg0, 0),
        (try_end),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),						#vc_menu
        (str_store_string, "$curr_menu_slot", "@Manage this {reg0?town:fort}."),	#vc_menu
      (end_try),
      ###
      (try_begin),
        #("walled_center_move_court",#15
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        
        (neg | party_slot_eq, "$current_town", slot_village_state, svs_under_siege),
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (faction_slot_eq, "fac_player_supporters_faction", slot_faction_leader, "trp_player"),
        (party_slot_eq, "$current_town", slot_town_lord, "trp_player"),
        (eq, "$g_encountered_party_faction", "fac_player_supporters_faction"),
        (neq, "$g_player_court", "$current_town"),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),						#vc_menu
        (str_store_string, "$curr_menu_slot", "@Move your court here."),	#vc_menu
      (end_try),
      ###
      (try_begin),
        #("castle_station_troops",#16
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        
        (party_get_slot, ":town_lord", "$current_town", slot_town_lord),
        (str_clear, s10),
        
        (assign, ":player_can_draw_from_garrison", 0),
        (try_begin), #option 1 - player is town lord
          (eq, ":town_lord", "trp_player"),
          (assign, ":player_can_draw_from_garrison", 1),
        (else_try), #option 2 - town is unassigned and part of the player faction
          (store_faction_of_party, ":faction", "$g_encountered_party"),
          (eq, ":faction", "fac_player_supporters_faction"),
          (neg | party_slot_ge, "$g_encountered_party", slot_town_lord, active_npcs_begin), #ie, zero or -1
          
          (assign, ":player_can_draw_from_garrison", 1),
        (else_try), #option 3 - town was captured by player
          (lt, ":town_lord", 0), #ie, unassigned
          (store_faction_of_party, ":castle_faction", "$g_encountered_party"),
          (eq, "$players_kingdom", ":castle_faction"),
          
          (eq, "$g_encountered_party", "$g_castle_requested_by_player"),
          
          #		  (str_store_string, s10, "str_retrieve_garrison_warning"),
          (assign, ":player_can_draw_from_garrison", 1),
        (else_try),
          (lt, ":town_lord", 0), #ie, unassigned
          (store_faction_of_party, ":castle_faction", "$g_encountered_party"),
          (eq, "$players_kingdom", ":castle_faction"),
          
          (store_party_size_wo_prisoners, ":party_size", "$g_encountered_party"),
          (eq, ":party_size", 0),
          
          #		  (str_store_string, s10, "str_retrieve_garrison_warning"),
          (assign, ":player_can_draw_from_garrison", 1),
        (else_try),
          (party_slot_ge, "$g_encountered_party", slot_town_lord, active_npcs_begin),
          (store_faction_of_party, ":castle_faction", "$g_encountered_party"),
          (eq, "$players_kingdom", ":castle_faction"),
          
          (troop_slot_eq, "trp_player", slot_troop_spouse, ":town_lord"),
          
          (assign, ":player_can_draw_from_garrison", 1),
        (try_end),
        
        (eq, ":player_can_draw_from_garrison", 1),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),						#vc_menu
        (str_store_string, "$curr_menu_slot", "@Manage the garrison."),	#vc_menu # {s10}
      (end_try),
      ###
      (try_begin),
        #("castle_wait",#17
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (this_or_next | ge, "$g_encountered_party_relation", 0),
        (eq,"$castle_undefended",1),
        (assign, ":can_rest", 1),
        (str_clear, s5),
        (try_begin),
          (neg | party_slot_eq, "$current_town", slot_town_lord, "trp_player"),
          (troop_get_slot, ":player_spouse", "trp_player", slot_troop_spouse),
          (neg | party_slot_eq, "$current_town", slot_town_lord, ":player_spouse"),
          
          (party_slot_ge, "$current_town", slot_town_lord, "trp_player"), #can rest for free in castles and towns with unassigned lords
          (store_faction_of_party, ":current_town_faction", "$current_town"),
          (neq, ":current_town_faction", "fac_player_supporters_faction"),
          (party_get_num_companions, ":num_men", "p_main_party"),
          (store_div, reg1, ":num_men", 4),
          (val_add, reg1, 1),
          (str_store_string, s5, "@ ({reg1} peningas per night)"),
          (try_begin),
            (eq, reg1, 1),
            (str_store_string, s5, "@ (1 pening per night)"),
          (end_try),
          (store_troop_gold, ":gold", "trp_player"),
          (lt, ":gold", reg1),
          (assign, ":can_rest", 0),
        (try_end),
        (eq, ":can_rest", 1),
        
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
        (str_store_string, "$curr_menu_slot", "@Wait here for some time."),		#vc_menu
      (end_try),
      ###
      (try_begin),
        #("town_alley",#18 #and reclutamiento
        (val_add, "$curr_menu_slot", 1),										#vc_menu
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (neg | party_slot_ge, "$current_town", recruit_permission_need, 1), #no recruit possibility x time
        (this_or_next | eq,"$entry_to_town_forbidden",0),
        (eq, "$sneaked_into_town",1),
        (party_get_slot, ":player_relation", "$current_town", slot_center_player_relation),
        (try_begin),
          (this_or_next | party_slot_eq, "$current_town", slot_town_lord, "trp_player"),
          (ge, ":player_relation",0), #relation must be positive with center
          (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),					#vc_menu
          (str_store_string, "$curr_menu_slot", "@Ask for potential recruits."),		#vc_menu
        (try_end),
      (end_try),
      ###
      (try_begin),
        #("town_visit_port",#19
        (val_add, "$curr_menu_slot", 1),										#Phaiak
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (party_slot_eq,"$current_town",slot_town_port, 1),
        (this_or_next | eq,"$entry_to_town_forbidden",0),
        (eq, "$sneaked_into_town",1),
        
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),				#Phaiak
        (str_store_string, "$curr_menu_slot", "@See the ships."),			#Phaiak
        
      (end_try),
      ###
      (try_begin),
        #("collect_taxes_qst",#20
        (val_add, "$curr_menu_slot", 1),										#Phaiak
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (check_quest_active, "qst_collect_taxes"),
        (quest_slot_eq, "qst_collect_taxes", slot_quest_target_center, "$current_town"),
        (str_clear, s1),
        (neg | quest_slot_eq, "qst_collect_taxes", slot_quest_current_state, 4),
        (quest_get_slot, ":quest_giver_troop", "qst_collect_taxes", slot_quest_giver_troop),
        (str_store_troop_name, s1, ":quest_giver_troop"),
        (quest_get_slot, reg5, "qst_collect_taxes", slot_quest_current_state),
        
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),				#Phaiak
        (str_store_string, "$curr_menu_slot", "@{reg5?Continue collecting taxes:Collect taxes}."),			#Phaiak
        
      (end_try),
      ###
      (try_begin),
        #("banquet",#21
        (val_add, "$curr_menu_slot", 1),
        #
        (neg | party_slot_eq, "$current_town", slot_village_state, svs_under_siege),
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        #(neq, "$current_town", "p_fort"), #para player lair chief player
        (party_slot_eq, "$current_town", slot_town_lord, "trp_player"),
        (assign, reg0, 1),
        (try_begin),
          (party_slot_eq, "$current_town", slot_party_type, spt_castle),
          (assign, reg0, 0),
        (try_end),
        #
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),
        (str_store_string, "$curr_menu_slot", "@Symbel for your men."),
      (end_try),
      ###
      (try_begin),
        #("set_sail",#22
        (val_add, "$curr_menu_slot", 1),
        (party_slot_ge, "$current_town", slot_party_1_ship_type, 1),
        (neg | party_slot_ge, "$current_town", slot_party_looted_left_days, 1),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),
        (str_store_string, "$curr_menu_slot", "@Sail from port."),
      (end_try),
      ###
      (try_begin),
        #("town_leave",#23
        (val_add, "$curr_menu_slot", 1),
        (quest_set_slot, "qst_vc_menu", "$curr_menu_slot", 1),
        (str_store_string, "$curr_menu_slot", "@Leave..."),
      (end_try),
      ###
  ]),
  
  # script_execute_town_menu_cosequence
  # Component: Scenic menu
  # Input: ":menu_number"
  ("execute_town_menu_cosequence",
    [(store_script_param, ":menu_number", 1),
      (assign, "$curr_menu_slot", 30),
      ###
      
      
      (try_begin),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##1
        (try_begin),
          (this_or_next | eq, "$all_doors_locked", 1),
          (eq, "$sneaked_into_town", 1),
          (display_message,"str_door_locked",0xFFFFAAAA),
        (else_try),
          (neq, "$campaign_type", camp_storyline),
          (neg | troop_slot_ge, "trp_player", slot_troop_renown, 125),
          (neq, "$g_player_eligible_feast_center_no", "$current_town"),
          (faction_slot_eq, "$g_encountered_party_faction", slot_faction_ai_state, sfai_feast),
          (faction_slot_eq, "$g_encountered_party_faction", slot_faction_ai_object, "$g_encountered_party"),
          (neg | check_quest_active, "qst_wed_betrothed"),
          (neg | check_quest_active, "qst_wed_betrothed_female"),
          (neg | troop_slot_ge, "trp_player", slot_troop_spouse, active_npcs_begin), #Married players always make the cut
          (jump_to_menu, "mnu_cannot_enter_court"),
        (else_try),
          (assign, "$town_entered", 1),
          (call_script, "script_enter_court", "$current_town"),
        (try_end),
        ##
      (else_try),#2
        
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##2
        (call_script, "script_fill_tournament_participants_troop", "$current_town", 1),
        (assign, "$g_tournament_cur_tier", 0),
        (assign, "$g_tournament_player_team_won", -1),
        (assign, "$g_tournament_bet_placed", 0),
        (assign, "$g_tournament_bet_win_amount", 0),
        (assign, "$g_tournament_last_bet_tier", -1),
        (assign, "$g_tournament_next_num_teams", 0),
        (assign, "$g_tournament_next_team_size", 0),
        (jump_to_menu, "mnu_town_tournament"),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##3
        (try_begin),
          (this_or_next | eq, "$all_doors_locked", 1),
          (eq, "$sneaked_into_town", 1),
          (display_message,"str_door_locked",0xFFFFAAAA),
        (else_try),
          (neg | troop_slot_ge, "trp_player", slot_troop_renown, 125),
          (neq, "$campaign_type", camp_storyline),
          (neq, "$g_player_eligible_feast_center_no", "$current_town"),
          
          (faction_slot_eq, "$g_encountered_party_faction", slot_faction_ai_state, sfai_feast),
          (faction_slot_eq, "$g_encountered_party_faction", slot_faction_ai_object, "$g_encountered_party"),
          
          (neg | check_quest_active, "qst_wed_betrothed"),
          (neg | check_quest_active, "qst_wed_betrothed_female"),
          
          (neg | troop_slot_ge, "trp_player", slot_troop_spouse, active_npcs_begin), #Married players always make the cut
          
          (jump_to_menu, "mnu_cannot_enter_court"),
        (else_try),
          (assign, "$town_entered", 1),
          (call_script, "script_enter_court", "$current_town"),
        (try_end),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##4
        #If the player is fighting his or her way out
        (try_begin),
          (eq, "$talk_context", tc_prison_break),
          (assign, "$talk_context", tc_escape),
          (assign, "$g_mt_mode", tcm_escape),
          (store_faction_of_party, ":town_faction", "$current_town"),
          (faction_get_slot, ":tier_2_troop", ":town_faction", slot_faction_tier_3_troop),
          (faction_get_slot, ":tier_3_troop", ":town_faction", slot_faction_tier_3_troop),
          (faction_get_slot, ":tier_4_troop", ":town_faction", slot_faction_tier_4_troop),
          (party_get_slot, ":town_scene", "$current_town", slot_town_center),
          (modify_visitors_at_site, ":town_scene"),
          (reset_visitors),
          
          #ideally we could alarm troops at locations
          (try_begin),
            #if guards have not gone to some other important happening at nearby
            #villages, then spawn 4 guards.  (example : fire)
            (party_get_slot, ":last_nearby_fire_time", "$current_town", slot_town_last_nearby_fire_time),
            (store_current_hours, ":cur_time"),
            (store_add, ":fire_finish_time", ":last_nearby_fire_time", fire_duration),
            
            (neg | is_between, ":cur_time", ":last_nearby_fire_time", ":fire_finish_time"),
            (store_time_of_day, ":cur_day_hour"),
            (try_begin), #there are 6 guards at day time (no fire ext)
              (ge, ":cur_day_hour", 6),
              (lt, ":cur_day_hour", 22),
              (set_visitors, 25, ":tier_2_troop", 2),
              (set_visitors, 26, ":tier_2_troop", 1),
              (set_visitors, 27, ":tier_3_troop", 2),
              (set_visitors, 28, ":tier_4_troop", 1),
            (else_try),  #only 4 guards because of night
              (set_visitors, 25, ":tier_2_troop", 1),
              (set_visitors, 26, ":tier_2_troop", 1),
              (set_visitors, 27, ":tier_3_troop", 1),
              (set_visitors, 28, ":tier_4_troop", 1),
            (try_end),
          (else_try),
            #if guards have gone to some other important happening at nearby
            #villages, then spawn only 1 guard.  (example : fire)
            (store_time_of_day, ":cur_day_hour"),
            (try_begin), #only 2 guard because there is a fire at one owned village
              (ge, ":cur_day_hour", 6),
              (lt, ":cur_day_hour", 22),
              (set_visitors, 25, ":tier_2_troop", 1),
              (set_visitors, 26, ":tier_2_troop", 0),
              (set_visitors, 27, ":tier_3_troop", 1),
              (set_visitors, 28, ":tier_4_troop", 0),
            (else_try), #only 1 guard because both night and there is a fire at one owned village
              (set_visitors, 25, ":tier_2_troop", 1),
              (set_visitors, 26, ":tier_2_troop", 0),
              (set_visitors, 27, ":tier_3_troop", 0),
              (set_visitors, 28, ":tier_4_troop", 0),
            (try_end),
          (try_end),
          (set_jump_mission,"mt_town_center"),
          (jump_to_scene, ":town_scene"),
          (change_screen_mission),
          #If you're already at escape, then talk context will reset
        (else_try),
          (assign, "$talk_context", 0),
          (call_script, "script_cf_enter_center_location_bandit_check"),
          #All other circumstances...
        (else_try),
          (party_get_slot, ":town_scene", "$current_town", slot_town_center),
          (modify_visitors_at_site, ":town_scene"),
          (reset_visitors),
          (assign, "$g_mt_mode", tcm_default),
          (store_faction_of_party, ":town_faction","$current_town"),
          
          (try_begin),
            (neq, ":town_faction", "fac_player_supporters_faction"),
            (faction_get_slot, ":troop_prison_guard", "$g_encountered_party_faction", slot_faction_prison_guard_troop),
            (faction_get_slot, ":troop_castle_guard", "$g_encountered_party_faction", slot_faction_castle_guard_troop),
            (faction_get_slot, ":tier_2_troop", ":town_faction", slot_faction_tier_2_troop),
            (faction_get_slot, ":tier_3_troop", ":town_faction", slot_faction_tier_3_troop),
          (else_try),
            (party_get_slot, ":town_original_faction", "$current_town", slot_center_original_faction),
            (faction_get_slot, ":troop_prison_guard", ":town_original_faction", slot_faction_prison_guard_troop),
            (faction_get_slot, ":troop_castle_guard", ":town_original_faction", slot_faction_castle_guard_troop),
            (faction_get_slot, ":tier_2_troop", ":town_original_faction", slot_faction_tier_2_troop),
            (faction_get_slot, ":tier_3_troop", ":town_original_faction", slot_faction_tier_3_troop),
          (try_end),
          (try_begin), #think about this, should castle guard have to go nearby fire too?  If he do
            #not go, killing 2 armored guard is too hard for player.  For now he goes too.
            #if guards have not gone to some other important happening at
            #nearby villages, then spawn 4 guards.  (example : fire)
            (party_get_slot, ":last_nearby_fire_time", "$current_town", slot_town_last_nearby_fire_time),
            (store_current_hours, ":cur_time"),
            (store_add, ":fire_finish_time", ":last_nearby_fire_time", fire_duration),
            
            (neg | is_between, ":cur_time", ":last_nearby_fire_time", ":fire_finish_time"),
            (set_visitor, 23, ":troop_castle_guard"),
          (try_end),
          (set_visitor, 24, ":troop_prison_guard"),
          
          (try_begin),
            (gt,":tier_2_troop", 0),
            (assign,reg0,":tier_3_troop"),
            (assign,reg1,":tier_3_troop"),
            (assign,reg2,":tier_2_troop"),
            (assign,reg3,":tier_2_troop"),
          (else_try),
            (assign,reg0,"trp_saxon_level1_landed"),
            (assign,reg1,"trp_saxon_level1_landed"),
            (assign,reg2,"trp_saxon_level1_companion"),
            (assign,reg3,"trp_saxon_level0_landed"),
          (try_end),
          (shuffle_range,0,4),
          
          (try_begin),
            #if guards have not gone to some other important happening at
            #nearby villages, then spawn 4 guards.  (example : fire)
            (party_get_slot, ":last_nearby_fire_time", "$current_town", slot_town_last_nearby_fire_time),
            (store_current_hours, ":cur_time"),
            (store_add, ":fire_finish_time", ":last_nearby_fire_time", fire_duration),
            
            (neg | is_between, ":cur_time", ":last_nearby_fire_time", ":fire_finish_time"),
            (set_visitor,25,reg0),
            (set_visitor,26,reg1),
            (set_visitor,27,reg2),
            (set_visitor,28,reg3),
          (try_end),
          
          (party_get_slot, ":spawned_troop", "$current_town", slot_town_armorer),
          (set_visitor, 9, ":spawned_troop"),
          (party_get_slot, ":spawned_troop", "$current_town", slot_town_weaponsmith),
          (set_visitor, 10, ":spawned_troop"),
          (party_get_slot, ":spawned_troop", "$current_town", slot_town_elder),
          (set_visitor, 11, ":spawned_troop"),
          (party_get_slot, ":spawned_troop", "$current_town", slot_town_horse_merchant),
          (set_visitor, 12, ":spawned_troop"),
          
          (party_get_slot, ":spawned_troop", "$current_town", slot_town_healer),
          (set_visitor, 16, ":spawned_troop"),
          
          # port troops start
          (try_begin),
            (party_slot_eq,"$current_town",slot_town_port, 1),
            (party_get_slot, reg0, "$current_town", slot_town_shipwright),
            (set_visitor, 13, "trp_port_crew"),
            (set_visitor, 14, reg0),
            #changes for VC-1656 begin
            (store_current_day, ":day"),
            (try_begin),
              (eq, "$current_town", "p_town_12"), #Lundenwic
              (val_mod, ":day", 4),
            (else_try),
              (eq, "$current_town", "p_town_10"), #York
              (val_mod, ":day", 5),
            (else_try),
              (eq, "$current_town", "p_town_5"), #Tunsberg
              (val_mod, ":day", 6),
            (else_try),
              (eq, "$current_town", "p_town_11"), #Dorestad
              (val_mod, ":day", 7),
            (try_end),
            (this_or_next | eq, ":day", 0),
            (party_slot_eq, "$current_town", slot_center_has_slavemarket, 1),
            (set_visitor, 15, "trp_port_pirate"),
            #changes for VC-1656 end
          (try_end),
          
          (call_script, "script_init_town_walkers"),
          (set_jump_mission,"mt_town_center"),
          (assign, ":override_state", af_override_horse),
          (try_begin),
            (eq, "$sneaked_into_town", 1), #setup disguise
            (assign, ":override_state", af_override_all),
          (try_end),
          (mission_tpl_entry_set_override_flags, "mt_town_center", 0, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_town_center", 2, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_town_center", 3, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_town_center", 4, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_town_center", 5, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_town_center", 6, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_town_center", 7, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_town_center", 8, ":override_state"),
          
          #VC-2404
          (try_begin),
            (neq, "$sneaked_into_town", 1), #not sneaked
            (call_script, "script_cf_player_use_second_outfit"),#is using second outfit?
            (try_for_range, ":entry", 0, 9),
              (call_script, "script_init_second_outfit", "mt_town_center", ":entry"),
              (neq, ":entry", 1),
              (mission_tpl_entry_set_override_flags, "mt_town_center", ":entry", af_override_outfit_1 | af_override_horse),
            (try_end),
            (mission_tpl_entry_set_override_flags, "mt_town_center", 1, af_override_outfit_1),
          (try_end),
          
          (try_begin),
            #(eq, "$vc_menu_active", 0), #phaiak
            (eq, "$town_entered", 0),
            (assign, "$town_entered", 1),
            (eq, "$town_nighttime", 0),
            (set_jump_entry, 1),
            #phaiak begin
            (try_begin),
              (party_slot_ge, "$current_town", slot_party_1_ship_type, 1),
              (set_jump_entry, 8),
            (try_end),
            #phaiak end
          (try_end),
          (jump_to_scene, ":town_scene"),
          (change_screen_mission),
        (try_end),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##4.1 (town_mayor)
        
        (party_get_slot, ":town_scene", "$current_town", slot_town_center),
        (party_get_slot, ":mayor", "$current_town", slot_town_elder),
        (modify_visitors_at_site,":town_scene"),
        (reset_visitors),
        (set_visitor,0,"trp_player"),
        (set_visitor,11,":mayor"),
        (set_jump_mission,"mt_conversation_encounter"),
        (jump_to_scene,":town_scene"),
        (change_screen_map_conversation, ":mayor"),
        
        # outfit presentation is currently cancelled:
        # (try_begin),
        # (call_script, "script_cf_enter_center_location_bandit_check"),
        # (else_try),
        # (call_script, "script_outfit_presentation_init"),
        # (start_presentation, "prsnt_outfit_party"),
        # (try_end),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##5
        (try_begin),
          (eq,"$all_doors_locked",1),
          (display_message,"str_door_locked",0xFFFFAAAA),
        (else_try),
          (call_script, "script_cf_enter_center_location_bandit_check"),
        (else_try),
          (assign, "$town_entered", 1),
          (set_jump_mission, "mt_town_default"),
          (mission_tpl_entry_set_override_flags, "mt_town_default", 0, af_override_horse),
          (try_begin),
            (eq, "$sneaked_into_town",1),
            (mission_tpl_entry_set_override_flags, "mt_town_default", 0, af_override_all),
          (else_try),
            #VC-2404
            (call_script, "script_cf_player_use_second_outfit"),#is using second outfit?
            (call_script, "script_init_second_outfit", "mt_town_default", 0),
            (mission_tpl_entry_set_override_flags, "mt_town_default", 0, af_override_outfit_1 | af_override_horse),
          (try_end),
          (party_get_slot, ":cur_scene", "$current_town", slot_town_tavern),
          (jump_to_scene, ":cur_scene"),
          (scene_set_slot, ":cur_scene", slot_scene_visited, 1),
          (assign, "$talk_context", tc_tavern_talk),
          (call_script, "script_initialize_tavern_variables"),
          (store_random_in_range, ":randomize_attacker_placement", 0, 4),
          (modify_visitors_at_site, ":cur_scene"),
          (reset_visitors),
          (assign, ":cur_entry", 17),
          #this is just a cheat right now
          #(troop_set_slot, "trp_belligerent_drunk", slot_troop_cur_center,
          #"$g_encountered_party"),
          (try_begin),
            (eq, "$cheat_mode", 1),
            (troop_get_slot, ":drunk_location", "trp_belligerent_drunk", slot_troop_cur_center),
            (try_begin),
              (eq, "$cheat_mode", 0),
            (else_try),
              (is_between, ":drunk_location", centers_begin, centers_end),
              (str_store_party_name, s4, ":drunk_location"),
              (display_message, "str_belligerent_drunk_in_s4"),
            (else_try),
              (display_message, "str_belligerent_drunk_not_found"),
            (try_end),
            
            (troop_get_slot, ":promoter_location", "trp_fight_promoter", slot_troop_cur_center),
            (try_begin),
              (eq, "$cheat_mode", 0),
            (else_try),
              (is_between, ":promoter_location", centers_begin, centers_end),
              (str_store_party_name, s4, ":promoter_location"),
              (display_message, "str_roughlooking_character_in_s4"),
            (else_try),
              (display_message, "str_roughlooking_character_not_found"),
            (try_end),
          (try_end),
          
          #this determines whether or not a lord who dislikes you will commission an
          #assassin
          (try_begin),
            (store_current_hours, ":hours"),
            (store_sub, ":hours_since_last_attempt", ":hours", "$g_last_assassination_attempt_time"),
            (gt, ":hours_since_last_attempt", 168),
            (try_for_range, ":lord", active_npcs_begin, active_npcs_end),
              (this_or_next | troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_debauched),
              (troop_slot_eq, ":lord", slot_lord_reputation_type, lrep_cunning),
              (troop_get_slot, ":led_party", ":lord", slot_troop_leaded_party),
              (party_is_active, ":led_party"),
              (party_get_attached_to, ":led_party_attached", ":led_party"),
              (eq, ":led_party_attached", "$g_encountered_party"),
              (call_script, "script_troop_get_relation_with_troop", "trp_player", ":lord"),
              (lt, reg0, -20),
              (assign, "$g_last_assassination_attempt_time", ":hours"),
              #					(assign, "$g_last_assassination_attempt_location",
              #					"$g_encountered_party"),
              #					(assign, "$g_last_assassination_attempt_perpetrator", ":lord"),
              
              (troop_set_slot, "trp_hired_assassin", slot_troop_cur_center, "$g_encountered_party"),
            (try_end),
          (try_end),
          
          (try_begin),
            (try_begin), #add-ons the snake
              (check_quest_active,"qst_blank_quest_24"),
              (neg | check_quest_succeeded, "qst_blank_quest_24"),
              (eq, "$current_town", "p_town_15"),
              (set_visitor, ":cur_entry", "trp_the_snake"),
            (else_try),
              (eq, ":randomize_attacker_placement", 0),
              (call_script, "script_setup_tavern_attacker", ":cur_entry"),
            (try_end),
            
            # (call_script, "script_setup_tavern_attacker", ":cur_entry"),
            
            (val_add, ":cur_entry", 1),
          (try_end),
          
          # (try_begin),
            # (eq, 1, 0),
            # (troop_slot_eq, "trp_fight_promoter", slot_troop_cur_center, "$current_town"),
            # (set_visitor, ":cur_entry", "trp_fight_promoter"),
            
            # (val_add, ":cur_entry", 1),
          # (try_end),
          
          (party_get_slot, ":mercenary_troop", "$current_town", slot_center_mercenary_troop_type),
          (party_get_slot, ":mercenary_amount", "$current_town", slot_center_mercenary_troop_amount),
          (try_begin),
            (gt, ":mercenary_troop", 0),
            (gt, ":mercenary_amount", 0),
            (set_visitor, ":cur_entry", ":mercenary_troop"),
            (val_add, ":cur_entry", 1),
          (try_end),
          
          (try_begin),
            (eq, ":randomize_attacker_placement", 1),
            (call_script, "script_setup_tavern_attacker", ":cur_entry"),
            
            (val_add, ":cur_entry", 1),
          (try_end),
          
          (try_begin), #mainquest chief anade frisio companion in tavern with quest
            (check_quest_active, "qst_kennemer_mission_2"),
            #(quest_slot_eq,"qst_kennemer_mission_2",slot_quest_current_state,
            #0),
            (eq, "$current_town", "p_town_11"),
            (neg | main_party_has_troop,"trp_npc8"),
            (troop_set_slot, "trp_npc8", slot_troop_met_previously, 0),
            (set_visitor, ":cur_entry", "trp_npc8"), #Reginhard
          (else_try),
            (eq, "$campaign_type", camp_storyline),
            (eq, "$current_town", "p_town_10"),
            (neg | main_party_has_troop,"trp_npc13"),
            (set_visitor, ":cur_entry", "trp_npc13"), #Helgi
          (else_try),
            (eq, "$campaign_type", camp_storyline),
            (eq, "$current_town", "p_town_1"),
            (neg | main_party_has_troop,"trp_npc9"),
            (set_visitor, ":cur_entry", "trp_npc9"), #Clovis
          (else_try),
            (eq, "$campaign_type", camp_storyline),
            (eq, "$current_town", "p_town_25"),
            (neg | main_party_has_troop,"trp_npc4"),
            (set_visitor, ":cur_entry", "trp_npc4"), #Donnchadh
          (else_try),
            (eq, "$campaign_type", camp_storyline),
            (eq, "$current_town", "p_town_26"),
            (neg | main_party_has_troop,"trp_npc5"),
            (set_visitor, ":cur_entry", "trp_npc5"), #Morgant
          (else_try),
            (eq, "$campaign_type", camp_storyline),
            (eq, "$current_town", "p_town_12"),
            (neg | main_party_has_troop,"trp_npc10"),
            (set_visitor, ":cur_entry", "trp_npc10"), #Ceawlin
          (else_try),
            (eq, "$campaign_type", camp_storyline),
            (eq, "$current_town", "p_town_24"),
            (neg | main_party_has_troop,"trp_npc14"),
            (set_visitor, ":cur_entry", "trp_npc14"), #Ailchu
          (else_try),
            (check_quest_active, "qst_the_fleet"),
            (quest_slot_eq,"qst_the_fleet",slot_quest_current_state, 2),
            (eq, "$current_town", "p_town_4"),
            (neg | main_party_has_troop,"trp_npc12"),
            (troop_set_slot, "trp_npc12", slot_troop_met_previously, 0),
            (set_visitor, ":cur_entry", "trp_npc12"), #asbjorn
          (else_try),
            (neq, "$campaign_type", camp_storyline), #only in sandbox active normal companions.  In Storyline, story assign them
            #chief
            
            (assign, ":continue_comp", 0),
            (try_for_range, ":companion_candidate", companions_begin, companions_end),
              (troop_slot_eq, ":companion_candidate", slot_troop_occupation, 0),
              (troop_slot_eq, ":companion_candidate", slot_troop_cur_center, "$current_town"),
              (neq, ":companion_candidate", "trp_npc1"), #caio isnt recruitable in taverns, only in Old Roman Quest.
              (neq, ":companion_candidate", "trp_npc7"), #Dwywei isnt recruitable in taverns, only in Old Roman Quest.
              (assign, ":continue_storyl", 0),
              (try_begin), #after storyline, no egil, no reginhard no bodo vc-3258
                (eq, "$continue_storyline", 1),
                (this_or_next | eq, ":companion_candidate", "trp_npc2"),#egil
                (this_or_next | eq, ":companion_candidate", "trp_npc6"), #bodo
                (eq, ":companion_candidate", "trp_npc8"), #reginhard
                (assign, ":continue_storyl", 1),
              (try_end),
              (neq, ":continue_storyl", 1),
              
              (neg | troop_slot_ge, ":companion_candidate", slot_troop_prisoner_of_party, centers_begin),
              (lt, ":continue_comp", 2),
              (set_visitor, ":cur_entry", ":companion_candidate"),
              (val_add, ":continue_comp", 1),
            (try_end),
            (val_add, ":cur_entry", 1),
          (try_end),
          
          (try_begin),
            (eq, ":randomize_attacker_placement", 2),
            (call_script, "script_setup_tavern_attacker", ":cur_entry"),
            
            (val_add, ":cur_entry", 1),
          (try_end),
          
          (try_begin), #this doubles the incidence of ransom brokers and (below) minstrels
            (party_get_slot, ":ransom_broker", "$current_town", slot_center_ransom_broker),
            (gt, ":ransom_broker", 0),
            
            (assign, reg0, ":ransom_broker"),
            (assign, reg1, "$current_town"),
            
            (set_visitor, ":cur_entry", ":ransom_broker"),
            (val_add, ":cur_entry", 1),
          (else_try),
            (is_between, "$g_talk_troop", ransom_brokers_begin, ransom_brokers_end),
            (store_add, ":alternative_town", "$current_town", 9),
            
            (try_begin),
              (ge, ":alternative_town", towns_end),
              (val_sub, ":alternative_town", 22),
            (try_end),
            (try_begin),
              (eq, "$cheat_mode", 1),
              (str_store_party_name, s3, "$current_town"),
              (str_store_party_name, s4, ":alternative_town"),
              (display_message, "@{!}DEBUG - Current town is {s3}, but also checking {s4}"),
            (try_end),
            
            (party_get_slot, ":ransom_broker", ":alternative_town", slot_center_ransom_broker),
            (gt, ":ransom_broker", 0),
            
            (set_visitor, ":cur_entry", ":ransom_broker"),
            (val_add, ":cur_entry", 1),
          (try_end),
          
          (try_begin),
            (party_get_slot, ":tavern_traveler", "$current_town", slot_center_tavern_traveler),
            (gt, ":tavern_traveler", 0),
            (set_visitor, ":cur_entry", ":tavern_traveler"),
            (val_add, ":cur_entry", 1),
          (try_end),
          
          (try_begin),
            (party_get_slot, ":tavern_minstrel", "$current_town", slot_center_tavern_minstrel),
            (gt, ":tavern_minstrel", 0),
            
            (set_visitor, ":cur_entry", ":tavern_minstrel"),
            (val_add, ":cur_entry", 1),
          (else_try),
            (store_add, ":alternative_town", "$current_town", 9),
            (try_begin),
              (ge, ":alternative_town", towns_end),
              (val_sub, ":alternative_town", 22),
            (try_end),
            (party_get_slot, ":tavern_minstrel", ":alternative_town", slot_center_tavern_minstrel),
            (gt, ":tavern_minstrel", 0),
            
            (set_visitor, ":cur_entry", ":tavern_minstrel"),
            (val_add, ":cur_entry", 1),
          (try_end),
          
          (try_begin),
            (party_get_slot, ":tavern_bookseller", "$current_town", slot_center_tavern_bookseller),
            (gt, ":tavern_bookseller", 0),
            (set_visitor, ":cur_entry", ":tavern_bookseller"),
            (val_add, ":cur_entry", 1),
          (try_end),
          
          (try_begin),
            (eq, ":randomize_attacker_placement", 3),
            (call_script, "script_setup_tavern_attacker", ":cur_entry"),
            (val_add, ":cur_entry", 1),
          (try_end),
          
          (try_begin),
            (neg | check_quest_active, "qst_eliminate_bandits_infesting_village"),
            (neg | check_quest_active, "qst_deal_with_bandits_at_lords_village"),
            (assign, ":end_cond", villages_end),
            (try_for_range, ":cur_village", villages_begin, ":end_cond"),
              (party_slot_eq, ":cur_village", slot_village_bound_center, "$current_town"),
              (party_slot_ge, ":cur_village", slot_village_infested_by_bandits, 1),
              (neg | party_slot_eq, ":cur_village", slot_town_lord, "trp_player"),
              (set_visitor, ":cur_entry", "trp_farmer_from_bandit_village"),
              (val_add, ":cur_entry", 1),
              (assign, ":end_cond", 0),
            (try_end),
          (try_end),
          
          (try_begin),
            (eq, "$g_starting_town", "$current_town"),
            
            (this_or_next | neg | check_quest_finished, "qst_collect_men"),
            (this_or_next | neg | check_quest_finished, "qst_learn_where_merchant_brother_is"),
            (this_or_next | neg | check_quest_finished, "qst_save_relative_of_merchant"),
            (this_or_next | neg | check_quest_finished, "qst_save_town_from_bandits"),
            (eq,  "$g_do_one_more_meeting_with_merchant", 1),
            
            (assign, ":troop_of_merchant", 0),
            (try_begin),
              (eq, "$g_encountered_party_faction", "fac_kingdom_1"),
              (assign, ":troop_of_merchant", "trp_swadian_merchant"),
            (else_try),
              (eq, "$g_encountered_party_faction", "fac_kingdom_2"),
              (assign, ":troop_of_merchant", "trp_vaegir_merchant"),
            (else_try),
              (eq, "$g_encountered_party_faction", "fac_kingdom_3"),
              (assign, ":troop_of_merchant", "trp_khergit_merchant"),
            (else_try),
              (eq, "$g_encountered_party_faction", "fac_kingdom_4"),
              (assign, ":troop_of_merchant", "trp_nord_merchant"),
            (else_try),
              (eq, "$g_encountered_party_faction", "fac_kingdom_5"),
              (assign, ":troop_of_merchant", "trp_rhodok_merchant"),
            (else_try),
              (eq, "$g_encountered_party_faction", "fac_kingdom_6"),
              (assign, ":troop_of_merchant", "trp_sarranid_merchant"),
            (try_end),
            (gt, ":troop_of_merchant", 0),
            
            (set_visitor, ":cur_entry", ":troop_of_merchant"),
            (val_add, ":cur_entry", 1),
          (try_end),
          
          (change_screen_mission),
        (try_end),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),	# town_merchant
        ##6
        
        ################################
        (try_begin),
          (this_or_next | eq,"$all_doors_locked",1),
          (eq,"$town_nighttime",1),
          (display_message,"str_door_locked",0xFFFFAAAA),
        (else_try),
          (assign, "$town_entered", 1),
          (set_jump_mission, "mt_town_default"),
          (mission_tpl_entry_set_override_flags, "mt_town_default", 0, af_override_horse),
          (try_begin),
            (eq, "$sneaked_into_town",1),
            (mission_tpl_entry_set_override_flags, "mt_town_default", 0, af_override_all),
          (else_try),
            #VC-2404
            (call_script, "script_cf_player_use_second_outfit"),#is using second outfit?
            (call_script, "script_init_second_outfit", "mt_town_default", 0),
            (mission_tpl_entry_set_override_flags, "mt_town_default", 0, af_override_outfit_1 | af_override_horse),
          (try_end),
          (party_get_slot, ":cur_scene", "$current_town", slot_town_store),
          (jump_to_scene, ":cur_scene"),
          (scene_set_slot, ":cur_scene", slot_scene_visited, 1),
          (change_screen_mission),
        (try_end),
        ####################################
        # (try_begin),
        # (this_or_next|eq,"$all_doors_locked",1),
        # (eq,"$town_nighttime",1),
        # (display_message,"str_door_locked",0xFFFFAAAA),
        # (else_try),
        # (assign, "$town_entered", 1),
        # (set_jump_mission, "mt_town_default"),
        # (mission_tpl_entry_set_override_flags, "mt_town_default", 0,
        # af_override_horse),
        # (try_begin),
        # (eq, "$sneaked_into_town",1),
        # (mission_tpl_entry_set_override_flags, "mt_town_default", 0,
        # af_override_all),
        # (try_end),
        # (party_get_slot, ":cur_scene", "$current_town",
        # slot_town_store),
        # (jump_to_scene, ":cur_scene"),
        # (scene_set_slot, ":cur_scene", slot_scene_visited, 1),
        # (change_screen_mission),
        # (try_end),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##7
        (try_begin),
          (this_or_next | eq,"$all_doors_locked",1),
          (eq,"$town_nighttime",1),
          (display_message,"str_door_locked",0xFFFFAAAA),
          (try_begin),
            (neq, "$g_vc_menu_turned_off", 1),
            (assign, "$vc_menu_active", "prsnt_vc_menu"),
            (call_script, "script_execute_town_menu_cosequence", slot_quest_menu_town_visit),
          (end_try),
        (else_try),
          (assign, "$g_mt_mode", abm_visit),
          (assign, "$town_entered", 1),
          (set_jump_mission, "mt_arena_melee_fight"),
          (party_get_slot, ":arena_scene", "$current_town", slot_town_arena),
          (modify_visitors_at_site, ":arena_scene"),
          (reset_visitors),
          (set_visitor, 43, "trp_arena_training_fighter_10"),
          (set_visitor, 44, "trp_arena_training_fighter_9"),
          #VC-2404
          (try_begin),
            (call_script, "script_cf_player_use_second_outfit"),#is using second outfit?
            (call_script, "script_init_second_outfit", "mt_arena_melee_fight", 50),
            (mission_tpl_entry_set_override_flags, "mt_arena_melee_fight", 50, af_override_outfit_1 | af_override_horse),
          (try_end),
          (set_jump_entry, 50),
          (jump_to_scene, ":arena_scene"),
          (scene_set_slot, ":arena_scene", slot_scene_visited, 1),
          (change_screen_mission),
        (try_end),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##8
        (try_begin),
          (eq, "$talk_context", tc_prison_break),
          (gt, "$g_main_attacker_agent", 0),
          
          (neg | agent_is_alive, "$g_main_attacker_agent"),
          
          (agent_get_troop_id, ":agent_type", "$g_main_attacker_agent"),
          (try_begin),
            (eq, "$g_encountered_party_faction", "fac_player_supporters_faction"),
            (party_get_slot, ":prison_guard_faction", "$current_town", slot_center_original_faction),
          (else_try),
            (assign, ":prison_guard_faction", "$g_encountered_party_faction"),
          (try_end),
          (faction_slot_eq, ":prison_guard_faction", slot_faction_prison_guard_troop, ":agent_type"),
          
          (call_script, "script_deduct_casualties_from_garrison"),
          (call_script, "script_enter_dungeon", "$current_town", "mt_visit_town_castle"),
          
        (else_try),
          (eq,"$all_doors_locked",1),
          (display_message,"str_door_locked",0xFFFFAAAA),
        (else_try),
          (this_or_next | party_slot_eq, "$current_town", slot_town_lord, "trp_player"),
          (eq, "$g_encountered_party_faction", "$players_kingdom"),
          (assign, "$town_entered", 1),
          (call_script, "script_enter_dungeon", "$current_town", "mt_visit_town_castle"),
        (else_try),
          (display_message,"str_door_locked",0xFFFFAAAA),
        (try_end),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##9
        (try_begin),
          (eq, "$talk_context", tc_prison_break),
          (assign, "$talk_context", tc_escape),
          
          (party_get_slot, ":cur_castle_exterior", "$current_town", slot_castle_exterior),
          (modify_visitors_at_site, ":cur_castle_exterior"),
          (reset_visitors),
          
          (assign, ":guard_no", 40),
          
          (party_get_num_companion_stacks, ":num_stacks", "$g_encountered_party"),
          (try_for_range, ":troop_iterator", 0, ":num_stacks"),
            #nearby fire condition start
            (party_get_slot, ":last_nearby_fire_time", "$current_town", slot_town_last_nearby_fire_time),
            (store_current_hours, ":cur_time"),
            (store_add, ":fire_finish_time", ":last_nearby_fire_time", fire_duration),
            (this_or_next | eq, ":guard_no", 40),
            (neg | is_between, ":cur_time", ":last_nearby_fire_time", ":fire_finish_time"),
            #nearby fire condition end
            
            (lt, ":guard_no", 47),
            (party_stack_get_troop_id, ":cur_troop_id", "$g_encountered_party", ":troop_iterator"),
            (neg | troop_is_hero, ":cur_troop_id"),
            (party_stack_get_size, ":stack_size","$g_encountered_party",":troop_iterator"),
            (party_stack_get_num_wounded, ":num_wounded","$g_encountered_party",":troop_iterator"),
            (val_sub, ":stack_size", ":num_wounded"),
            (gt, ":stack_size", 0),
            (party_stack_get_troop_dna,":troop_dna", "$g_encountered_party", ":troop_iterator"),
            (set_visitor, ":guard_no", ":cur_troop_id", ":troop_dna"),
            (val_add, ":guard_no", 1),
          (try_end),
          #(set_jump_entry, 1),
          (set_visitor, 7, "$g_player_troop"),
          
          (set_jump_mission,"mt_castle_visit"),
          (jump_to_scene, ":cur_castle_exterior"),
          (change_screen_mission),
          #If you're already at escape, then talk context will reset
        (else_try),
          (assign, "$talk_context", tc_town_talk),
          
          (assign, "$g_mt_mode", tcm_default),
          
          (party_get_slot, ":cur_castle_exterior", "$current_town", slot_castle_exterior),
          (modify_visitors_at_site,":cur_castle_exterior"),
          (reset_visitors),
          
          # port troops start >> no forts as port in the moment
          # (try_begin),
          # (party_slot_eq,"$current_town",slot_town_port, 1),
          # (set_visitor, 13, "trp_port_crew"),
          # (party_get_slot, reg0, "$current_town", slot_town_shipwright),
          # (set_visitor, 14, reg0),
          # (set_visitor, 15, "trp_port_pirate"),
          # (try_end),
          
          (try_begin),
            (neq, "$g_encountered_party_faction", "fac_player_supporters_faction"),
            (faction_get_slot, ":troop_prison_guard", "$g_encountered_party_faction", slot_faction_prison_guard_troop),
          (else_try),
            (party_get_slot, ":town_original_faction", "$current_town", slot_center_original_faction),
            (faction_get_slot, ":troop_prison_guard", ":town_original_faction", slot_faction_prison_guard_troop),
          (try_end),
          (party_get_slot, ":seneschal", "$current_town", slot_town_seneschal),
          (call_script, "script_init_town_walkers"), #chief anadido walkers a castillos castle walkers
          (set_visitor, 24, ":troop_prison_guard"),
          (set_visitor, 38, ":seneschal"),
          
          (assign, ":guard_no", 40),
          
          (party_get_num_companion_stacks, ":num_stacks", "$g_encountered_party"),
          (try_for_range, ":troop_iterator", 0, ":num_stacks"),
            #nearby fire condition start
            (party_get_slot, ":last_nearby_fire_time", "$current_town", slot_town_last_nearby_fire_time),
            (store_current_hours, ":cur_time"),
            (store_add, ":fire_finish_time", ":last_nearby_fire_time", fire_duration),
            (neg | is_between, ":cur_time", ":fire_finish_time", ":last_nearby_fire_time"),
            
            (lt, ":guard_no", 47),
            (party_stack_get_troop_id, ":cur_troop_id", "$g_encountered_party", ":troop_iterator"),
            (neg | troop_is_hero, ":cur_troop_id"),
            (party_stack_get_size, ":stack_size","$g_encountered_party",":troop_iterator"),
            (party_stack_get_num_wounded, ":num_wounded","$g_encountered_party",":troop_iterator"),
            (val_sub, ":stack_size", ":num_wounded"),
            (gt, ":stack_size", 0),
            (party_stack_get_troop_dna,":troop_dna","$g_encountered_party",":troop_iterator"),
            (set_visitor, ":guard_no", ":cur_troop_id", ":troop_dna"),
            
            (val_add, ":guard_no", 1),
          (try_end),
          
          (try_begin),
            (eq, "$town_entered", 0),
            (assign, "$town_entered", 1),
            # #VC-2242:
            # (set_jump_entry, 0),
            # (else_try),
            # (set_jump_entry, 1),
          (try_end),
          (set_jump_entry, 1),
          
          (try_begin),	#VC: Fixing a native issue of castles
            (party_get_slot, ":castle_scene", "$current_town", slot_town_castle),
            (store_current_scene, ":curr_scene"),
            (eq, ":curr_scene", ":castle_scene"),
            (set_jump_entry, 2),
          (try_end),
          
          (assign, ":override_state", af_override_horse),
          (try_begin),
            (eq, "$sneaked_into_town", 1), #setup disguise
            (assign, ":override_state", af_override_all),
          (try_end),
          (set_jump_mission, "mt_castle_visit"),
          
          (mission_tpl_entry_set_override_flags, "mt_castle_visit", 0, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_castle_visit", 1, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_castle_visit", 2, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_castle_visit", 3, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_castle_visit", 4, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_castle_visit", 5, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_castle_visit", 6, ":override_state"),
          (mission_tpl_entry_set_override_flags, "mt_castle_visit", 7, ":override_state"),
          
          #VC-2404
          (try_begin),
            (neq, "$sneaked_into_town", 1), #not sneaked
            (call_script, "script_cf_player_use_second_outfit"),#is using second outfit?
            (try_for_range, ":entry", 0, 8),
              (call_script, "script_init_second_outfit", "mt_castle_visit", ":entry"),
              (mission_tpl_entry_set_override_flags, "mt_castle_visit", ":entry", af_override_outfit_1 | af_override_horse),
            (try_end),
          (try_end),
          
          (jump_to_scene, ":cur_castle_exterior"),
          (change_screen_mission),
        (try_end),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##10
        (store_sub, ":town_order", "$current_town", towns_begin),
        (store_add, ":master_craftsman", "trp_town_1_master_craftsman", ":town_order"),
        (party_get_slot, ":item_produced", "$current_town", slot_center_player_enterprise),
        (assign, ":enterprise_scene", "scn_enterprise_mill"),
        (try_begin),
          (eq, ":item_produced", "itm_bread"),
          (assign, ":enterprise_scene", "scn_enterprise_mill"),
        (else_try),
          (eq, ":item_produced", "itm_ale"),
          (assign, ":enterprise_scene", "scn_enterprise_brewery"),
        (else_try),
          (eq, ":item_produced", "itm_tar"),
          (assign, ":enterprise_scene", "scn_enterprise_oil_press"),
        (else_try),
          (eq, ":item_produced", "itm_wine"),
          (assign, ":enterprise_scene", "scn_enterprise_winery"),
        (else_try),
          (eq, ":item_produced", "itm_timber"),
          (assign, ":enterprise_scene", "scn_enterprise_tannery"),
        (else_try),
          (eq, ":item_produced", "itm_vc_wool_cloth"),
          (assign, ":enterprise_scene", "scn_enterprise_wool_weavery"),
        (else_try),
          (eq, ":item_produced", "itm_linen"),
          (assign, ":enterprise_scene", "scn_enterprise_linen_weavery"),
        (else_try),
          (eq, ":item_produced", "itm_silver"),
          (assign, ":enterprise_scene", "scn_enterprise_dyeworks"),
        (else_try),
          (eq, ":item_produced", "itm_tools"),
          (assign, ":enterprise_scene", "scn_enterprise_smithy"),
        (try_end),
        (modify_visitors_at_site,":enterprise_scene"),
        (reset_visitors),
        (try_for_range, ":visiterator", 30, 35),
          (set_visitor, ":visiterator", "trp_quarry_trabajador",), #add workers
        (try_end),
        
        (set_visitor,0,"trp_player"),
        (set_visitor,17,":master_craftsman"),
        (set_jump_mission,"mt_town_default"),
        (jump_to_scene,":enterprise_scene"),
        (change_screen_mission),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##11
        (jump_to_menu, "mnu_lady_visit"),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),	#trade_with_merchants
        ##12
        (try_begin),
          (call_script, "script_cf_enter_center_location_bandit_check"),
        (else_try),
          (jump_to_menu,"mnu_town_trade"),
        (try_end),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##13 (port travel)
        
        (party_get_slot, ":town_scene", "$current_town", slot_town_center),
        (party_get_slot, ":shipwright", "$current_town", slot_town_shipwright),
        (modify_visitors_at_site,":town_scene"),
        (reset_visitors),
        (set_visitor,0,"trp_player"),
        (set_visitor,14,":shipwright"),
        (set_jump_mission,"mt_conversation_encounter"),
        (jump_to_scene,":town_scene"),
        (change_screen_map_conversation, ":shipwright"),
        
        # outfit presentation is currently cancelled:
        # (try_begin),
        # (call_script, "script_cf_enter_center_location_bandit_check"),
        # (else_try),
        # (call_script, "script_outfit_presentation_init"),
        # (start_presentation, "prsnt_outfit_party"),
        # (try_end),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##14
        (assign, "$g_next_menu", "mnu_town"),
        (jump_to_menu, "mnu_center_manage"),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##15
        (jump_to_menu, "mnu_establish_court"),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##16
        (change_screen_exchange_members,1),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##17!!
        (assign, "$auto_enter_town", "$current_town"),
        (assign, "$g_town_visit_after_rest", 1),
        (assign, "$g_last_rest_center", "$current_town"),
        (assign, "$g_last_rest_payment_until", -1),
        
        (rest_for_hours_interactive, 24 * 7, 5, 0), #rest while not attackable
        (change_screen_return),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##18!!!
        
        # Thats the mno for the new recruiting in towns.  It is handled in
        # presentations
        
        (jump_to_menu, "mnu_recruit_volunteers_town"),
        
        # (party_get_slot, reg11, "$current_town", slot_town_alley),
        # (set_jump_mission, "mt_ai_training"),
        # (jump_to_scene, reg11),
        # (change_screen_mission),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##19
        (assign, "$vc_menu_active", "prsnt_vc_submenu_ships"),
        (assign, "$ship_menu_state", 1),
        (set_jump_mission,"mt_town_center"),
        (set_jump_entry, 1),
        (party_get_slot, ":town_scene", "$current_town", slot_town_center),
        (jump_to_scene,":town_scene"),
        #(jump_to_scene, "scn_port"),
        (change_screen_mission,0),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##20
        (jump_to_menu, "mnu_collect_taxes"),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##21
        (store_troop_gold,":money","trp_player"),
        (try_begin),
          (gt,":money",999),
          (troop_remove_gold, "trp_player", 1000),
          (jump_to_menu, "mnu_banquete_moral"),
        (else_try),
          (display_message,"str_no_money"),
        (try_end),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##22
        (try_begin),
          (call_script, "script_cf_crew_fit_in_ships", "$current_town"),
          (assign, "$vc_menu_active", 0),
          #(assign,"$g_leave_town",1), #?
          (call_script, "script_give_player_ships_from_party_to_party", "$current_town", "p_main_party"),
          (party_get_slot, ":curr_port", "$current_town", slot_party_port_party),
          (party_get_position, pos2, ":curr_port"),
          (party_set_position, "p_main_party", pos2),
          (call_script, "script_switch_to_water_consequences"),
        (else_try),
          (display_message, "str_cant_set_sail"),
          (assign, "$auto_enter_town", "$g_encountered_party"),
        (try_end),
        (change_screen_return,0),
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##23
        (assign, "$g_permitted_to_center",0),
        #(change_screen_return,0),
        (stop_all_sounds, 0),	#VC-2095
        (assign, "$g_encountered_party", -1), #VC-3853 disable second outfit for skill modifier update
        (change_screen_map), # fixing VC-491
        ##
      (else_try),
        (val_add, "$curr_menu_slot", 1),
        (eq, ":menu_number", "$curr_menu_slot"),
        ##24
        
        ##
      (else_try),
      (end_try),]),
  
  # script_update_party_icon
  # Component: Map icons
  # Input: ":party_no"
  # Output:
  ("update_party_icon",
    [(store_script_param, ":party_no", 1),
      
      (call_script, "script_party_count_members_with_full_health", ":party_no"),
      (assign, ":party_size", reg0),
      (party_get_template_id, ":party_template", ":party_no"),
      (store_faction_of_party, ":party_faction", ":party_no"),
      (try_begin),
        ## villager etc.
        (this_or_next | eq, ":party_faction", "fac_christians"),
        (this_or_next | eq, ":party_faction", "fac_pagans"),
        (this_or_next | eq, ":party_template", "pt_fisher_ship"),
        (this_or_next | eq, ":party_template", "pt_traveller_ship"),
        (this_or_next | eq, ":party_template", "pt_slave_trader_ship"),
        (this_or_next | party_slot_eq, ":party_no", slot_party_type, spt_levy),
        (party_slot_eq, ":party_no", slot_party_type, spt_village_farmer),
        (try_begin),
          # on land
          (party_slot_eq, ":party_no", slot_party_on_water, 0),
          (try_begin),
            (gt, ":party_size", 90),
            (party_set_icon, ":party_no", "icon_farmer_10"),
          (else_try),
            (gt, ":party_size", 60),
            (party_set_icon, ":party_no", "icon_farmer_7"),
          (else_try),
            (gt, ":party_size", 40),
            (party_set_icon, ":party_no", "icon_farmer_5"),
          (else_try),
            (gt, ":party_size", 20),
            (party_set_icon, ":party_no", "icon_farmer_3"),
          (else_try),
            (party_set_icon, ":party_no", "icon_farmer_1"),
          (try_end),
          #(party_set_icon, ":party_no", "icon_peasant"),
        (else_try),
          # on water
          (party_slot_eq, ":party_no", slot_party_on_water, 1),
          (try_begin),
            (gt, ":party_size", 120),
            (party_set_icon, ":party_no", "icon_merchants_7"),
          (else_try),
            (gt, ":party_size", 80),
            (party_set_icon, ":party_no", "icon_merchants_5"),
          (else_try),
            (gt, ":party_size", 40),
            (party_set_icon, ":party_no", "icon_merchants_3"),
          (else_try),
            (party_set_icon, ":party_no", "icon_merchants_1"),
          (try_end),
        (try_end),
      (else_try),
        
        ## caravans
        
        (this_or_next | party_slot_eq, ":party_no", slot_party_type, spt_kingdom_caravan),
        (eq, ":party_template", "pt_merchant_caravan"),
        (try_begin),
          # on land
          (party_slot_eq, ":party_no", slot_party_on_water, 0),
          (try_begin),
            (gt, ":party_size", 50),
            (party_set_icon, ":party_no", "icon_caravan_6"),
          (else_try),
            (gt, ":party_size", 20),
            (party_set_icon, ":party_no", "icon_caravan_3"),
          (else_try),
            (gt, ":party_size", 10),
            (party_set_icon, ":party_no", "icon_caravan_1"),
          (else_try),
            (party_set_icon, ":party_no", "icon_caravan_1"),
          (try_end),
        (else_try),
          # on water
          #(party_slot_eq, ":party_no", slot_party_on_water, 1),
          (try_begin),
            (gt, ":party_size", 120),
            (party_set_icon, ":party_no", "icon_merchants_7"),
          (else_try),
            (gt, ":party_size", 80),
            (party_set_icon, ":party_no", "icon_merchants_5"),
          (else_try),
            (gt, ":party_size", 40),
            (party_set_icon, ":party_no", "icon_merchants_3"),
          (else_try),
            (party_set_icon, ":party_no", "icon_merchants_1"),
          (try_end),
        (try_end),
      (else_try),
        
        ## quest parties (VC-3329)
        (this_or_next | eq, ":party_template", "pt_spy_partners"),
        (this_or_next | eq, ":party_template", "pt_spy"),
        (eq, ":party_template", "pt_sacrificed_messenger"),
        (try_begin),
          # on land
          (party_slot_eq, ":party_no", slot_party_on_water, 0),
          (party_set_icon, ":party_no", "icon_gray_knight"),
        (else_try),
          # on water
          (party_set_icon, ":party_no", "icon_merchants_1"),
        (try_end),
      (else_try),
        
        ## lord parties iconos per size
        (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
        (try_begin),
          # on land
          (party_slot_eq, ":party_no", slot_party_on_water, 0),
          (try_begin),
            (gt, ":party_size", 300),
            (party_set_icon, ":party_no", "icon_lords_10"),
          (else_try),
            (gt, ":party_size", 180),
            (party_set_icon, ":party_no", "icon_lords_7"),
          (else_try),
            (gt, ":party_size", 120),
            (party_set_icon, ":party_no", "icon_lords_5"),
          (else_try),
            (gt, ":party_size", 60),
            (party_set_icon, ":party_no", "icon_lords_3"),
          (else_try),
            (party_set_icon, ":party_no", "icon_lords_1"),
          (try_end),
        (else_try),
          # on water
          (try_begin),
            (gt, ":party_size", 300),
            (party_set_icon, ":party_no", "icon_ships_7"),
          (else_try),
            (gt, ":party_size", 200),
            (party_set_icon, ":party_no", "icon_ships_5"),
          (else_try),
            (gt, ":party_size", 100),
            (party_set_icon, ":party_no", "icon_ships_3"),
          (else_try),
            (party_set_icon, ":party_no", "icon_ships_1"),
          (try_end),
        (try_end),
      (else_try),
        ## bandit and routed parties iconos per size
        (neg|is_between, ":party_template", "pt_steppe_bandit_lair", "pt_bandit_lair_templates_end"),
        (this_or_next | eq, ":party_faction", "fac_outlaws"),
        (this_or_next | eq, ":party_faction", "fac_manhunters"),
        (this_or_next | eq, ":party_faction", "fac_mountain_bandits"),
        (this_or_next | eq, ":party_faction", "fac_forest_bandits"),
        (this_or_next | eq, ":party_faction", "fac_deserters"),
        (eq, ":party_template", "pt_routed_warriors"),
        (try_begin),
          # on land
          (party_slot_eq, ":party_no", slot_party_on_water, 0),
          # (neq, ":party_no", "pt_sea_raiders_ships"),
          # (neq, ":party_no", "pt_sea_raiders_ships2"),
          # (neq, ":party_no", "pt_sea_raiders_ships3"),
          (try_begin),
            (gt, ":party_size", 90),
            (party_set_icon, ":party_no", "icon_warriors_10"),
          (else_try),
            (gt, ":party_size", 60),
            (party_set_icon, ":party_no", "icon_warriors_7"),
          (else_try),
            (gt, ":party_size", 40),
            (party_set_icon, ":party_no", "icon_warriors_5"),
          (else_try),
            (gt, ":party_size", 20),
            (party_set_icon, ":party_no", "icon_warriors_3"),
          (else_try),
            (party_set_icon, ":party_no", "icon_warriors_1"),
          (try_end),
        (else_try),
          # on water
          (try_begin),
            (gt, ":party_size", 300),
            (party_set_icon, ":party_no", "icon_ships_7"),
          (else_try),
            (gt, ":party_size", 200),
            (party_set_icon, ":party_no", "icon_ships_5"),
          (else_try),
            (gt, ":party_size", 100),
            (party_set_icon, ":party_no", "icon_ships_3"),
          (else_try),
            (party_set_icon, ":party_no", "icon_ships_1"),
          (try_end),
        (try_end),
      (try_end),]),
  
  # script_game_missile_launch
  # Input: arg1 = shooter_agent_id, arg2 = weapon_item_id, pos1 =
  # weapon_item_position
  # Output: none
  # ("game_missile_launch", [
  # (play_sound, "snd_distant_chicken"),
  # ]),
  
  # script_game_missile_dives_into_water
  # Input: arg1 = missile_item_id, arg2 = launcher_item_id, arg3 =
  # shooter_agent_id, pos1 = missile_position_on_water
  # Output: none
  ("game_missile_dives_into_water",
    [(copy_position, pos51, pos1),
      (particle_system_burst_no_sync, "psys_water_hit_a", pos51, 8),
      (position_move_z, pos51, 5, 1),
      (particle_system_burst_no_sync, "psys_water_hit_b", pos51, 4),
      (play_sound_at_position,"snd_missile_dive",pos51),]),
  
  # script_cost_per_village_recruit by Motomataru chief
  # MOTO chief
  # INPUT: none
  # OUTPUT: reg0
  ("cost_per_village_recruit", [(party_get_slot, ":commoner_trust", "$current_town", slot_center_player_relation),
      (try_begin),
        (ge, ":commoner_trust", 0),
        (store_div, reg0, ":commoner_trust", -11),
      (else_try),
        (store_div, reg0, ":commoner_trust", -5),
      (try_end),
      #(val_add, reg0, 10),
      (val_add, reg0, 20), #chief double (Moto confirm you are ok), idea is pay elder soborn too.  In
      #addition ,recruits were too cheap in Brytenwalda
      (val_max, reg0, 1),]),
  
  # # AI with Formations Scripts
  # script_calculate_decision_numbers by motomataru
  # Input: AI team, size relative to battle in %
  # Output: reg0 - battle presence plus level bump, reg1 - level bump (team avg
  # level / 3)
  ("calculate_decision_numbers", [(store_script_param, ":team_no", 1),
      (store_script_param, ":battle_presence", 2),
      (try_begin),
        (team_get_slot, reg0, ":team_no", slot_team_level),
        (store_div, reg1, reg0, 3),
        (store_add, reg0, ":battle_presence", reg1),	#decision w.r.t.  all enemy teams
      (try_end)]),
  
  
  # script_team_field_ranged_tactics by motomataru
  # Input: AI team, size relative to largest team in %, size relative to battle
  # in %
  # Output: none
  ("team_field_ranged_tactics", [(store_script_param, ":team_no", 1),
      (store_script_param, ":rel_army_size", 2),
      (store_script_param, ":battle_presence", 3),
      (assign, ":division", grc_archers), #Pre-Many Divisions
      (assign, ":bg_pos", Archers_Pos), #Pre-Many Divisions
      
      (try_begin),
        (store_add, ":slot", slot_team_d0_size, ":division"),
        (team_slot_eq, ":team_no", ":slot", 0),
        (try_begin),	#undo reversion to BP_Jockey (see below)
          (lt, "$battle_phase", BP_Fight),
          (call_script, "script_cf_any_fighting"),
          (call_script, "script_cf_count_casualties"),
          (assign, "$battle_phase", BP_Fight),
        (try_end),
      (else_try),
        (call_script, "script_battlegroup_get_position", ":bg_pos", ":team_no", ":division"),
        (call_script, "script_team_get_position_of_enemies", Enemy_Team_Pos, ":team_no", grc_everyone),
        (call_script, "script_point_y_toward_position", ":bg_pos", Enemy_Team_Pos),
        
        (store_add, ":slot", slot_team_d0_closest_enemy_special_dist, ":division"),	#distance to nearest enemy infantry agent
        (team_get_slot, ":distance_to_enemy", ":team_no", ":slot"),
        (try_begin),
          (eq, ":distance_to_enemy", 0),
          (call_script, "script_get_nearest_enemy_battlegroup_location", Nearest_Enemy_Battlegroup_Pos, ":team_no", ":bg_pos"),
          (assign, ":distance_to_enemy", reg0),
        (try_end),
        
        (try_begin),	#avoid being provoked from defensive position
          (ge, "$battle_phase", BP_Fight),
          (try_begin),
            (call_script, "script_cf_any_fighting"),
          (else_try),
            (assign, "$battle_phase", BP_Jockey),
            (assign, "$clock_reset", 0),
          (try_end),
        (try_end),
        
        (store_add, ":slot", slot_team_d0_is_fighting, ":division"),
        (team_get_slot, ":is_firing", ":team_no", ":slot"),
        (store_add, ":slot", slot_team_d0_size, grc_infantry),
        (team_get_slot, ":num_infantry", ":team_no", ":slot"),
        
        (call_script, "script_calculate_decision_numbers", ":team_no", ":battle_presence"),
        (assign, ":decision_index", reg0),
        (assign, ":level_bump", reg1),
        (try_begin),
          (gt, ":decision_index", 86),	#outpower enemies more than 6:1?
          (team_get_movement_order, reg0, ":team_no", ":division"),
          (try_begin),
            (neq, reg0, mordr_charge),
            (team_give_order, ":team_no", ":division", mordr_charge),
          (try_end),
          
        (else_try),
          (lt, ":decision_index", 14),	#outpowered more than 6:1?
          (eq, ":num_infantry", 0),	#no infantry to delay enemy?
          (team_get_movement_order, reg0, ":team_no", ":division"),
          (try_begin),
            (neq, reg0, mordr_retreat),
            (team_give_order, ":team_no", ":division", mordr_retreat),
          (try_end),
          
        (else_try),
          (ge, "$battle_phase", BP_Jockey),
          (store_add, ":slot", slot_team_d0_low_ammo, ":division"),
          (team_slot_ge, ":team_no", ":slot", 1),	#running out of ammo?
          (team_get_movement_order, reg0, ":team_no", ":division"),
          (try_begin),
            (neq, reg0, mordr_charge),
            (team_give_order, ":team_no", ":division", mordr_charge),
          (try_end),
          
        (else_try),
          (ge, "$battle_phase", BP_Fight),
          (eq, ":is_firing", 0),
          (gt, ":decision_index", Advance_More_Point),
          (le, ":distance_to_enemy", AI_long_range),	#closer than reposition?
          (team_give_order, ":team_no", ":division", mordr_advance),
          
          #hold somewhere
        (else_try),
          (store_add, ":decision_index", ":rel_army_size", ":level_bump"),	#decision w.r.t.  largest enemy team
          (assign, ":move_archers", 0),
          
          (init_position, Team_Starting_Point),
          (team_get_slot, reg0, ":team_no", slot_team_starting_x),
          (position_set_x, Team_Starting_Point, reg0),
          (team_get_slot, reg0, ":team_no", slot_team_starting_y),
          (position_set_y, Team_Starting_Point, reg0),
          (position_set_z_to_ground_level, Team_Starting_Point),
          
          (try_begin),
            (eq, "$battle_phase", BP_Setup),
            (assign, ":move_archers", 1),
          (else_try),
            (ge, "$battle_phase", BP_Fight),
            (try_begin),
              (neg | is_between, ":distance_to_enemy", AI_charge_distance, AI_long_range),
              (assign, ":move_archers", 1),
            (else_try),
              (lt, ":decision_index", Hold_Point),	#probably coming from a defensive position (see below)
              (eq, "$FormAI_AI_no_defense", 0),	#player hasn't set disallow defense option?
              (eq, ":is_firing", 0),	#probably because player team has retreated
              (assign, ":move_archers", 1),
            (try_end),
          (else_try),	#jockey phase
            (this_or_next | gt, "$FormAI_AI_no_defense", 0),	#player has set disallow defense option OR
            (ge, ":decision_index", Hold_Point),	#not starting in a defensive position (see below)
            (try_begin),
              (gt, ":distance_to_enemy", AI_long_range),	#enemy very far off
              (assign, ":move_archers", 1),
            (else_try),
              (call_script, "script_point_y_toward_position", Team_Starting_Point, ":bg_pos"),
              (position_get_rotation_around_z, reg0, Team_Starting_Point),
              (position_get_rotation_around_z, reg1, ":bg_pos"),
              (val_sub, reg0, reg1),
              (this_or_next | is_between, reg0, -45, 45),	#only move if within "cone of advancement" to prevent constant adjusting at
              #border OR
              (eq, ":is_firing", 0),	#if not firing for some reason (hill in way?)
              
              (try_begin),
                (eq, ":num_infantry", 0),	#no infantry to wait for
                (assign, ":move_archers", 1),
              (else_try),
                (call_script, "script_battlegroup_get_position", Infantry_Pos, ":team_no", grc_infantry),
                (get_distance_between_positions, ":infantry_to_enemy", Infantry_Pos, Enemy_Team_Pos),
                (get_distance_between_positions, ":archers_to_enemy", ":bg_pos", Enemy_Team_Pos),
                (val_sub, ":infantry_to_enemy", ":archers_to_enemy"),
                (le, ":infantry_to_enemy", 1500),	#don't outstrip infantry when closing
                (assign, ":move_archers", 1),
              (try_end),
            (try_end),
          (try_end),
          
          (try_begin),
            (gt, ":move_archers", 0),
            (try_begin),
              (lt, ":decision_index", Hold_Point),	#outnumbered?
              (eq, "$FormAI_AI_no_defense", 0),	#player hasn't set disallow defense option?
              (lt, "$battle_phase", BP_Fight),
              (neq, ":team_no", 1),	#not attacker?
              (neq, ":team_no", 3),	#not ally of attacker?
              (store_div, ":distance_to_move", ":distance_to_enemy", 6),	#middle of rear third of battlefield
              (assign, ":hill_search_radius", ":distance_to_move"),
              
            (else_try),
              (try_begin),
                (ge, "$battle_phase", BP_Fight),
                (copy_position, ":bg_pos", Team_Starting_Point),
                (call_script, "script_point_y_toward_position", ":bg_pos", Enemy_Team_Pos),
                (try_begin),
                  (gt, ":num_infantry", 0),
                  (store_add, ":slot", slot_team_d0_closest_enemy, grc_infantry),
                  (team_get_slot, ":enemy_agent_nearest_infantry", ":team_no", ":slot"),
                  (le, ":enemy_agent_nearest_infantry", 0),
                  (agent_get_team, ":target_team", ":enemy_agent_nearest_infantry"),
                  (agent_get_division, ":target_division", ":enemy_agent_nearest_infantry"),
                  (call_script, "script_battlegroup_get_position", Nearest_Enemy_Battlegroup_Pos, ":target_team", ":target_division"),
                  (get_distance_between_positions, ":distance_to_enemy", ":bg_pos", Nearest_Enemy_Battlegroup_Pos),
                (else_try),
                  (call_script, "script_get_nearest_enemy_battlegroup_location", Nearest_Enemy_Battlegroup_Pos, ":team_no", ":bg_pos"),
                  (assign, ":distance_to_enemy", reg0),
                (try_end),
              (try_end),
              
              (try_begin),
                (eq, "$battle_phase", BP_Setup),
                (assign, ":shot_distance", AI_long_range),
              (else_try),
                (assign, ":shot_distance", AI_firing_distance),
                (store_sub, reg1, AI_firing_distance, AI_charge_distance),
                (val_sub, reg1, 200),	#subtract two meters to prevent automatically provoking melee from forward
                #enemy infantry
                (store_add, ":slot", slot_team_d0_percent_throwers, ":division"),
                (team_get_slot, reg0, ":team_no", ":slot"),
                (val_mul, reg1, reg0),
                (val_div, reg1, 100),
                (val_sub, ":shot_distance", reg1),
              (try_end),
              
              (store_sub, ":distance_to_move", ":distance_to_enemy", ":shot_distance"),
              (store_div, ":hill_search_radius", ":shot_distance", 3),	#limit so as not to run into enemy
              (try_begin),
                (lt, "$battle_phase", BP_Fight),
                (try_begin),
                  (this_or_next | eq, "$battle_phase", BP_Setup),
                  (lt, ":battle_presence", Advance_More_Point),	#expect to meet halfway?
                  (val_div, ":distance_to_move", 2),
                (try_end),
              (try_end),
            (try_end),
            
            (position_move_y, ":bg_pos", ":distance_to_move", 0),
            (try_begin),
              (lt, "$battle_phase", BP_Fight),
              (copy_position, pos1, ":bg_pos"),
              (store_div, reg0, ":hill_search_radius", 100),
              (call_script, "script_find_high_ground_around_pos1_corrected", ":bg_pos", reg0),
            (try_end),
          (try_end),
          
          (team_get_movement_order, reg0, ":team_no", ":division"),
          (try_begin),
            (neq, reg0, mordr_hold),
            (team_give_order, ":team_no", ":division", mordr_hold),
          (try_end),
          (call_script, "script_set_formation_destination", ":team_no", ":division", ":bg_pos"),
        (try_end),
      (try_end)]),
  
  
  # script_team_field_melee_tactics by motomataru #EDITED FOR SLOTS BY
  # CABA...many divisions changes necessary
  # Input: AI team, size relative to largest team in %, size relative to battle
  # in %
  # Output: none
  ("team_field_melee_tactics", [(store_script_param, ":team_no", 1),
      #	(store_script_param, ":rel_army_size", 2),
      (store_script_param, ":battle_presence", 3),
      (call_script, "script_calculate_decision_numbers", ":team_no", ":battle_presence"),
      
      #mop up if outnumber enemies more than 6:1
      (try_begin),
        (gt, reg0, 86),
        (try_for_range, ":division", 0, 9),
          (store_add, ":slot", slot_team_d0_size, ":division"),
          (team_slot_ge, ":team_no", ":slot", 1),
          (store_add, ":slot", slot_team_d0_type, ":division"),
          (neg | team_slot_eq, ":team_no", ":slot", sdt_archer),
          (neg | team_slot_eq, ":team_no", ":slot", sdt_skirmisher),
          (call_script, "script_formation_end", ":team_no", ":division"),
          (team_get_movement_order, reg0, ":team_no", ":division"),
          (try_begin),
            (neq, reg0, mordr_charge),
            (team_give_order, ":team_no", ":division", mordr_charge),
          (try_end),
        (try_end),
        
      (else_try),
        (assign, ":num_enemies", 0),
        (try_for_range, ":enemy_team_no", 0, 4),
          (teams_are_enemies, ":enemy_team_no", ":team_no"),
          (team_get_slot, ":value", ":enemy_team_no", slot_team_size),
          (val_add, ":num_enemies", ":value"),
        (try_end),
        
        (gt, ":num_enemies", 0),
        (call_script, "script_team_get_position_of_enemies", Enemy_Team_Pos, ":team_no", grc_everyone),
        
        (store_add, ":slot", slot_team_d0_size, grc_archers),
        (team_get_slot, ":num_archers", ":team_no", ":slot"),
        (try_begin),
          (eq, ":num_archers", 0),
          (assign, ":enemy_bg_nearest_archers_dist", Far_Away),
          (assign, ":archer_order", mordr_charge),
        (else_try),
          (call_script, "script_battlegroup_get_position", Archers_Pos, ":team_no", grc_archers),
          (call_script, "script_point_y_toward_position", Archers_Pos, Enemy_Team_Pos),
          (call_script, "script_get_nearest_enemy_battlegroup_location", pos0, ":team_no", Archers_Pos),
          (assign, ":enemy_bg_nearest_archers_dist", reg0),
          (team_get_movement_order, ":archer_order", ":team_no", grc_archers),
        (try_end),
        
        (store_add, ":slot", slot_team_d0_size, grc_infantry),
        (team_get_slot, ":num_infantry", ":team_no", ":slot"),
        (try_begin),
          (eq, ":num_infantry", 0),
          (assign, ":enemy_bg_nearest_infantry_dist", Far_Away),
          (assign, ":enemy_agent_nearest_infantry_dist", Far_Away),
        (else_try),
          (call_script, "script_battlegroup_get_position", Infantry_Pos, ":team_no", grc_infantry),
          (call_script, "script_get_nearest_enemy_battlegroup_location", pos0, ":team_no", Infantry_Pos),
          (assign, ":enemy_bg_nearest_infantry_dist", reg0),
          (store_add, ":slot", slot_team_d0_closest_enemy_dist, grc_infantry),
          (team_get_slot, ":enemy_agent_nearest_infantry_dist", ":team_no", ":slot"),
          (eq, ":enemy_agent_nearest_infantry_dist", 0),	#happens when player turns off closest agent mechanism (see mod options)
          (assign, ":enemy_agent_nearest_infantry_dist", ":enemy_bg_nearest_infantry_dist"),
        (try_end),
        
        (store_add, ":slot", slot_team_d0_size, grc_cavalry),
        (team_get_slot, ":num_cavalry", ":team_no", ":slot"),
        (try_begin),
          (eq, ":num_cavalry", 0),
          (assign, ":enemy_bg_nearest_cavalry_dist", Far_Away),
          (assign, ":enemy_agent_nearest_cavalry_dist", Far_Away),
        (else_try),
          (call_script, "script_battlegroup_get_position", Cavalry_Pos, ":team_no", grc_cavalry),
          (call_script, "script_get_nearest_enemy_battlegroup_location", pos0, ":team_no", Cavalry_Pos),
          (assign, ":enemy_bg_nearest_cavalry_dist", reg0),
          (store_add, ":slot", slot_team_d0_closest_enemy_dist, grc_cavalry),
          (team_get_slot, ":enemy_agent_nearest_cavalry_dist", ":team_no", ":slot"),
          (eq, ":enemy_agent_nearest_cavalry_dist", 0),	#happens when player turns off closest agent mechanism (see mod options)
          (assign, ":enemy_agent_nearest_cavalry_dist", ":enemy_bg_nearest_infantry_dist"),
        (try_end),
        
        (try_begin),
          (lt, "$battle_phase", BP_Fight),
          (this_or_next | le, ":enemy_bg_nearest_infantry_dist", AI_charge_distance),
          (this_or_next | le, ":enemy_bg_nearest_cavalry_dist", AI_charge_distance),
          (le, ":enemy_bg_nearest_archers_dist", AI_charge_distance),
          (assign, "$battle_phase", BP_Fight),
        (else_try),
          (lt, "$battle_phase", BP_Jockey),
          (this_or_next | le, ":enemy_agent_nearest_infantry_dist", AI_long_range),
          (le, ":enemy_agent_nearest_cavalry_dist", AI_long_range),
          (assign, "$battle_phase", BP_Jockey),
        (try_end),
        
        (team_get_leader, ":team_leader", ":team_no"),
        (assign, ":place_leader_by_infantry", 0),
        
        #infantry AI
        (store_add, ":slot", slot_team_d0_closest_enemy, grc_infantry),
        (team_get_slot, ":enemy_agent_nearest_infantry", ":team_no", ":slot"),
        (try_begin),
          (this_or_next | le, ":num_infantry", 0),
          (le, ":enemy_agent_nearest_infantry", 0),
          (assign, ":infantry_order", ":archer_order"),
          
          #deal with mounted heroes that team_give_order() treats as infantry
          ##CABA...could change their division?
          (team_get_movement_order, reg0, ":team_no", grc_infantry),
          (try_begin),
            (neq, reg0, ":infantry_order"),
            (team_give_order, ":team_no", grc_infantry, ":infantry_order"),
          (try_end),
          (try_begin),
            (gt, ":num_archers", 0),
            (copy_position, pos1, Archers_Pos),
            (position_move_y, pos1, 1000, 0),
            (call_script, "script_set_formation_destination", ":team_no", grc_infantry, pos1),
          (else_try),
            (call_script, "script_set_formation_destination", ":team_no", grc_infantry, Cavalry_Pos),
          (try_end),
          
        (else_try),
          (agent_get_position, Nearest_Enemy_Troop_Pos, ":enemy_agent_nearest_infantry"),
          (agent_get_team, ":enemy_agent_nearest_infantry_team", ":enemy_agent_nearest_infantry"),
          (agent_get_division, ":enemy_agent_nearest_infantry_div", ":enemy_agent_nearest_infantry"),
          
          (assign, ":sum_level_enemy_infantry", 0),
          (try_for_range, ":enemy_team_no", 0, 4),
            (teams_are_enemies, ":enemy_team_no", ":team_no"),
            (try_for_range, ":enemy_division", 0, 9),
              (store_add, ":slot", slot_team_d0_type, ":enemy_division"),
              (team_get_slot, ":value", ":enemy_team_no", ":slot"),
              (this_or_next | eq, ":value", sdt_polearm),
              (eq, ":value", sdt_infantry),
              (store_add, ":slot", slot_team_d0_size, ":enemy_division"),
              (team_get_slot, ":value", ":enemy_team_no", ":slot"),
              (store_add, ":slot", slot_team_d0_level, ":enemy_division"),
              (team_get_slot, reg0, ":enemy_team_no", ":slot"),
              (val_mul, ":value", reg0),
              (val_add, ":sum_level_enemy_infantry", ":value"),
            (try_end),
          (try_end),
          
          (store_mul, ":percent_level_enemy_infantry", ":sum_level_enemy_infantry", 100),
          (val_div, ":percent_level_enemy_infantry", ":num_enemies"),
          (try_begin),
            (teams_are_enemies, ":team_no", "$fplayer_team_no"),
            (assign, ":combined_level", 0),
            (assign, ":combined_team_size", 0),
            (assign, ":combined_num_infantry", ":num_infantry"),
          (else_try),
            (store_add, ":slot", slot_team_d0_level, grc_infantry),
            (team_get_slot, ":combined_level", "$fplayer_team_no", ":slot"),
            (team_get_slot, ":combined_team_size", "$fplayer_team_no", slot_team_size),
            (store_add, ":slot", slot_team_d0_size, grc_infantry),
            (team_get_slot, ":combined_num_infantry", "$fplayer_team_no", ":slot"),
            (val_add, ":combined_num_infantry", ":num_infantry"),
          (try_end),
          (store_mul, ":percent_level_infantry", ":combined_num_infantry", 100),
          (store_add, ":slot", slot_team_d0_level, grc_infantry),
          (team_get_slot, ":level_infantry", ":team_no", ":slot"),
          (val_add, ":combined_level", ":level_infantry"),
          (val_mul, ":percent_level_infantry", ":combined_level"),
          (team_get_slot, reg0, ":team_no", slot_team_size),
          (val_add, ":combined_team_size", reg0),
          (val_div, ":percent_level_infantry", ":combined_team_size"),
          
          (assign, ":infantry_order", mordr_charge),
          (try_begin),	#enemy far away AND ranged not charging
            (gt, ":enemy_bg_nearest_archers_dist", AI_charge_distance),
            (gt, ":enemy_agent_nearest_infantry_dist", AI_charge_distance),
            (neq, ":archer_order", mordr_charge),
            (try_begin),	#fighting not started OR not enough infantry
              (this_or_next | le, "$battle_phase", BP_Jockey),
              (lt, ":percent_level_infantry", ":percent_level_enemy_infantry"),
              (assign, ":infantry_order", mordr_hold),
            (try_end),
          (try_end),
          
          # bum rush enemy archers?
          (try_begin),
            # (le, ":level_infantry", AI_Poor_Troop_Level), unfortunately leaves them
            # susceptible to rings of archers
            (store_add, ":slot", slot_team_d0_type, ":enemy_agent_nearest_infantry_div"),
            (this_or_next | team_slot_eq, ":enemy_agent_nearest_infantry_team", ":enemy_agent_nearest_infantry_div", sdt_archer),
            (team_slot_eq, ":enemy_agent_nearest_infantry_team", ":enemy_agent_nearest_infantry_div", sdt_skirmisher),
            (get_distance_between_positions, reg0, Infantry_Pos, Nearest_Enemy_Troop_Pos),
            (le, reg0, AI_charge_distance),
            (call_script, "script_formation_end", ":team_no", grc_infantry),
            (team_get_movement_order, reg0, ":team_no", grc_infantry),
            (try_begin),
              (neq, reg0, mordr_charge),
              (team_give_order, ":team_no", grc_infantry, mordr_charge),
            (try_end),
            
            #else attempt to make formation somewhere
          (else_try),
            (store_add, ":slot", slot_team_d0_formation, grc_infantry),
            (team_get_slot, ":infantry_formation", ":team_no", ":slot"),
            (team_get_leader, ":enemy_leader", ":enemy_agent_nearest_infantry_team"),
            
            #consider new formation
            (try_begin),
              (store_add, ":slot", slot_team_d0_is_fighting, grc_infantry),
              (this_or_next | le, ":infantry_formation", formation_none),
              (this_or_next | eq, ":infantry_formation", formation_default),
              (team_slot_eq, ":team_no", ":slot", 0),
              
              (call_script, "script_get_default_formation", ":team_no"),
              (assign, ":infantry_formation", reg0),
              (agent_get_class, ":enemy_nearest_troop_class", ":enemy_agent_nearest_infantry"),
              
              (assign, ":num_enemy_cavalry", 0),
              (try_for_range, ":enemy_team_no", 0, 4),
                (teams_are_enemies, ":enemy_team_no", ":team_no"),
                (team_get_slot, ":value", ":enemy_team_no", slot_team_num_cavalry),
                (val_add, ":num_enemy_cavalry", ":value"),
              (try_end),
              
              (store_mul, ":percent_enemy_cavalry", ":num_enemy_cavalry", 100),
              (val_div, ":percent_enemy_cavalry", ":num_enemies"),
              (try_begin),
                (gt, ":infantry_formation", formation_none),
                (try_begin),
                  (gt, ":percent_enemy_cavalry", 66),
                  (assign, ":infantry_formation", formation_square),
                (else_try),
                  (neq, ":enemy_nearest_troop_class", grc_cavalry),
                  (neq, ":enemy_nearest_troop_class", grc_archers),
                  (neq, ":enemy_agent_nearest_infantry", ":enemy_leader"),
                  (ge, ":num_infantry", 21),
                  (store_add, ":slot", slot_team_d0_size, ":enemy_agent_nearest_infantry_div"),
                  (team_get_slot, reg0, ":enemy_agent_nearest_infantry_team", ":slot"),
                  (gt, reg0, ":num_infantry"),	#got fewer troops?
                  (store_add, ":slot", slot_team_d0_armor, grc_infantry),
                  (team_get_slot, ":average_armor", ":team_no", ":slot"),
                  (store_add, ":slot", slot_team_d0_armor, ":enemy_agent_nearest_infantry_div"),
                  (team_get_slot, reg0, ":enemy_agent_nearest_infantry_team", ":slot"),
                  (gt, ":average_armor", reg0),	#got better armor?
                  (assign, ":infantry_formation", formation_wedge),
                (try_end),
              (try_end),
            (try_end),	#consider new formation
            
            (try_begin),
              (call_script, "script_cf_battlegroup_valid_formation", ":team_no", grc_infantry, ":infantry_formation"),
              (store_add, ":slot", slot_team_d0_formation, grc_infantry),
              (team_set_slot, ":team_no", ":slot", ":infantry_formation"),
              
              #adjust spacing for long swung weapons
              (store_add, ":slot", slot_team_d0_swung_weapon_length, grc_infantry),
              (team_get_slot, ":spacing", ":team_no", ":slot"),
              (val_add, ":spacing", 25),	#rounding for 50cm
              (val_div, ":spacing", 50),
              (store_add, ":slot", slot_team_d0_formation_space, grc_infantry),
              (team_set_slot, ":team_no", ":slot", ":spacing"),
              
              (assign, ":place_leader_by_infantry", 1),
              
            (else_try),
              (call_script, "script_formation_end", ":team_no", grc_infantry),
              (team_get_movement_order, reg0, ":team_no", grc_infantry),
              (try_begin),
                (neq, reg0, ":infantry_order"),
                (team_give_order, ":team_no", grc_infantry, ":infantry_order"),
              (try_end),
              (eq, ":infantry_order", mordr_hold),
              (assign, ":place_leader_by_infantry", 1),
            (try_end),
            
            #hold near archers?
            (try_begin),
              (eq, ":infantry_order", mordr_hold),
              (gt, ":num_archers", 0),
              # (copy_position, pos1, Archers_Pos),
              (team_get_order_position, pos1, ":team_no", grc_archers),	#anticipate archers
              (position_move_x, pos1, -100, 0),
              (try_begin),
                (this_or_next | eq, ":enemy_agent_nearest_infantry_div", grc_cavalry),
                (gt, ":percent_level_infantry", ":percent_level_enemy_infantry"),
                (call_script, "script_battlegroup_dist_center_to_front", ":team_no", grc_infantry),	#make sure to clear archers
                (store_mul, ":distance_to_move", reg0, 2),
                (val_add, ":distance_to_move", 1000),
                (position_move_y, pos1, ":distance_to_move", 0),	#move ahead of archers in anticipation of charges
              (else_try),
                (position_move_y, pos1, -1000, 0),
              (try_end),
              
              #obtain destination
            (else_try),
              (assign, ":target_division", -1),
              (try_begin),
                (store_add, ":slot", slot_team_d0_is_fighting, grc_infantry),
                (team_slot_eq, ":team_no", ":slot", 0),	#not engaged?
                (gt, ":enemy_bg_nearest_archers_dist", AI_charge_distance),	#don't have to protect archers?
                # (lt, ":percent_enemy_cavalry", 100), #non-cavalry exist?  MOTO next
                # command tests
                
                #prefer non-cavalry target (that infantry can catch)
                (store_add, ":slot", slot_team_d0_closest_enemy_special_dist, grc_infantry),
                (team_get_slot, ":distance_to_enemy_troop", ":team_no", ":slot"),
                (gt, ":distance_to_enemy_troop", 0),
                (store_add, ":slot", slot_team_d0_closest_enemy_special, grc_infantry),
                (team_get_slot, ":enemy_nearest_non_cav_agent", ":team_no", ":slot"),
                (gt, ":enemy_nearest_non_cav_agent", 0),
                (agent_get_position, pos60, ":enemy_nearest_non_cav_agent"),
                (agent_get_team, ":enemy_non_cav_team", ":enemy_nearest_non_cav_agent"),
                (team_get_leader, reg0, ":enemy_non_cav_team"),
                (try_begin),
                  (eq, ":enemy_nearest_non_cav_agent", reg0),	#team leader?
                  (assign, ":distance_to_enemy_group", Far_Away),
                (else_try),
                  (agent_get_division, ":target_division", ":enemy_nearest_non_cav_agent"),
                  (store_add, ":slot", slot_team_d0_target_team, grc_infantry),
                  (team_set_slot, ":team_no", ":slot", ":enemy_non_cav_team"),
                  (store_add, ":slot", slot_team_d0_target_division, grc_infantry),
                  (team_set_slot, ":team_no", ":slot", ":target_division"),
                  (call_script, "script_battlegroup_get_attack_destination", pos1, ":team_no", grc_infantry, ":enemy_non_cav_team", ":target_division"),
                  (call_script, "script_get_distance_to_battlegroup", ":enemy_non_cav_team", ":target_division", Infantry_Pos),
                  (assign, ":distance_to_enemy_group", reg0),
                (try_end),
                
                #chase nearest target
              (else_try),
                (assign, ":distance_to_enemy_troop", ":enemy_agent_nearest_infantry_dist"),
                (copy_position, pos60, Nearest_Enemy_Troop_Pos),
                (try_begin),
                  (eq, ":enemy_agent_nearest_infantry", ":enemy_leader"),
                  (assign, ":distance_to_enemy_group", Far_Away),
                (else_try),
                  (assign, ":target_division", ":enemy_agent_nearest_infantry_div"),
                  (store_add, ":slot", slot_team_d0_target_team, grc_infantry),
                  (team_set_slot, ":team_no", ":slot", ":enemy_agent_nearest_infantry_team"),
                  (store_add, ":slot", slot_team_d0_target_division, grc_infantry),
                  (team_set_slot, ":team_no", ":slot", ":target_division"),
                  (call_script, "script_battlegroup_get_attack_destination", pos1, ":team_no", grc_infantry, ":enemy_agent_nearest_infantry_team", ":target_division"),
                  (call_script, "script_get_distance_to_battlegroup", ":enemy_agent_nearest_infantry_team", ":target_division", Infantry_Pos),
                  (assign, ":distance_to_enemy_group", reg0),
                (try_end),
              (try_end),
              
              #reassemble if too scattered
              (try_begin),
                (call_script, "script_get_distance_to_battlegroup", ":team_no", grc_infantry, pos60),	#we're using enemy troop as a reference
                (val_sub, reg0, ":distance_to_enemy_troop"),
                (gt, reg0, 1500),	#division center too far from where it should be (probably because of
                #reinforcing troops)
                (position_copy_origin, pos1, Infantry_Pos),	#gather at average position
                (call_script, "script_battlegroup_dist_center_to_front", ":team_no", grc_infantry),
                (assign, ":distance_to_move", reg0),
                (store_mul, reg0, 350, formation_reform_interval),
                (val_add, ":distance_to_move", reg0),	#one interval movement
                (position_move_y, pos1, ":distance_to_move"),	#keep rear moving forward
                
                #attack leader if is closest troop
              (else_try),
                (eq, ":target_division", -1),
                (position_copy_origin, pos1, pos60),
                (call_script, "script_point_y_toward_position", Infantry_Pos, pos1),
                (position_copy_rotation, pos1, Infantry_Pos),
                
                #move no farther than nearest troop if its unit is far off
              (else_try),
                (call_script, "script_battlegroup_dist_center_to_front", ":team_no", grc_infantry),
                (val_add, ":distance_to_enemy_troop", reg0),	#distance to center of bg from nearest edge
                (store_sub, reg0, ":distance_to_enemy_group", ":distance_to_enemy_troop"),
                (gt, reg0, AI_charge_distance),
                (position_copy_origin, pos1, Infantry_Pos),
                (position_move_y, pos1, ":distance_to_enemy_troop"),
                
                #shift dead player troops right to clear allies when both attacking the
                #same enemy battlegroup
              (else_try),
                (eq, ":team_no", "$fplayer_team_no"),
                (store_add, ":ally_team", "$fplayer_team_no", 2),
                (neg | teams_are_enemies, ":ally_team", "$fplayer_team_no"),
                (store_add, ":slot", slot_team_d0_size, grc_infantry),
                (team_slot_ge, ":ally_team", ":slot", 1),
                (store_add, ":slot", slot_team_d0_target_team, grc_infantry),
                (team_get_slot, ":target_team", "$fplayer_team_no", ":slot"),
                (team_slot_eq, ":ally_team", ":slot", ":target_team"),
                (store_add, ":slot", slot_team_d0_target_division, grc_infantry),
                (team_slot_eq, ":ally_team", ":slot", ":target_division"),
                (call_script, "script_battlegroup_get_position", pos0, ":ally_team", grc_infantry),
                (get_distance_between_positions, ":distance_to_ally", Infantry_Pos, pos0),
                (lt, ":distance_to_ally", ":distance_to_enemy_group"),	#shift only when not in melee to avoid rotation
                (call_script, "script_battlegroup_get_action_radius", ":ally_team", grc_infantry),	#move larger group less to maintain center
                (val_div, reg0, 2),	#function returns length of bg
                (position_move_x, pos1, reg0),
                
                #shift allies left to clear dead player troops when both attacking the
                #same enemy battlegroup
              (else_try),
                (main_hero_fallen),
                (eq, AI_Replace_Dead_Player, 1),
                (neq, ":team_no", "$fplayer_team_no"),
                (neg | teams_are_enemies, ":team_no", "$fplayer_team_no"),
                (store_add, ":slot", slot_team_d0_size, grc_infantry),
                (team_slot_ge, "$fplayer_team_no", ":slot", 1),
                (store_add, ":slot", slot_team_d0_target_team, grc_infantry),
                (team_get_slot, ":target_team", "$fplayer_team_no", ":slot"),
                (team_slot_eq, ":team_no", ":slot", ":target_team"),
                (store_add, ":slot", slot_team_d0_target_division, grc_infantry),
                (team_slot_eq, "$fplayer_team_no", ":slot", ":target_division"),
                (call_script, "script_battlegroup_get_position", pos0, "$fplayer_team_no", grc_infantry),
                (get_distance_between_positions, ":distance_to_ally", Infantry_Pos, pos0),
                (lt, ":distance_to_ally", ":distance_to_enemy_group"),	#shift only when not in melee to avoid rotation
                (call_script, "script_battlegroup_get_action_radius", "$fplayer_team_no", grc_infantry),	#move larger group less to maintain center
                (val_div, reg0, -2),	#function returns length of bg
                (position_move_x, pos1, reg0),
              (try_end),
            (try_end),	#obtain destination
            
            (call_script, "script_set_formation_destination", ":team_no", grc_infantry, pos1),
            
            (try_begin),
              (store_add, ":slot", slot_team_d0_formation, grc_infantry),
              (neg | team_slot_eq, ":team_no", ":slot", formation_none),
              (team_slot_ge, ":team_no", ":slot", formation_none),
              (call_script, "script_get_centering_amount", ":infantry_formation", ":num_infantry", ":spacing"),
              (position_move_x, pos1, reg0),
              (call_script, "script_form_infantry", ":team_no", grc_infantry, ":team_leader", ":spacing", 0, ":infantry_formation"),
            (try_end),
          (try_end),	#attempt to make formation somewhere
        (try_end),
        
        #cavalry AI
        (try_begin),
          (gt, ":num_cavalry", 0),
          
          #get distance to nearest enemy battlegroup(s)
          (store_add, ":slot", slot_team_d0_armor, grc_cavalry),
          (team_get_slot, ":average_armor", ":team_no", ":slot"),
          (assign, ":nearest_threat_distance", Far_Away),
          (assign, ":nearest_target_distance", Far_Away),
          (assign, ":num_targets", 0),
          (try_for_range, ":enemy_team_no", 0, 4),
            (team_slot_ge, ":enemy_team_no", slot_team_size, 1),
            (teams_are_enemies, ":enemy_team_no", ":team_no"),
            (try_for_range, ":enemy_division", 0, 9),
              (store_add, ":slot", slot_team_d0_size, ":enemy_division"),
              (team_get_slot, ":size_enemy_battle_group", ":enemy_team_no", ":slot"),
              (gt, ":size_enemy_battle_group", 0),
              (call_script, "script_battlegroup_get_position", pos0, ":enemy_team_no", ":enemy_division"),
              (get_distance_between_positions, ":distance_of_enemy", Cavalry_Pos, pos0),
              (try_begin),	#threat or target?
                (store_add, ":slot", slot_team_d0_weapon_length, ":enemy_division"),
                (team_get_slot, reg0, ":enemy_team_no", ":slot"),
                (assign, ":decision_index", reg0),
                (store_add, ":slot", slot_team_d0_armor, ":enemy_division"),
                (team_get_slot, reg0, ":enemy_team_no", ":slot"),
                (val_mul, ":decision_index", reg0),
                (val_mul, ":decision_index", ":size_enemy_battle_group"),
                (val_div, ":decision_index", ":average_armor"),
                (val_div, ":decision_index", ":num_cavalry"),
                (try_begin),
                  (neq, ":enemy_division", grc_cavalry),
                  (val_div, ":decision_index", 2),	#double count cavalry vs.  foot soldiers
                (try_end),
                (gt, ":decision_index", 100),
                (try_begin),
                  (gt, ":nearest_threat_distance", ":distance_of_enemy"),
                  (copy_position, Nearest_Threat_Pos, pos0),
                  (assign, ":nearest_threat_distance", ":distance_of_enemy"),
                (try_end),
              (else_try),
                (val_add, ":num_targets", 1),
                (gt, ":nearest_target_distance", ":distance_of_enemy"),
                (copy_position, Nearest_Target_Pos, pos0),
                (assign, ":nearest_target_distance", ":distance_of_enemy"),
                (store_add, ":slot", slot_team_d0_target_team, grc_cavalry),
                (team_set_slot, ":team_no", ":slot", ":enemy_team_no"),
                (store_add, ":slot", slot_team_d0_target_division, grc_cavalry),
                (team_set_slot, ":team_no", ":slot", ":enemy_division"),
              (try_end),
            (try_end),
          (try_end),
          (try_begin),
            (eq, ":nearest_threat_distance", Far_Away),
            (assign, ":nearest_target_guarded", 0),
          (else_try),
            (eq, ":nearest_target_distance", Far_Away),
            (assign, ":nearest_target_guarded", 1),
          (else_try),
            (get_distance_between_positions, reg0, Nearest_Target_Pos, Nearest_Threat_Pos),
            (store_div, reg1, AI_charge_distance, 2),
            (try_begin),	#ignore target too close to threat
              (le, reg0, reg1),
              (assign, ":nearest_target_guarded", 1),
            (else_try),
              (assign, ":nearest_target_guarded", 0),
            (try_end),
          (try_end),
          
          (assign, ":cavalry_order", mordr_charge), ##CABA HERE
          (try_begin),
            (teams_are_enemies, ":team_no", 0),
            (neg | team_slot_ge, 1, slot_team_reinforcement_stage, AI_Max_Reinforcements),
            (neg | team_slot_eq, 1, slot_team_reinforcement_stage, "$attacker_reinforcement_stage"),
            (assign, ":cavalry_order", mordr_hold),
          (else_try),
            (teams_are_enemies, ":team_no", 1),
            (neg | team_slot_ge, 0, slot_team_reinforcement_stage, AI_Max_Reinforcements),
            (neg | team_slot_eq, 0, slot_team_reinforcement_stage, "$defender_reinforcement_stage"),
            (assign, ":cavalry_order", mordr_hold),
          (else_try),
            (neq, ":infantry_order", mordr_charge),
            (try_begin),
              (le, "$battle_phase", BP_Jockey),
              (assign, ":cavalry_order", mordr_hold),
            (else_try),
              (eq, ":nearest_target_distance", Far_Away),
              (try_begin),
                (eq, ":num_archers", 0),
                (assign, ":distance_to_archers", 0),
              (else_try),
                (get_distance_between_positions, ":distance_to_archers", Cavalry_Pos, Archers_Pos),
              (try_end),
              (try_begin),
                (this_or_next | gt, ":enemy_agent_nearest_cavalry_dist", AI_charge_distance),
                (gt, ":distance_to_archers", AI_charge_distance),
                (assign, ":cavalry_order", mordr_hold),
              (try_end),
            (try_end),
          (try_end),
          
          (try_begin),
            (eq, ":team_no", 0),
            (assign, ":cav_destination", Team0_Cavalry_Destination),
          (else_try),
            (eq, ":team_no", 1),
            (assign, ":cav_destination", Team1_Cavalry_Destination),
          (else_try),
            (eq, ":team_no", 2),
            (assign, ":cav_destination", Team2_Cavalry_Destination),
          (else_try),
            (eq, ":team_no", 3),
            (assign, ":cav_destination", Team3_Cavalry_Destination),
          (try_end),
          (store_add, ":slot", slot_team_d0_percent_ranged, grc_cavalry),
          (team_get_slot, reg0, ":team_no", ":slot"),
          
          #horse archers don't use wedge
          (try_begin),
            (ge, reg0, 50),
            (call_script, "script_formation_end", ":team_no", grc_cavalry),
            (try_begin),
              (eq, ":num_archers", 0),
              (team_get_movement_order, reg0, ":team_no", grc_cavalry),
              (try_begin),
                (neq, reg0, mordr_charge),
                (team_give_order, ":team_no", grc_cavalry, mordr_charge),
              (try_end),
            (else_try),
              (team_get_movement_order, reg0, ":team_no", grc_cavalry),
              (try_begin),
                (neq, reg0, ":cavalry_order"),
                (team_give_order, ":team_no", grc_cavalry, ":cavalry_order"),
              (try_end),
              (copy_position, ":cav_destination", Archers_Pos),
              (position_move_y, ":cav_destination", -500, 0),
              (call_script, "script_set_formation_destination", ":team_no", grc_cavalry, ":cav_destination"),
            (try_end),
            
            #close in with no unguarded target farther off, free fight
          (else_try),
            (eq, ":cavalry_order", mordr_charge),
            (this_or_next | eq, ":num_archers", 0),
            (le, ":enemy_agent_nearest_cavalry_dist", AI_charge_distance),
            (try_begin),
              (eq, ":num_targets", 1),
              (eq, ":nearest_target_guarded", 0),
              (gt, ":nearest_target_distance", ":nearest_threat_distance"),
              (assign, reg0, 0),
            (else_try),
              (ge, ":num_targets", 2),
              (eq, ":nearest_target_guarded", 1),
              (assign, reg0, 0),
            (else_try),
              (assign, reg0, 1),
            (try_end),
            (eq, reg0, 1),
            (call_script, "script_formation_end", ":team_no", grc_cavalry),
            (team_get_movement_order, reg0, ":team_no", grc_cavalry),
            (try_begin),
              (neq, reg0, mordr_charge),
              (team_give_order, ":team_no", grc_cavalry, mordr_charge),
            (try_end),
            
            #grand charge if target closer than threat AND not guarded
          (else_try),
            (lt, ":nearest_target_distance", ":nearest_threat_distance"),
            (eq, ":nearest_target_guarded", 0),
            (call_script, "script_formation_end", ":team_no", grc_cavalry),
            (team_get_movement_order, reg0, ":team_no", grc_cavalry),
            (try_begin),
              (neq, reg0, mordr_hold),
              (team_give_order, ":team_no", grc_cavalry, mordr_hold),
            (try_end),
            
            #lead archers up to firing point
            (try_begin),
              (gt, ":nearest_target_distance", AI_firing_distance),
              (eq, ":cavalry_order", mordr_hold),
              (try_begin),
                (eq, ":num_archers", 0),
                (copy_position, ":cav_destination", Cavalry_Pos),	#must be reinforcements, so gather at average position
              (else_try),
                (copy_position, ":cav_destination", Archers_Pos),
                (position_move_y, ":cav_destination", AI_charge_distance, 0),
              (try_end),
              
              #then CHARRRRGE!
            (else_try),
              (copy_position, ":cav_destination", Cavalry_Pos),
              (call_script, "script_point_y_toward_position", ":cav_destination", Nearest_Target_Pos),
              (position_move_y, ":cav_destination", ":nearest_target_distance", 0),
            (try_end),
            (call_script, "script_set_formation_destination", ":team_no", grc_cavalry, ":cav_destination"),
            
            #make a wedge somewhere
          (else_try),
            (try_begin),
              (eq, ":cavalry_order", mordr_charge),
              (neq, ":nearest_target_distance", Far_Away),
              (copy_position, ":cav_destination", Cavalry_Pos),
              (call_script, "script_point_y_toward_position", ":cav_destination", Nearest_Target_Pos),
              (position_move_y, ":cav_destination", ":nearest_target_distance", 0),
              (position_move_y, ":cav_destination", AI_charge_distance, 0),	#charge on through to the other side
            (else_try),
              (neq, ":cavalry_order", mordr_charge),
              (eq, ":num_archers", 0),
              (copy_position, ":cav_destination", Cavalry_Pos),	#must be reinforcements, so gather at average position
            (else_try),
              (copy_position, ":cav_destination", Archers_Pos),	#hold near archers
              (position_move_x, ":cav_destination", 500, 0),
              (position_move_y, ":cav_destination", -1000, 0),
            (try_end),
            
            #move around threat in the way to destination
            (try_begin),
              (neq, ":nearest_threat_distance", Far_Away),
              (call_script, "script_point_y_toward_position", Cavalry_Pos, Nearest_Threat_Pos),
              (call_script, "script_point_y_toward_position", Nearest_Threat_Pos, ":cav_destination"),
              (position_get_rotation_around_z, reg0, Cavalry_Pos),
              (position_get_rotation_around_z, reg1, Nearest_Threat_Pos),
              (store_sub, ":rotation_diff", reg0, reg1),
              (try_begin),
                (lt, ":rotation_diff", -180),
                (val_add, ":rotation_diff", 360),
              (else_try),
                (gt, ":rotation_diff", 180),
                (val_sub, ":rotation_diff", 360),
              (try_end),
              
              (try_begin),
                (is_between, ":rotation_diff", -135, 136),
                (copy_position, ":cav_destination", Cavalry_Pos),
                (assign, ":distance_to_move", AI_firing_distance),
                (try_begin),	#target is left of threat
                  (is_between, ":rotation_diff", -135, 0),
                  (val_mul, ":distance_to_move", -1),
                (try_end),
                (position_move_x, ":cav_destination", ":distance_to_move", 0),
                (store_sub, ":distance_to_move", ":nearest_threat_distance", AI_firing_distance),
                (position_move_y, ":cav_destination", ":distance_to_move", 0),
                (call_script, "script_point_y_toward_position", ":cav_destination", Cavalry_Pos),
                (position_rotate_z, ":cav_destination", 180),
              (try_end),
            (try_end),
            (get_scene_boundaries, pos0, pos1),
            (position_get_x, reg0, ":cav_destination"),
            (position_get_x, reg1, pos0),
            (val_max, reg0, reg1),
            (position_get_x, reg1, pos1),
            (val_min, reg0, reg1),
            (position_set_x, ":cav_destination", reg0),
            (position_get_y, reg0, ":cav_destination"),
            (position_get_y, reg1, pos0),
            (val_max, reg0, reg1),
            (position_get_y, reg1, pos1),
            (val_min, reg0, reg1),
            (position_set_y, ":cav_destination", reg0),
            (position_set_z_to_ground_level, ":cav_destination"),
            
            (try_begin),
              (call_script, "script_cf_battlegroup_valid_formation", ":team_no", grc_cavalry, formation_wedge),
              (copy_position, pos1, ":cav_destination"),
              (call_script, "script_form_cavalry", ":team_no", grc_cavalry, ":team_leader", 0, 0),
              (store_add, ":slot", slot_team_d0_formation, grc_cavalry),
              (team_set_slot, ":team_no", ":slot", formation_wedge),
              # (team_give_order, ":team_no", grc_cavalry, mordr_hold),
            (else_try),
              (call_script, "script_formation_end", ":team_no", grc_cavalry),
              (team_get_movement_order, reg0, ":team_no", grc_cavalry),
              (try_begin),
                (neq, reg0, ":cavalry_order"),
                (team_give_order, ":team_no", grc_cavalry, ":cavalry_order"),
              (try_end),
            (try_end),
            (call_script, "script_set_formation_destination", ":team_no", grc_cavalry, ":cav_destination"),
          (try_end),
        (try_end),
        
        #place leader
        (try_begin),
          (ge, ":team_leader", 0),
          (agent_is_alive, ":team_leader"),
          (agent_slot_eq, ":team_leader", slot_agent_is_running_away, 0),
          (try_begin),
            (le, ":num_infantry", 0),
            (try_begin),
              (this_or_next | le, ":num_archers", 0),
              (eq, ":archer_order", mordr_retreat),
              
              (assign, ":more_reinforcements", 1),
              (try_begin),
                (teams_are_enemies, ":team_no", 0),
                (team_slot_ge, 1, slot_team_reinforcement_stage, AI_Max_Reinforcements),
                (assign, ":more_reinforcements", 0),
              (else_try),
                (teams_are_enemies, ":team_no", 1),
                (team_slot_ge, 0, slot_team_reinforcement_stage, AI_Max_Reinforcements),
                (assign, ":more_reinforcements", 0),
              (try_end),
              (eq, ":more_reinforcements", 0),
              
              (agent_clear_scripted_mode, ":team_leader"),
              (agent_start_running_away, ":team_leader"),
              (agent_set_slot, ":team_leader",  slot_agent_is_running_away, 1),
            (else_try),
              (eq, ":archer_order", mordr_charge),
              (agent_clear_scripted_mode, ":team_leader"),
            (else_try),
              (copy_position, pos1, Archers_Pos),
              (position_move_y, pos1, -1000, 0),
              (agent_set_scripted_destination, ":team_leader", pos1, 1),
            (try_end),
          (else_try),
            (neq, ":place_leader_by_infantry", 0),
            (call_script, "script_battlegroup_get_position", pos1, ":team_no", grc_infantry),
            (team_get_order_position, pos0, ":team_no", grc_infantry),
            (call_script, "script_point_y_toward_position", pos1, pos0),
            (call_script, "script_battlegroup_get_action_radius", ":team_no", grc_infantry),
            (val_div, reg0, 2),	#bring to edge of battlegroup
            (position_move_x, pos1, reg0, 0),
            (position_move_x, pos1, 100, 0),
            (agent_set_scripted_destination, ":team_leader", pos1, 1),
          (else_try),
            (agent_clear_scripted_mode, ":team_leader"),
          (try_end),
        (try_end),
      (try_end),
      
  ]),
  
  # script_field_tactics by motomataru
  # Input: flag 1 to include ranged
  # Output: none
  ("field_tactics", [(store_script_param, ":include_ranged", 1),
      
      (assign, ":largest_team_size", 0),
      (assign, ":battle_size", 0),
      (try_for_range, ":ai_team", 0, 4),
        (team_get_slot, ":team_size", ":ai_team", slot_team_size),
        (gt, ":team_size", 0),
        (team_get_slot, ":team_cav_size", ":ai_team", slot_team_num_cavalry),
        (store_add, ":team_adj_size", ":team_size", ":team_cav_size"),	#double count cavalry to capture effect on battlefield
        (val_add, ":battle_size", ":team_adj_size"),
        
        (try_begin),
          (neq, ":ai_team", "$fplayer_team_no"),
          (neg | teams_are_enemies, ":ai_team", "$fplayer_team_no"),
          (team_get_slot, ":player_team_adj_size", "$fplayer_team_no", slot_team_adj_size),
          (val_add, ":team_adj_size", ":player_team_adj_size"),	#ally team takes player team into account
          (team_set_slot, "$fplayer_team_no", slot_team_adj_size, ":team_adj_size"),	#and vice versa
        (try_end),
        (team_set_slot, ":ai_team", slot_team_adj_size, ":team_adj_size"),
        
        (lt, ":largest_team_size", ":team_adj_size"),
        (assign, ":largest_team_size", ":team_adj_size"),
      (try_end),
      
      #apply tactics to every AI team
      (set_show_messages, 0),
      (try_for_range, ":ai_team", 0, 4),
        (team_get_slot, ":ai_team_size", ":ai_team", slot_team_adj_size),
        (gt, ":ai_team_size", 0),
        
        (assign, ":do_it", 0),
        (try_begin),
          (neq, ":ai_team", "$fplayer_team_no"),
          (assign, ":do_it", 1),
        (else_try),
          (main_hero_fallen),    #have AI take over for mods with post-player battle action
          (eq, AI_Replace_Dead_Player, 1),
          (assign, ":do_it", 1),
        (try_end),
        (eq, ":do_it", 1),
        
        (team_get_slot, ":ai_faction", ":ai_team", slot_team_faction),
        (try_begin),
          (neq, AI_for_kingdoms_only, 0),
          (neq, ":ai_faction", fac_deserters),	#deserters have military training
          (neq, ":ai_faction", fac_mountain_bandits),	#scoti, frank and dena pirates have military training Chief anade
          (neg | is_between, ":ai_faction", cultures_begin, kingdoms_end),
          
          (call_script, "script_formation_end", ":ai_team", grc_everyone),
          (team_get_movement_order, reg0, ":ai_team", grc_everyone),
          (try_begin),
            (neq, reg0, mordr_charge),
            (team_give_order, ":ai_team", grc_everyone, mordr_charge),
          (try_end),
          
          #uses tactics
        (else_try),
          (val_mul, ":ai_team_size", 100),
          (store_div, ":team_percentage", ":ai_team_size", ":largest_team_size"),
          (store_div, ":team_battle_presence", ":ai_team_size", ":battle_size"),
          (try_begin),
            (eq, ":include_ranged", 1),
            (try_begin),
              (store_mod, ":team_phase", ":ai_team", 2),
              (eq, ":team_phase", 0),
              (assign, ":time_slice", 0),
            (else_try),
              (store_div, ":time_slice", Reform_Trigger_Modulus, 2),
            (try_end),
            
            (store_mod, reg0, "$ranged_clock", Reform_Trigger_Modulus),
            (this_or_next | eq, reg0, ":time_slice"),
            (eq, "$battle_phase", BP_Setup),
            (call_script, "script_team_field_ranged_tactics", ":ai_team", ":team_percentage", ":team_battle_presence"),
          (try_end),
          
          (try_begin),
            (gt, "$fplayer_team_no", 0),	#not a spectator
            (neg | main_hero_fallen),
            (store_add, ":slot", slot_team_d0_target_team, grc_infantry),
            (team_slot_eq, ":ai_team", ":slot", "$fplayer_team_no"),
            (store_add, ":slot", slot_team_d0_target_division, grc_infantry),
            (team_get_slot, ":enemy_division", ":ai_team", ":slot"),
            (store_add, ":slot", slot_team_d0_size, ":enemy_division"),
            (team_slot_ge, "$fplayer_team_no", ":slot", 1),
            (store_add, ":slot", slot_team_d0_fclock, ":enemy_division"),
            (team_get_slot, ":fclock", "$fplayer_team_no", ":slot"),
            (store_mod, reg0, ":fclock", Reform_Trigger_Modulus),
            (store_div, ":time_slice", Reform_Trigger_Modulus, 2),
          (else_try),
            (store_mod, reg0, "$ranged_clock", Reform_Trigger_Modulus),
            (store_mod, ":team_phase", ":ai_team", 2),
            (eq, ":team_phase", 0),
            (assign, ":time_slice", 0),
          (else_try),
            (store_div, ":time_slice", Reform_Trigger_Modulus, 2),
          (try_end),
          
          (eq, reg0, ":time_slice"),
          (call_script, "script_team_field_melee_tactics", ":ai_team", ":team_percentage", ":team_battle_presence"),
        (try_end),
      (try_end),
      (set_show_messages, 1),]),
  
  
  # # Utilities used by AI by motomataru
  
  # script_find_high_ground_around_pos1_corrected by motomataru
  # Input: arg1: destination position
  #			arg2: search_radius (in meters)
  #			pos1 should hold center_position_no
  # Output: destination contains highest ground within a <search_radius> meter
  # square around pos1
  # Also uses position registers: pos0
  ("find_high_ground_around_pos1_corrected", [(store_script_param, ":destination_pos", 1),
      (store_script_param, ":search_radius", 2),
      (assign, ":fixed_point_multiplier", 1),
      (convert_to_fixed_point, ":fixed_point_multiplier"),
      (set_fixed_point_multiplier, 1),
      
      (position_get_x, ":o_x", pos1),
      (position_get_y, ":o_y", pos1),
      (store_sub, ":min_x", ":o_x", ":search_radius"),
      (store_sub, ":min_y", ":o_y", ":search_radius"),
      (store_add, ":max_x", ":o_x", ":search_radius"),
      (store_add, ":max_y", ":o_y", ":search_radius"),
      
      (get_scene_boundaries, ":destination_pos", pos0),
      (position_get_x, ":scene_min_x", ":destination_pos"),
      (position_get_x, ":scene_max_x", pos0),
      (position_get_y, ":scene_min_y", ":destination_pos"),
      (position_get_y, ":scene_max_y", pos0),
      (val_max, ":min_x", ":scene_min_x"),
      (val_max, ":min_y", ":scene_min_y"),
      (val_min, ":max_x", ":scene_max_x"),
      (val_min, ":max_y", ":scene_max_y"),
      
      (assign, ":highest_pos_z", -100),
      (copy_position, ":destination_pos", pos1),
      (init_position, pos0),
      
      (try_for_range, ":i_x", ":min_x", ":max_x"),
        (try_for_range, ":i_y", ":min_y", ":max_y"),
          (position_set_x, pos0, ":i_x"),
          (position_set_y, pos0, ":i_y"),
          (position_set_z_to_ground_level, pos0),
          (position_get_z, ":cur_pos_z", pos0),
          (try_begin),
            (gt, ":cur_pos_z", ":highest_pos_z"),
            (copy_position, ":destination_pos", pos0),
            (assign, ":highest_pos_z", ":cur_pos_z"),
          (try_end),
        (try_end),
      (try_end),
      
      (set_fixed_point_multiplier, ":fixed_point_multiplier"),]),
  
  
  # script_cf_count_casualties by motomataru
  # Input: none
  # Output: evalates T/F, reg0 num casualties
  ("cf_count_casualties", [(assign, ":num_casualties", 0),
      (try_for_agents,":cur_agent"),
        (try_begin),
          (this_or_next | agent_is_wounded, ":cur_agent"),
          (this_or_next | agent_slot_eq, ":cur_agent", slot_agent_is_running_away, 1),
          (neg | agent_is_alive, ":cur_agent"),
          (val_add, ":num_casualties", 1),
        (try_end),
      (try_end),
      (assign, reg0, ":num_casualties"),
      (gt, ":num_casualties", 0)]),
  
  
  # script_cf_any_fighting by motomataru
  # Input: none
  # Output: evalates T/F
  ("cf_any_fighting", [(assign, ":any_fighting", 0),
      (try_for_range, ":team", 0, 4),
        (team_slot_ge, ":team", slot_team_size, 1),
        (eq, ":any_fighting", 0),
        (assign, ":num_divs", 9),
        (try_for_range, ":division", 0, ":num_divs"),
          (store_add, ":slot", slot_team_d0_is_fighting, ":division"),
          (team_slot_ge, ":team", ":slot", 1),
          (assign, ":any_fighting", 1),
          (assign, ":num_divs", 0),
        (try_end),
      (try_end),
      
      #lag this check to be sure
      (store_mission_timer_c, ":time_stamp"),
      (val_max, ":time_stamp", 6),  #avoid false positives the first five seconds of mission
      (try_begin),	#time lag
        (gt, ":any_fighting", 0),
        (assign, "$teams_last_fighting", ":time_stamp"),
      (try_end),
      (assign, ":fighting_finished", formation_reform_interval),
      (val_max, ":fighting_finished", 5),
      (val_add, ":fighting_finished", "$teams_last_fighting"),
      (gt, ":fighting_finished", ":time_stamp"),]),
  
  
  # script_get_nearest_enemy_battlegroup_location by motomataru
  # Input: destination position, fron team, from position
  # Output: destination position, reg0 with distance
  # Run script_store_battlegroup_data before calling!
  ("get_nearest_enemy_battlegroup_location", [(store_script_param, ":bgposition", 1),
      (store_script_param, ":team_no", 2),
      (store_script_param, ":from_pos", 3),
      (assign, ":distance_to_nearest_enemy_battlegoup", Far_Away),
      (try_for_range, ":enemy_team_no", 0, 4),
        (team_slot_ge, ":enemy_team_no", slot_team_size, 1),
        (teams_are_enemies, ":enemy_team_no", ":team_no"),
        (try_for_range, ":enemy_division", 0, 9),
          (store_add, ":slot", slot_team_d0_size, ":enemy_division"),
          (team_slot_ge, ":enemy_team_no", ":slot", 1),
          (call_script, "script_battlegroup_get_position", pos0, ":enemy_team_no", ":enemy_division"),
          (get_distance_between_positions, reg0, pos0, ":from_pos"),
          (try_begin),
            (gt, ":distance_to_nearest_enemy_battlegoup", reg0),
            (assign, ":distance_to_nearest_enemy_battlegoup", reg0),
            (copy_position, ":bgposition", pos0),
          (try_end),
        (try_end),
      (try_end),
      (assign, reg0, ":distance_to_nearest_enemy_battlegoup")]),
  # #AI end
  
  # #Formations Scripts
  # script_field_start_position by motomataru
  # Input: team
  # Output: pos2 = current army position advanced by cavalry wedge depth over
  # infantry formation depth
  # Originally written to prevent map border accidents when setting up player
  # army at its spawn point
  ("field_start_position", [(store_script_param, ":fteam", 1),
      
      (assign, ":depth_cavalry", 0),
      (assign, ":largest_mounted_division_size", 0),
      (team_get_leader, ":fleader", ":fteam"),
      
      (try_begin),
        (ge, ":fleader", 0),
        (agent_get_position, pos2, ":fleader"),
      (else_try),
        (call_script, "script_battlegroup_get_position", pos2, ":fteam", grc_everyone),
      (try_end),
      
      (try_for_range, ":division", 0, 9),
        (store_add, ":slot", slot_team_d0_type, ":division"),
        (team_slot_eq, ":fteam", ":slot", sdt_cavalry),
        (store_add, ":slot", slot_team_d0_size, ":division"),
        (team_get_slot, reg0, ":fteam", ":slot"),
        (lt, ":largest_mounted_division_size", reg0),
        (assign, ":largest_mounted_division_size", reg0),
      (try_end),
      
      (try_begin),
        (gt, ":largest_mounted_division_size", 0),
        (val_mul, ":largest_mounted_division_size", 2),
        (convert_to_fixed_point, ":largest_mounted_division_size"),
        (store_sqrt, ":depth_cavalry", ":largest_mounted_division_size"),
        (convert_from_fixed_point, ":depth_cavalry"),
        (val_sub, ":depth_cavalry", 1),
        
        (store_mul, reg0, formation_start_spread_out, 50),
        (val_add, reg0, formation_minimum_spacing_horse_length),
        (val_mul, ":depth_cavalry", reg0),
        
        (store_mul, ":depth_infantry", formation_start_spread_out, 50),
        (val_add, ":depth_infantry", formation_minimum_spacing),
        (val_mul, ":depth_infantry", 2),
        (val_sub, ":depth_cavalry", ":depth_infantry"),
        
        (gt, ":depth_cavalry", 0),
        (call_script, "script_team_get_position_of_enemies", Enemy_Team_Pos, ":fteam", grc_everyone),
        (call_script, "script_point_y_toward_position", pos2, Enemy_Team_Pos),
        (position_move_y, pos2, ":depth_cavalry"),
      (try_end),]),
  
  # script_division_reset_places by motomataru
  # Input: none
  # Output: none
  # Resets globals for placing divisions around player for
  # script_battlegroup_place_around_leader
  ("division_reset_places", [(assign, "$next_cavalry_place", formation_minimum_spacing_horse_width),	#first spot RIGHT of the player
      (assign, "$next_archer_place", 1000),	#first spot 10m FRONT of the player
      (assign, "$next_infantry_place", -1 * formation_minimum_spacing_horse_width),	#first spot LEFT of the player
  ]),
  
  # script_battlegroup_place_around_leader by motomataru
  # Input: team, division, team leader
  # Output: pos61 division position, moves pos1
  ("battlegroup_place_around_leader", [(store_script_param, ":fteam", 1),
      (store_script_param, ":fdivision", 2),
      (store_script_param, ":fleader", 3),
      
      (try_begin),
        (le, ":fleader", 0),
        (display_message, "@{!}script_battlegroup_place_around_leader: invalid leader agent (bad call)"),
        
      (else_try),
        (agent_get_group, reg0, ":fleader"),
        (neq, reg0, ":fteam"),
        (display_message, "@{!}script_battlegroup_place_around_leader: leader team mismatch (bad call)"),
        
      (else_try),
        (agent_get_position, pos1, ":fleader"),
        (call_script, "script_battlegroup_place_around_pos1", ":fteam", ":fdivision", ":fleader"),
      (try_end),]),
  
  # script_battlegroup_place_around_pos1 by motomataru
  # Input: team, division
  # Output: pos61 division position, moves pos1
  ("battlegroup_place_around_pos1", [(store_script_param, ":fteam", 1),
      (store_script_param, ":fdivision", 2),
      (store_script_param, ":fleader", 3),
      
      (assign, ":store_fpm", 1),
      (convert_to_fixed_point, ":store_fpm"),
      (set_fixed_point_multiplier, 100),
      
      (store_sub, ":player_division", "$FormAI_player_in_division", 1),
      (try_begin),
        (eq, ":player_division", ":fdivision"),
        (assign, ":first_member_is_player", 1),
      (else_try),
        (assign, ":first_member_is_player", 0),
      (try_end),
      
      (try_begin),
        (eq, "$FormAI_autorotate", 1),
        (call_script, "script_team_get_position_of_enemies", Enemy_Team_Pos, ":fteam", grc_everyone),
        (neq, reg0, 0),	#more than 0 enemies still alive?
        (call_script, "script_point_y_toward_position", pos1, Enemy_Team_Pos),
      (try_end),
      
      (store_add, ":slot", slot_team_d0_type, ":fdivision"),
      (team_get_slot, ":sd_type", ":fteam", ":slot"),
      (store_add, ":slot", slot_team_d0_size, ":fdivision"),
      (team_get_slot, ":num_troops", ":fteam", ":slot"),
      (store_add, ":slot", slot_team_d0_formation, ":fdivision"),
      (team_get_slot, ":fformation", ":fteam", ":slot"),
      (store_add, ":slot", slot_team_d0_formation_space, ":fdivision"),
      (team_get_slot, ":formation_extra_spacing", ":fteam", ":slot"),
      
      #handle memorized placement
      (try_begin),
        (eq, ":first_member_is_player", 0),
        (store_add, ":slot", slot_faction_d0_mem_relative_x_flag, ":fdivision"),
        (faction_get_slot, ":value", "fac_player_faction", ":slot"),	#only used for player now
        (neq, ":value", 0),
        
        (position_move_x, pos1, ":value", 0),
        (store_add, ":slot", slot_faction_d0_mem_relative_y, ":fdivision"),
        (faction_get_slot, ":value", "fac_player_faction", ":slot"),	#only used for player now
        (position_move_y, pos1, ":value", 0),
        (copy_position, pos61, pos1),
        (try_begin),
          (gt, ":fformation", formation_none),
          (try_begin),
            (this_or_next | eq, ":sd_type", sdt_cavalry),
            (eq, ":sd_type", sdt_harcher),
            (call_script, "script_form_cavalry", ":fteam", ":fdivision", ":fleader", ":formation_extra_spacing", 0),
          (else_try),
            (eq, ":sd_type", sdt_archer),
            (call_script, "script_get_centering_amount", formation_default, ":num_troops", ":formation_extra_spacing"),
            (val_mul, reg0, -1),
            (position_move_x, pos1, reg0, 0),
            (call_script, "script_form_archers", ":fteam", ":fdivision", ":fleader", ":formation_extra_spacing", 0, ":fformation"),
          (else_try),
            (call_script, "script_get_centering_amount", ":fformation", ":num_troops", ":formation_extra_spacing"),
            (position_move_x, pos1, reg0, 0),
            (call_script, "script_form_infantry", ":fteam", ":fdivision", ":fleader", ":formation_extra_spacing", 0, ":fformation"),
          (try_end),
        (try_end),
        
        #default placement per division type
      (else_try),
        (this_or_next | eq, ":sd_type", sdt_cavalry),
        (eq, ":sd_type", sdt_harcher),
        (try_begin),
          (eq, ":first_member_is_player", 0),
          (position_move_x, pos1, "$next_cavalry_place", 0),
        (try_end),
        
        (try_begin),
          (gt, ":fformation", formation_none),
          (store_mul, ":troop_space", ":formation_extra_spacing", 50),
          (val_add, ":troop_space", formation_minimum_spacing_horse_width),
          (convert_to_fixed_point, ":num_troops"),
          (store_sqrt, ":formation_width", ":num_troops"),
          (val_mul, ":formation_width", ":troop_space"),
          (convert_from_fixed_point, ":formation_width"),
          (val_sub, ":formation_width", ":troop_space"),
          (store_div, reg0, ":formation_width", 2),
          (position_move_x, pos1, reg0, 0),	#cavalry set up RIGHT of leader
          (copy_position, pos61, pos1),
          (call_script, "script_form_cavalry", ":fteam", ":fdivision", ":fleader", ":formation_extra_spacing", ":first_member_is_player"),
          
          #handle Native's way of doing things
        (else_try),
          (store_mul, ":troop_space", ":formation_extra_spacing", 133),	#cm added by each Spread Out
          (val_add, ":troop_space", 150),	#minimum spacing
          
          #WFaS multi-ranks
          (try_begin),
            (eq, ":fformation", formation_2_row),
            (val_div, ":num_troops", 2),
          (else_try),
            (eq, ":fformation", formation_3_row),
            (val_div, ":num_troops", 3),
          (else_try),
            (eq, ":fformation", formation_4_row),
            (val_div, ":num_troops", 4),
          (else_try),
            (eq, ":fformation", formation_5_row),
            (val_div, ":num_troops", 5),
            
          (else_try),	#WB multi-ranks
            (lt, ":formation_extra_spacing", 0),
            (assign, ":troop_space", 200),
            (val_mul, ":formation_extra_spacing", -1),
            (val_add, ":formation_extra_spacing", 1),
            (val_div, ":num_troops", ":formation_extra_spacing"),
          (try_end),
          
          (store_mul, ":formation_width", ":num_troops", ":troop_space"),
          (store_div, reg0, ":formation_width", 2),
          (position_move_x, pos1, reg0, 0),	#cavalry set up RIGHT of leader
          (copy_position, pos61, pos1),
        (try_end),
        
        (try_begin),
          (eq, ":first_member_is_player", 0),
          (val_add, "$next_cavalry_place", ":formation_width"),
          (val_add, "$next_cavalry_place", formation_minimum_spacing_horse_width),
        (try_end),
        
      (else_try),
        (eq, ":sd_type", sdt_archer),
        (try_begin),
          (eq, ":first_member_is_player", 0),
          (position_move_y, pos1, "$next_archer_place"),	#archers set up FRONT of leader
          (val_add, "$next_archer_place", 500),	#next archers 5m FRONT of these
        (try_end),
        (copy_position, pos61, pos1),
        (try_begin),
          (gt, ":fformation", formation_none),
          (call_script, "script_get_centering_amount", formation_default, ":num_troops", ":formation_extra_spacing"),
          (val_mul, reg0, -1),
          (position_move_x, pos1, reg0, 0),
          (call_script, "script_form_archers", ":fteam", ":fdivision", ":fleader", ":formation_extra_spacing", ":first_member_is_player", ":fformation"),
        (try_end),
        
      (else_try),
        (eq, ":sd_type", sdt_skirmisher),
        (try_begin),
          (eq, ":first_member_is_player", 0),
          (position_move_y, pos1, "$next_archer_place"),	#skirmishers set up FRONT of leader
          (val_add, "$next_archer_place", 500),	#next archers 5m FRONT of these
        (try_end),
        (copy_position, pos61, pos1),
        (try_begin),
          (gt, ":fformation", formation_none),
          (call_script, "script_get_centering_amount", ":fformation", ":num_troops", ":formation_extra_spacing"),
          (position_move_x, pos1, reg0, 0),
          (call_script, "script_form_infantry", ":fteam", ":fdivision", ":fleader", ":formation_extra_spacing", ":first_member_is_player", ":fformation"),
        (try_end),
        
      (else_try),
        (try_begin),
          (eq, ":first_member_is_player", 0),
          (position_move_x, pos1, "$next_infantry_place", 0),
        (try_end),
        (copy_position, pos61, pos1),
        
        (try_begin),
          (gt, ":fformation", formation_none),
          (call_script, "script_form_infantry", ":fteam", ":fdivision", ":fleader", ":formation_extra_spacing", ":first_member_is_player", ":fformation"),
          (call_script, "script_get_centering_amount", ":fformation", ":num_troops", ":formation_extra_spacing"),
          (store_mul, ":formation_width", 2, reg0),
          (store_mul, ":troop_space", ":formation_extra_spacing", 50),
          (val_add, ":troop_space", formation_minimum_spacing),
          (val_add, ":formation_width", ":troop_space"),
          (val_mul, reg0, -1),	#infantry set up LEFT of leader
          (position_move_x, pos61, reg0, 0),
          
          #handle Native's way of doing things
        (else_try),
          (store_mul, ":troop_space", ":formation_extra_spacing", 75),	#Native minimum spacing not consistent but less than this
          (val_add, ":troop_space", 100),	#minimum spacing
          
          #WFaS multi-ranks
          (try_begin),
            (eq, ":fformation", formation_2_row),
            (val_div, ":num_troops", 2),
          (else_try),
            (eq, ":fformation", formation_3_row),
            (val_div, ":num_troops", 3),
          (else_try),
            (eq, ":fformation", formation_4_row),
            (val_div, ":num_troops", 4),
          (else_try),
            (eq, ":fformation", formation_5_row),
            (val_div, ":num_troops", 5),
            
          (else_try),	#WB multi-ranks
            (lt, ":formation_extra_spacing", 0),
            (assign, ":troop_space", 150),
            (val_mul, ":formation_extra_spacing", -1),
            (val_add, ":formation_extra_spacing", 1),
            (val_div, ":num_troops", ":formation_extra_spacing"),
          (try_end),
          
          (store_mul, ":formation_width", ":num_troops", ":troop_space"),
          (store_div, reg0, ":formation_width", 2),
          (val_mul, reg0, -1),	#infantry set up LEFT of leader
          (position_move_x, pos61, reg0, 0),
        (try_end),
        
        (try_begin),
          (eq, ":first_member_is_player", 0),
          (val_sub, "$next_infantry_place", ":formation_width"),	#next infantry 1m LEFT of these
          (val_sub, "$next_infantry_place", 100),
        (try_end),
      (try_end),
      
      (store_add, ":slot", slot_team_d0_move_order, ":fdivision"),
      (team_set_slot, ":fteam", ":slot", mordr_hold),
      (set_show_messages, 0),
      (team_get_movement_order, reg0, ":fteam", ":fdivision"),
      (try_begin),
        (neq, reg0, mordr_hold),
        (team_give_order, ":fteam", ":fdivision", mordr_hold),
      (try_end),
      (call_script, "script_set_formation_destination", ":fteam", ":fdivision", pos61),
      (set_show_messages, 1),
      (set_fixed_point_multiplier, ":store_fpm"),]),
  
  # script_form_cavalry by motomataru
  # Input: (pos1), team, division, agent number of team leader, spacing, flag
  # TRUE to include team leader in formation
  # Output: none
  # Form in wedge, (now not) excluding horse archers
  # Creates formation starting at pos1
  ("form_cavalry", [(store_script_param, ":fteam", 1),
      (store_script_param, ":fdivision", 2),
      (store_script_param, ":fleader", 3),
      (store_script_param, ":formation_extra_spacing", 4),
      (store_script_param, ":include_leader", 5),
      (store_mul, ":extra_space", ":formation_extra_spacing", 50),
      (store_add, ":x_distance", formation_minimum_spacing_horse_width, ":extra_space"),
      (store_add, ":y_distance", formation_minimum_spacing_horse_length, ":extra_space"),
      (assign, ":max_level", 0),
      (try_for_agents, ":agent"),
        (call_script, "script_cf_valid_formation_member", ":fteam", ":fdivision", ":fleader", ":agent"),
        (agent_get_troop_id, ":troop_id", ":agent"),
        (store_character_level, ":troop_level", ":troop_id"),
        (gt, ":troop_level", ":max_level"),
        (assign, ":max_level", ":troop_level"),
      (end_try),
      (assign, ":column", 1),
      (assign, ":rank_dimension", 1),
      (store_mul, ":neg_y_distance", ":y_distance", -1),
      (store_mul, ":neg_x_distance", ":x_distance", -1),
      (store_div, ":wedge_adj", ":x_distance", 2),
      (store_div, ":neg_wedge_adj", ":neg_x_distance", 2),
      (assign, ":form_left", 1),
      (try_begin),
        (eq, ":include_leader", 0),
        (store_add, ":slot", slot_team_d0_first_member, ":fdivision"),
        (team_set_slot, ":fteam", ":slot", -1),
      (else_try),	#after leader, move to next position (copied from below)
        (team_set_slot, ":fteam", ":slot", ":fleader"),
        (try_begin),
          (eq, ":form_left", 1),
          (position_move_x, pos1, ":neg_x_distance", 0),
        (else_try),
          (position_move_x, pos1, ":x_distance", 0),
        (try_end),
        (val_add, ":column", 1),
        (gt, ":column", ":rank_dimension"),
        (position_move_y, pos1, ":neg_y_distance", 0),
        (try_begin),
          (neq, ":form_left", 1),
          (assign, ":form_left", 1),
          (position_move_x, pos1, ":neg_wedge_adj", 0),
        (else_try),
          (assign, ":form_left", 0),
          (position_move_x, pos1, ":wedge_adj", 0),
        (try_end),
        (assign, ":column", 1),
        (val_add, ":rank_dimension", 1),
      (try_end),
      
      (val_add, ":max_level", 1),
      (try_for_range_backwards, ":rank_level", 0, ":max_level"),	#put troops with highest exp in front
        (try_for_agents, ":agent"),
          (agent_get_troop_id, ":troop_id", ":agent"),
          (store_character_level, ":troop_level", ":troop_id"),
          (eq, ":troop_level", ":rank_level"),
          (call_script, "script_cf_valid_formation_member", ":fteam", ":fdivision", ":fleader", ":agent"),
          (agent_set_scripted_destination, ":agent", pos1, 1),
          (try_begin),	#First Agent
            (store_add, ":slot", slot_team_d0_first_member, ":fdivision"),
            (neg | team_slot_ge, ":fteam", ":slot", 0),
            (team_set_slot, ":fteam", ":slot", ":agent"),
          (try_end),
          (try_begin),
            (eq, ":form_left", 1),
            (position_move_x, pos1, ":neg_x_distance", 0),
          (else_try),
            (position_move_x, pos1, ":x_distance", 0),
          (try_end),
          (val_add, ":column", 1),
          (gt, ":column", ":rank_dimension"),
          (position_move_y, pos1, ":neg_y_distance", 0),
          (try_begin),
            (neq, ":form_left", 1),
            (assign, ":form_left", 1),
            (position_move_x, pos1, ":neg_wedge_adj", 0),
          (else_try),
            (assign, ":form_left", 0),
            (position_move_x, pos1, ":wedge_adj", 0),
          (try_end),
          (assign, ":column", 1),
          (val_add, ":rank_dimension", 1),
        (end_try),
      (end_try),]),
  
  # script_form_archers by motomataru
  # Input: (pos1), team, division, agent number of team leader, spacing, flag
  # TRUE to include team leader in formation, formation
  # Output: none
  # Form in line, staggered if formation = formation_ranks
  # Creates formation starting at pos1
  ("form_archers", [(store_script_param, ":fteam", 1),
      (store_script_param, ":fdivision", 2),
      (store_script_param, ":fleader", 3),
      (store_script_param, ":formation_extra_spacing", 4),
      (store_script_param, ":include_leader", 5),
      (store_script_param, ":archers_formation", 6),
      (store_mul, ":extra_space", ":formation_extra_spacing", 50),
      (store_add, ":distance", formation_minimum_spacing, ":extra_space"),		#minimum distance between troops
      (assign, ":total_move_y", 0),	#staggering variable
      (try_begin),
        (eq, ":include_leader", 0),
        (store_add, ":slot", slot_team_d0_first_member, ":fdivision"),
        (team_set_slot, ":fteam", ":slot", -1),
      (else_try),	#after leader, move to next position (copied from below)
        (team_set_slot, ":fteam", ":slot", ":fleader"),
        (position_move_x, pos1, ":distance", 0),
        (try_begin),
          (eq, ":archers_formation", formation_ranks),
          (val_add, ":total_move_y", 75),
          (try_begin),
            (le, ":total_move_y", 150),
            (position_move_y, pos1, 75, 0),
          (else_try),
            (position_move_y, pos1, -150, 0),
            (assign, ":total_move_y", 0),
          (try_end),
        (try_end),
      (try_end),
      
      (try_for_agents, ":agent"),
        (call_script, "script_cf_valid_formation_member", ":fteam", ":fdivision", ":fleader", ":agent"),
        (agent_set_scripted_destination, ":agent", pos1, 1),
        (try_begin),	#First Agent
          (store_add, ":slot", slot_team_d0_first_member, ":fdivision"),
          (neg | team_slot_ge, ":fteam", ":slot", 0),
          (team_set_slot, ":fteam", ":slot", ":agent"),
        (try_end),
        (position_move_x, pos1, ":distance", 0),
        (try_begin),
          (eq, ":archers_formation", formation_ranks),
          (val_add, ":total_move_y", 75),
          (try_begin),
            (le, ":total_move_y", 150),
            (position_move_y, pos1, 75, 0),
          (else_try),
            (position_move_y, pos1, -150, 0),
            (assign, ":total_move_y", 0),
          (try_end),
        (try_end),
      (try_end),]),
  
  # script_form_infantry by motomataru
  # Input: (pos1), team, division, agent number of team leader, spacing, flag
  # TRUE to include team leader in formation, formation
  # Output: none
  # If input "formation" is formation_default, will select a formation based on
  # faction
  # Creates formation starting at pos1
  ("form_infantry", [(store_script_param, ":fteam", 1),
      (store_script_param, ":fdivision", 2),
      (store_script_param, ":fleader", 3),
      (store_script_param, ":formation_extra_spacing", 4),
      (store_script_param, ":include_leader", 5),
      (store_script_param, ":infantry_formation", 6),
      (store_mul, ":extra_space", ":formation_extra_spacing", 50),
      (store_add, ":distance", formation_minimum_spacing, ":extra_space"),		#minimum distance between troops
      (store_mul, ":neg_distance", ":distance", -1),
      (store_add, ":slot", slot_team_d0_size, ":fdivision"),
      (team_get_slot, ":num_troops", ":fteam", ":slot"),
      (try_begin),
        (eq, ":infantry_formation", formation_default),
        (call_script, "script_get_default_formation", ":fteam"),
        (assign, ":infantry_formation", reg0),
      (try_end),
      (team_get_weapon_usage_order, ":weapon_order", ":fteam", ":fdivision"),
      (team_get_hold_fire_order, ":fire_order", ":fteam", ":fdivision"),
      (assign, ":form_left", 1),
      (assign, ":column", 1),
      (assign, ":rank", 1),
      
      (try_begin),
        (eq, ":infantry_formation", formation_square),
        (convert_to_fixed_point, ":num_troops"),
        (store_sqrt, ":square_dimension", ":num_troops"),
        (convert_from_fixed_point, ":square_dimension"),
        (val_add, ":square_dimension", 1),
        (try_begin),
          (eq, ":include_leader", 0),
          (store_add, ":slot", slot_team_d0_first_member, ":fdivision"),
          (team_set_slot, ":fteam", ":slot", -1),
        (else_try),	#after leader, move to next position (copied from below)
          (team_set_slot, ":fteam", ":slot", ":fleader"),
          (try_begin),
            (eq, ":form_left", 1),
            (position_move_x, pos1, ":neg_distance", 0),
          (else_try),
            (position_move_x, pos1, ":distance", 0),
          (try_end),
          (val_add, ":column", 1),
          (gt, ":column", ":square_dimension"),
          (position_move_y, pos1, ":neg_distance", 0),
          (try_begin),
            (neq, ":form_left", 1),
            (assign, ":form_left", 1),
            (position_move_x, pos1, ":neg_distance", 0),
          (else_try),
            (assign, ":form_left", 0),
            (position_move_x, pos1, ":distance", 0),
          (try_end),
          (assign, ":column", 1),
          (val_add, ":rank", 1),
        (try_end),
        
        (try_for_agents, ":agent"),
          (call_script, "script_cf_valid_formation_member", ":fteam", ":fdivision", ":fleader", ":agent"),
          (call_script, "script_switch_to_noswing_weapons", ":agent", ":distance"),
          
          (try_begin),
            (eq, "$battle_phase", BP_Deploy),
            (agent_set_scripted_destination, ":agent", pos1),
          (else_try),
            (call_script, "script_formation_process_agent_move", ":fteam", ":fdivision", ":agent", ":rank"),
          (try_end),
          
          (try_begin),
            (eq, formation_reequip, 1),
            (eq, ":weapon_order", wordr_use_any_weapon),
            (try_begin),
              (this_or_next | eq, ":rank", 1),
              (this_or_next | ge, ":rank", ":square_dimension"),
              (this_or_next | eq, ":column", 1),
              (ge, ":column", ":square_dimension"),
              (call_script, "script_equip_best_melee_weapon", ":agent", 0, 0, ":fire_order"),
              (agent_set_slot, ":agent", slot_agent_inside_formation, 0),
              (agent_ai_set_always_attack_in_melee, ":agent", 0),
            (else_try),
              (agent_get_slot, ":closest_enemy", ":agent", slot_agent_nearest_enemy_agent),
              (try_begin),
                (neq, ":closest_enemy", -1),
                (agent_get_position, pos0, ":closest_enemy"),
                (get_distance_between_positions, ":enemy_distance", pos0, pos1),
                (le, ":enemy_distance", ":distance"),	#enemy closer than friends?
                (neg | position_is_behind_position, pos0, pos1),
                (call_script, "script_equip_best_melee_weapon", ":agent", 0, 0, ":fire_order"),
                (try_begin),
                  (position_is_behind_position, pos1, pos0),
                  (agent_ai_set_always_attack_in_melee, ":agent", 1),
                (else_try),
                  (agent_ai_set_always_attack_in_melee, ":agent", 0),
                (try_end),
              (else_try),
                (call_script, "script_equip_best_melee_weapon", ":agent", 0, 1, ":fire_order"),
                (agent_ai_set_always_attack_in_melee, ":agent", 1),
              (try_end),
              (agent_set_slot, ":agent", slot_agent_inside_formation, 1),
            (try_end),
          (try_end),
          (try_begin),
            (eq, ":form_left", 1),
            (position_move_x, pos1, ":neg_distance", 0),
          (else_try),
            (position_move_x, pos1, ":distance", 0),
          (try_end),
          (val_add, ":column", 1),
          (gt, ":column", ":square_dimension"),
          (position_move_y, pos1, ":neg_distance", 0),
          (try_begin),
            (neq, ":form_left", 1),
            (assign, ":form_left", 1),
            (position_move_x, pos1, ":neg_distance", 0),
          (else_try),
            (assign, ":form_left", 0),
            (position_move_x, pos1, ":distance", 0),
          (try_end),
          (assign, ":column", 1),
          (val_add, ":rank", 1),
        (end_try),
        
      (else_try),
        (eq, ":infantry_formation", formation_wedge),
        (try_for_range, reg0, 0, 50),
          (troop_set_slot, "trp_temp_array_a", reg0, 0),
        (try_end),
        (assign, ":max_level", 0),
        (try_for_agents, ":agent"),
          (call_script, "script_cf_valid_formation_member", ":fteam", ":fdivision", ":fleader", ":agent"),
          (call_script, "script_switch_to_noswing_weapons", ":agent", ":distance"),
          (agent_get_troop_id, ":troop_id", ":agent"),
          (store_character_level, ":troop_level", ":troop_id"),
          (troop_set_slot, "trp_temp_array_a", ":troop_level", 1),
          (gt, ":troop_level", ":max_level"),
          (assign, ":max_level", ":troop_level"),
        (end_try),
        
        (assign, ":rank_dimension", 1),
        (store_div, ":wedge_adj", ":distance", 2),
        (store_div, ":neg_wedge_adj", ":neg_distance", 2),
        (try_begin),
          (eq, ":include_leader", 0),
          (store_add, ":slot", slot_team_d0_first_member, ":fdivision"),
          (team_set_slot, ":fteam", ":slot", -1),
        (else_try),	#after leader, move to next position (copied from below)
          (team_set_slot, ":fteam", ":slot", ":fleader"),
          (try_begin),
            (eq, ":form_left", 1),
            (position_move_x, pos1, ":neg_distance", 0),
          (else_try),
            (position_move_x, pos1, ":distance", 0),
          (try_end),
          (val_add, ":column", 1),
          (gt, ":column", ":rank_dimension"),
          (position_move_y, pos1, ":neg_distance", 0),
          (try_begin),
            (neq, ":form_left", 1),
            (assign, ":form_left", 1),
            (position_move_x, pos1, ":neg_wedge_adj", 0),
          (else_try),
            (assign, ":form_left", 0),
            (position_move_x, pos1, ":wedge_adj", 0),
          (try_end),
          (assign, ":column", 1),
          (val_add, ":rank_dimension", 1),
        (try_end),
        
        (val_add, ":max_level", 1),
        (try_for_range_backwards, ":rank_level", 0, ":max_level"),	#put troops with highest exp in front
          (troop_slot_eq, "trp_temp_array_a", ":rank_level", 1),
          (try_for_agents, ":agent"),
            (agent_get_troop_id, ":troop_id", ":agent"),
            (store_character_level, ":troop_level", ":troop_id"),
            (eq, ":troop_level", ":rank_level"),
            (call_script, "script_cf_valid_formation_member", ":fteam", ":fdivision", ":fleader", ":agent"),
            
            (try_begin),
              (eq, "$battle_phase", BP_Deploy),
              (agent_set_scripted_destination, ":agent", pos1),
            (else_try),
              (call_script, "script_formation_process_agent_move", ":fteam", ":fdivision", ":agent", ":rank_dimension"),
            (try_end),
            
            (try_begin),
              (eq, formation_reequip, 1),
              (eq, ":weapon_order", wordr_use_any_weapon),
              (try_begin),
                (this_or_next | eq, ":column", 1),
                (ge, ":column", ":rank_dimension"),
                (call_script, "script_equip_best_melee_weapon", ":agent", 0, 0, ":fire_order"),
                (agent_set_slot, ":agent", slot_agent_inside_formation, 0),
                (agent_ai_set_always_attack_in_melee, ":agent", 0),
              (else_try),
                (agent_get_slot, ":closest_enemy", ":agent", slot_agent_nearest_enemy_agent),
                (try_begin),
                  (neq, ":closest_enemy", -1),
                  (agent_get_position, pos0, ":closest_enemy"),
                  (get_distance_between_positions, ":enemy_distance", pos0, pos1),
                  (le, ":enemy_distance", ":distance"),	#enemy closer than friends?
                  (neg | position_is_behind_position, pos0, pos1),
                  (call_script, "script_equip_best_melee_weapon", ":agent", 0, 0, ":fire_order"),
                  (try_begin),
                    (position_is_behind_position, pos1, pos0),
                    (agent_ai_set_always_attack_in_melee, ":agent", 1),
                  (else_try),
                    (agent_ai_set_always_attack_in_melee, ":agent", 0),
                  (try_end),
                (else_try),
                  (call_script, "script_equip_best_melee_weapon", ":agent", 0, 1, ":fire_order"),
                  (agent_ai_set_always_attack_in_melee, ":agent", 1),
                (try_end),
                (agent_set_slot, ":agent", slot_agent_inside_formation, 1),
              (try_end),
            (try_end),
            (try_begin),
              (eq, ":form_left", 1),
              (position_move_x, pos1, ":neg_distance", 0),
            (else_try),
              (position_move_x, pos1, ":distance", 0),
            (try_end),
            (val_add, ":column", 1),
            (gt, ":column", ":rank_dimension"),
            (position_move_y, pos1, ":neg_distance", 0),
            (try_begin),
              (neq, ":form_left", 1),
              (assign, ":form_left", 1),
              (position_move_x, pos1, ":neg_wedge_adj", 0),
            (else_try),
              (assign, ":form_left", 0),
              (position_move_x, pos1, ":wedge_adj", 0),
            (try_end),
            (assign, ":column", 1),
            (val_add, ":rank_dimension", 1),
          (end_try),
        (end_try),
        
      (else_try),
        (eq, ":infantry_formation", formation_ranks),
        (try_for_range, reg0, 0, 50),
          (troop_set_slot, "trp_temp_array_a", reg0, 0),
        (try_end),
        (call_script, "script_calculate_default_ranks", ":num_troops"),
        (store_div, ":rank_dimension", ":num_troops", reg1),
        (val_add, ":rank_dimension", 1),
        (assign, ":max_level", 0),
        (try_for_agents, ":agent"),
          (call_script, "script_cf_valid_formation_member", ":fteam", ":fdivision", ":fleader", ":agent"),
          (call_script, "script_switch_to_noswing_weapons", ":agent", ":distance"),
          (agent_get_troop_id, ":troop_id", ":agent"),
          (store_character_level, ":troop_level", ":troop_id"),
          (troop_set_slot, "trp_temp_array_a", ":troop_level", 1),
          (gt, ":troop_level", ":max_level"),
          (assign, ":max_level", ":troop_level"),
        (end_try),
        
        (try_begin),
          (eq, ":include_leader", 0),
          (store_add, ":slot", slot_team_d0_first_member, ":fdivision"),
          (team_set_slot, ":fteam", ":slot", -1),
        (else_try),	#after leader, move to next position (copied from below)
          (team_set_slot, ":fteam", ":slot", ":fleader"),
          (try_begin),
            (eq, ":form_left", 1),
            (position_move_x, pos1, ":neg_distance", 0),
          (else_try),
            (position_move_x, pos1, ":distance", 0),
          (try_end),
          (val_add, ":column", 1),
          
          (gt, ":column", ":rank_dimension"),	#next rank?
          (position_move_y, pos1, ":neg_distance", 0),
          (try_begin),
            (neq, ":form_left", 1),
            (assign, ":form_left", 1),
            (position_move_x, pos1, ":neg_distance", 0),
          (else_try),
            (assign, ":form_left", 0),
            (position_move_x, pos1, ":distance", 0),
          (try_end),
          (assign, ":column", 1),
          (val_add, ":rank", 1),
        (try_end),
        
        (val_add, ":max_level", 1),
        (try_for_range_backwards, ":rank_level", 0, ":max_level"),	#put troops with highest exp in front
          (troop_slot_eq, "trp_temp_array_a", ":rank_level", 1),
          (try_for_agents, ":agent"),
            (agent_get_troop_id, ":troop_id", ":agent"),
            (store_character_level, ":troop_level", ":troop_id"),
            (eq, ":troop_level", ":rank_level"),
            (call_script, "script_cf_valid_formation_member", ":fteam", ":fdivision", ":fleader", ":agent"),
            
            (try_begin),
              (eq, "$battle_phase", BP_Deploy),
              (agent_set_scripted_destination, ":agent", pos1),
            (else_try),
              (call_script, "script_formation_process_agent_move", ":fteam", ":fdivision", ":agent", ":rank"),
            (try_end),
            
            (try_begin),
              (eq, formation_reequip, 1),
              (eq, ":weapon_order", wordr_use_any_weapon),
              (try_begin),
                (eq, ":rank", 1),
                (call_script, "script_equip_best_melee_weapon", ":agent", 0, 0, ":fire_order"),
                (agent_set_slot, ":agent", slot_agent_inside_formation, 0),
                (agent_ai_set_always_attack_in_melee, ":agent", 0),
              (else_try),
                (agent_get_slot, ":closest_enemy", ":agent", slot_agent_nearest_enemy_agent),
                (try_begin),
                  (neq, ":closest_enemy", -1),
                  (agent_get_position, pos0, ":closest_enemy"),
                  (get_distance_between_positions, ":enemy_distance", pos0, pos1),
                  (le, ":enemy_distance", ":distance"),	#enemy closer than friends?
                  (neg | position_is_behind_position, pos0, pos1),
                  (call_script, "script_equip_best_melee_weapon", ":agent", 0, 0, ":fire_order"),
                  (try_begin),
                    (position_is_behind_position, pos1, pos0),
                    (agent_ai_set_always_attack_in_melee, ":agent", 1),
                  (else_try),
                    (agent_ai_set_always_attack_in_melee, ":agent", 0),
                  (try_end),
                (else_try),
                  (call_script, "script_equip_best_melee_weapon", ":agent", 0, 1, ":fire_order"),
                  (agent_ai_set_always_attack_in_melee, ":agent", 1),
                (try_end),
                (agent_set_slot, ":agent", slot_agent_inside_formation, 1),
              (try_end),
            (try_end),
            (try_begin),
              (eq, ":form_left", 1),
              (position_move_x, pos1, ":neg_distance", 0),
            (else_try),
              (position_move_x, pos1, ":distance", 0),
            (try_end),
            (val_add, ":column", 1),
            
            (gt, ":column", ":rank_dimension"),	#next rank?
            (position_move_y, pos1, ":neg_distance", 0),
            (try_begin),
              (neq, ":form_left", 1),
              (assign, ":form_left", 1),
              (position_move_x, pos1, ":neg_distance", 0),
            (else_try),
              (assign, ":form_left", 0),
              (position_move_x, pos1, ":distance", 0),
            (try_end),
            (assign, ":column", 1),
            (val_add, ":rank", 1),
          (end_try),
        (end_try),
        
      (else_try),
        (eq, ":infantry_formation", formation_shield),
        (call_script, "script_calculate_default_ranks", ":num_troops"),
        (store_div, ":rank_dimension", ":num_troops", reg1),
        (val_add, ":rank_dimension", 1),
        (try_begin),
          (eq, ":include_leader", 0),
          (store_add, ":slot", slot_team_d0_first_member, ":fdivision"),
          (team_set_slot, ":fteam", ":slot", -1),
        (else_try),	#after leader, move to next position (copied from below)
          (team_set_slot, ":fteam", ":slot", ":fleader"),
          (try_begin),
            (eq, ":form_left", 1),
            (position_move_x, pos1, ":neg_distance", 0),
          (else_try),
            (position_move_x, pos1, ":distance", 0),
          (try_end),
          (val_add, ":column", 1),
          
          (gt, ":column", ":rank_dimension"),	#next rank?
          (position_move_y, pos1, ":neg_distance", 0),
          (try_begin),
            (neq, ":form_left", 1),
            (assign, ":form_left", 1),
            (position_move_x, pos1, ":neg_distance", 0),
          (else_try),
            (assign, ":form_left", 0),
            (position_move_x, pos1, ":distance", 0),
          (try_end),
          (assign, ":column", 1),
          (val_add, ":rank", 1),
        (try_end),
        
        (troop_set_slot, "trp_temp_array_a", 0, 0),	#short weap agent array
        (troop_set_slot, "trp_temp_array_b", 0, 0),	#medium weap agent array
        (troop_set_slot, "trp_temp_array_c", 0, 0),	#long weap agent array
        
        (try_for_agents, ":agent"),
          (call_script, "script_cf_valid_formation_member", ":fteam", ":fdivision", ":fleader", ":agent"),
          (call_script, "script_switch_to_noswing_weapons", ":agent", ":distance"),
          
          (assign, ":cur_score", 0),
          (try_for_range, ":item_slot", ek_item_0, ek_head),
            (agent_get_item_slot, ":item", ":agent", ":item_slot"),
            (gt, ":item", itm_no_item),
            (item_get_type, ":weapon_type", ":item"),
            (neq, ":weapon_type", itp_type_shield),
            
            (try_begin),
              (call_script, "script_cf_is_weapon_ranged", ":item", 1),
              
            (else_try),
              (item_get_weapon_length, ":item_length", ":item"),
              (lt, ":cur_score", ":item_length"),
              (assign, ":cur_score", ":item_length"),
            (try_end),
          (try_end),
          
          (try_begin),
            (eq, ":cur_score", 0),	#no melee weapons
            (assign, ":cur_array", "trp_temp_array_c"),
          (else_try),
            (le, ":cur_score", Third_Max_Weapon_Length),
            (assign, ":cur_array", "trp_temp_array_a"),
          (else_try),
            (gt, ":cur_score", 2 * Third_Max_Weapon_Length),
            (assign, ":cur_array", "trp_temp_array_c"),
          (else_try),
            (assign, ":cur_array", "trp_temp_array_b"),
          (try_end),
          
          (troop_get_slot, ":array_end", ":cur_array", 0),
          (val_add, ":array_end", 1),
          (troop_set_slot, ":cur_array", ":array_end", ":agent"),
          (troop_set_slot, ":cur_array", 0, ":array_end"),
          (agent_set_slot, ":agent", slot_agent_positioned, 0),
        (try_end),
        
        #find shields first
        (store_add, ":arrays_end", "trp_temp_array_c", 1),
        (try_for_range, ":cur_array", "trp_temp_array_a", ":arrays_end"),
          (troop_get_slot, ":array_end", ":cur_array", 0),
          (val_add, ":array_end", 1),
          
          (try_for_range, ":slot", 1, ":array_end"),
            (troop_get_slot, ":agent", ":cur_array", ":slot"),
            (assign, ":form_up", 0),
            
            (try_for_range, ":item_slot", ek_item_0, ek_head),
              (agent_get_item_slot, ":item", ":agent", ":item_slot"),
              (gt, ":item", itm_no_item),
              (item_get_type, ":weapon_type", ":item"),
              (eq, ":weapon_type", itp_type_shield),
              (item_get_weapon_length, reg0, ":item"),	#gets shield width, which is always defined (see header_items)
              (ge, reg0, 25),	#wider than troop?
              (assign, ":form_up", 1),
            (try_end),
            
            (eq, ":form_up", 1),
            (agent_set_slot, ":agent", slot_agent_positioned, 1),
            (agent_set_slot, ":agent", slot_agent_inside_formation, 0),
            
            (try_begin),
              (eq, "$battle_phase", BP_Deploy),
              (agent_set_scripted_destination, ":agent", pos1),
            (else_try),
              (call_script, "script_formation_process_agent_move", ":fteam", ":fdivision", ":agent", ":rank"),
            (try_end),
            
            (try_begin),
              (eq, formation_reequip, 1),
              (eq, ":weapon_order", wordr_use_any_weapon),
              (call_script, "script_equip_best_melee_weapon", ":agent", 1, 0, ":fire_order"),	#best weapon, force shield
            (try_end),
            
            (try_begin),
              (eq, ":form_left", 1),
              (position_move_x, pos1, ":neg_distance", 0),
            (else_try),
              (position_move_x, pos1, ":distance", 0),
            (try_end),
            (val_add, ":column", 1),
            
            (gt, ":column", ":rank_dimension"),	#next rank?
            (position_move_y, pos1, ":neg_distance", 0),
            (try_begin),
              (neq, ":form_left", 1),
              (assign, ":form_left", 1),
              (position_move_x, pos1, ":neg_distance", 0),
            (else_try),
              (assign, ":form_left", 0),
              (position_move_x, pos1, ":distance", 0),
            (try_end),
            (assign, ":column", 1),
            (val_add, ":rank", 1),
            
            #break loops
            (assign, ":array_end", ":slot"),
            (assign, ":arrays_end", ":cur_array"),
          (try_end),
        (try_end),
        
        #add rest of division
        (store_add, ":arrays_end", "trp_temp_array_c", 1),
        (try_for_range, ":cur_array", "trp_temp_array_a", ":arrays_end"),
          (troop_get_slot, ":array_end", ":cur_array", 0),
          (val_add, ":array_end", 1),
          
          (try_for_range, ":slot", 1, ":array_end"),
            (troop_get_slot, ":agent", ":cur_array", ":slot"),
            (agent_slot_eq, ":agent", slot_agent_positioned, 0),
            (agent_set_slot, ":agent", slot_agent_positioned, 1),
            
            (try_begin),
              (eq, ":rank", 1),
              (agent_set_slot, ":agent", slot_agent_inside_formation, 0),
            (else_try),
              (agent_set_slot, ":agent", slot_agent_inside_formation, 1),
            (try_end),
            
            (try_begin),
              (eq, "$battle_phase", BP_Deploy),
              (agent_set_scripted_destination, ":agent", pos1),
            (else_try),
              (call_script, "script_formation_process_agent_move", ":fteam", ":fdivision", ":agent", ":rank"),
            (try_end),
            
            (try_begin),
              (eq, formation_reequip, 1),
              (eq, ":weapon_order", wordr_use_any_weapon),
              
              (try_begin),
                (eq, ":rank", 1),
                (call_script, "script_equip_best_melee_weapon", ":agent", 0, 0, ":fire_order"),
                (agent_ai_set_always_attack_in_melee, ":agent", 0),
                
                #enemy closer than friends?
              (else_try),
                (agent_get_slot, ":closest_enemy", ":agent", slot_agent_nearest_enemy_agent),
                (neq, ":closest_enemy", -1),
                (agent_get_position, pos0, ":closest_enemy"),
                (get_distance_between_positions, ":enemy_distance", pos0, pos1),
                (le, ":enemy_distance", ":distance"),
                (neg | position_is_behind_position, pos0, pos1),
                (call_script, "script_equip_best_melee_weapon", ":agent", 0, 0, ":fire_order"),
                
                #behind enemy?
                (try_begin),
                  (position_is_behind_position, pos1, pos0),
                  (agent_ai_set_always_attack_in_melee, ":agent", 1),
                (else_try),
                  (agent_ai_set_always_attack_in_melee, ":agent", 0),
                (try_end),
                
                #equip longest weapon and avoid defensive
              (else_try),
                (call_script, "script_equip_best_melee_weapon", ":agent", 0, 1, ":fire_order"),
                (agent_ai_set_always_attack_in_melee, ":agent", 1),
              (try_end),
            (try_end),
            
            (try_begin),
              (eq, ":form_left", 1),
              (position_move_x, pos1, ":neg_distance", 0),
            (else_try),
              (position_move_x, pos1, ":distance", 0),
            (try_end),
            (val_add, ":column", 1),
            
            (gt, ":column", ":rank_dimension"),	#next rank?
            (position_move_y, pos1, ":neg_distance", 0),
            (try_begin),
              (neq, ":form_left", 1),
              (assign, ":form_left", 1),
              (position_move_x, pos1, ":neg_distance", 0),
            (else_try),
              (assign, ":form_left", 0),
              (position_move_x, pos1, ":distance", 0),
            (try_end),
            (assign, ":column", 1),
            (val_add, ":rank", 1),
          (try_end),
        (try_end),
      (try_end),
      
      #calculate percent in place from counts from section above (see
      #script_formation_process_agent_move)
      (store_add, ":slot", slot_team_d0_size, ":fdivision"),
      (team_get_slot, ":num_troops", ":fteam", ":slot"),
      (store_add, ":slot", slot_team_d0_percent_in_place, ":fdivision"),
      
      (try_begin),
        (eq, ":num_troops", 0),
        (team_set_slot, ":fteam", ":slot", 0),
      (else_try),
        (team_get_slot, reg0, ":fteam", ":slot"),
        (val_mul, reg0, 100),
        (val_div, reg0, ":num_troops"),
        (team_set_slot, ":fteam", ":slot", reg0),
      (try_end),
  ]),
  
  # script_get_default_formation by motomataru
  # Input: team id
  # Output: reg0 default formation
  ("get_default_formation", [(store_script_param, ":fteam", 1),
      (team_get_slot, ":ffaction", ":fteam", slot_team_faction),
      (try_begin),
        (this_or_next | eq, ":ffaction", fac_player_supporters_faction),
        (eq, ":ffaction", fac_player_faction),
        (is_between, "$players_kingdom", npc_kingdoms_begin, npc_kingdoms_end),
        (assign, ":ffaction", "$players_kingdom"),
      (try_end),
      
      (try_begin),
        (is_between, ":ffaction", "fac_player_faction", kingdoms_end),
        (faction_slot_ge, ":ffaction", slot_faction_culture, 1),
        (faction_get_slot, ":ffaction", ":ffaction", slot_faction_culture),
      (try_end),
      
      #assign default formation
      (try_begin),
        (eq, ":ffaction", fac_culture_norse),
        (assign, reg0, formation_shield),
      (else_try),
        (eq, ":ffaction", fac_culture_saxon),
        (assign, reg0, formation_shield),
      (else_try),
        (eq, ":ffaction", fac_culture_angle),
        (assign, reg0, formation_shield),
      (else_try),
        (eq, ":ffaction", fac_culture_welsh),
        (assign, reg0, formation_ranks),
      (else_try),
        (eq, ":ffaction", fac_culture_irish),
        (assign, reg0, formation_ranks),
      (else_try),
        (eq, ":ffaction", fac_culture_scotch),
        (assign, reg0, formation_ranks),
      (else_try),
        (this_or_next | eq, ":ffaction", fac_player_supporters_faction),
        (eq, ":ffaction", fac_player_faction),	#independent player
        (assign, reg0, formation_ranks),
      (else_try),
        (assign, reg0, formation_none),	#riffraff don't use formations
      (try_end),]),
  
  # script_switch_to_noswing_weapons by motomataru
  # Input: agent, formation spacing
  # Output: none
  ("switch_to_noswing_weapons", [(store_script_param, ":agent", 1),
      (store_script_param, ":formation_spacing", 2),
      (try_for_range, ":item_slot", ek_item_0, ek_head),
        (agent_get_item_slot, ":item", ":agent", ":item_slot"),
        (call_script, "script_cf_is_thrusting_weapon", ":item"),
        (item_get_weapon_length, ":weap_len",":item"),
        
        (try_begin),
          (ge, ":weap_len", ":formation_spacing"),	#avoid switching when weapon still has room to be swung
          (item_get_slot, ":noswing_version", ":item", slot_item_alternate),
          (gt, ":noswing_version", "itm_items_end"),
          (agent_unequip_item, ":agent", ":item", ":item_slot"),	#assumes first ek_* are the weapons
          (agent_equip_item, ":agent", ":noswing_version", ":item_slot"),	#assumes first ek_* are the weapons
          
          #undo legacy switches
        (else_try),
          (gt, ":item", "itm_items_end"),
          (item_get_slot, ":original_version", ":item", slot_item_alternate),
          (agent_unequip_item, ":agent", ":item", ":item_slot"),	#assumes first ek_* are the weapons
          (agent_equip_item, ":agent", ":original_version", ":item_slot"),	#assumes first ek_* are the weapons
        (try_end),
      (try_end),]),
  
  # script_switch_from_noswing_weapons by motomataru
  # Input: agent
  # Output: none
  ("switch_from_noswing_weapons", [(store_script_param, ":agent", 1),
      (try_for_range, ":item_slot", ek_item_0, ek_head),
        (agent_get_item_slot, ":item", ":agent", ":item_slot"),
        (gt, ":item", "itm_items_end"),
        (item_get_slot, ":original_version", ":item", slot_item_alternate),
        (agent_unequip_item, ":agent", ":item", ":item_slot"),	#assumes first ek_* are the weapons
        (agent_equip_item, ":agent", ":original_version", ":item_slot"),	#assumes first ek_* are the weapons
      (try_end),]),
  
  # script_formation_process_agent_move by motomataru
  # Input: (pos1), team, division, agent, which rank of formation agent is in
  # Output: (pos1) may change to reference first agent's anticipated position
  # This function sets scripted destination and performs other tasks related to
  # making the formation look nice on the move (and more)
  ("formation_process_agent_move", [(store_script_param, ":fteam", 1),
      (store_script_param, ":fdivision", 2),
      (store_script_param, ":agent", 3),
      (store_script_param, ":rank", 4),
      
      (agent_set_scripted_destination, ":agent", pos1, 1),
      
      (agent_get_position, Current_Pos, ":agent"),
      (get_distance_between_positions, ":distance_to_go", Current_Pos, pos1),
      
      (store_add, ":slot", slot_team_d0_speed_limit, ":fdivision"),
      (team_get_slot, ":speed_limit", ":fteam", ":slot"),
      
      (agent_get_speed, Speed_Pos, ":agent"),
      (position_transform_position_to_parent, Temp_Pos, Current_Pos, Speed_Pos),
      (call_script, "script_point_y_toward_position", Current_Pos, Temp_Pos),	#get direction of travel
      (store_mul, ":expected_travel", reg0, formation_reform_interval),
      (store_div, ":speed", ":expected_travel", Km_Per_Hour_To_Cm),
      
      #First Agent
      (try_begin),
        (store_add, ":slot", slot_team_d0_first_member, ":fdivision"),
        (neg | team_slot_ge, ":fteam", ":slot", 0),
        (team_set_slot, ":fteam", ":slot", ":agent"),
        
        (try_begin),	#reset speed when first member stopped
          (le, ":speed", 5),	#minimum observed speed
          (store_add, ":slot", slot_team_d0_speed_limit, ":fdivision"),
          (team_set_slot, ":fteam", ":slot", Top_Speed),
          (agent_set_speed_limit, ":agent", Top_Speed),
          
        (else_try),	#first member in motion
          (val_mul, ":speed", 2),	#after terrain & encumbrance, agents tend to move about half their speed limit
          (try_begin),	#speed up if everyone caught up
            (store_add, ":slot", slot_team_d0_percent_in_place, ":fdivision"),
            (team_slot_ge, ":fteam", ":slot", 100),
            (try_begin),
              (ge, ":speed", ":speed_limit"),
              (val_add, ":speed_limit", 1),
            (try_end),
          (else_try),	#else slow down
            (val_min, ":speed_limit", ":speed"),
            (val_sub, ":speed_limit", 1),
            (val_max, ":speed_limit", 5),	#minimum observed speed
          (try_end),
          
          #build formation from first agent
          (store_add, ":slot", slot_team_d0_prev_first_member, ":fdivision"),
          (team_slot_eq, ":fteam", ":slot", ":agent"),	#looking at same first member as last call?
          
          (call_script, "script_battlegroup_get_position", Temp_Pos, ":fteam", ":fdivision"),
          (get_distance_between_positions, ":distance_from_group", Current_Pos, Temp_Pos),
          (call_script, "script_battlegroup_get_action_radius", ":fteam", ":fdivision"),
          (val_div, reg0, 2),	#function returns length of bg
          (val_sub, ":distance_from_group", reg0),
          (lt, ":distance_from_group", 2000),	#within 20m of rest of division?
          
          (store_mul, ":expected_travel", ":speed_limit", Km_Per_Hour_To_Cm),
          (lt, ":expected_travel", ":distance_to_go"),	#more than one call from destination?
          
          (store_add, ":slot", slot_team_d0_speed_limit, ":fdivision"),
          (team_set_slot, ":fteam", ":slot", ":speed_limit"),
          (agent_set_speed_limit, ":agent", ":speed_limit"),
          
          (copy_position, Temp_Pos, Current_Pos),
          (call_script, "script_point_y_toward_position", Temp_Pos, pos1),
          (position_move_y, Temp_Pos, ":expected_travel", 0),	#anticipate where first member will be next
          (position_copy_rotation, Temp_Pos, pos1),	#conserve destination facing of formation
          (copy_position, pos1, Temp_Pos),	#reference the rest of the formation to first member's anticipated position
        (try_end),
        
        (store_add, ":slot", slot_team_d0_percent_in_place, ":fdivision"),
        (team_set_slot, ":fteam", ":slot", 1),	#reinit: always count first member as having arrived
        (store_add, ":slot", slot_team_d0_prev_first_member, ":fdivision"),
        (team_set_slot, ":fteam", ":slot", ":agent"),
        
        #Not First Agent
      (else_try),
        (try_begin),
          (le, ":speed", 0),
          (assign, ":speed_limit", Top_Speed),
        (else_try),
          (neg | position_is_behind_position, pos1, Current_Pos),
          (store_div, ":speed_limit", ":distance_to_go", Km_Per_Hour_To_Cm),
          (val_max, ":speed_limit", 1),
        (else_try),
          (store_add, ":slot", slot_team_d0_is_fighting, ":fdivision"),
          (team_slot_eq, ":fteam", ":slot", 0),
          (assign, ":speed_limit", 1),
        (else_try),
          (assign, ":speed_limit", Top_Speed),
        (try_end),
        (agent_set_speed_limit, ":agent", ":speed_limit"),
        (try_begin),
          (this_or_next | le, ":speed", 0),	#reached previous destination or blocked OR
          (this_or_next | lt, ":speed_limit", Top_Speed),	#destination within reach OR
          (position_is_behind_position, pos1, Current_Pos),	#agent ahead of formation
          (store_add, ":slot", slot_team_d0_percent_in_place, ":fdivision"),
          (team_get_slot, reg0, ":fteam", ":slot"),
          (val_add, reg0, 1),
          (team_set_slot, ":fteam", ":slot", reg0),
        (try_end),
      (try_end),
      
      #Housekeeping
      (agent_set_slot, ":agent", slot_agent_formation_rank, ":rank"),]),
  
  # script_pick_native_formation by motomataru
  # Input: team, division
  # Output: reg0 with formation_*_row (see module_constants)
  #         reg1 with number of rows
  ("pick_native_formation", [(store_script_param, ":team", 1),
      (store_script_param, ":division", 2),
      
      (store_add, ":slot", slot_team_d0_size, ":division"),
      (team_get_slot, ":bg_size", ":team", ":slot"),
      
      (try_begin),
        (eq, ":bg_size", 0),	#script_store_battlegroup_data is not being called
        (team_get_leader, ":leader", ":team"),
        (try_for_agents, ":agent"),
          (call_script, "script_cf_valid_formation_member", ":team", ":division", ":leader", ":agent"),
          (val_add, ":bg_size", 1),
        (try_end),
      (try_end),
      
      (call_script, "script_calculate_default_ranks", ":bg_size"),
      (try_begin),
        (eq, reg1, 1),
        (assign, reg0, formation_1_row),
      (else_try),
        (eq, reg1, 2),
        (assign, reg0, formation_2_row),
      (else_try),
        (eq, reg1, 3),
        (assign, reg0, formation_3_row),
      (else_try),
        (this_or_next | eq, reg1, 4),
        (eq, Native_Formations_Implementation, WB_Implementation),
        (assign, reg0, formation_4_row),
        (assign, reg1, 4),
      (else_try),
        (assign, reg0, formation_5_row),
        (assign, reg1, 5),
      (try_end)]),
  
  # script_calculate_default_ranks by motomataru
  # Input: number of troops
  # Output: reg1 with number of rows
  # calculates number of rows closest to the old Roman 5:1 cohort arrangement
  # (80 troops in 4 rows)
  # quadratic formula to solve (5R^2 + 5(R+1)^2)/2
  ("calculate_default_ranks", [(store_script_param, ":bg_size", 1),
      
      (val_mul, ":bg_size", 20),
      (val_sub, ":bg_size", 25),
      (convert_to_fixed_point, ":bg_size"),
      (store_sqrt, reg1, ":bg_size"),
      (convert_from_fixed_point, reg1),
      (val_sub, reg1, 5),
      (val_div, reg1, 10),
      (val_add, reg1, 1),]),
  
  # script_get_centering_amount by motomataru
  # Input: formation type, number of troops, extra spacing
  #        Use formation type formation_default to use script for archer line
  # Output: reg0 number of centimeters to adjust x-position to center formation
  ("get_centering_amount", [(store_script_param, ":troop_formation", 1),
      (store_script_param, ":num_troops", 2),
      (store_script_param, ":extra_spacing", 3),
      (store_mul, ":troop_space", ":extra_spacing", 50),
      (val_add, ":troop_space", formation_minimum_spacing),
      (assign, reg0, 0),
      (try_begin),
        (eq, ":troop_formation", formation_square),
        (convert_to_fixed_point, ":num_troops"),
        (store_sqrt, reg0, ":num_troops"),
        (convert_from_fixed_point, reg0),
        (val_mul, reg0, ":troop_space"),
        # (val_sub, reg0, ":troop_space"), MOTO not needed because column added in
        # script_form_infantry
      (else_try),
        (this_or_next | eq, ":troop_formation", formation_ranks),
        (eq, ":troop_formation", formation_shield),
        (call_script, "script_calculate_default_ranks", ":num_troops"),
        (assign, ":num_ranks", reg1),
        (store_div, reg0, ":num_troops", ":num_ranks"),
        (try_begin),
          (store_mod, reg1, ":num_troops", ":num_ranks"),
          (eq, reg1, 0),
          (val_sub, reg0, 1),
        (try_end),
        (val_mul, reg0, ":troop_space"),
      (else_try),
        (eq, ":troop_formation", formation_default),	#assume these are archers in a line
        (store_mul, reg0, ":num_troops", ":troop_space"),
      (try_end),
      (val_div, reg0, 2),]),
  
  # script_formation_end
  # Input: team, division
  # Output: none
  ("formation_end", [(store_script_param, ":fteam", 1),
      (store_script_param, ":fdivision", 2),
      (try_begin),
        (store_add, ":slot", slot_team_d0_formation, ":fdivision"),
        (neg | team_slot_eq, ":fteam", ":slot", formation_none),
        (team_slot_ge, ":fteam", ":slot", formation_none),
        
        (try_begin),
          (eq, Native_Formations_Implementation, WFaS_Implementation),
          (team_set_slot, ":fteam", ":slot", formation_2_row),
        (else_try),
          (team_set_slot, ":fteam", ":slot", formation_none),
        (try_end),
        
        (team_get_leader, ":leader", ":fteam"),
        
        (try_for_agents, ":agent"),
          (agent_is_alive, ":agent"),
          (agent_is_human, ":agent"),
          (agent_get_group, ":team", ":agent"),
          (eq, ":team", ":fteam"),
          (neq, ":leader", ":agent"),
          (agent_get_division, ":bgdivision", ":agent"),
          (eq, ":bgdivision", ":fdivision"),
          (agent_clear_scripted_mode, ":agent"),
          (call_script, "script_switch_from_noswing_weapons", ":agent"),
          (agent_ai_set_always_attack_in_melee, ":agent", 0),
          (agent_set_speed_limit, ":agent", 100),
          (agent_set_slot, ":agent", slot_agent_formation_rank, 0),
          (agent_set_slot, ":agent", slot_agent_inside_formation, 0),
        (try_end),
        
        (try_begin),
          (eq, ":fteam", "$fplayer_team_no"),
          (store_add, ":slot", slot_team_d0_formation_space, ":fdivision"),
          (team_get_slot, ":div_spacing", "$fplayer_team_no", ":slot"),
          
          #adjust for differences between the systems of spreading out (Native spreads
          #out about twice as much)
          (try_begin),
            (eq, Native_Formations_Implementation, WFaS_Implementation),
            (assign, ":max_spacing", 3),
          (else_try),
            (assign, ":max_spacing", 2),
          (try_end),
          
          (store_mul, ":double_max", ":max_spacing", 2),
          
          (try_begin),
            (ge, ":div_spacing", ":double_max"),	#beyond Native max
            (assign, ":div_spacing", ":max_spacing"),
          (else_try),
            (gt, ":div_spacing", 0),
            (set_show_messages, 0),
            (team_give_order, "$fplayer_team_no", ":fdivision", mordr_stand_closer),
            (set_show_messages, 1),
            (val_div, ":div_spacing", 2),
          (try_end),
          
          (team_set_slot, "$fplayer_team_no", ":slot", ":div_spacing"),
        (try_end),
      (try_end),]),
  
  # script_formation_move_position by motomataru
  # Input: team, division, formation current position, (1 to advance or -1 to
  # withdraw or 0 to redirect)
  # Output: pos1 (offset for centering)
  ("formation_move_position", [(store_script_param, ":fteam", 1),
      (store_script_param, ":fdivision", 2),
      (store_script_param, ":fcurrentpos", 3),
      (store_script_param, ":direction", 4),
      (copy_position, pos1, ":fcurrentpos"),
      (call_script, "script_team_get_position_of_enemies", Enemy_Team_Pos, ":fteam", grc_everyone),
      (try_begin),
        (neq, reg0, 0),	#more than 0 enemies still alive?
        (copy_position, pos1, ":fcurrentpos"),	#restore current formation "position"
        (call_script, "script_point_y_toward_position", pos1, Enemy_Team_Pos),	#record angle from center to enemy
        (assign, ":distance_to_enemy", reg0),
        (call_script, "script_get_formation_destination", pos61, ":fteam", ":fdivision"),
        (get_distance_between_positions, ":move_amount", pos1, pos61),	#distance already moving from previous orders
        (val_add, ":move_amount", 1000),
        (try_begin),
          (gt, ":direction", 0),	#moving forward?
          (gt, ":move_amount", ":distance_to_enemy"),
          (assign, ":move_amount", ":distance_to_enemy"),
        (try_end),
        (val_mul, ":move_amount", ":direction"),
        (position_move_y, pos1, ":move_amount", 0),
        (position_get_x, ":from_x", pos1),
        (position_get_y, ":from_y", pos1),
        (try_begin),
          (is_between, ":from_x", "$g_bound_left", "$g_bound_right"),
          (is_between, ":from_y", "$g_bound_bottom", "$g_bound_top"),
          (try_begin),
            (lt, ":distance_to_enemy", 1000),	#less than a move away?
            (position_copy_rotation, pos1, pos61),	#avoid rotating formation
          (try_end),
          (call_script, "script_set_formation_destination", ":fteam", ":fdivision", pos1),
          (store_add, ":slot", slot_team_d0_size, ":fdivision"),
          (team_get_slot, ":num_troops", ":fteam", ":slot"),
          (store_add, ":slot", slot_team_d0_formation_space, ":fdivision"),
          (team_get_slot, ":formation_extra_spacing", ":fteam", ":slot"),
          (try_begin),
            (store_add, ":slot", slot_team_d0_type, ":fdivision"),
            (neg | team_slot_eq, ":fteam", ":slot", sdt_archer),
            (store_add, ":slot", slot_team_d0_formation, ":fdivision"),
            (team_get_slot, ":fformation", ":fteam", ":slot"),
            (call_script, "script_get_centering_amount", ":fformation", ":num_troops", ":formation_extra_spacing"),
          (else_try),
            (call_script, "script_get_centering_amount", formation_default, ":num_troops", ":formation_extra_spacing"),
            (val_mul, reg0, -1),
          (try_end),
          (position_move_x, pos1, reg0, 0),
          
          #out of bounds
        (else_try),
          (copy_position, pos1, ":fcurrentpos"),	#restore current formation "position"
        (try_end),
      (try_end),]),
  
  # script_cf_battlegroup_valid_formation
  # Input: team, division, formation
  # Output: reg0: troop count/1 if too few troops/0 if wrong type
  ("cf_battlegroup_valid_formation", [(store_script_param, ":fteam", 1),
      (store_script_param, ":fdivision", 2),
      (store_script_param, ":fformation", 3),
      
      (assign, ":valid_type", 0),
      (store_add, ":slot", slot_team_d0_type, ":fdivision"),
      (team_get_slot, ":sd_type", ":fteam", ":slot"),
      (try_begin), #Eventually make this more complex with the sub-divisions
        (this_or_next | eq, ":sd_type", sdt_cavalry),
        (eq, ":sd_type", sdt_harcher),
        (assign, ":size_minimum", formation_min_cavalry_troops),
        (try_begin),
          (eq, ":fformation", formation_wedge),
          (assign, ":valid_type", 1),
        (try_end),
      (else_try),
        (eq, ":sd_type", sdt_archer),
        (assign, ":size_minimum", formation_min_foot_troops),
        (try_begin),
          # (this_or_next|eq, ":fformation", formation_ranks), uncheck for proper
          # ranks
          (eq, ":fformation", formation_default),
          (assign, ":valid_type", 1),
        (try_end),
      (else_try),
        (assign, ":size_minimum", formation_min_foot_troops),
        (gt, ":fformation", formation_none),
        (assign, ":valid_type", 1), #all types valid
      (try_end),
      
      (try_begin),
        (eq, ":valid_type", 0),
        (assign, ":num_troops", 0),
      (else_try),
        (store_add, ":slot", slot_team_d0_size, ":fdivision"),
        (team_get_slot, ":num_troops", ":fteam", ":slot"),
        (lt, ":num_troops", ":size_minimum"),
        (assign, ":num_troops", 1),
      (try_end),
      
      (assign, reg0, ":num_troops"),
      (gt, ":num_troops", 1)]),
  
  # script_cf_valid_formation_member by motomataru #CABA - Modified for
  # Classify_agent phase out
  # Input: team, division, agent number of team leader, test agent
  # Output: failure indicates agent is not member of formation
  ("cf_valid_formation_member", [(store_script_param, ":fteam", 1),
      (store_script_param, ":fdivision", 2),
      (store_script_param, ":fleader", 3),
      (store_script_param, ":agent", 4),
      (neq, ":fleader", ":agent"),
      (agent_get_division, ":bgdivision", ":agent"),
      # (try_begin), #Maintain any changed divisions MOTO done in
      # script_store_battlegroup_data
      # (agent_slot_ge, ":agent", slot_agent_new_division, 0),
      # (neg|agent_slot_eq, ":agent", slot_agent_new_division, ":bgdivision"),
      # (agent_get_slot, ":bgdivision", ":agent", slot_agent_new_division),
      # (agent_set_division, ":agent", ":bgdivision"),
      # (try_end),
      (eq, ":bgdivision", ":fdivision"),
      (agent_get_group, ":team", ":agent"),
      (eq, ":team", ":fteam"),
      (agent_is_alive, ":agent"),
      (agent_is_human, ":agent"),
      (agent_slot_eq, ":agent", slot_agent_is_running_away, 0),]),
  
  # script_player_order_regular_formations
  # Input: formation
  ("player_order_regular_formations", [
      (store_script_param_1, ":formation"),
      (call_script, "script_division_reset_places"),
      (try_for_range, ":division", 0, 9),
        (class_is_listening_order, "$fplayer_team_no", ":division"),
        (store_add, ":slot", slot_team_d0_target_team, ":division"),
        (team_set_slot, "$fplayer_team_no", ":slot", -1),
        (store_add, ":slot", slot_team_d0_size, ":division"),
        (team_slot_ge, "$fplayer_team_no", ":slot", 1),
        (store_add, ":slot", slot_team_d0_formation, ":division"),
        (team_set_slot, "$fplayer_team_no", ":slot", ":formation"),
        (set_show_messages, 0),
        (call_script, "script_formation_to_native_order", "$fplayer_team_no", ":division", ":formation"),
        (set_show_messages, 1),
      (try_end),
  ]),
  
  # script_player_order_extended_formations
  # Input: formation
  ("player_order_extended_formations", [
      (store_script_param_1, ":formation"),
      (call_script, "script_division_reset_places"),
      (try_for_range, ":division", 0, 9),
        (class_is_listening_order, "$fplayer_team_no", ":division"),
        (store_add, ":slot", slot_team_d0_target_team, ":division"),
        (team_set_slot, "$fplayer_team_no", ":slot", -1),
        (store_add, ":slot", slot_team_d0_size, ":division"),
        (team_slot_ge, "$fplayer_team_no", ":slot", 1),
        (store_add, ":slot", slot_team_d0_fclock, ":division"),
        (team_set_slot, "$fplayer_team_no", ":slot", 1),
        (call_script, "script_player_attempt_formation", ":division", ":formation", 1),
      (try_end),
  ]),
  
  # #Player team formations functions
  # script_player_attempt_formation
  # Inputs: arg1: division
  #			arg2: formation identifier (formation_*)
  #         arg3: flag 1 to form at current location (rather than next to
  #         player), flag 2 to form as if player were at Target_Pos
  # Output: none
  # Designed JUST for infantry
  ("player_attempt_formation", [(store_script_param, ":fdivision", 1),
      (store_script_param, ":fformation", 2),
      (store_script_param, ":form_on_spot", 3),
      (set_fixed_point_multiplier, 100),
      (try_begin),
        (eq, ":fformation", formation_ranks),
        (str_store_string, s1, "@ranks"),
      (else_try),
        (eq, ":fformation", formation_shield),
        (str_store_string, s1, "@shield wall"),
      (else_try),
        (eq, ":fformation", formation_wedge),
        (str_store_string, s1, "@wedge"),
      (else_try),
        (eq, ":fformation", formation_square),
        (str_store_string, s1, "@square"),
      (else_try),
        (str_store_string, s1, "@up"),
      (try_end),
      (str_store_class_name, s2, ":fdivision"),
      
      (try_begin),
        (call_script, "script_cf_battlegroup_valid_formation", "$fplayer_team_no", ":fdivision", ":fformation"),
        (try_begin),	#new formation?
          (store_add, ":slot", slot_team_d0_formation, ":fdivision"),
          (neg | team_slot_eq, "$fplayer_team_no", ":slot", ":fformation"),
          (team_set_slot, "$fplayer_team_no", ":slot", ":fformation"),
          (store_add, reg1, ":fdivision", 1),
          (display_message, "@Division {reg1} {s2} forming {s1}."),
          (store_add, ":slot", slot_team_d0_fclock, ":fdivision"),
          (team_set_slot, "$fplayer_team_no", ":slot", 1),
          (store_add, ":slot", slot_team_d0_target_team, ":fdivision"),
          (team_set_slot, "$fplayer_team_no", ":slot", -1),
          
          (store_add, ":slot", slot_team_d0_formation_space, ":fdivision"),
          (team_get_slot, ":div_spacing", "$fplayer_team_no", ":slot"),
          
          #bring unformed divisions into sync with formations' minimum
          (set_show_messages, 0),
          (assign, reg0, ":div_spacing"),
          (try_for_range, reg1, reg0, formation_start_spread_out),	#spread out for ease of forming up
            (team_give_order, "$fplayer_team_no", ":fdivision", mordr_spread_out),
            (val_add, ":div_spacing", 1),
          (try_end),
          (set_show_messages, 1),
          
          (team_set_slot, "$fplayer_team_no", ":slot", ":div_spacing"),
        (try_end),
        
        #divisions must stop to order themselves
        (store_add, ":slot", slot_team_d0_move_order, ":fdivision"),
        (team_get_slot, ":div_order", "$fplayer_team_no", ":slot"),
        (try_begin),
          (this_or_next | eq, ":div_order", mordr_stand_ground),
          (this_or_next | eq, ":div_order", mordr_charge),
          (eq, ":div_order", mordr_retreat),
          (call_script, "script_battlegroup_get_position", pos1, "$fplayer_team_no", ":fdivision"),
          (team_give_order, "$fplayer_team_no", ":fdivision", mordr_hold),
          (call_script, "script_set_formation_destination", "$fplayer_team_no", ":fdivision", pos1),
        (try_end),
        
      (else_try),
        (assign, ":return_val", reg0),
        (call_script, "script_formation_end", "$fplayer_team_no", ":fdivision"),
        (gt, ":fformation", formation_none),
        (store_add, reg1, ":fdivision", 1),
        (try_begin),
          (gt, ":return_val", 0),
          (display_message, "@Not enough troops in division {reg1} {s2} to form {s1}."),
        (else_try),
          (store_add, ":slot", slot_team_d0_type, ":fdivision"),
          (team_get_slot, reg0, "$fplayer_team_no", ":slot"),
          (call_script, "script_str_store_division_type_name", s3, reg0),
          (display_message, "@Division {reg1} {s2} is an {s3} division and cannot form {s1}.", color_bad_news),
        (try_end),
      (try_end),
      
      (try_begin),
        (eq, ":form_on_spot", 0),
        (call_script, "script_battlegroup_place_around_leader", "$fplayer_team_no", ":fdivision", "$fplayer_agent_no"),
      (else_try),
        (eq, ":form_on_spot", 2),
        (copy_position, pos1, Target_Pos),
        (call_script, "script_battlegroup_place_around_pos1", "$fplayer_team_no", ":fdivision", "$fplayer_agent_no"),
      (try_end),]),
  
  # script_player_formation_end
  # Input: division
  # Output: none
  ("player_formation_end", [(store_script_param, ":fdivision", 1),
      
      (call_script, "script_formation_end", "$fplayer_team_no", ":fdivision"),
      
      (store_add, ":slot", slot_team_d0_type, ":fdivision"),
      (str_store_class_name, s1, ":fdivision"),
      (try_begin),
        (this_or_next | team_slot_eq, "$fplayer_team_no", ":slot", sdt_infantry),
        (team_slot_eq, "$fplayer_team_no", ":slot", sdt_polearm),
        (display_message, "@{s1}: infantry formation disassembled."),
      (else_try),
        (team_slot_eq, "$fplayer_team_no", ":slot", sdt_archer),
        (display_message, "@{s1}: archer formation disassembled."),
      (else_try),
        (team_slot_eq, "$fplayer_team_no", ":slot", sdt_skirmisher),
        (display_message, "@{s1}: skirmisher formation disassembled."),
      (else_try),
        (this_or_next | team_slot_eq, "$fplayer_team_no", ":slot", sdt_cavalry),
        (team_slot_eq, "$fplayer_team_no", ":slot", sdt_harcher),
        (display_message, "@{s1}: cavalry formation disassembled."),
      (else_try),
        (display_message, "@{s1}: formation disassembled."),
      (try_end),]),
  
  # script_player_order_formations by motomataru TODO add native weapon
  # commands
  # Inputs: arg1: order to formation (mordr_*)
  # Output: none
  ("player_order_formations", [
      (store_script_param, ":forder", 1),
      (set_fixed_point_multiplier, 100),
      
      (try_begin), #On hold, any formations reform in new location
        (eq, ":forder", mordr_hold),
        (call_script, "script_division_reset_places"),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          (store_add, ":slot", slot_team_d0_target_team, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", -1),
          (store_add, ":slot", slot_team_d0_size, ":division"),	#apply to all divisions (not just formations)
          (team_slot_ge, "$fplayer_team_no", ":slot", 1),
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":formation", "$fplayer_team_no", ":slot"),
          (call_script, "script_player_attempt_formation", ":division", ":formation", 0),
        (try_end),
        
      (else_try),	#Follow is hold repeated frequently
        (eq, ":forder", mordr_follow),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          (store_add, ":slot", slot_team_d0_target_team, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", -1),
          (store_add, ":slot", slot_team_d0_size, ":division"),	#apply to all divisions (not just formations)
          (team_slot_ge, "$fplayer_team_no", ":slot", 1),
          
          (store_add, ":slot", slot_team_d0_formation, ":division"),	#update formations
          (team_get_slot, ":formation", "$fplayer_team_no", ":slot"),
          (call_script, "script_player_attempt_formation", ":division", ":formation", 0),
          
          (store_add, ":slot", slot_team_d0_move_order, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", ":forder"),
        (try_end),
        
      (else_try),	#charge or retreat ends formation
        (this_or_next | eq, ":forder", mordr_charge),
        (eq, ":forder", mordr_retreat),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          (store_add, ":slot", slot_team_d0_target_team, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", -1),
          (store_add, ":slot", slot_team_d0_move_order, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", ":forder"),
          
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":formation", "$fplayer_team_no", ":slot"),
          (gt, ":formation", formation_none),
          (call_script, "script_player_formation_end", ":division"),
        (try_end),
        
      (else_try),
        (eq, ":forder", mordr_form_1_row),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":formation", "$fplayer_team_no", ":slot"),
          (try_begin),
            (gt, ":formation", formation_none),
            (call_script, "script_formation_end", "$fplayer_team_no", ":division"),
          (try_end),
          (team_set_slot, "$fplayer_team_no", ":slot", formation_1_row),
          (store_add, ":slot", slot_team_d0_formation_num_ranks, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", 1),
        (try_end),
        
      (else_try),
        (eq, ":forder", mordr_form_2_row),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":formation", "$fplayer_team_no", ":slot"),
          (try_begin),
            (gt, ":formation", formation_none),
            (call_script, "script_formation_end", "$fplayer_team_no", ":division"),
          (try_end),
          (team_set_slot, "$fplayer_team_no", ":slot", formation_2_row),
          (store_add, ":slot", slot_team_d0_formation_num_ranks, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", 2),
        (try_end),
        
      (else_try),
        (eq, ":forder", mordr_form_3_row),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":formation", "$fplayer_team_no", ":slot"),
          (try_begin),
            (gt, ":formation", formation_none),
            (call_script, "script_formation_end", "$fplayer_team_no", ":division"),
          (try_end),
          (team_set_slot, "$fplayer_team_no", ":slot", formation_3_row),
          (store_add, ":slot", slot_team_d0_formation_num_ranks, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", 3),
        (try_end),
        
      (else_try),
        (eq, ":forder", mordr_form_4_row),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":formation", "$fplayer_team_no", ":slot"),
          (try_begin),
            (gt, ":formation", formation_none),
            (call_script, "script_formation_end", "$fplayer_team_no", ":division"),
          (try_end),
          (team_set_slot, "$fplayer_team_no", ":slot", formation_4_row),
          (store_add, ":slot", slot_team_d0_formation_num_ranks, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", 4),
        (try_end),
        
      (else_try),
        (eq, ":forder", mordr_form_5_row),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":formation", "$fplayer_team_no", ":slot"),
          (try_begin),
            (gt, ":formation", formation_none),
            (call_script, "script_formation_end", "$fplayer_team_no", ":division"),
          (try_end),
          (team_set_slot, "$fplayer_team_no", ":slot", formation_5_row),
          (store_add, ":slot", slot_team_d0_formation_num_ranks, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", 5),
        (try_end),
        
      (else_try),	#dismount ends formation
        (eq, ":forder", mordr_dismount),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (neg | team_slot_eq, "$fplayer_team_no", ":slot", formation_none),
          (team_slot_ge, "$fplayer_team_no", ":slot", formation_none),
          (try_begin),
            (store_add, ":slot", slot_team_d0_type, ":division"),
            (this_or_next | team_slot_eq, "$fplayer_team_no", ":slot", sdt_cavalry),
            (team_slot_eq, "$fplayer_team_no", ":slot", sdt_harcher),
            (call_script, "script_formation_end", "$fplayer_team_no", ":division"),
            (display_message, "@Cavalry formation disassembled."),
            
          (else_try),	#address bug that cavalry in scripted mode won't dismount
            (try_for_agents, ":agent"),
              (agent_is_alive, ":agent"),
              (agent_is_human, ":agent"),
              (agent_get_group, ":team", ":agent"),
              (eq, ":team", "$fplayer_team_no"),
              (neq, "$fplayer_agent_no", ":agent"),
              (agent_get_division, ":bgdivision", ":agent"),
              (eq, ":bgdivision", ":division"),
              (agent_clear_scripted_mode, ":agent"),
              (agent_set_speed_limit, ":agent", 100),
            (try_end),
          (try_end),
        (try_end),
        
      (else_try),
        (eq, ":forder", mordr_advance),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          (store_add, ":slot", slot_team_d0_target_team, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", -1),
          (store_add, ":slot", slot_team_d0_move_order, ":division"),
          (team_get_slot, ":prev_order", "$fplayer_team_no", ":slot"),
          (team_set_slot, "$fplayer_team_no", ":slot", ":forder"),
          
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":formation", "$fplayer_team_no", ":slot"),
          (gt, ":formation", formation_none),
          
          (store_add, ":slot", slot_team_d0_fclock, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", 1),
          
          (call_script, "script_formation_current_position", pos63, "$fplayer_team_no", ":division"),
          (try_begin),
            (neq, ":prev_order", mordr_advance),
            (call_script, "script_set_formation_destination", "$fplayer_team_no", ":division", pos63),
          (try_end),
          (call_script, "script_formation_move_position", "$fplayer_team_no", ":division", pos63, 1),
          
          (store_add, ":slot", slot_team_d0_formation_space, ":division"),
          (team_get_slot, ":div_spacing", "$fplayer_team_no", ":slot"),
          (store_add, ":slot", slot_team_d0_type, ":division"),
          (try_begin),
            (team_slot_eq, "$fplayer_team_no", ":slot", sdt_archer),
            (call_script, "script_form_archers", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", 0, ":formation"),
          (else_try),
            (this_or_next | team_slot_eq, "$fplayer_team_no", ":slot", sdt_cavalry),
            (team_slot_eq, "$fplayer_team_no", ":slot", sdt_harcher),
            (call_script, "script_form_cavalry", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", 0),
          (else_try),
            (call_script, "script_form_infantry", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", 0, ":formation"),
          (try_end),
        (try_end),
        
      (else_try),
        (eq, ":forder", mordr_fall_back),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          (store_add, ":slot", slot_team_d0_target_team, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", -1),
          (store_add, ":slot", slot_team_d0_move_order, ":division"),
          (team_get_slot, ":prev_order", "$fplayer_team_no", ":slot"),
          (team_set_slot, "$fplayer_team_no", ":slot", ":forder"),
          
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":formation", "$fplayer_team_no", ":slot"),
          (gt, ":formation", formation_none),
          
          (store_add, ":slot", slot_team_d0_fclock, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", 1),
          
          (call_script, "script_formation_current_position", pos63, "$fplayer_team_no", ":division"),
          (try_begin),
            (neq, ":prev_order", mordr_fall_back),
            (call_script, "script_set_formation_destination", "$fplayer_team_no", ":division", pos63),
          (try_end),
          (call_script, "script_formation_move_position", "$fplayer_team_no", ":division", pos63, -1),
          
          (store_add, ":slot", slot_team_d0_formation_space, ":division"),
          (team_get_slot, ":div_spacing", "$fplayer_team_no", ":slot"),
          (store_add, ":slot", slot_team_d0_type, ":division"),
          (try_begin),
            (team_slot_eq, "$fplayer_team_no", ":slot", sdt_archer),
            (call_script, "script_form_archers", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", 0, ":formation"),
          (else_try),
            (this_or_next | team_slot_eq, "$fplayer_team_no", ":slot", sdt_cavalry),
            (team_slot_eq, "$fplayer_team_no", ":slot", sdt_harcher),
            (call_script, "script_form_cavalry", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", 0),
          (else_try),
            (call_script, "script_form_infantry", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", 0, ":formation"),
          (try_end),
        (try_end),
        
      (else_try),
        (eq, ":forder", mordr_stand_closer),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          
          (try_begin),
            (eq, Native_Formations_Implementation, WB_Implementation),
            (assign, ":min_spacing", -3),	#WB formations go down to four ranks by using Stand Closer
          (else_try),
            (assign, ":min_spacing", 0),
          (try_end),
          
          (store_add, ":slot", slot_team_d0_formation_space, ":division"),
          (team_get_slot, ":div_spacing", "$fplayer_team_no", ":slot"),
          (gt, ":div_spacing", ":min_spacing"),
          (val_sub, ":div_spacing", 1),
          (team_set_slot, "$fplayer_team_no", ":slot", ":div_spacing"),
          
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":formation", "$fplayer_team_no", ":slot"),
          (gt, ":formation", formation_none),
          
          (store_add, ":slot", slot_team_d0_fclock, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", 1),
          
          (try_begin),	#bring unformed divisions into sync with formations' minimum
            (lt, ":div_spacing", 0),
            (set_show_messages, 0),
            (assign, reg0, ":div_spacing"),
            (try_for_range, reg1, reg0, 0),
              (team_give_order, "$fplayer_team_no", ":division", mordr_spread_out),
              (val_add, ":div_spacing", 1),
            (try_end),
            (set_show_messages, 1),
            (store_add, ":slot", slot_team_d0_formation_space, ":division"),
            (team_set_slot, "$fplayer_team_no", ":slot", ":div_spacing"),
            
          (else_try),
            (call_script, "script_get_formation_destination", pos1, "$fplayer_team_no", ":division"),
            (try_begin),
              (store_add, ":slot", slot_team_d0_first_member, ":division"),
              (team_slot_eq, "$fplayer_team_no", ":slot", "$fplayer_agent_no"),
              (assign, ":first_member_is_player", 1),
            (else_try),
              (assign, ":first_member_is_player", 0),
            (try_end),
            (store_add, ":slot", slot_team_d0_type, ":division"),
            (try_begin),
              (team_slot_eq, "$fplayer_team_no", ":slot", sdt_archer),
              (store_add, ":slot", slot_team_d0_size, ":division"),
              (team_get_slot, ":troop_count", "$fplayer_team_no", ":slot"),
              (call_script, "script_get_centering_amount", formation_default, ":troop_count", ":div_spacing"),
              (val_mul, reg0, -1),
              (position_move_x, pos1, reg0),
              (call_script, "script_form_archers", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", ":first_member_is_player", ":formation"),
            (else_try),
              (this_or_next | team_slot_eq, "$fplayer_team_no", ":slot", sdt_cavalry),
              (team_slot_eq, "$fplayer_team_no", ":slot", sdt_harcher),
              (call_script, "script_form_cavalry", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", ":first_member_is_player"),
            (else_try),
              (store_add, ":slot", slot_team_d0_size, ":division"),
              (team_get_slot, ":troop_count", "$fplayer_team_no", ":slot"),
              (call_script, "script_get_centering_amount", ":formation", ":troop_count", ":div_spacing"),
              (position_move_x, pos1, reg0),
              (call_script, "script_form_infantry", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", ":first_member_is_player", ":formation"),
            (try_end),
          (try_end),
        (try_end),
        
      (else_try),
        (eq, ":forder", mordr_spread_out),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          
          (try_begin),
            (eq, Native_Formations_Implementation, WFaS_Implementation),
            (assign, ":max_spacing", 3),
          (else_try),
            (assign, ":max_spacing", 2),
          (try_end),
          
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":formation", "$fplayer_team_no", ":slot"),
          (store_add, ":slot", slot_team_d0_formation_space, ":division"),
          (team_get_slot, ":div_spacing", "$fplayer_team_no", ":slot"),
          (try_begin),
            (this_or_next | lt, ":div_spacing", ":max_spacing"),
            (gt, ":formation", formation_none),
            (val_add, ":div_spacing", 1),
          (try_end),
          (team_set_slot, "$fplayer_team_no", ":slot", ":div_spacing"),
          
          (gt, ":formation", formation_none),
          
          (store_add, ":slot", slot_team_d0_fclock, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", 1),
          
          #bring unformed divisions into sync with formations' minimum
          (set_show_messages, 0),
          (assign, reg0, ":div_spacing"),
          (try_for_range, reg1, reg0, 1),
            (team_give_order, "$fplayer_team_no", ":division", mordr_spread_out),
            (val_add, ":div_spacing", 1),
          (try_end),
          (set_show_messages, 1),
          (store_add, ":slot", slot_team_d0_formation_space, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", ":div_spacing"),
          
          (call_script, "script_get_formation_destination", pos1, "$fplayer_team_no", ":division"),
          (try_begin),
            (store_add, ":slot", slot_team_d0_first_member, ":division"),
            (team_slot_eq, "$fplayer_team_no", ":slot", "$fplayer_agent_no"),
            (assign, ":first_member_is_player", 1),
          (else_try),
            (assign, ":first_member_is_player", 0),
          (try_end),
          (store_add, ":slot", slot_team_d0_type, ":division"),
          (try_begin),
            (team_slot_eq, "$fplayer_team_no", ":slot", sdt_archer),
            (store_add, ":slot", slot_team_d0_size, ":division"),
            (team_get_slot, ":troop_count", "$fplayer_team_no", ":slot"),
            (call_script, "script_get_centering_amount", formation_default, ":troop_count", ":div_spacing"),
            (val_mul, reg0, -1),
            (position_move_x, pos1, reg0),
            (call_script, "script_form_archers", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", ":first_member_is_player", ":formation"),
          (else_try),
            (this_or_next | team_slot_eq, "$fplayer_team_no", ":slot", sdt_cavalry),
            (team_slot_eq, "$fplayer_team_no", ":slot", sdt_harcher),
            (call_script, "script_form_cavalry", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", ":first_member_is_player"),
          (else_try),
            (store_add, ":slot", slot_team_d0_size, ":division"),
            (team_get_slot, ":troop_count", "$fplayer_team_no", ":slot"),
            (call_script, "script_get_centering_amount", ":formation", ":troop_count", ":div_spacing"),
            (position_move_x, pos1, reg0),
            (call_script, "script_form_infantry", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", ":first_member_is_player", ":formation"),
          (try_end),
        (try_end),
        
      (else_try),
        (eq, ":forder", mordr_stand_ground),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          (store_add, ":slot", slot_team_d0_target_team, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", -1),
          (store_add, ":slot", slot_team_d0_move_order, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", ":forder"),
          
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":formation", "$fplayer_team_no", ":slot"),
          (gt, ":formation", formation_none),
          
          (store_add, ":slot", slot_team_d0_fclock, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", 1),
          
          (call_script, "script_formation_current_position", pos63, "$fplayer_team_no", ":division"),
          (copy_position, pos1, pos63),
          (store_add, ":slot", slot_team_d0_formation_space, ":division"),
          (team_get_slot, ":div_spacing", "$fplayer_team_no", ":slot"),
          
          (try_begin),
            (store_add, ":slot", slot_team_d0_first_member, ":division"),
            (team_slot_eq, "$fplayer_team_no", ":slot", "$fplayer_agent_no"),
            (assign, ":first_member_is_player", 1),
          (else_try),
            (assign, ":first_member_is_player", 0),
          (try_end),
          (store_add, ":slot", slot_team_d0_type, ":division"),
          (try_begin),
            (team_slot_eq, "$fplayer_team_no", ":slot", sdt_archer),
            (store_add, ":slot", slot_team_d0_size, ":division"),
            (team_get_slot, ":troop_count", "$fplayer_team_no", ":slot"),
            (call_script, "script_get_centering_amount", formation_default, ":troop_count", ":div_spacing"),
            (val_mul, reg0, -1),
            (position_move_x, pos1, reg0),
            (call_script, "script_form_archers", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", ":first_member_is_player", ":formation"),
          (else_try),
            (this_or_next | team_slot_eq, "$fplayer_team_no", ":slot", sdt_cavalry),
            (team_slot_eq, "$fplayer_team_no", ":slot", sdt_harcher),
            (call_script, "script_form_cavalry", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", ":first_member_is_player"),
          (else_try),
            (store_add, ":slot", slot_team_d0_size, ":division"),
            (team_get_slot, ":troop_count", "$fplayer_team_no", ":slot"),
            (call_script, "script_get_centering_amount", ":formation", ":troop_count", ":div_spacing"),
            (position_move_x, pos1, reg0),
            (call_script, "script_form_infantry", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", ":first_member_is_player", ":formation"),
          (try_end),
          (call_script, "script_set_formation_destination", "$fplayer_team_no", ":division", pos63),
        (try_end),
      (try_end)]),
  
  # script_memorize_division_placements by motomataru
  # Inputs: none
  # Output: none
  ("memorize_division_placements", [(set_fixed_point_multiplier, 100),
      (call_script, "script_team_get_position_of_enemies", Enemy_Team_Pos, "$fplayer_team_no", grc_everyone),
      (assign, ":num_enemies", reg0),
      
      (try_for_range, ":division", 0, 9),
        (class_is_listening_order, "$fplayer_team_no", ":division"),
        (store_add, ":slot", slot_team_d0_size, ":division"),
        (team_slot_ge, "$fplayer_team_no", ":slot", 1),
        
        (store_add, ":slot", slot_team_d0_formation, ":division"),
        (team_get_slot, ":value", "$fplayer_team_no", ":slot"),
        (store_add, ":slot", slot_faction_d0_mem_formation, ":division"),
        (faction_set_slot, "fac_player_faction", ":slot", ":value"),
        
        (store_add, ":slot", slot_team_d0_formation_space, ":division"),
        (team_get_slot, ":value", "$fplayer_team_no", ":slot"),
        (store_add, ":slot", slot_faction_d0_mem_formation_space, ":division"),
        (faction_set_slot, "fac_player_faction", ":slot", ":value"),
        
        (agent_get_position, pos1, "$fplayer_agent_no"),
        (try_begin),
          (neq, ":num_enemies", 0),	#more than 0 enemies still alive?
          (call_script, "script_point_y_toward_position", pos1, Enemy_Team_Pos),
        (try_end),
        # (call_script, "script_get_formation_destination", Current_Pos,
        # "$fplayer_team_no", ":division"),
        (team_get_order_position, Current_Pos, "$fplayer_team_no", ":division"),	#use this to capture Native Advance and Fall Back positioning
        (position_transform_position_to_local, Temp_Pos, pos1, Current_Pos), #Temp_Pos = vector to division w.r.t.  leader facing enemy
        
        (position_get_x, ":value", Temp_Pos),
        (store_add, ":slot", slot_faction_d0_mem_relative_x_flag, ":division"),
        (faction_set_slot, "fac_player_faction", ":slot", ":value"),
        
        (position_get_y, ":value", Temp_Pos),
        (store_add, ":slot", slot_faction_d0_mem_relative_y, ":division"),
        (faction_set_slot, "fac_player_faction", ":slot", ":value"),
        
        (store_add, ":slot", slot_team_d0_type, ":division"),
        (team_get_slot, ":value", "$fplayer_team_no", ":slot"),
        (call_script, "script_str_store_division_type_name", s1, ":value"),
        (store_add, reg0, ":division", 1),
        (display_message, "@The placement of {s1} division {reg0} memorized."),
      (try_end),]),
  
  # script_default_division_placements by motomataru
  # Inputs: none
  # Output: none
  ("default_division_placements", [(try_for_range, ":division", 0, 9),
        (class_is_listening_order, "$fplayer_team_no", ":division"),
        (store_add, ":slot", slot_faction_d0_mem_relative_x_flag, ":division"),	#use as flag
        (faction_set_slot, "fac_player_faction", ":slot", 0),
        
        (store_add, ":slot", slot_team_d0_size, ":division"),
        (team_slot_ge, "$fplayer_team_no", ":slot", 1),
        (store_add, ":slot", slot_team_d0_type, ":division"),
        (team_get_slot, ":value", "$fplayer_team_no", ":slot"),
        (call_script, "script_str_store_division_type_name", s1, ":value"),
        (store_add, reg0, ":division", 1),
        (display_message, "@The placement of {s1} division {reg0} set to default."),
      (try_end),]),
  
  # script_process_place_divisions by motomataru
  # Inputs: none
  # Output: none
  # Expects team_set_order_position has been done
  ("process_place_divisions", [(assign, ":num_bgroups", 0),
      (try_for_range_backwards, ":division", 0, 9), #running backwards to avoid "garbage" divisions
        (class_is_listening_order, "$fplayer_team_no", ":division"),
        (store_add, ":slot", slot_team_d0_target_team, ":division"),
        (team_set_slot, "$fplayer_team_no", ":slot", -1),
        (store_add, ":slot", slot_team_d0_size, ":division"),
        (team_slot_ge, "$fplayer_team_no", ":slot", 1),
        (store_add, ":slot", slot_team_d0_fclock, ":division"),
        (team_set_slot, "$fplayer_team_no", ":slot", 1),
        (team_get_order_position, pos1, "$fplayer_team_no", ":division"),
        (val_add, ":num_bgroups", 1),
      (try_end),
      
      (try_begin),
        (gt, ":num_bgroups", 0),
        (copy_position, Target_Pos, pos1),	#kludge around team_get_order_position rotation problems
        
        (try_begin),
          (eq, "$battle_phase", BP_Deploy),
          
          (try_begin),
            (eq, "$g_is_quick_battle", 1),
            (assign, reg0, 5),
          (else_try),
            (party_get_skill_level, reg0, "p_main_party", "skl_tactics"),
          (try_end),
          (store_mul, ":range_limit", reg0, 1000),
          
          (agent_get_position, Temp_Pos, "$fplayer_agent_no"),
          (get_distance_between_positions, reg0, Target_Pos, Temp_Pos),
          (lt, ":range_limit", reg0),
          (display_message, "@Your party's tactical skill limits how far away you can deploy your troops!"),
          (call_script, "script_point_y_toward_position", Temp_Pos, Target_Pos),
          (copy_position, Target_Pos, Temp_Pos),
          (position_move_y, Target_Pos, ":range_limit"),
        (try_end),
        
        #player designating target battlegroup?
        (assign, ":distance_to_enemy", Far_Away),
        (try_for_range, ":team", 0, 4),
          (teams_are_enemies, ":team", "$fplayer_team_no"),
          (team_slot_ge, ":team", slot_team_size, 1),
          (try_for_range, ":division", 0, 9),
            (store_add, ":slot", slot_team_d0_size, ":division"),
            (team_slot_ge, ":team", ":slot", 1),
            (call_script, "script_battlegroup_get_position", Temp_Pos, ":team", ":division"),
            (get_distance_between_positions, reg0, Target_Pos, Temp_Pos),
            (gt, ":distance_to_enemy", reg0),
            (assign, ":distance_to_enemy", reg0),
            (assign, ":closest_enemy_team", ":team"),
            (assign, ":closest_enemy_division", ":division"),
          (try_end),
        (try_end),
        
        (call_script, "script_battlegroup_get_action_radius", ":closest_enemy_team", ":closest_enemy_division"),
        (assign, ":radius_enemy_battlegroup", reg0),
        
        (try_begin),
          (le, ":distance_to_enemy", ":radius_enemy_battlegroup"),	#target position within radius of an enemy battlegroup?
          (le, ":distance_to_enemy", AI_charge_distance),	#limit so player can place divisions near large enemy battlegroups without
          #selecting them
          (call_script, "script_battlegroup_get_position", Target_Pos, ":closest_enemy_team", ":closest_enemy_division"),
          (gt, ":num_bgroups", 1),
          (store_add, ":slot", slot_team_d0_type, ":closest_enemy_division"),
          (team_get_slot, reg0, ":closest_enemy_team", ":slot"),
          (call_script, "script_str_store_division_type_name", s1, reg0),
          (display_message, "@...and attack enemy {s1} division!"),
        (try_end),
        
        (call_script, "script_team_get_position_of_enemies", Enemy_Team_Pos, "$fplayer_team_no", grc_everyone),
        (call_script, "script_point_y_toward_position", Target_Pos, Enemy_Team_Pos),
        
        #place player divisions
        (agent_get_position, pos49, "$fplayer_agent_no"),
        (try_for_range, ":division", 0, 9),
          (class_is_listening_order, "$fplayer_team_no", ":division"),
          (store_add, ":slot", slot_team_d0_size, ":division"),
          (team_get_slot, ":troop_count", "$fplayer_team_no", ":slot"),
          (gt, ":troop_count", 0),
          
          (try_begin),
            (le, ":distance_to_enemy", ":radius_enemy_battlegroup"),	#target position within radius of an enemy battlegroup?
            (le, ":distance_to_enemy", AI_charge_distance),	#limit so player can place divisions near large enemy battlegroups without
            #selecting them
            (store_add, ":slot", slot_team_d0_target_team, ":division"),
            (team_set_slot, "$fplayer_team_no", ":slot", ":closest_enemy_team"),
            (store_add, ":slot", slot_team_d0_target_division, ":division"),
            (team_set_slot, "$fplayer_team_no", ":slot", ":closest_enemy_division"),
          (try_end),
          
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":fformation", "$fplayer_team_no", ":slot"),
          
          (try_begin),
            (gt, ":num_bgroups", 1),
            (agent_set_position, "$fplayer_agent_no", Target_Pos),	#fake out script_battlegroup_place_around_leader
            (call_script, "script_player_attempt_formation", ":division", ":fformation", 0),
          (else_try),
            (try_begin),
              (le, ":distance_to_enemy", ":radius_enemy_battlegroup"),	#target position within radius of an enemy battlegroup?
              (le, ":distance_to_enemy", AI_charge_distance),	#limit so player can place divisions near large enemy battlegroups without
              #selecting them
              (call_script, "script_battlegroup_get_attack_destination", Target_Pos, "$fplayer_team_no", ":division", ":closest_enemy_team", ":closest_enemy_division"),
              (store_add, ":slot", slot_team_d0_type, ":closest_enemy_division"),
              (team_get_slot, reg0, ":closest_enemy_team", ":slot"),
              (call_script, "script_str_store_division_type_name", s1, reg0),
              (display_message, "@...and attack enemy {s1} division!"),
            (try_end),
            
            (call_script, "script_set_formation_destination", "$fplayer_team_no", ":division", Target_Pos),
            
            (gt, ":fformation", formation_none),
            (store_add, ":slot", slot_team_d0_formation_space, ":division"),
            (team_get_slot, ":div_spacing", "$fplayer_team_no", ":slot"),
            (try_begin),
              (store_add, ":slot", slot_team_d0_type, ":division"),
              (team_get_slot, ":sd_type", "$fplayer_team_no", ":slot"),
              (neq, ":sd_type", sdt_cavalry),
              (neq, ":sd_type", sdt_harcher),
              (try_begin),
                (eq, ":sd_type", sdt_archer),
                (call_script, "script_get_centering_amount", formation_default, ":troop_count", ":div_spacing"),
                (val_mul, reg0, -1),
                (assign, ":script", "script_form_archers"),
              (else_try),
                (call_script, "script_get_centering_amount", ":fformation", ":troop_count", ":div_spacing"),
                (assign, ":script", "script_form_infantry"),
              (try_end),
              (position_move_x, Target_Pos, reg0),
            (else_try),
              (assign, ":script", "script_form_cavalry"),
            (try_end),
            (copy_position, pos1, Target_Pos),
            (call_script, ":script", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", 0, ":fformation"),
          (try_end),
          (store_add, ":slot", slot_team_d0_move_order, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", mordr_hold),
        (try_end), #division loop
        (agent_set_position, "$fplayer_agent_no", pos49),
      (try_end),	#num_bgroups > 0
  ]),
  
  # script_process_player_division_positioning by motomataru
  # Inputs: none
  # Output: none
  # Expects Enemy_Team_Pos
  ("process_player_division_positioning", [(call_script, "script_division_reset_places"),
      
      #implement HOLD OVER THERE when player lets go of key
      (try_begin),
        (ge, "$gk_order_hold_over_there", HOT_F1_held),
        (neg | game_key_is_down, gk_order_1),
        (assign, "$gk_order_hold_over_there", HOT_no_order),
        (call_script, "script_process_place_divisions"),
      (try_end),	#HOLD OVER THERE
      
      #periodic functions
      (assign, ":save_autorotate", "$FormAI_autorotate"),
      (assign, "$FormAI_autorotate", 0),
      (try_for_range, ":division", 0, 9),
        (store_add, ":slot", slot_team_d0_size, ":division"),
        (team_get_slot, ":troop_count", "$fplayer_team_no", ":slot"),
        (gt, ":troop_count", 0),
        
        (store_add, ":slot", slot_team_d0_target_team, ":division"),
        (team_get_slot, ":target_team", "$fplayer_team_no", ":slot"),
        (store_add, ":slot", slot_team_d0_target_division, ":division"),
        (team_get_slot, ":target_division", "$fplayer_team_no", ":slot"),
        (try_begin),
          (ge, ":target_team", 0),	#enemy battlegroup targeted?
          (store_add, ":slot", slot_team_d0_size, ":target_division"),
          (team_get_slot, reg0, ":target_team", ":slot"),
          
          (le, reg0, 0),	#target destroyed?
          (store_add, ":slot", slot_team_d0_target_team, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", -1),
          
          (store_add, ":slot", slot_team_d0_type, ":target_division"),
          (team_get_slot, reg0, ":target_team", ":slot"),
          (call_script, "script_str_store_division_type_name", s1, reg0),
          
          (str_store_class_name, s2, ":division"),
          (display_message, "@{s2}: returning after destroying enemy {s1} division."),
          (store_add, ":slot", slot_team_d0_move_order, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", mordr_follow),
        (try_end),
        
        (store_add, ":slot", slot_team_d0_fclock, ":division"),
        (team_get_slot, ":fclock", "$fplayer_team_no", ":slot"),
        (store_mod, ":time_slice", ":fclock", Reform_Trigger_Modulus),
        (val_add, ":fclock", 1),
        (team_set_slot, "$fplayer_team_no", ":slot", ":fclock"),
        
        (try_begin),
          (store_add, ":slot", slot_team_d0_move_order, ":division"),
          (team_slot_eq, "$fplayer_team_no", ":slot", mordr_follow),
          (call_script, "script_battlegroup_place_around_leader", "$fplayer_team_no", ":division", "$fplayer_agent_no"),
          (team_set_slot, "$fplayer_team_no", ":slot", mordr_follow),	#override script_battlegroup_place_around_leader
          
          #periodically reform
        (else_try),
          (eq, ":time_slice", 0),
          (team_get_movement_order, reg0, "$fplayer_team_no", ":division"),
          (neq, reg0, mordr_stand_ground),
          
          (call_script, "script_team_get_position_of_enemies", Enemy_Team_Pos, "$fplayer_team_no", grc_everyone),
          (store_add, ":slot", slot_team_d0_formation, ":division"),
          (team_get_slot, ":fformation", "$fplayer_team_no", ":slot"),
          (try_begin),
            (gt, ":fformation", formation_none),
            (store_add, ":slot", slot_team_d0_formation_space, ":division"),
            (team_get_slot, ":div_spacing", "$fplayer_team_no", ":slot"),
            (store_add, ":slot", slot_team_d0_type, ":division"),
            (team_get_slot, ":sd_type", "$fplayer_team_no", ":slot"),
            
            (try_begin),
              (store_add, ":slot", slot_team_d0_first_member, ":division"),
              (team_slot_eq, "$fplayer_team_no", ":slot", "$fplayer_agent_no"),
              (assign, ":first_member_is_player", 1),
            (else_try),
              (assign, ":first_member_is_player", 0),
            (try_end),
            
            (try_begin),
              (ge, ":target_team", 0),	#enemy battlegroup targeted?
              (try_begin),
                (this_or_next | eq, ":sd_type", sdt_archer),
                (this_or_next | eq, ":sd_type", sdt_harcher),
                (eq, ":sd_type", sdt_skirmisher),
                (store_add, ":slot", slot_team_d0_is_fighting, ":division"),
                (team_slot_ge, "$fplayer_team_no", ":slot", 1),	#ranged are firing?
                (call_script, "script_formation_current_position", pos1, "$fplayer_team_no", ":division"),	#stop advancing
              (else_try),
                (call_script, "script_battlegroup_get_attack_destination", pos1, "$fplayer_team_no", ":division", ":target_team", ":target_division"),
              (try_end),
              
            (else_try),
              (call_script, "script_get_formation_destination", pos1, "$fplayer_team_no", ":division"),
              (store_add, ":slot", slot_team_d0_is_fighting, ":division"),
              (team_get_slot, ":is_fighting", "$fplayer_team_no", ":slot"),
              (try_begin),
                (neq, ":sd_type", sdt_cavalry),
                (neq, ":sd_type", sdt_harcher),
                (neq, ":is_fighting", 0),
                (eq, ":first_member_is_player", 0),
                (position_move_y, pos1, -2000),
              (try_end),
              (call_script, "script_point_y_toward_position", pos1, Enemy_Team_Pos),
              (try_begin),
                (neq, ":sd_type", sdt_cavalry),
                (neq, ":sd_type", sdt_harcher),
                (neq, ":is_fighting", 0),
                (eq, ":first_member_is_player", 0),
                (position_move_y, pos1, 2000),
              (try_end),
            (try_end),
            
            (call_script, "script_set_formation_destination", "$fplayer_team_no", ":division", pos1),
            
            (try_begin),
              (eq, ":sd_type", sdt_archer),
              (call_script, "script_get_centering_amount", formation_default, ":troop_count", ":div_spacing"),
              (val_mul, reg0, -1),
              (position_move_x, pos1, reg0),
            (else_try),
              (neq, ":sd_type", sdt_cavalry),
              (neq, ":sd_type", sdt_harcher),
              (call_script, "script_get_centering_amount", ":fformation", ":troop_count", ":div_spacing"),
              (position_move_x, pos1, reg0),
            (try_end),
            
            (try_begin),
              (eq, ":sd_type", sdt_archer),
              (call_script, "script_form_archers", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", ":first_member_is_player", ":fformation"),
            (else_try),
              (this_or_next | eq, ":sd_type", sdt_cavalry),
              (eq, ":sd_type", sdt_harcher),
              (try_begin),
                (ge, ":target_team", 0),	#enemy battlegroup targeted?
                (call_script, "script_formation_current_position", pos29, "$fplayer_team_no", ":division"),
                (call_script, "script_battlegroup_get_position", Enemy_Team_Pos, ":target_team", ":target_division"),
                (get_distance_between_positions, ":distance_to_enemy", pos29, Enemy_Team_Pos),
                
                (call_script, "script_battlegroup_get_action_radius", "$fplayer_team_no", ":division"),
                (assign, ":combined_radius", reg0),
                (call_script, "script_battlegroup_get_action_radius", ":target_team", ":target_division"),
                (val_add, ":combined_radius", reg0),
                
                (le, ":distance_to_enemy", ":combined_radius"),
                (call_script, "script_formation_end", "$fplayer_team_no", ":division"),
                (str_store_class_name, s1, ":division"),
                (display_message, "@{s1}: cavalry formation disassembled."),
                (set_show_messages, 0),
                (team_give_order, "$fplayer_team_no", ":division", mordr_charge),
                (set_show_messages, 1),
              (else_try),
                (call_script, "script_form_cavalry", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", ":first_member_is_player"),
              (try_end),
            (else_try),
              (call_script, "script_form_infantry", "$fplayer_team_no", ":division", "$fplayer_agent_no", ":div_spacing", ":first_member_is_player", ":fformation"),
            (try_end),
            
          (else_try),	#divisions not in formation
            (ge, ":target_team", 0),	#enemy battlegroup targeted?
            (store_add, ":slot", slot_team_d0_target_division, ":division"),
            (team_get_slot, ":target_division", "$fplayer_team_no", ":slot"),
            (try_begin),
              (this_or_next | eq, ":sd_type", sdt_archer),
              (this_or_next | eq, ":sd_type", sdt_harcher),
              (eq, ":sd_type", sdt_skirmisher),
              (store_add, ":slot", slot_team_d0_is_fighting, ":division"),
              (team_slot_ge, "$fplayer_team_no", ":slot", 1),	#ranged are firing?
              (call_script, "script_battlegroup_get_position", pos1, "$fplayer_team_no", ":division"),	#stop advancing
            (else_try),
              (call_script, "script_battlegroup_get_attack_destination", pos1, "$fplayer_team_no", ":division", ":target_team", ":target_division"),
            (try_end),
            (call_script, "script_set_formation_destination", "$fplayer_team_no", ":division", pos1),
            (team_get_movement_order, ":existing_order", "$fplayer_team_no", ":division"),
            (try_begin),
              (ge, ":target_team", 0),	#enemy battlegroup targeted?
              (call_script, "script_battlegroup_get_position", pos29, "$fplayer_team_no", ":division"),
              (call_script, "script_battlegroup_get_position", Enemy_Team_Pos, ":target_team", ":target_division"),
              (get_distance_between_positions, ":distance_to_enemy", pos29, Enemy_Team_Pos),
              
              (call_script, "script_battlegroup_get_action_radius", "$fplayer_team_no", ":division"),
              (assign, ":combined_radius", reg0),
              (call_script, "script_battlegroup_get_action_radius", ":target_team", ":target_division"),
              (val_add, ":combined_radius", reg0),
              
              (le, ":distance_to_enemy", ":combined_radius"),
              (try_begin),
                (neq, ":existing_order", mordr_charge),
                (set_show_messages, 0),
                (team_give_order, "$fplayer_team_no", ":division", mordr_charge),
                (set_show_messages, 1),
              (try_end),
            (else_try),
              (neq, ":existing_order", mordr_hold),
              (set_show_messages, 0),
              (team_give_order, "$fplayer_team_no", ":division", mordr_hold),
              (set_show_messages, 1),
            (try_end),
          (try_end),
        (try_end),	#Periodic Reform
      (try_end),	#Division Loop
      
      (assign, "$FormAI_autorotate", ":save_autorotate"),]),
  
  
  # #Utilities used by formations
  # script_cf_is_thrusting_weapon by motomataru
  # Input: item
  # Output: T/F
  ("cf_is_thrusting_weapon", [(store_script_param, ":item", 1),
      (is_between, ":item", weapons_begin, weapons_end),
      (item_get_thrust_damage, ":thrust_damage", ":item"),
      (item_get_swing_damage, ":swing_damage", ":item"),
      (val_mul, ":thrust_damage", 3),	#it seems thrusts connect faster than swings (as they should) although the
      #animations seem to take the same time
      (val_div, ":thrust_damage", 2),	#factor this in with approximation 2*PI/4 (distance swing must travel vs.
      #thrust) This ignores length, which creates too large a differential.  This is
      #a happy medium.
      (ge, ":thrust_damage", ":swing_damage"),]),
  
  # script_cf_is_weapon_ranged by motomataru
  # Input: weapon ID, flag 0/1 to consider thrown weapons
  # Output: T/F
  ("cf_is_weapon_ranged", [(store_script_param, ":weapon", 1),
      (store_script_param, ":include_thrown", 2),
      
      (assign, ":test_val", 0),
      (try_begin),
        (ge, ":weapon", 0),
        (item_get_type, ":type", ":weapon"),
        (try_begin),
          (this_or_next | eq, ":type", itp_type_bow),
          (this_or_next | eq, ":type", itp_type_crossbow),
          (this_or_next | eq, ":type", itp_type_pistol),
          (eq, ":type", itp_type_musket),
          (assign, ":test_val", 1),
        (else_try),
          (eq, ":type", itp_type_thrown),
          (neq, ":include_thrown", 0),
          (assign, ":test_val", 1),
        (try_end),
      (try_end),
      
      (neq, ":test_val", 0),]),
  
  # script_equip_best_melee_weapon by motomataru
  # Input: agent id, flag to force shield, flag to force for length ALONE,
  # current fire order
  # Output: none
  ("equip_best_melee_weapon", [(store_script_param, ":agent", 1),
      (store_script_param, ":force_shield", 2),
      (store_script_param, ":force_length", 3),
      (store_script_param, ":fire_order", 4),
      
      (agent_get_wielded_item, ":cur_wielded", ":agent", 0),
      (try_begin),
        (call_script, "script_cf_is_weapon_ranged", ":cur_wielded", 0),
        (agent_get_ammo, ":ammo", ":agent", 1),
        (gt, ":ammo", 0),
        
      (else_try),
        #priority items
        (assign, ":shield", itm_no_item),
        (assign, ":weapon", itm_no_item),
        (try_for_range, ":item_slot", ek_item_0, ek_head),
          (agent_get_item_slot, ":item", ":agent", ":item_slot"),
          (gt, ":item", itm_no_item),
          (item_get_type, ":weapon_type", ":item"),
          (try_begin),
            (eq, ":weapon_type", itp_type_shield),
            (assign, ":shield", ":item"),
          (else_try),
            (eq, ":weapon_type", itp_type_thrown),
            (eq, ":fire_order", aordr_fire_at_will),
            # (agent_get_ammo, ":ammo", ":agent", 0), #assume infantry would have no
            # other kind of ranged weapon
            # (gt, ":ammo", 0),
            (assign, ":weapon", ":item"),	#use thrown weapons first
          (try_end),
        (try_end),
        
        #select weapon
        (try_begin),
          (eq, ":weapon", itm_no_item),
          (assign, ":cur_score", 0),
          (try_for_range, ":item_slot", ek_item_0, ek_head),
            (agent_get_item_slot, ":item", ":agent", ":item_slot"),
            (gt, ":item", itm_no_item),
            (item_get_type, ":weapon_type", ":item"),
            (neq, ":weapon_type", itp_type_shield),
            
            (try_begin),
              (item_has_property, ":item", itp_two_handed),
              (assign, reg0, 1),
            (else_try),
              (assign, reg0, 0),
            (try_end),
            
            (this_or_next | eq, reg0, 0),
            (this_or_next | eq, ":force_shield", 0),
            (eq, ":shield", itm_no_item),
            
            (try_begin),
              (call_script, "script_cf_is_weapon_ranged", ":item", 1),
              
            (else_try),
              (try_begin),
                (neq, ":force_length", 0),
                (item_get_weapon_length, ":item_length", ":item"),
                (try_begin),
                  (lt, ":cur_score", ":item_length"),
                  (assign, ":cur_score", ":item_length"),
                  (assign, ":weapon", ":item"),
                (try_end),
              (else_try),
                (agent_get_troop_id, ":troop_id", ":agent"),
                (troop_is_guarantee_horse, ":troop_id"),
                (agent_get_horse, ":horse", ":agent"),
                (le, ":horse", 0),
                (try_for_range, ":item_slot", ek_item_0, ek_head),
                  (agent_get_item_slot, ":item", ":agent", ":item_slot"),
                  (gt, ":item", itm_no_item),
                  (item_get_type, ":weapon_type", ":item"),
                  (eq, ":weapon_type", itp_type_one_handed_wpn),
                  (item_get_swing_damage, ":swing", ":item"),
                  (gt, ":swing", 19),
                  (assign, ":weapon", ":item"),
                (try_end),
              (else_try),
                (agent_get_troop_id, ":troop_id", ":agent"),
                (assign, ":imod", imod_plain),
                (try_begin),    #only heroes have item modifications
                  (troop_is_hero, ":troop_id"),
                  (try_for_range, ":troop_item_slot",  ek_item_0, ek_head),    # heroes have only 4 possible weapons (equipped)
                    (troop_get_inventory_slot, reg0, ":troop_id", ":troop_item_slot"),  #Find Item Slot with same item ID as Equipped Weapon
                    (eq, reg0, ":item"),
                    (troop_get_inventory_slot_modifier, ":imod", ":troop_id", ":troop_item_slot"),
                  (try_end),
                (try_end),
                (call_script, "script_evaluate_item", ":item", ":imod"),
                (lt, ":cur_score", reg0),
                (assign, ":cur_score", reg0),
                (assign, ":weapon", ":item"),
              (try_end),
            (try_end),  #melee weapon
          (try_end),  #weapon slot loop
        (try_end),  #select weapon
        
        #equip selected items if needed
        (try_begin),
          (neq, ":cur_wielded", ":weapon"),
          (try_begin),
            (gt, ":shield", itm_no_item),
            (agent_get_wielded_item, reg0, ":agent", 1),
            (neq, reg0, ":shield"),	#reequipping secondary will UNequip (from experience)
            (agent_set_wielded_item, ":agent", ":shield"),
          (try_end),
          (gt, ":weapon", itm_no_item),
          (agent_set_wielded_item, ":agent", ":weapon"),
        (try_end),
      (try_end),]),
  
  # script_set_formation_destination by motomataru
  # Input: team, troop class, position
  # Kluge around buggy *_order_position functions for teams 0-3
  ("set_formation_destination", [(store_script_param, ":fteam", 1),
      (store_script_param, ":fdivision", 2),
      (store_script_param, ":fposition", 3),
      
      (position_get_x, ":x", ":fposition"),
      (position_get_y, ":y", ":fposition"),
      (position_get_rotation_around_z, ":zrot", ":fposition"),
      
      (store_add, ":slot", slot_team_d0_destination_x, ":fdivision"),
      (team_set_slot, ":fteam", ":slot", ":x"),
      (store_add, ":slot", slot_team_d0_destination_y, ":fdivision"),
      (team_set_slot, ":fteam", ":slot", ":y"),
      (store_add, ":slot", slot_team_d0_destination_zrot, ":fdivision"),
      (team_set_slot, ":fteam", ":slot", ":zrot"),
      
      (team_set_order_position, ":fteam", ":fdivision", ":fposition"),]),
  
  # script_get_formation_destination by motomataru
  # Input: position, team, troop class
  # Output: input position (pos0 used)
  # Kluge around buggy *_order_position functions for teams 0-3
  ("get_formation_destination", [(store_script_param, ":fposition", 1),
      (store_script_param, ":fteam", 2),
      (store_script_param, ":fdivision", 3),
      (init_position, ":fposition"),
      # (try_begin),
      #(is_between, ":fteam", 0, 4), #Caba - this will always pass MOTO except in
      #mods with more than four teams (eg SWC arena) but now obsolete by other
      #limits
      (store_add, ":slot", slot_team_d0_destination_x, ":fdivision"),
      (team_get_slot, ":x", ":fteam", ":slot"),
      (store_add, ":slot", slot_team_d0_destination_y, ":fdivision"),
      (team_get_slot, ":y", ":fteam", ":slot"),
      (store_add, ":slot", slot_team_d0_destination_zrot, ":fdivision"),
      (team_get_slot, ":zrot", ":fteam", ":slot"),
      
      (position_set_x, ":fposition", ":x"),
      (position_set_y, ":fposition", ":y"),
      (position_rotate_z, ":fposition", ":zrot"),
      # (else_try),
      # (store_add, ":slot", slot_team_d0_first_member, ":fdivision"), #only
      # defined for divisions in formation
      # (team_get_slot, reg0, ":fteam", ":slot"),
      # (try_begin), # "launder" team_get_order_position shutting down
      # position_move_x
      # (gt, reg0, -1),
      # (team_get_order_position, ":fposition", ":fteam", ":fdivision"),
      # (agent_get_position, pos0, reg0),
      # (agent_set_position, reg0, ":fposition"),
      # (agent_get_position, ":fposition", reg0),
      # (agent_set_position, reg0, pos0),
      # (try_end),
      # (try_end),
      (position_set_z_to_ground_level, ":fposition"),]),
  
  # script_formation_current_position by motomataru
  # Input: destination position (not pos0), team, division
  # Output: in destination position
  # As opposed to script_battlegroup_get_position, this obtains target rotation
  # from formation destination and positions at the center of the front
  ("formation_current_position", [(store_script_param, ":fposition", 1),
      (store_script_param, ":fteam", 2),
      (store_script_param, ":fdivision", 3),
      (call_script, "script_battlegroup_get_position", ":fposition", ":fteam", ":fdivision"),
      (call_script, "script_get_formation_destination", pos0, ":fteam", ":fdivision"),
      (position_copy_rotation, ":fposition", pos0),
      (call_script, "script_battlegroup_dist_center_to_front", ":fteam", ":fdivision"),
      (position_move_y, ":fposition", reg0, 0),]),
  
  # script_str_store_division_type_name by motomataru
  # Input: destination, division type (sdt_*)
  # Output: none
  ("str_store_division_type_name", [(store_script_param, ":str_reg", 1),
      (store_script_param, ":division_type", 2),
      (try_begin),
        (eq, ":division_type", sdt_infantry),
        (str_store_string, ":str_reg", "@infantry"),
      (else_try),
        (eq, ":division_type", sdt_archer),
        (str_store_string, ":str_reg", "@archer"),
      (else_try),
        (eq, ":division_type", sdt_cavalry),
        (str_store_string, ":str_reg", "@cavalry"),
      (else_try),
        (eq, ":division_type", sdt_polearm),
        (str_store_string, ":str_reg", "@polearm"),
      (else_try),
        (eq, ":division_type", sdt_skirmisher),
        (str_store_string, ":str_reg", "@skirmisher"),
      (else_try),
        (eq, ":division_type", sdt_harcher),
        (str_store_string, ":str_reg", "@mounted archer"),
      (else_try),
        (eq, ":division_type", sdt_support),
        (str_store_string, ":str_reg", "@support"),
      (else_try),
        (eq, ":division_type", sdt_bodyguard),
        (str_store_string, ":str_reg", "@bodyguard"),
      (else_try),
        (str_store_string, ":str_reg", "@undetermined type of"),
      (try_end),]),
  
  # script_formation_to_native_order by motomataru
  # Input: team, division, formation
  # Output: issues team_give_order with appropriate command to make formation
  ("formation_to_native_order",
    [(store_script_param, ":team", 1),
      (store_script_param, ":division", 2),
      (store_script_param, ":formation", 3),
      
      (try_begin),
        (gt, ":formation", formation_none),	#custom formation (bad call)
        
      (else_try),
        (eq, Native_Formations_Implementation, WB_Implementation),
        (store_add, ":slot", slot_team_d0_formation_space, ":division"),
        (team_get_slot, ":spacing", ":team", ":slot"),
        (val_sub, ":spacing", ":formation"),	#formation constants indicate number of "Stand Closer"
        (set_show_messages, 0),
        (try_for_range, reg0, 0, ":spacing"),
          (team_give_order, ":team", ":division", mordr_stand_closer),
        (try_end),
        (set_show_messages, 1),
        (team_set_slot, ":team", ":slot", ":spacing"),
        
        #WFAS implementation
      (else_try),
        (eq, ":formation", formation_1_row),
        (team_give_order, ":team", ":division", mordr_form_1_row),
      (else_try),
        (eq, ":formation", formation_2_row),
        (team_give_order, ":team", ":division", mordr_form_2_row),
      (else_try),
        (eq, ":formation", formation_3_row),
        (team_give_order, ":team", ":division", mordr_form_3_row),
      (else_try),
        (eq, ":formation", formation_4_row),
        (team_give_order, ":team", ":division", mordr_form_4_row),
      (else_try),
        (eq, ":formation", formation_5_row),
        (team_give_order, ":team", ":division", mordr_form_5_row),
      (try_end)]),
  
  # script_point_y_toward_position by motomataru
  # Input: from position, to position
  # Output: reg0 distance in cm
  # Basically, points the first position at the second, so then simple move_y
  # will move back and forth and move_x side to side
  # Things like cast_ray work with this as well
  ("point_y_toward_position", [(store_script_param, ":from_position", 1),
      (store_script_param, ":to_position", 2),
      (assign, ":save_fpm", 1),
      (convert_to_fixed_point, ":save_fpm"),
      (set_fixed_point_multiplier, 100),  #to match cm returned by get_distance_between_positions
      
      #remove current rotation
      (position_get_x, ":from_x", ":from_position"),
      (position_get_y, ":from_y", ":from_position"),
      (position_get_z, ":from_z", ":from_position"),
      (init_position, ":from_position"),
      (position_set_x, ":from_position", ":from_x"),
      (position_set_y, ":from_position", ":from_y"),
      (position_set_z, ":from_position", ":from_z"),
      
      #horizontal rotation
      (position_get_x, ":change_in_x", ":to_position"),
      (val_sub, ":change_in_x", ":from_x"),
      (position_get_y, ":change_in_y", ":to_position"),
      (val_sub, ":change_in_y", ":from_y"),
      
      (try_begin),
        (this_or_next | neq, ":change_in_y", 0),
        (neq, ":change_in_x", 0),
        (store_atan2, ":theta", ":change_in_y", ":change_in_x"),
        (assign, ":ninety", 90),
        (convert_to_fixed_point, ":ninety"),
        (val_sub, ":theta", ":ninety"),	#point Y axis at to position
        (position_rotate_z_floating, ":from_position", ":theta"),
      (try_end),
      
      #vertical rotation
      (get_distance_between_positions, ":distance_between", ":from_position", ":to_position"),
      (try_begin),
        (gt, ":distance_between", 0),
        (position_get_z, ":dist_z_to_sine", ":to_position"),
        (val_sub, ":dist_z_to_sine", ":from_z"),
        (val_div, ":dist_z_to_sine", ":distance_between"),
        (store_asin, ":theta", ":dist_z_to_sine"),
        (position_rotate_x_floating, ":from_position", ":theta"),
      (try_end),
      
      (assign, reg0, ":distance_between"),
      (set_fixed_point_multiplier, ":save_fpm"),]),
  
  #script_agent_fix_division
  #Input: agent_id
  #Output: nothing (agent divisions changed, slot set)
  #To fix AI troop divisions from the engine applying player's party divisions
  #on all agents
  #This is called after agent_reassign_team, so can safely assume correct team
  #is set
  ("agent_fix_division",
    [(store_script_param_1, ":agent"),
      (agent_set_slot, ":agent", slot_agent_new_division, -1),
      (get_player_agent_no, ":player"),	#after_mission_start triggers are called after spawn, so globals can't be used
      #yet
      
      (try_begin),
        (ge, ":player", 0),
        (neq, ":agent", ":player"),
        (agent_is_human, ":agent"),
        (agent_get_group, ":player_team", ":player"),
        (agent_get_group, ":team", ":agent"),
        (this_or_next | main_hero_fallen),
        (neq, ":team", ":player_team"),
        
        (assign, ":target_division", grc_infantry),
        (agent_get_horse, ":horse", ":agent"),
        (agent_get_troop_id, ":troop_no", ":agent"),
        
        #logic from script_troop_default_division
        #limited to the three divisions the AI currently uses
        (try_begin),
          (this_or_next | ge, ":horse", 0),
          (troop_is_guarantee_horse, ":troop_no"),
          (assign, ":target_division", grc_cavalry),
          
          # (try_begin),
          # (eq, ":flag_0_for_expanded", 0),
          # (try_for_range, reg0, 0, ":inv_cap"),
          # (troop_get_inventory_slot, ":item", ":troop_no", reg0),
          # (call_script, "script_cf_is_weapon_ranged", ":item", 1),
          # (assign, ":target_division", sdt_harcher),
          # (try_end),
          # (try_end),
          
        (else_try),
          (troop_is_guarantee_ranged, ":troop_no"),
          # (assign, ":has_ranged", 0),
          
          (try_for_range, ":item_slot", ek_item_0, ek_head),
            (agent_get_item_slot, ":item", ":agent", ":item_slot"),
            (call_script, "script_cf_is_weapon_ranged", ":item", 1),
            (agent_get_ammo, reg1, ":agent", 0),
            (ge, reg1, minimum_ranged_ammo),  #more than two to throw on a charge?
            # (item_get_type, reg1, ":item"),
            # (try_begin),
            # (eq, reg1, itp_type_thrown),
            # (eq, ":flag_0_for_expanded", 0),
            # (neq, ":target_division", grc_archers),
            # (assign, ":target_division", sdt_skirmisher),
            # (else_try),
            (assign, ":target_division", grc_archers),
            # (try_end),
            # (assign, ":has_ranged", 1),
          (try_end),
          
          # (neq, ":has_ranged", 0),
          
          # (else_try),
          # (eq, ":flag_0_for_expanded", 0),
          # (try_for_range, reg0, 0, ":inv_cap"),
          # (troop_get_inventory_slot, ":item", ":troop_no", reg0),
          # (call_script, "script_cf_is_thrusting_weapon", ":item"),
          # (item_get_type, reg1, ":item"),
          # (eq, reg1, itp_type_polearm),
          # (assign, ":target_division", sdt_polearm),
          # (try_end),
        (try_end),
        
        (agent_get_division, ":division", ":agent"),
        (neq, ":division", ":target_division"),
        (agent_set_division, ":agent", ":target_division"),
        (agent_set_slot, ":agent", slot_agent_new_division, ":target_division"),
      (try_end),]),
  
  # script_store_battlegroup_type
  # Input: team, division
  # Output: reg0 and slot_team_dx_type with sdt_* value
  # Automatically called from store_battlegroup_data
  ("store_battlegroup_type", [(store_script_param_1, ":fteam"),
      (store_script_param_2, ":fdivision"),
      
      (assign, ":count_infantry", 0),
      (assign, ":count_archer", 0),
      (assign, ":count_cavalry", 0),
      (assign, ":count_harcher", 0),
      (assign, ":count_polearms", 0),
      (assign, ":count_skirmish", 0),
      (assign, ":count_support", 0),
      (assign, ":count_bodyguard", 0),
      
      (team_get_leader, ":leader", ":fteam"),
      
      (try_for_agents, ":cur_agent"),
        (call_script, "script_cf_valid_formation_member", ":fteam", ":fdivision", ":leader", ":cur_agent"),
        (agent_get_troop_id, ":cur_troop", ":cur_agent"),
        (agent_get_ammo, ":cur_ammo", ":cur_agent", 0),
        
        (try_begin),
          (neg | troop_is_hero, ":cur_troop"),
          (try_begin), #Cavalry
            (agent_get_horse, reg0, ":cur_agent"),
            (ge, reg0, 0),
            (try_begin),
              (ge, ":cur_ammo", minimum_ranged_ammo),
              (val_add, ":count_harcher", 1),
            (else_try),
              (val_add, ":count_cavalry", 1),
            (try_end),
          (else_try), #Archers
            (ge, ":cur_ammo", minimum_ranged_ammo),
            # #use when troops are equipped with ranged at start of battle
            # (agent_get_class, ":bgclass", ":cur_agent"),
            # (eq, ":bgclass", grc_archers),
            # #end use when troops equipped with ranged at start of battle
            (assign, ":end", ek_head),
            (try_for_range, ":i", ek_item_0, ":end"),
              (agent_get_item_slot, ":item", ":cur_agent", ":i"),
              (gt, ":item", 0),
              (item_get_type, ":weapontype", ":item"),
              (is_between, ":weapontype", itp_type_bow, itp_type_thrown),  # bow or crossbow
              (assign, ":end", ek_item_0), #loop Break
            (try_end),
            (try_begin),
              (eq, ":end", ek_head), #failed to find bow or crossbow
              (val_add, ":count_skirmish", 1),
            (else_try),
              (val_add, ":count_archer", 1),
            (try_end),
          (else_try), #Infantry
            (assign, ":end", ek_head),
            (try_for_range, ":i", ek_item_0, ":end"),
              (agent_get_item_slot, ":item", ":cur_agent", ":i"),
              (call_script, "script_cf_is_thrusting_weapon", ":item"),
              (item_get_type, ":weapontype", ":item"),
              (eq, ":weapontype", itp_type_polearm),
              (assign, ":end", ek_item_0), #loop Break
            (try_end),
            (try_begin),
              (eq, ":end", ek_head), #failed to find a polearm
              (val_add, ":count_infantry", 1),
            (else_try),
              (val_add, ":count_polearms", 1),
            (try_end),
          (try_end),
        (else_try), #Heroes
          (assign, ":support_skills", 0), #OPEN TO SUGGESTIONS HERE ?skl_trade, skl_spotting, skl_pathfinding,
          #skl_tracking?
          (store_skill_level, reg0, skl_engineer, ":cur_troop"),
          (val_add, ":support_skills", reg0),
          (store_skill_level, reg0, skl_first_aid, ":cur_troop"),
          (val_add, ":support_skills", reg0),
          (store_skill_level, reg0, skl_surgery, ":cur_troop"),
          (val_add, ":support_skills", reg0),
          (store_skill_level, reg0, skl_wound_treatment, ":cur_troop"),
          (val_add, ":support_skills", reg0),
          (try_begin),
            (gt, ":support_skills", 5),
            (val_add, ":count_support", 1),
          (else_try),
            (val_add, ":count_bodyguard", 1),
          (try_end),
        (try_end), #Regular v Hero
      (try_end), #Agent Loop
      
      #Do Comparisons With Counts, set ":div_type"
      (assign, ":slot", slot_team_d0_type),
      (team_set_slot, scratch_team, ":slot", ":count_infantry"),
      (val_add, ":slot", 1),
      (team_set_slot, scratch_team, ":slot", ":count_archer"),
      (val_add, ":slot", 1),
      (team_set_slot, scratch_team, ":slot", ":count_cavalry"),
      (val_add, ":slot", 1),
      (team_set_slot, scratch_team, ":slot", ":count_polearms"),
      (val_add, ":slot", 1),
      (team_set_slot, scratch_team, ":slot", ":count_skirmish"),
      (val_add, ":slot", 1),
      (team_set_slot, scratch_team, ":slot", ":count_harcher"),
      (val_add, ":slot", 1),
      (team_set_slot, scratch_team, ":slot", ":count_support"),
      (val_add, ":slot", 1),
      (team_set_slot, scratch_team, ":slot", ":count_bodyguard"),
      
      (assign, ":count_to_beat", 0),
      (assign, ":count_total", 0),
      (try_for_range, ":type", sdt_infantry, sdt_infantry + 8), #only 8 sdt_types at the moment
        (store_add, ":slot", slot_team_d0_type, ":type"),
        (team_get_slot, ":count", scratch_team, ":slot"),
        (val_add, ":count_total", ":count"),
        (lt, ":count_to_beat", ":count"),
        (assign, ":count_to_beat", ":count"),
        (assign, ":div_type", ":type"),
      (try_end),
      
      (val_mul, ":count_to_beat", 2),
      (try_begin),
        (lt, ":count_to_beat", ":count_total"), #Less than half of this division
        (assign, ":count_to_beat", 0),
        (assign, ":div_type", -1),
        (try_for_range, ":type", sdt_infantry, sdt_infantry + 3), #check main types for a majority
          (store_add, ":slot", slot_team_d0_type, ":type"),
          (team_get_slot, ":count", scratch_team, ":slot"),
          (val_add, ":slot", 3),	#subtype is three more than main type
          (team_get_slot, reg0, scratch_team, ":slot"),
          (val_add, ":count", reg0),
          (lt, ":count_to_beat", ":count"),
          (assign, ":count_to_beat", ":count"),
          (assign, ":div_type", ":type"),
        (try_end),
        
        (val_mul, ":count_to_beat", 2),
        (lt, ":count_to_beat", ":count_total"), #Less than half of this division
        (assign, ":div_type", sdt_unknown), #Or 0
      (try_end),
      
      #hard-code traditional infantry division (avoid player confusion for mods
      #which arm troops with ranged at start of battle)
      (try_begin),
        (eq, ":fdivision", grc_infantry),
        (neq, ":div_type", sdt_polearm),
        (assign, ":div_type", sdt_infantry),
      (try_end),
      
      (store_add, ":slot", slot_team_d0_type, ":fdivision"),
      (team_set_slot, ":fteam", ":slot", ":div_type"),
      (assign, reg0, ":div_type"),]),
  
  # script_store_battlegroup_data by motomataru #EDITED TO SLOTS FOR MANY
  # DIVISIONS BY CABA'DRIN
  # Input: none
  # Output: sets positions and globals to track data on ALL groups in a battle
  # Globals used: pos0, pos1, reg0
  ("store_battlegroup_data", [(assign, ":team0_leader", 0),
      (assign, ":team0_x_leader", 0),
      (assign, ":team0_y_leader", 0),
      (assign, ":team0_zrot_leader", 0),
      (assign, ":team0_level_leader", 0),
      (assign, ":team1_leader", 0),
      (assign, ":team1_x_leader", 0),
      (assign, ":team1_y_leader", 0),
      (assign, ":team1_zrot_leader", 0),
      (assign, ":team1_level_leader", 0),
      (assign, ":team2_leader", 0),
      (assign, ":team2_x_leader", 0),
      (assign, ":team2_y_leader", 0),
      (assign, ":team2_zrot_leader", 0),
      (assign, ":team2_level_leader", 0),
      (assign, ":team3_leader", 0),
      (assign, ":team3_x_leader", 0),
      (assign, ":team3_y_leader", 0),
      (assign, ":team3_zrot_leader", 0),
      (assign, ":team3_level_leader", 0),
      
      #save some info
      (try_for_range, ":division", 0, 9),
        (store_add, ":slot", slot_team_d0_size, ":division"),
        (try_begin),
          (team_slot_ge, "$fplayer_team_no", ":slot", 1),
          (store_add, ":slot", slot_team_d0_exists, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", 1),
          
        (else_try),
          (store_add, ":slot", slot_team_d0_exists, ":division"),
          (team_set_slot, "$fplayer_team_no", ":slot", 0),
        (try_end),
      (try_end),
      
      #Team Slots reset every mission, like agent slots, but just to be sure for
      #when it gets called during the mission
      (try_for_range, ":slot", reset_team_stats_begin, reset_team_stats_end), #Those within the "RESET GROUP" in formations_constants
        (try_for_range, ":team", 0, 4),
          (team_set_slot, ":team", ":slot", 0),
        (try_end),
      (try_end),
      
      (try_for_agents, ":cur_agent"),
        (agent_set_slot, ":cur_agent", slot_agent_nearest_enemy_agent, -1),
        
        (agent_is_alive, ":cur_agent"),
        (agent_is_human, ":cur_agent"),
        (agent_slot_eq, ":cur_agent", slot_agent_is_running_away, 0),
        
        (agent_get_group, ":bgteam", ":cur_agent"),
        (agent_get_division, ":bgdivision", ":cur_agent"),
        (agent_get_class, ":agent_class", ":cur_agent"),
        (agent_get_position, pos1, ":cur_agent"),
        
        (try_begin),
          (agent_is_non_player, ":cur_agent"),
          
          (store_add, ":slot", slot_team_d0_type, ":bgdivision"),
          (team_get_slot, ":bgtype", ":bgteam", ":slot"),
          (this_or_next | eq, ":bgtype", sdt_cavalry),	#assigned to horsed division
          (eq, ":bgtype", sdt_harcher),
          
          (team_get_riding_order, reg0, ":bgteam", ":bgdivision"),
          (neq, reg0, rordr_dismount),
          
          (team_get_order_position, pos0, ":bgteam", ":bgdivision"),
          (get_distance_between_positions, ":old_distance", pos0, pos1),
          (gt, ":old_distance", AI_charge_distance),	#agent is out of formation?
          
          (assign, ":target_type", ":bgtype"),
          
          (try_begin),
            (eq, ":agent_class", grc_infantry),	#Native has transferred this agent to infantry
            (assign, ":target_type", sdt_infantry),
            
            (try_for_range, ":item_slot", ek_item_0, ek_head),
              (eq, ":bgteam", "$fplayer_team_no"),	#AI doesn't use extended right now
              (agent_get_item_slot, ":item", ":cur_agent", ":item_slot"),
              (call_script, "script_cf_is_thrusting_weapon", ":item"),
              (item_get_type, reg0, ":item"),
              (eq, reg0, itp_type_polearm),
              (assign, ":target_type", sdt_polearm),
            (try_end),
            
          (else_try),
            (eq, ":agent_class", grc_archers),	#Native has transferred this agent to archers
            (assign, ":target_type", sdt_archer),
            
            (try_for_range, ":item_slot", ek_item_0, ek_head),
              (eq, ":bgteam", "$fplayer_team_no"),	#AI doesn't use extended right now
              (agent_get_item_slot, ":item", ":cur_agent", ":item_slot"),
              (call_script, "script_cf_is_weapon_ranged", ":item", 1),
              (agent_get_ammo, reg1, ":cur_agent", 0),
              (ge, reg1, minimum_ranged_ammo),  #more than two to throw on a charge?
              (item_get_type, reg0, ":item"),
              (eq, reg0, itp_type_thrown),
              (assign, ":target_type", sdt_skirmisher),
            (try_end),
          (try_end),
          
          (neq, ":target_type", ":bgtype"),
          (assign, ":bgdivision", ":target_type"),
          
          (try_for_range_backwards, ":new_division", 0, 9),
            (store_add, ":slot", slot_team_d0_size, ":new_division"),
            (team_get_slot, reg0, ":bgteam", ":slot"),
            (gt, reg0, 0),
            
            (store_add, ":slot", slot_team_d0_type, ":new_division"),
            (team_get_slot, reg0, ":bgteam", ":slot"),
            (eq, reg0, ":target_type"),
            
            (assign, ":bgdivision", ":new_division"),
          (try_end),
          
          (try_begin),
            (store_add, ":slot", slot_team_d0_exists, ":bgdivision"),
            (team_slot_eq, "$fplayer_team_no", ":slot", 0),	#division does not yet exist?
            (agent_is_alive, "$fplayer_agent_no"),
            (store_add, ":slot", slot_team_d0_move_order, ":bgdivision"),
            (neg | team_slot_eq, "$fplayer_team_no", ":slot", mordr_follow),
            (team_set_slot, "$fplayer_team_no", ":slot", mordr_follow),
            (set_show_messages, 0),
            (team_give_order, "$fplayer_team_no", ":bgdivision", mordr_follow),
            (set_show_messages, 1),
          (try_end),
          
          (agent_set_slot, ":cur_agent", slot_agent_new_division, ":bgdivision"),	#reassign
          (agent_set_division, ":cur_agent", ":bgdivision"),
          
        (else_try),	#Maintain any changed divisions (apparently agents get switched back)
          (agent_is_non_player, ":cur_agent"),
          (agent_slot_ge, ":cur_agent", slot_agent_new_division, 0),
          (neg | agent_slot_eq, ":cur_agent", slot_agent_new_division, ":bgdivision"),
          (agent_get_slot, ":bgdivision", ":cur_agent", slot_agent_new_division),
          (agent_set_division, ":cur_agent", ":bgdivision"),
        (try_end),
        (agent_get_troop_id, ":cur_troop", ":cur_agent"),
        (try_begin),
          (game_in_multiplayer_mode),
          (try_begin),
            (this_or_next | is_between, ":cur_troop", multiplayer_addon_troops_begin, multiplayer_addon_troops_end),#it's a player
            (is_between, ":cur_troop", multiplayer_troops_begin, multiplayer_troops_end),	#it's a player
            (assign, ":bgdivision", -1),
          (try_end),
        (else_try),
          (team_get_leader, ":leader", ":bgteam"),
          (eq, ":leader", ":cur_agent"),
          (assign, ":bgdivision", -1),
        (try_end),
        (store_character_level, ":cur_level", ":cur_troop"),
        (agent_get_ammo, ":cur_ammo", ":cur_agent", 0),
        
        #get weapon characteristics
        (assign, ":cur_weapon_type", 0),
        (assign, ":cur_weapon_length", 0),
        (assign, ":cur_swung_weapon_length", 0),
        (agent_get_wielded_item, ":cur_weapon", ":cur_agent", 0),
        (try_begin),
          (is_between, ":cur_weapon", weapons_begin, weapons_end),
          (neg | is_between, ":cur_weapon", estandartes_begin, estandartes_end),	#put exceptions here, such as standards, that will otherwise force a lot of
          #extra spacing for nothing
          (item_get_weapon_length, ":cur_weapon_length", ":cur_weapon"),
          
          (try_begin),
            (call_script, "script_cf_is_thrusting_weapon", ":cur_weapon"),
          (else_try),
            (assign, ":cur_swung_weapon_length", ":cur_weapon_length"),
          (try_end),
        (try_end),
        
        #add up armor
        (assign, ":cur_avg_armor", 0),
        (try_for_range, ":item_slot", ek_head, ek_horse),
          (agent_get_item_slot, ":armor", ":cur_agent", ":item_slot"),
          (gt, ":armor", itm_no_item),
          (item_get_head_armor, reg0, ":armor"),
          (val_add, ":cur_avg_armor", reg0),
          (item_get_body_armor, reg0, ":armor"),
          (val_add, ":cur_avg_armor", reg0),
          (item_get_leg_armor, reg0, ":armor"),
          (val_add, ":cur_avg_armor", reg0),
        (try_end),
        (agent_get_wielded_item, ":armor", ":cur_agent", 1),	#include shield
        (try_begin),
          (gt, ":armor", itm_no_item),
          (item_get_type, ":item_type", ":armor"),
          (eq, ":item_type", itp_type_shield),
          (item_get_body_armor, reg0, ":armor"),
          (val_add, ":cur_avg_armor", reg0),
        (try_end),
        (val_div, ":cur_avg_armor", 3),	#average the zones (head, body, leg)
        
        #average with horse armor for mounted agents
        (agent_get_horse, ":cur_horse", ":cur_agent"),
        (try_begin),
          (gt, ":cur_horse", -1),
          (agent_get_item_id, ":itm_horse", ":cur_horse"),
          (gt, ":itm_horse", itm_no_item),
          (item_get_body_armor, reg0, ":itm_horse"),
          (val_add, ":cur_avg_armor", reg0),
          (val_div, ":cur_avg_armor", 2),
        (try_end),
        
        (position_get_x, ":x_value", pos1),
        (position_get_y, ":y_value", pos1),
        (position_get_rotation_around_z, ":zrot_value", pos1),
        (try_begin),
          (eq, ":bgdivision", -1), #Leaders
          (try_begin),
            (eq, ":bgteam", 0),
            (assign, ":team0_leader", 1),
            (assign, ":team0_x_leader", ":x_value"),
            (assign, ":team0_y_leader", ":y_value"),
            (assign, ":team0_zrot_leader", ":zrot_value"),
            (assign, ":team0_level_leader", ":cur_level"),
          (else_try),
            (eq, ":bgteam", 1),
            (assign, ":team1_leader", 1),
            (assign, ":team1_x_leader", ":x_value"),
            (assign, ":team1_y_leader", ":y_value"),
            (assign, ":team1_zrot_leader", ":zrot_value"),
            (assign, ":team1_level_leader", ":cur_level"),
          (else_try),
            (eq, ":bgteam", 2),
            (assign, ":team2_leader", 1),
            (assign, ":team2_x_leader", ":x_value"),
            (assign, ":team2_y_leader", ":y_value"),
            (assign, ":team2_zrot_leader", ":zrot_value"),
            (assign, ":team2_level_leader", ":cur_level"),
          (else_try),
            (eq, ":bgteam", 3),
            (assign, ":team3_leader", 1),
            (assign, ":team3_x_leader", ":x_value"),
            (assign, ":team3_y_leader", ":y_value"),
            (assign, ":team3_zrot_leader", ":zrot_value"),
            (assign, ":team3_level_leader", ":cur_level"),
          (try_end),
        (else_try),
          # (agent_get_ammo, reg0, ":cur_agent", 1), #Division in Melee
          (try_begin),
            # (le, reg0, 0), #not wielding ranged weapon?
            (agent_get_attack_action, reg0, ":cur_agent"),
            (gt, reg0, 0),
            (store_add, ":slot", slot_team_d0_is_fighting, ":bgdivision"),
            (team_get_slot, reg0, ":bgteam", ":slot"),
            (val_add, reg0, 1),
            (team_set_slot, ":bgteam", ":slot", reg0),
          (try_end),
          
          (store_add, ":slot", slot_team_d0_size, ":bgdivision"), #Division Count
          (team_get_slot, ":value", ":bgteam", ":slot"),
          (val_add, ":value", 1),
          (team_set_slot, ":bgteam", ":slot", ":value"),
          
          (try_begin),
            (ge, ":cur_ammo", minimum_ranged_ammo),
            (store_add, ":slot", slot_team_d0_percent_ranged, ":bgdivision"), #Division Percentage are Archers
            (team_get_slot, ":value", ":bgteam", ":slot"),
            (val_add, ":value", 1),
            (team_set_slot, ":bgteam", ":slot", ":value"),
          (else_try),
            (store_add, ":slot", slot_team_d0_low_ammo, ":bgdivision"), #Division Running out of Ammo Flag
            (team_set_slot, ":bgteam", ":slot", 1),
          (try_end),
          
          (try_begin),
            (eq, ":cur_weapon_type", itp_type_thrown),
            (store_add, ":slot", slot_team_d0_percent_throwers, ":bgdivision"), #Division Percentage are Throwers
            (team_get_slot, ":value", ":bgteam", ":slot"),
            (val_add, ":value", 1),
            (team_set_slot, ":bgteam", ":slot", ":value"),
          (try_end),
          
          (store_add, ":slot", slot_team_d0_level, ":bgdivision"), #Division Level
          (team_get_slot, ":value", ":bgteam", ":slot"),
          (val_add, ":value", ":cur_level"),
          (team_set_slot, ":bgteam", ":slot", ":value"),
          
          (store_add, ":slot", slot_team_d0_weapon_length, ":bgdivision"), #Division Weapon Length
          (team_get_slot, ":value", ":bgteam", ":slot"),
          (val_add, ":value", ":cur_weapon_length"),
          (team_set_slot, ":bgteam", ":slot", ":value"),
          
          (store_add, ":slot", slot_team_d0_swung_weapon_length, ":bgdivision"), #Division Swung Weapon Length
          (team_get_slot, ":value", ":bgteam", ":slot"),
          (try_begin),
            (lt, ":value", ":cur_swung_weapon_length"),
            (team_set_slot, ":bgteam", ":slot", ":cur_swung_weapon_length"),
          (try_end),
          
          (store_add, ":slot", slot_team_d0_armor, ":bgdivision"), #Division Armor
          (team_get_slot, ":value", ":bgteam", ":slot"),
          (val_add, ":value", ":cur_avg_armor"),
          (team_set_slot, ":bgteam", ":slot", ":value"),
          
          (try_begin),	#Division First Rank Shortest Weapon Length
            (agent_slot_eq, ":cur_agent", slot_agent_formation_rank, 1),
            (store_add, ":slot", slot_team_d0_front_weapon_length, ":bgdivision"),
            (team_get_slot, ":value", ":bgteam", ":slot"),
            (this_or_next | eq, ":value", 0),
            (gt, ":value", ":cur_weapon_length"),
            (team_set_slot, ":bgteam", ":slot", ":cur_weapon_length"),
          (try_end),
          
          (store_add, ":slot", slot_team_d0_avg_x, ":bgdivision"), #Position X
          (team_get_slot, ":value", ":bgteam", ":slot"),
          (val_add, ":value", ":x_value"),
          (team_set_slot, ":bgteam", ":slot", ":value"),
          
          (store_add, ":slot", slot_team_d0_avg_y, ":bgdivision"), #Position Y
          (team_get_slot, ":value", ":bgteam", ":slot"),
          (val_add, ":value", ":y_value"),
          (team_set_slot, ":bgteam", ":slot", ":value"),
          
          (store_add, ":slot", slot_team_d0_avg_zrot, ":bgdivision"), #Rotation
          (team_get_slot, ":value", ":bgteam", ":slot"),
          (val_add, ":value", ":zrot_value"),
          (team_set_slot, ":bgteam", ":slot", ":value"),
        (try_end), #Leader vs Regular
        
        (try_begin),
          (eq, ":agent_class", grc_archers),
          (team_get_slot, ":value", ":bgteam", slot_team_num_archers),
          (val_add, ":value", 1),
          (team_set_slot, ":bgteam", slot_team_num_archers, ":value"),
          
        (else_try),
          (eq, ":agent_class", grc_cavalry),
          (team_get_slot, ":value", ":bgteam", slot_team_num_cavalry),
          (val_add, ":value", 1),
          (team_set_slot, ":bgteam", slot_team_num_cavalry, ":value"),
          
        (else_try),
          (eq, ":agent_class", grc_infantry),
          (team_get_slot, ":value", ":bgteam", slot_team_num_infantry),
          (val_add, ":value", 1),
          (team_set_slot, ":bgteam", slot_team_num_infantry, ":value"),
        (try_end),
        
        #find nearest enemy agent
        (assign, ":nearest_runner", -1),
        (agent_ai_get_num_cached_enemies, ":num_nearby_agents", ":cur_agent"),
        (try_for_range, reg0, 0, ":num_nearby_agents"),
          (agent_ai_get_cached_enemy, ":enemy_agent", ":cur_agent", reg0),
          (agent_is_alive, ":enemy_agent"),
          
          (try_begin),
            (eq, ":nearest_runner", -1),
            (assign, ":nearest_runner", ":enemy_agent"),
            
          (else_try),
            (agent_get_position, pos0, ":enemy_agent"),
            (get_distance_between_positions, ":new_distance", pos0, pos1),
            (agent_get_position, pos0, ":nearest_runner"),
            (get_distance_between_positions, ":old_distance", pos0, pos1),
            (lt, ":new_distance", ":old_distance"),
            (assign, ":nearest_runner", ":enemy_agent"),
          (try_end),
          
          (agent_slot_eq, ":enemy_agent", slot_agent_is_running_away, 0),
          
          (try_begin),
            (agent_get_slot, ":closest_enemy", ":cur_agent", slot_agent_nearest_enemy_agent),
            (eq, ":closest_enemy", -1),
            (agent_set_slot, ":cur_agent", slot_agent_nearest_enemy_agent, ":enemy_agent"),
            
          (else_try),
            (agent_get_position, pos0, ":enemy_agent"),
            (get_distance_between_positions, ":new_distance", pos0, pos1),
            (agent_get_position, pos0, ":closest_enemy"),
            (get_distance_between_positions, ":old_distance", pos0, pos1),
            (lt, ":new_distance", ":old_distance"),
            (agent_set_slot, ":cur_agent", slot_agent_nearest_enemy_agent, ":enemy_agent"),
          (try_end),
        (try_end),
        (try_begin),
          (agent_slot_eq, ":cur_agent", slot_agent_nearest_enemy_agent, -1),
          (agent_set_slot, ":cur_agent", slot_agent_nearest_enemy_agent, ":nearest_runner"),
        (try_end),
        
        #exploit closest agent data
        (try_begin),
          (agent_get_slot, ":closest_enemy", ":cur_agent", slot_agent_nearest_enemy_agent),
          (neq, ":closest_enemy", -1),
          (agent_get_position, pos0, ":closest_enemy"),
          (get_distance_between_positions, ":closest_distance", pos0, pos1),
          
          #check target of AI agent behavior
          (try_begin),
            (agent_is_non_player, ":cur_agent"),
            
            (agent_ai_get_behavior_target, ":cur_targeted_agent", ":cur_agent"),
            (neq, ":closest_enemy", ":cur_targeted_agent"),
            
            (this_or_next | neg | agent_is_non_player, ":closest_enemy"),	#AI can always sense player behind them (balancing factor, dedicated to
            #Idibil)
            (neg | position_is_behind_position, pos0, pos1),
            
            (lt, ":closest_distance", 2000),	#Assuming rethink is expensive, don't bother beyond 20m
            
            (store_add, ":slot", slot_team_d0_formation, ":bgdivision"),
            (team_get_slot, ":value", ":bgteam", ":slot"),
            (this_or_next | eq, formation_rethink_for_formations_only, 0),
            (gt, ":value", formation_none),
            
            (agent_force_rethink, ":cur_agent"),
          (try_end),
          
          #update division information
          (try_begin),
            (ge, ":bgdivision", 0),	#not leaders
            
            (try_begin),
              (lt, ":closest_distance", 350),
              (agent_get_division, reg0, ":closest_enemy"),
              (store_add, ":slot", slot_team_d0_enemy_supporting_melee, reg0),
              (agent_get_group, reg0, ":closest_enemy"),
              (team_get_slot, ":value", reg0, ":slot"),
              (val_add, ":value", 1),
              (team_set_slot, reg0, ":slot", ":value"),
            (try_end),
            
            (store_add, ":slot", slot_team_d0_closest_enemy_dist, ":bgdivision"),
            (team_get_slot, ":old_distance", ":bgteam", ":slot"),
            (try_begin),
              (this_or_next | eq, ":old_distance", 0),
              (lt, ":closest_distance", ":old_distance"),
              (team_set_slot, ":bgteam", ":slot", ":closest_distance"),
              (store_add, ":slot", slot_team_d0_closest_enemy, ":bgdivision"),
              (team_set_slot, ":bgteam", ":slot", ":closest_enemy"),
            (try_end),
            
            (assign, ":doit", 0),
            (agent_get_class, ":enemy_agent_class", ":closest_enemy"),
            (store_add, ":slot", slot_team_d0_type, ":bgdivision"),
            (team_get_slot, ":value", ":bgteam", ":slot"),
            
            #AI infantry division tracks non-infantry to preferably chase
            (try_begin),
              (this_or_next | eq, ":value", sdt_polearm),
              (eq, ":value", sdt_infantry),
              (neq, ":enemy_agent_class", grc_cavalry),
              (assign, ":doit", 1),
              
              #AI archer division tracks infantry to avoid
            (else_try),
              (this_or_next | eq, ":value", sdt_archer),
              (eq, ":value", sdt_skirmisher),
              (eq, ":enemy_agent_class", grc_infantry),
              (assign, ":doit", 1),
            (try_end),
            
            (eq, ":doit", 1),
            (store_add, ":slot", slot_team_d0_closest_enemy_special_dist, ":bgdivision"),
            (team_get_slot, ":old_distance", ":bgteam", ":slot"),
            (try_begin),
              (this_or_next | eq, ":old_distance", 0),
              (lt, ":closest_distance", ":old_distance"),
              (team_set_slot, ":bgteam", ":slot", ":closest_distance"),
              (store_add, ":slot", slot_team_d0_closest_enemy_special, ":bgdivision"),
              (team_set_slot, ":bgteam", ":slot", ":closest_enemy"),
            (try_end),
          (try_end),	#update division info
        (try_end),	#exploit closest agent data
      (try_end), #Agent Loop
      
      #calculate team sizes, sum positions; within calculate battle group averages
      (try_for_range, ":team", 0, 4),
        (assign, ":team_size", 0),
        (assign, ":team_level", 0),
        (assign, ":team_x", 0),
        (assign, ":team_y", 0),
        (assign, ":team_zrot", 0),
        
        (try_for_range, ":division", 0, 9),
          #sum for team averages
          (store_add, ":slot", slot_team_d0_size, ":division"),
          (team_get_slot, ":division_size", ":team", ":slot"),
          (gt, ":division_size", 0),
          (val_add, ":team_size", ":division_size"),
          
          (store_add, ":slot", slot_team_d0_level, ":division"),
          (team_get_slot, ":division_level", ":team", ":slot"),
          (val_add, ":team_level", ":division_level"),
          
          (store_add, ":slot", slot_team_d0_avg_x, ":division"),
          (team_get_slot, ":division_x", ":team", ":slot"),
          (val_add, ":team_x", ":division_x"),
          
          (store_add, ":slot", slot_team_d0_avg_y, ":division"),
          (team_get_slot, ":division_y", ":team", ":slot"),
          (val_add, ":team_y", ":division_y"),
          
          (store_add, ":slot", slot_team_d0_avg_zrot, ":division"),
          (team_get_slot, ":division_zrot", ":team", ":slot"),
          (val_add, ":team_zrot", ":division_zrot"),
          
          #calculate battle group averages
          (store_add, ":slot", slot_team_d0_level, ":division"),
          (val_div, ":division_level", ":division_size"),
          (team_set_slot, ":team", ":slot", ":division_level"),
          
          (store_add, ":slot", slot_team_d0_percent_ranged, ":division"),
          (team_get_slot, ":value", ":team", ":slot"),
          (val_mul, ":value", 100),
          (val_div, ":value", ":division_size"),
          (team_set_slot, ":team", ":slot", ":value"),
          
          (store_add, ":slot", slot_team_d0_percent_throwers, ":division"),
          (team_get_slot, ":value", ":team", ":slot"),
          (val_mul, ":value", 100),
          (val_div, ":value", ":division_size"),
          (team_set_slot, ":team", ":slot", ":value"),
          
          (store_add, ":slot", slot_team_d0_weapon_length, ":division"),
          (team_get_slot, ":value", ":team", ":slot"),
          (val_div, ":value", ":division_size"),
          (team_set_slot, ":team", ":slot", ":value"),
          
          # (store_add, ":slot", slot_team_d0_swung_weapon_length, ":division"), MOTO
          # systematic testing shows best to use max swung weapon length as basis for
          # formation spacing
          # (team_get_slot, ":value", ":team", ":slot"),
          # (val_div, ":value", ":division_size"),
          # (team_set_slot, ":team", ":slot", ":value"),
          
          # (store_add, ":slot", slot_team_d0_front_agents, ":division"), MOTO front
          # rank should be within shortest weapon distance, not average
          # (team_get_slot, reg0, ":team", ":slot"),
          # (try_begin),
          # (gt, reg0, 0),
          # (store_add, ":slot", slot_team_d0_front_weapon_length, ":division"),
          # (team_get_slot, ":value", ":team", ":slot"),
          # (val_div, ":value", reg0),
          # (team_set_slot, ":team", ":slot", ":value"),
          # (try_end),
          
          (store_add, ":slot", slot_team_d0_avg_x, ":division"),
          (val_div, ":division_x", ":division_size"),
          (team_set_slot, ":team", ":slot", ":division_x"),
          
          (store_add, ":slot", slot_team_d0_avg_y, ":division"),
          (val_div, ":division_y", ":division_size"),
          (team_set_slot, ":team", ":slot", ":division_y"),
          
          (store_add, ":slot", slot_team_d0_avg_zrot, ":division"),
          (val_div, ":division_zrot", ":division_size"),
          (team_set_slot, ":team", ":slot", ":division_zrot"),
          
          (store_add, ":slot", slot_team_d0_type, ":division"),
          (team_get_slot, reg0, ":team", ":slot"),
          (try_begin),
            (neg | is_between, reg0, 0, 8),	#TODO reset on reinforcements
            (call_script, "script_store_battlegroup_type", ":team", ":division"),
          (try_end),
        (try_end), #Division Loop
        
        #Team Leader Additions
        (try_begin),
          (eq, ":team", 0),
          (val_add, ":team_size", ":team0_leader"),
          (val_add, ":team_level", ":team0_level_leader"),
          (val_add, ":team_x", ":team0_x_leader"),
          (val_add, ":team_y", ":team0_y_leader"),
          (val_add, ":team_zrot", ":team0_zrot_leader"),
        (else_try),
          (eq, ":team", 1),
          (val_add, ":team_size", ":team1_leader"),
          (val_add, ":team_level", ":team1_level_leader"),
          (val_add, ":team_x", ":team1_x_leader"),
          (val_add, ":team_y", ":team1_y_leader"),
          (val_add, ":team_zrot", ":team1_zrot_leader"),
        (else_try),
          (eq, ":team", 2),
          (val_add, ":team_size", ":team2_leader"),
          (val_add, ":team_level", ":team2_level_leader"),
          (val_add, ":team_x", ":team2_x_leader"),
          (val_add, ":team_y", ":team2_y_leader"),
          (val_add, ":team_zrot", ":team2_zrot_leader"),
        (else_try),
          (eq, ":team", 3),
          (val_add, ":team_size", ":team3_leader"),
          (val_add, ":team_level", ":team3_level_leader"),
          (val_add, ":team_x", ":team3_x_leader"),
          (val_add, ":team_y", ":team3_y_leader"),
          (val_add, ":team_zrot", ":team3_zrot_leader"),
        (try_end),
        
        #calculate team averages
        (gt, ":team_size", 0),
        (team_set_slot, ":team", slot_team_size, ":team_size"),
        (val_div, ":team_level", ":team_size"),
        (team_set_slot, ":team", slot_team_level, ":team_level"),
        
        (val_div, ":team_x", ":team_size"),
        (team_set_slot, ":team", slot_team_avg_x, ":team_x"),
        (val_div, ":team_y", ":team_size"),
        (team_set_slot, ":team", slot_team_avg_y, ":team_y"),
        (val_div, ":team_zrot", ":team_size"),
        (team_set_slot, ":team", slot_team_avg_zrot, ":team_zrot"),
      (try_end), #Team Loop
  ]),
  
  # script_cf_division_data_available by motomataru
  ("cf_division_data_available", [(assign, ":evidence", 0),
      (try_for_range, ":team", 0, 4),
        (team_slot_ge, ":team", slot_team_size, 1),
        (assign, ":evidence", 1),
      (try_end),
      (neq, ":evidence", 0)]),
  
  # script_battlegroup_get_position by motomataru #CABA - EDITED TO USE SLOTS,
  # NOT STORED POS NUMBERS
  # Input: destination position, team, division
  # Output: battle group position
  #			average team position if division input NOT set to 0-8
  ("battlegroup_get_position", [(store_script_param, ":bgposition", 1),
      (store_script_param, ":bgteam", 2),
      (store_script_param, ":bgdivision", 3),
      
      (assign, ":x", 0),
      (assign, ":y", 0),
      (init_position, ":bgposition"),
      (try_begin),
        (neg | is_between, ":bgdivision", 0, 9),
        (team_slot_ge, ":bgteam", slot_team_size, 1),
        (team_get_slot, ":x", ":bgteam", slot_team_avg_x),
        (team_get_slot, ":y", ":bgteam", slot_team_avg_y),
        (team_get_slot, ":zrot", ":bgteam", slot_team_avg_zrot),
      (else_try),
        (is_between, ":bgdivision", 0, 9),
        (store_add, ":slot", slot_team_d0_size, ":bgdivision"),
        (team_slot_ge, ":bgteam", ":slot", 1),
        
        (store_add, ":slot", slot_team_d0_avg_x, ":bgdivision"),
        (team_get_slot, ":x", ":bgteam", ":slot"),
        
        (store_add, ":slot", slot_team_d0_avg_y, ":bgdivision"),
        (team_get_slot, ":y", ":bgteam", ":slot"),
        
        (store_add, ":slot", slot_team_d0_avg_zrot, ":bgdivision"),
        (team_get_slot, ":zrot", ":bgteam", ":slot"),
      (try_end),
      (position_set_x, ":bgposition", ":x"),
      (position_set_y, ":bgposition", ":y"),
      (position_rotate_z, ":bgposition", ":zrot", 0),
      (position_set_z_to_ground_level, ":bgposition"),]),
  
  # script_battlegroup_get_attack_destination by motomataru
  # Input: destination position, team, division, target team, target division
  # Output: melee position against target battlegroup
  ("battlegroup_get_attack_destination", [(store_script_param, ":bgposition", 1),
      (store_script_param, ":bgteam", 2),
      (store_script_param, ":bgdivision", 3),
      (store_script_param, ":enemy_team", 4),
      (store_script_param, ":enemy_division", 5),
      
      (store_add, ":slot", slot_team_d0_formation, ":bgdivision"),
      (team_get_slot, ":bgformation", ":bgteam", ":slot"),
      (try_begin),
        (le, ":bgformation", formation_none),
        (call_script, "script_battlegroup_get_position", ":bgposition", ":bgteam", ":bgdivision"),
      (else_try),
        (call_script, "script_formation_current_position", ":bgposition", ":bgteam", ":bgdivision"),
      (try_end),
      
      #distance to enemy center
      (store_add, ":slot", slot_team_d0_formation, ":enemy_division"),
      (team_get_slot, ":enemy_formation", ":enemy_team", ":slot"),
      (call_script, "script_battlegroup_get_position", Enemy_Team_Pos, ":enemy_team", ":enemy_division"),
      (get_distance_between_positions, ":distance_to_move", ":bgposition", Enemy_Team_Pos),
      
      (call_script, "script_battlegroup_get_action_radius", ":bgteam", ":bgdivision"),
      (assign, ":bgwidth", reg0),
      (call_script, "script_battlegroup_get_action_radius", ":enemy_team", ":enemy_division"),
      (store_add, ":combined_width", ":bgwidth", reg0),
      
      (assign, ":min_radius", reg0),
      (val_min, ":min_radius", ":bgwidth"),
      (val_div, ":min_radius", 2),	#function returns length of bg
      
      (try_begin),
        (gt, ":bgformation", formation_none),	#in formation AND
        (le, ":distance_to_move", ":combined_width"),	#close to enemy
        (store_mul, reg0, -350, formation_reform_interval),	#back up one move (to avoid wild swings / reversals on overruns)
        (position_move_y, ":bgposition", reg0),
        (get_distance_between_positions, ":distance_to_move", ":bgposition", Enemy_Team_Pos),
      (try_end),
      
      #subtract enemy center to edge-of-contact (determined by minimum half-width
      #between the two battlegroups)
      (call_script, "script_get_distance_to_battlegroup", ":enemy_team", ":enemy_division", ":bgposition"),
      (store_mul, ":angle_adjusted_half_depth", ":min_radius", reg2),	#reg2 is cosine glancing angle, FP
      (convert_from_fixed_point, ":angle_adjusted_half_depth"),
      (try_begin),
        (neq, ":enemy_formation", formation_wedge),
        (call_script, "script_battlegroup_dist_center_to_front", ":enemy_team", ":enemy_division"),
        (val_max, ":angle_adjusted_half_depth", reg0),
      (try_end),
      (val_sub, ":distance_to_move", ":angle_adjusted_half_depth"),
      
      #modify by bg center to edge-of-contact, if needed
      (call_script, "script_battlegroup_dist_center_to_front", ":bgteam", ":bgdivision"),
      (assign, ":bg_half_depth", reg0),
      (try_begin),
        (le, ":bgformation", formation_none),
        (val_sub, ":distance_to_move", ":bg_half_depth"),	#position from script_battlegroup_get_position is in middle of bg
      (else_try),
        (eq, ":bgformation", formation_wedge),
        (call_script, "script_battlegroup_dist_center_to_front", ":enemy_team", ":enemy_division"),
        (val_add, ":distance_to_move", reg0),	#move in from nearest edge found by script_get_distance_to_battlegroup
        (val_add, ":distance_to_move", ":bg_half_depth"),	#drive wedge through target formation!
      (try_end),
      
      #modify by speed differential
      (try_begin),
        (gt, ":enemy_formation", formation_none),
        (neq, ":enemy_formation", formation_default),
        
        (store_add, ":slot", slot_team_d0_first_member, ":enemy_division"),
        (team_get_slot, reg0, ":enemy_team", ":slot"),
        (agent_is_active, reg0),
        
        (agent_get_speed, Speed_Pos, reg0),
        (init_position, Temp_Pos),
        (get_distance_between_positions, ":enemy_formation_speed", Speed_Pos, Temp_Pos),
        (val_mul, ":enemy_formation_speed", formation_reform_interval),	#calculate distance to next call
        
        (try_begin),
          (position_is_behind_position, ":bgposition", Enemy_Team_Pos),	#attacking from rear?
          (val_add, ":distance_to_move", ":enemy_formation_speed"),	#catch up to anticipated position
        (else_try),	#attacking enemy formation from front
          (store_add, ":slot", slot_team_d0_is_fighting, ":bgdivision"),
          (team_slot_eq, ":bgteam", ":slot", 0),
          (val_sub, ":distance_to_move", ":enemy_formation_speed"),	#avoid overrunning enemy
        (try_end),
      (try_end),
      
      (store_add, ":slot", slot_team_d0_front_weapon_length, ":bgdivision"),
      (team_get_slot, ":striking_distance", ":bgteam", ":slot"),
      (val_sub, ":distance_to_move", ":striking_distance"),
      
      (call_script, "script_point_y_toward_position", ":bgposition", Enemy_Team_Pos),
      (position_move_y, ":bgposition", ":distance_to_move"),]),
  
  # script_battlegroup_dist_center_to_front by motomataru
  # Input: team, division
  # Output: reg0 distance to front of battlegroup from center in cm
  ("battlegroup_dist_center_to_front", [(store_script_param, ":bgteam", 1),
      (store_script_param, ":bgdivision", 2),
      
      (store_add, ":slot", slot_team_d0_formation_space, ":bgdivision"),
      (team_get_slot, ":spacing", ":bgteam", ":slot"),
      (store_add, ":slot", slot_team_d0_formation, ":bgdivision"),
      (team_get_slot, ":bgformation", ":bgteam", ":slot"),
      
      (try_begin),
        (eq, ":bgformation", formation_none),	#single row
        (assign, ":depth", 0),
        
        #WFaS multi-ranks
      (else_try),
        (eq, ":bgformation", formation_2_row),
        (assign, ":depth", 100),
      (else_try),
        (eq, ":bgformation", formation_3_row),
        (assign, ":depth", 200),
      (else_try),
        (eq, ":bgformation", formation_4_row),
        (assign, ":depth", 300),
      (else_try),
        (eq, ":bgformation", formation_5_row),
        (assign, ":depth", 400),
        
      (else_try),	#WB multi-ranks
        (lt, ":spacing", 0),
        (store_mul, ":depth", ":spacing", -1),
        (val_mul, ":depth", 100),
        
        #Non Native
      (else_try),
        (store_add, ":slot", slot_team_d0_size, ":bgdivision"),
        (team_get_slot, ":size_enemy_battlegroup", ":bgteam", ":slot"),
        (store_mul, ":row_depth", ":spacing", 50),
        (val_add, ":row_depth", formation_minimum_spacing),
        
        (this_or_next | eq, ":bgformation", formation_ranks),
        (eq, ":bgformation", formation_shield),
        (call_script, "script_calculate_default_ranks", ":size_enemy_battlegroup"),
        (val_sub, reg1, 1),
        (store_mul, ":depth", ":row_depth", reg1),
        
      (else_try),
        (convert_to_fixed_point, ":size_enemy_battlegroup"),
        (store_sqrt, ":columns", ":size_enemy_battlegroup"),
        
        (eq, ":bgformation", formation_square),
        (convert_from_fixed_point, ":columns"),
        (val_add, ":columns", 1),	#see script_form_infantry
        (store_div, ":rows", ":size_enemy_battlegroup", ":columns"),
        (store_mul, ":depth", ":row_depth", ":rows"),
        (convert_from_fixed_point, ":depth"),
        (val_sub, ":depth", ":row_depth"),
        
      (else_try),
        (eq, ":bgformation", formation_wedge),
        (store_mul, ":depth", ":row_depth", ":columns"),	#approximation
        (convert_from_fixed_point, ":depth"),
      (try_end),
      
      (try_begin),
        (neq, ":bgformation", formation_wedge),
        (store_div, reg0, ":depth", 2),
      (else_try),
        (store_mul, reg0, ":depth", 2),	#another approximation (height - inner radius)
        (val_div, reg0, 3),
      (try_end),]),
  
  # script_battlegroup_get_action_radius by motomataru
  # Input: team, division
  # Output: reg0 radius of battlegroup's "zone of control" (now length of
  # battlegroup in cm)
  ("battlegroup_get_action_radius", [(store_script_param, ":bgteam", 1),
      (store_script_param, ":bgdivision", 2),
      
      (store_add, ":slot", slot_team_d0_size, ":bgdivision"),
      (team_get_slot, ":size_battlegroup", ":bgteam", ":slot"),
      (store_add, ":slot", slot_team_d0_formation, ":bgdivision"),
      (team_get_slot, ":formation", ":bgteam", ":slot"),
      (store_add, ":slot", slot_team_d0_type, ":bgdivision"),
      (team_get_slot, ":div_type", ":bgteam", ":slot"),
      (store_add, ":slot", slot_team_d0_formation_space, ":bgdivision"),
      (team_get_slot, ":spacing", ":bgteam", ":slot"),
      
      (try_begin),
        (this_or_next | eq, ":div_type", sdt_archer),
        (le, ":formation", formation_none),	#Native formation
        
        (store_mul, ":troop_space", ":spacing", 75),	#Native minimum spacing not consistent but about this
        (val_add, ":troop_space", 100),	#minimum spacing
        
        #WFaS multi-ranks
        (try_begin),
          (eq, ":formation", formation_2_row),
          (val_div, ":size_battlegroup", 2),
        (else_try),
          (eq, ":formation", formation_3_row),
          (val_div, ":size_battlegroup", 3),
        (else_try),
          (eq, ":formation", formation_4_row),
          (val_div, ":size_battlegroup", 4),
        (else_try),
          (eq, ":formation", formation_5_row),
          (val_div, ":size_battlegroup", 5),
          
        (else_try),	#WB multi-ranks
          (lt, ":spacing", 0),
          (assign, ":troop_space", 150),
          (val_mul, ":spacing", -1),
          (val_add, ":spacing", 1),
          (val_div, ":size_battlegroup", ":spacing"),
        (try_end),
        
        (store_mul, ":formation_width", ":size_battlegroup", ":troop_space"),
        (store_div, reg0, ":formation_width", 2),
        
      (else_try),
        (eq, ":formation", formation_wedge),
        (call_script, "script_get_centering_amount", formation_square, ":size_battlegroup", ":spacing"),	#approximation
        (val_mul, reg0, 7),
        (val_div, reg0, 6),
      (else_try),
        (try_begin),
          (lt, ":spacing", 0),
          (assign, reg0, ":bgteam"),
          (assign, reg1, ":bgdivision"),
          (assign, reg2, ":formation"),
          (display_message, "@{!}battlegroup_get_action_radius: negative radius for team {reg0} division {reg1} formation {reg2}"),
        (try_end),
        (call_script, "script_get_centering_amount", ":formation", ":size_battlegroup", ":spacing"),
      (try_end),
      
      (val_mul, reg0, 2),]),
  
  # script_team_get_position_of_enemies by motomataru
  # Input: destination position, team, troop class/division
  # Output: destination position: average position if reg0 > 0
  #			reg0: number of enemies
  # Run script_store_battlegroup_data before calling!
  ("team_get_position_of_enemies", [(store_script_param, ":enemy_position", 1),
      (store_script_param, ":team_no", 2),
      (store_script_param, ":troop_type", 3),
      (assign, ":pos_x", 0),
      (assign, ":pos_y", 0),
      (assign, ":total_size", 0),
      (try_begin),
        (neq, ":troop_type", grc_everyone),
        (assign, ":closest_distance", Far_Away),
        (call_script, "script_battlegroup_get_position", Temp_Pos, ":team_no", grc_everyone),
      (try_end),
      
      (try_for_range, ":other_team", 0, 4),
        (teams_are_enemies, ":other_team", ":team_no"),
        (try_begin),
          (eq, ":troop_type", grc_everyone),
          (team_get_slot, ":team_size", ":other_team", slot_team_size),
          (try_begin),
            (gt, ":team_size", 0),
            (call_script, "script_battlegroup_get_position", ":enemy_position", ":other_team", grc_everyone),
            (position_get_x, reg0, ":enemy_position"),
            (val_mul, reg0, ":team_size"),
            (val_add, ":pos_x", reg0),
            (position_get_y, reg0, ":enemy_position"),
            (val_mul, reg0, ":team_size"),
            (val_add, ":pos_y", reg0),
          (try_end),
        (else_try),	#for multiple divisions, should find the CLOSEST of a given type
          (assign, ":team_size", 0),
          (try_for_range, ":enemy_battle_group", 0, 9),
            (store_add, ":slot", slot_team_d0_size, ":enemy_battle_group"),
            (team_get_slot, ":troop_count", ":other_team", ":slot"),
            (gt, ":troop_count", 0),
            (store_add, ":slot", slot_team_d0_type, ":enemy_battle_group"),
            (team_get_slot, ":bg_type", ":other_team", ":slot"),
            (store_sub, ":bg_root_type", ":bg_type", 3), #subtype is three more than main type
            (this_or_next | eq, ":bg_type", ":troop_type"),
            (eq, ":bg_root_type", ":troop_type"),
            (val_add, ":team_size", ":troop_count"),
            (call_script, "script_battlegroup_get_position", ":enemy_position", ":other_team", ":enemy_battle_group"),
            (get_distance_between_positions, reg0, Temp_Pos, ":enemy_position"),
            (lt, reg0, ":closest_distance"),
            (assign, ":closest_distance", reg0),
            (position_get_x, ":pos_x", ":enemy_position"),
            (position_get_y, ":pos_y", ":enemy_position"),
          (try_end),
        (try_end),
        (val_add, ":total_size", ":team_size"),
      (try_end),
      
      (try_begin),
        (eq, ":total_size", 0),
        (init_position, ":enemy_position"),
      (else_try),
        (eq, ":troop_type", grc_everyone),
        (val_div, ":pos_x", ":total_size"),
        (position_set_x, ":enemy_position", ":pos_x"),
        (val_div, ":pos_y", ":total_size"),
        (position_set_y, ":enemy_position", ":pos_y"),
        (position_set_z_to_ground_level, ":enemy_position"),
      (else_try),
        (position_set_x, ":enemy_position", ":pos_x"),
        (position_set_y, ":enemy_position", ":pos_y"),
        (position_set_z_to_ground_level, ":enemy_position"),
      (try_end),
      
      (assign, reg0, ":total_size"),]),
  
  # script_get_distance_to_battlegroup by motomataru
  # Gets distance from "from position" to the theoretical nearest side of the
  # battlegroup, accounting for rotation of battlegroup
  # Input: bg team, bg division, from position
  # Output: reg2 abs (cos (BG direction - 90 - direction from "from position"))
  # fixed point
  #         reg1 BG radius x reg2 in cms
  #         reg0 distance in cms between BG position and "from position" minus
  #         reg1 (could be negative)
  # Uses pos0, pos61
  ("get_distance_to_battlegroup", [(store_script_param, ":bgteam", 1),
      (store_script_param, ":bgdivision", 2),
      (store_script_param, ":from_pos", 3),
      
      (store_add, ":slot", slot_team_d0_formation, ":bgdivision"),
      (team_get_slot, ":bgformation", ":bgteam", ":slot"),
      (call_script, "script_battlegroup_get_action_radius", ":bgteam", ":bgdivision"),
      (store_div, ":radius", reg0, 2),	#function returns length of bg
      (assign, ":min_cos_theta", 1),
      (convert_to_fixed_point, ":min_cos_theta"),
      (try_begin),
        (eq, ":bgformation", formation_wedge),
        (val_mul, ":min_cos_theta", 58),	#relation inscribed circle radius to half side: 1 / sqrt 3
        (val_div, ":min_cos_theta", 100),
      (else_try),
        (gt, ":radius", 0),
        (call_script, "script_battlegroup_dist_center_to_front", ":bgteam", ":bgdivision"),
        (val_mul, ":min_cos_theta", reg0),
        (val_div, ":min_cos_theta", ":radius"),
      (else_try),
        (assign, ":min_cos_theta", 0),
      (try_end),
      
      #acquire rotations
      (call_script, "script_battlegroup_get_position", pos0, ":bgteam", ":bgdivision"),
      (try_begin),
        (gt, ":bgformation", formation_none),
        (neq, ":bgformation", formation_default),
        (call_script, "script_get_formation_destination", pos61, ":bgteam", ":bgdivision"),
        (position_copy_rotation, pos0, pos61),
      (try_end),
      
      (copy_position, pos61, ":from_pos"),
      (call_script, "script_point_y_toward_position", pos61, pos0),
      (assign, ":distance_to_battlegroup", reg0),
      
      #calculate difference from center of bg
      (get_angle_between_positions, ":theta", pos61, pos0),
      (val_sub, ":theta", 9000),
      (store_cos, ":cos_theta", ":theta"),
      (val_abs, ":cos_theta"),
      (val_max, ":cos_theta", ":min_cos_theta"),	#doing depth considerations this way allows calling func to use angle; it also
      #avoids Pythagorean calcs
      
      (store_mul, reg1, ":radius", ":cos_theta"),
      (convert_from_fixed_point, reg1),
      (val_sub, ":distance_to_battlegroup", reg1),
      (assign, reg0, ":distance_to_battlegroup"),
      (assign, reg2, ":cos_theta"),]),
  
  ("init_noswing_weapons", make_noswing_weapons(items)),
  
  ##########################formations motomataru chief
  ##########################acaban#######################
  
  # script_get_item_modifier_effects moto chief
  # Input: itp_*, imod_*
  # Output: reg0 damage effect
  #         reg1 speed effect
  #         reg2 armor effect
  #         reg3 hit points effect
  #         reg4 difficulty effect
  #         reg5 price factor
  #         s0 descriptor string
  ("get_item_modifier_effects", [(store_script_param, ":type", 1),
      (store_script_param, ":imod", 2),
      
      (assign, ":damage", 0),
      (assign, ":speed", 0),
      (assign, ":armor", 0),
      (assign, ":hit_points", 0),
      (assign, ":difficulty", 0),
      (assign, ":price_factor", 100),
      
      (try_begin),
        (eq, ":type", itp_type_horse),
        (try_begin),
          (eq, ":imod", imod_lame),
          (assign, ":speed", -10),
          (assign, ":price_factor", 40),
          (str_store_string, s0, "@Lame"),
        (else_try),
          (eq, ":imod", imod_swaybacked),
          (assign, ":speed", -4),
          (assign, ":price_factor", 60),
          (str_store_string, s0, "@Swaybacked"),
        (else_try),
          (eq, ":imod", imod_timid),
          (assign, ":speed", 2),
          (assign, ":price_factor", 120),
          (str_store_string, s0, "@Timid"),
        (else_try),
          (eq, ":imod", imod_meek),
          (assign, ":speed", 2),
          (assign, ":price_factor", 120),
          (str_store_string, s0, "@Meek"),
        (else_try),
          (eq, ":imod", imod_stubborn),
          (assign, ":hit_points", 5),
          (assign, ":difficulty", 1),
          (assign, ":price_factor", 90),
          (str_store_string, s0, "@Stubborn"),
        (else_try),
          (eq, ":imod", imod_heavy),
          (assign, ":damage", 4),
          (assign, ":armor", 3),
          (assign, ":hit_points", 10),
          (assign, ":price_factor", 150),
          (str_store_string, s0, "@Heavy"),
        (else_try),
          (eq, ":imod", imod_spirited),
          (assign, ":damage", 1),
          (assign, ":speed", 2),
          (assign, ":price_factor", 160),
          (str_store_string, s0, "@Spirited"),
        (else_try),
          (eq, ":imod", imod_champion),
          (assign, ":damage", 2),
          (assign, ":speed", 4),
          (assign, ":difficulty", 2),
          (assign, ":price_factor", 170),
          (str_store_string, s0, "@Champion"),
        (try_end),
        
      (else_try),
        (eq, ":type", itp_type_shield),
        (try_begin),
          (eq, ":imod", imod_cracked),
          (assign, ":armor", -4),
          (assign, ":hit_points", -56),
          (assign, ":price_factor", 50),
          (str_store_string, s0, "@Cracked"),
        (else_try),
          (eq, ":imod", imod_battered),
          (assign, ":armor", -2),
          (assign, ":hit_points", -26),
          (assign, ":price_factor", 75),
          (str_store_string, s0, "@Battered"),
        (else_try),
          (eq, ":imod", imod_thick),
          (assign, ":armor", 2),
          (assign, ":hit_points", 47),
          (assign, ":price_factor", 120),
          (str_store_string, s0, "@Thick"),
        (else_try),
          (eq, ":imod", imod_reinforced),
          (assign, ":armor", 4),
          (assign, ":hit_points", 63),
          (assign, ":price_factor", 150),
          (str_store_string, s0, "@Reinforced"),
        (try_end),
        
      (else_try),
        (ge, ":type", itp_type_head_armor),
        (le, ":type", itp_type_hand_armor),
        (try_begin),
          (eq, ":imod", imod_cracked),
          (assign, ":armor", -4),
          (assign, ":price_factor", 50),
          (str_store_string, s0, "@Cracked"),
        (else_try),
          (eq, ":imod", imod_rusty),
          (assign, ":armor", -3),
          (assign, ":price_factor", 55),
          (str_store_string, s0, "@Rusty"),
        (else_try),
          (eq, ":imod", imod_tattered),
          (assign, ":armor", -3),
          (assign, ":price_factor", 40),
          (str_store_string, s0, "@Tattered"),
        (else_try),
          (eq, ":imod", imod_ragged),
          (assign, ":armor", -2),
          (assign, ":price_factor", 60),
          (str_store_string, s0, "@Ragged"),
        (else_try),
          (eq, ":imod", imod_battered),
          (assign, ":armor", -2),
          (assign, ":price_factor", 75),
          (str_store_string, s0, "@Battered"),
        (else_try),
          (eq, ":imod", imod_crude),
          (assign, ":armor", -1),
          (assign, ":price_factor", 83),
          (str_store_string, s0, "@Crude"),
        (else_try),
          (eq, ":imod", imod_sturdy),
          (assign, ":armor", 1),
          (assign, ":price_factor", 120),
          (str_store_string, s0, "@Sturdy"),
        (else_try),
          (eq, ":imod", imod_thick),
          (assign, ":armor", 2),
          (assign, ":price_factor", 140),
          (str_store_string, s0, "@Thick"),
        (else_try),
          (eq, ":imod", imod_hardened),
          (assign, ":armor", 3),
          (assign, ":price_factor", 160),
          (str_store_string, s0, "@Hardened"),
        (else_try),
          (eq, ":imod", imod_reinforced),
          (assign, ":armor", 4),
          (assign, ":price_factor", 180),
          (str_store_string, s0, "@Reinforced"),
        (else_try),
          (eq, ":imod", imod_lordly),
          (assign, ":armor", 5),
          (assign, ":price_factor", 400),
          (str_store_string, s0, "@Lordly"),
        (try_end),
        
      (else_try),
        (this_or_next | eq, ":type", itp_type_one_handed_wpn),
        (this_or_next | eq, ":type", itp_type_two_handed_wpn),
        (this_or_next | eq, ":type", itp_type_polearm),
        (this_or_next | eq, ":type", itp_type_bow),
        (this_or_next | eq, ":type", itp_type_crossbow),
        (this_or_next | eq, ":type", itp_type_pistol),
        (eq, ":type", itp_type_musket),
        
        (try_begin),
          (eq, ":imod", imod_rotten),		#idea is to use this for a completly broken weapon
          (assign, ":damage", -20),
          (assign, ":price_factor", 5),
          (str_store_string, s0, "@Broken"),
        (else_try),
          (eq, ":imod", imod_cracked),
          (assign, ":damage", -5),
          (assign, ":price_factor", 40),
          (str_store_string, s0, "@Cracked"),
        (else_try),
          (eq, ":imod", imod_rusty),
          (assign, ":damage", -3),
          (assign, ":price_factor", 55),
          (str_store_string, s0, "@Rusty"),
        (else_try),
          (eq, ":imod", imod_bent),
          (assign, ":damage", -3),
          (assign, ":speed", -3),
          (assign, ":price_factor", 60),
          (str_store_string, s0, "@Bent"),
        (else_try),
          (eq, ":imod", imod_chipped),
          (assign, ":damage", -1),
          (assign, ":price_factor", 72),
          (str_store_string, s0, "@Chipped"),
        (else_try),
          (eq, ":imod", imod_heavy),
          (assign, ":damage", 2),
          (assign, ":speed", -2),
          (assign, ":difficulty", 1),
          (assign, ":price_factor", 120),
          (str_store_string, s0, "@Heavy"),
        (else_try),
          (eq, ":imod", imod_strong),
          (assign, ":damage", 3),
          (assign, ":speed", -3),
          (assign, ":difficulty", 2),
          (assign, ":price_factor", 150),
          (str_store_string, s0, "@Strong"),
        (else_try),
          (eq, ":imod", imod_balanced),
          (assign, ":damage", 3),
          (assign, ":speed", 3),
          (assign, ":price_factor", 165),
          (str_store_string, s0, "@Balanced"),
        (else_try),
          (eq, ":imod", imod_tempered),
          (assign, ":damage", 4),
          (assign, ":price_factor", 180),
          (str_store_string, s0, "@Tempered"),
        (else_try),
          (eq, ":imod", imod_masterwork),
          (assign, ":damage", 5),
          (assign, ":speed", 1),
          (assign, ":difficulty", 4),
          (assign, ":price_factor", 400),
          (str_store_string, s0, "@Masterwork"),
        (else_try),
          (eq, ":imod", imod_crude),
          (assign, ":damage", -2),
          (assign, ":price_factor", 83),
        (try_end),
        
      (else_try),
        (this_or_next | eq, ":type", itp_type_arrows),
        (this_or_next | eq, ":type", itp_type_bolts),
        (this_or_next | eq, ":type", itp_type_bullets),
        (eq, ":type", itp_type_thrown),
        
        (try_begin),
          (eq, ":imod", imod_large_bag),
          #       (assign, ":damage", 1), #just make better than plain
          (assign, ":price_factor", 110),
          (str_store_string, s0, "@Large Bag of"),
        (else_try),
          (eq, ":imod", imod_bent),
          (assign, ":damage", -3),
          (assign, ":price_factor", 65),
          (str_store_string, s0, "@Bent"),
        (else_try),
          (eq, ":imod", imod_cracked),
          (assign, ":damage", -5),
          (assign, ":price_factor", 50),
          (str_store_string, s0, "@Cracked"),
        (else_try),
          (eq, ":imod", imod_heavy),
          (assign, ":damage", 2),
          (assign, ":price_factor", 130),
          (str_store_string, s0, "@Heavy"),
        (else_try),
          (eq, ":imod", imod_balanced),
          (assign, ":damage", 3),
          (assign, ":price_factor", 150),
          (str_store_string, s0, "@Balanced"),
        (try_end),
      (try_end),
      
      (assign, reg0, ":damage"),
      (assign, reg1, ":speed"),
      (assign, reg2, ":armor"),
      (assign, reg3, ":hit_points"),
      (assign, reg4, ":difficulty"),
      (assign, reg5, ":price_factor"),]),
  
  # script_cf_troop_may_use_item
  # Input: troop_ID, item_ID, item_mod
  # Output: s0 imod descriptor
  #         s1 failure string
  ("cf_troop_may_use_item", [(store_script_param, ":troop_id", 1),
      (store_script_param, ":item_id", 2),
      (store_script_param, ":item_mod", 3),
      
      (assign, ":check_val", 0),
      (item_get_difficulty, ":difficulty", ":item_id"),
      (str_store_troop_name, s2, ":troop_id"),
      
      (item_get_type, ":item_type", ":item_id"),
      (call_script, "script_get_item_modifier_effects", ":item_type", ":item_mod"),
      (val_add, ":difficulty", reg4),
      
      (try_begin),
        (ge, ":item_type", itp_type_head_armor),
        (le, ":item_type", itp_type_hand_armor),
        (store_attribute_level, ":check_val", ":troop_id", ca_strength),
        (str_store_string, s1, "@Strength."),
        
      (else_try),
        (eq, ":item_type", itp_type_shield), #skl_shield hide, depend on weapon master.  However, we save it hide for
        #troops in troops.py
        (store_skill_level, ":check_val", skl_shield, ":troop_id"),
        (str_store_string, s1, "@Shield skill."),
        
      (else_try),
        (eq, ":item_type", itp_type_thrown),
        (store_skill_level, ":check_val", skl_power_throw, ":troop_id"),
        (str_store_string, s1, "@Power Throw skill."),
        
      (else_try),
        (eq, ":item_type", itp_type_bow),
        (store_skill_level, ":check_val", skl_power_draw, ":troop_id"),
        (str_store_string, s1, "@Power Draw skill."),
        
      (else_try),
        (eq, ":item_type", itp_type_horse),
        (store_skill_level, ":check_val", skl_riding, ":troop_id"),
        (str_store_string, s1, "@Riding skill."),
        
      (else_try),
        (this_or_next | eq, ":item_type", itp_type_one_handed_wpn),
        (this_or_next | eq, ":item_type", itp_type_two_handed_wpn),
        (this_or_next | eq, ":item_type", itp_type_polearm),
        (this_or_next | eq, ":item_type", itp_type_crossbow),
        (this_or_next | eq, ":item_type", itp_type_pistol),
        (eq, ":item_type", itp_type_musket),
        (store_attribute_level, ":check_val", ":troop_id", ca_strength),
        (str_store_string, s1, "@Strength."),
      (try_end),
      
      (store_sub, reg1, ":difficulty", ":check_val"),
      (str_store_string, s1, "@{s2} needs {reg1} more {s1}"),
      (le, reg1, 0),]),
  
  # script_store_item_price
  # Input: item_ID, item_mod
  # Output: reg0
  ("store_item_price", [(store_script_param, ":item_id", 1),
      (store_script_param, ":item_mod", 2),
      (item_get_value, ":price", ":item_id"),
      (try_begin),
        (gt, ":item_id", "itm_no_item"),
        (call_script, "script_game_get_item_buy_price_factor", ":item_id"),
        (val_mul, ":price", reg0),
        
        (item_get_type, ":item_type", ":item_id"),
        (call_script, "script_get_item_modifier_effects", ":item_type", ":item_mod"),
        (val_mul, ":price", reg5),
        (val_div, ":price", 10000),
      (try_end),
      
      (assign, reg0, ":price"),]),
  
  # script_evaluate_item moto chief
  # Input: item_id, item_mod
  # Output: reg0 value meant to compare items of a given type
  ("evaluate_item", [(store_script_param, ":item_id", 1),
      (store_script_param, ":item_mod", 2),
      
      (assign, ":ret_val", 0),
      (try_begin),
        (gt, ":item_id", "itm_no_item"),
        
        (item_get_type, ":item_type", ":item_id"),
        (call_script, "script_get_item_modifier_effects", ":item_type", ":item_mod"),
        (assign, ":damage", reg0),
        (assign, ":speed", reg1),
        (assign, ":armor", reg2),
        (assign, ":hit_points", reg3),
        
        #Armor
        (try_begin),
          (ge, ":item_type", itp_type_head_armor),
          (le, ":item_type", itp_type_hand_armor),
          
          #construct comparison value
          (item_get_head_armor, ":value", ":item_id"),
          (val_add, ":armor", ":value"),
          (item_get_body_armor, ":value", ":item_id"),
          (val_add, ":armor", ":value"),
          (item_get_leg_armor, ":value", ":item_id"),
          (val_add, ":armor", ":value"),
          (assign, ":ret_val", ":armor"),
          
          #Ranged Weapons
        (else_try),
          (call_script, "script_cf_is_weapon_ranged", ":item_id", 1),
          
          #construct comparison value
          (item_get_thrust_damage, ":value", ":item_id"),
          (val_add, ":damage", ":value"),
          
          (item_get_speed_rating, ":value", ":item_id"),
          (val_add, ":value", ":speed"),
          (val_mul, ":damage", ":value"),
          
          (item_get_missile_speed,  ":value", ":item_id"),
          (val_mul, ":damage", ":value"),
          
          (item_get_accuracy, ":value", ":item_id"),
          (val_mul, ":damage", ":value"),
          (assign, ":ret_val", ":damage"),
          
          #Melee Weapons
        (else_try),
          (ge, ":item_type", itp_type_one_handed_wpn),
          (le, ":item_type", itp_type_polearm),
          
          #construct comparison value
          (item_get_thrust_damage, ":value", ":item_id"),
          (item_get_swing_damage, reg2, ":item_id"),
          (val_max, ":value", reg2),  #TW formula.  Also avoids problems with script_switch_to_noswing_weapons
          (val_add, ":damage", ":value"),
          
          (item_get_speed_rating, ":value", ":item_id"),
          (val_add, ":value", ":speed"),
          (val_mul, ":damage", ":value"),
          
          (item_get_weapon_length, ":value", ":item_id"),
          (convert_to_fixed_point, ":value"),
          (store_sqrt, reg2, ":value"),
          (convert_from_fixed_point, reg2),
          (val_mul, ":damage", reg2),
          (assign, ":ret_val", ":damage"),
          
          #Shields
        (else_try),
          (eq, ":item_type", itp_type_shield),
          
          #construct comparison value
          (item_get_body_armor, ":value", ":item_id"),
          (val_add, ":armor", ":value"),
          
          (item_get_hit_points, ":value", ":item_id"),
          (val_add, ":value", ":hit_points"),
          (val_div, ":value", 17),  #attempt to make it comparable to armors
          (val_add, ":armor", ":value"),
          
          #shields' protection modified by size, speed
          (item_get_weapon_length, ":value", ":item_id"),
          (val_mul, ":armor", ":value"),
          (val_div, ":armor", Outfit_Thorax_Length),
          
          (item_get_speed_rating, ":value", ":item_id"),
          (val_add, ":value", ":speed"),
          (val_mul, ":armor", ":value"),
          (val_div, ":armor", Outfit_Fast_Weapon_Speed),
          
          (val_mul, ":armor", 3), #fudge factor
          (assign, ":ret_val", ":armor"),
          
          #Horses
        (else_try),
          (eq, ":item_type", itp_type_horse),
          
          #construct comparison value
          (item_get_body_armor, ":value", ":item_id"),
          (val_add, ":armor", ":value"),
          (val_mul, ":armor", 4), #figure it takes 3-4 hits to kill a horse, so this is the hit value of each
          #point of armor
          
          (item_get_hit_points, ":value", ":item_id"),
          (val_add, ":value", ":hit_points"),
          (val_add, ":armor", ":value"),
          
          (item_get_horse_speed, ":value", ":item_id"),
          (val_add, ":value", ":speed"),
          (val_mul, ":armor", ":value"),
          (assign, ":ret_val", ":armor"),
          
          #Missiles
        (else_try),
          (this_or_next | eq, ":item_type", itp_type_arrows),
          (this_or_next | eq, ":item_type", itp_type_bolts),
          (eq, ":item_type", itp_type_bullets),
        (try_end),
      (try_end),
      
      (assign, reg0, ":ret_val"),]),
  
  #moto troop tree chief
  # script_troop_tree_precurse
  # Input: troop, number of upgrade, number of upgrade2
  # Output: reg0 number upgrade, reg1 upgrade2
  ("troop_tree_precurse", [(store_script_param, ":troop", 1),
      (store_script_param, ":ret_val_0", 2),
      (store_script_param, ":ret_val_1", 3),
      
      (assign, ":max_branch_0", ":ret_val_0"),
      (troop_get_upgrade_troop, ":next_troop", ":troop", 0),
      (try_begin),
        (gt, ":next_troop", 0),
        (store_add, ":max_branch_0", ":ret_val_0", 1),
        (call_script, "script_troop_tree_precurse", ":next_troop", ":max_branch_0", ":ret_val_1"),
        (val_max, ":max_branch_0", reg0),
        (val_max, ":ret_val_1", reg1),
      (try_end),
      
      (troop_get_upgrade_troop, ":next_troop", ":troop", 1),
      (try_begin),
        (gt, ":next_troop", 0),
        (val_add, ":ret_val_0", 1),
        (val_add, ":ret_val_1", 1),
        (call_script, "script_troop_tree_precurse", ":next_troop", ":ret_val_0", ":ret_val_1"),
        (val_max, ":ret_val_0", reg0),
        (val_max, ":ret_val_1", reg1),
      (try_end),
      
      (val_max, ":ret_val_0", ":max_branch_0"),
      (assign, reg0, ":ret_val_0"),
      (assign, reg1, ":ret_val_1"),]),
  
  # script_troop_tree_recurse
  # Input: troop, x_pos, y_pos
  # Output: reg0 augmented y_pos
  ("troop_tree_recurse", [(store_script_param, ":troop", 1),
      (store_script_param, ":x_pos", 2),
      (store_script_param, ":y_pos", 3),
      (store_script_param, ":next_y", 3),
      
      # Fix height of pic - VC-2379
      (val_min, "$troop_tree_pic_height", 272),
      
      (store_div, ":scaler", Troop_Tree_Area_Height, "$troop_tree_pic_height"),
      (store_div, ":scaled_width", Troop_Tree_Tableau_Width, ":scaler"),
      (store_div, ":scaled_height", Troop_Tree_Tableau_Height, ":scaler"),
      
      (store_mul, reg2, ":troop", 2), #picture with weapons (see script_add_troop_to_cur_tableau_for_party)
      (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_troop_tree_pic", reg2),
      
      # START Troop Detail: saves data to array - VC-2379
      (val_add, "$troop_tree_counter", 1),
      (troop_set_slot, "trp_temp_array_a", "$troop_tree_counter", reg1), # overlay id
      (troop_set_slot, "trp_temp_array_b", "$troop_tree_counter", ":troop"), # troop_id
      # END Troop Detail: saves data to array
      
      (store_div, reg3, ":scaled_width", 3),  #half too much for some reason
      (store_sub, reg2, ":x_pos", reg3),
      (position_set_x, pos1, reg2),
      (position_set_y, pos1, ":y_pos"),
      (overlay_set_position, reg1, pos1),
      
      (position_set_x, pos1, ":scaled_width"),
      (position_set_y, pos1, ":scaled_height"),
      (overlay_set_size, reg1, pos1),
      
      # (overlay_set_additional_render_height, reg1, 10), #float over lines MOTO
      # doesn't help
      
      (str_store_troop_name, s0, ":troop"),
      (create_text_overlay, reg1, "@{s0}", tf_center_justify),
      (position_set_x, pos1, ":x_pos"),
      (position_set_y, pos1, ":y_pos"),
      (overlay_set_position, reg1, pos1),
      
      (store_div, ":text_scaler", Troop_Tree_Area_Width, "$troop_tree_pic_width"),
      (val_max, ":text_scaler", ":scaler"),
      (store_div, reg2, 3500 * Screen_Undistort_Width_Num / Screen_Undistort_Width_Den, ":text_scaler"),
      (position_set_x, pos1, reg2),
      (store_div, reg2, 3500, ":text_scaler"),
      (position_set_y, pos1, reg2),
      (overlay_set_size, reg1, pos1),
      
      (store_div, reg3, ":scaled_height", 2),
      (store_sub, reg2, ":y_pos", reg3),
      (store_div, ":height_adjust", "$troop_tree_pic_height", 2),
      
      (store_add, ":next_x", ":x_pos", "$troop_tree_pic_width"),
      (troop_get_upgrade_troop, ":next_troop", ":troop", 0),
      (try_begin),
        (gt, ":next_troop", 0),
        (create_mesh_overlay, reg1, "mesh_white_plane"),
        (position_set_x, pos1, ":x_pos"),
        (store_add, reg2, ":y_pos", ":height_adjust"),
        (position_set_y, pos1, reg2),
        (overlay_set_position, reg1, pos1),
        
        (store_mul, reg2, "$troop_tree_pic_width", 50),
        (position_set_x, pos1, reg2),
        (position_set_y, pos1, 50 * 4),
        (overlay_set_size, reg1, pos1),
        
        (overlay_set_color, reg1, Troop_Tree_Line_Color),
        
        (call_script, "script_troop_tree_recurse", ":next_troop", ":next_x", ":next_y"),
        (assign, ":next_y", reg0),
      (try_end),
      
      (troop_get_upgrade_troop, ":next_troop", ":troop", 1),
      (try_begin),
        (gt, ":next_troop", 0),
        (val_sub, ":next_y", "$troop_tree_pic_height"),
        
        #half length horizontal, moved halfway
        (create_mesh_overlay, reg1, "mesh_white_plane"),
        (store_div, reg2, "$troop_tree_pic_width", 2),
        (val_add, reg2, ":x_pos"),
        (position_set_x, pos1, reg2),
        (store_add, reg2, ":next_y", ":height_adjust"),
        (position_set_y, pos1, reg2),
        (overlay_set_position, reg1, pos1),
        
        (store_mul, reg2, "$troop_tree_pic_width", 50),
        (val_div, reg2, 2),
        (position_set_x, pos1, reg2),
        (position_set_y, pos1, 50 * 4),
        (overlay_set_size, reg1, pos1),
        
        (overlay_set_color, reg1, Troop_Tree_Line_Color),
        
        #vertical to connect
        (create_mesh_overlay, reg1, "mesh_white_plane"),
        (store_div, reg2, "$troop_tree_pic_width", 2),
        (val_add, reg2, ":x_pos"),
        (position_set_x, pos1, reg2),
        (store_add, reg2, ":next_y", ":height_adjust"),
        (position_set_y, pos1, reg2),
        (overlay_set_position, reg1, pos1),
        
        (position_set_x, pos1, 50 * 3), #3/4 to undistort in wide screens
        (store_sub, reg2, ":y_pos", ":next_y"),
        (val_mul, reg2, 50),
        (position_set_y, pos1, reg2),
        (overlay_set_size, reg1, pos1),
        
        (overlay_set_color, reg1, Troop_Tree_Line_Color),
        
        (call_script, "script_troop_tree_recurse", ":next_troop", ":next_x", ":next_y"),
        (assign, ":next_y", reg0),
      (try_end),
      
      (assign, reg0, ":next_y"),]),
  
  #troop tree acaba
  # script_recurse_party_around_pos0 MOTO chief
  # Input: target party, radius, flag 0 for land
  # Output: pos1 new position
  ("recurse_party_around_pos0", [(store_script_param, ":party", 1),
      (store_script_param, ":radius", 2),
      (store_script_param, ":zero_for_land", 3),
      
      (try_begin),
        (eq,  ":zero_for_land", 0),
        (map_get_land_position_around_position, pos1, pos0, ":radius"),
      (else_try),
        (map_get_random_position_around_position, pos1, pos0, ":radius"),
      (try_end),
      
      (position_get_x, reg0, pos1),
      (convert_from_fixed_point, reg0),
      (try_begin),
        (neg | is_between, reg0, map_min_x, map_max_x),
        (call_script, "script_recurse_party_around_pos0", ":party", ":radius", ":zero_for_land"),
        
      (else_try),
        (position_get_y, reg0, pos1),
        (convert_from_fixed_point, reg0),
        (neg | is_between, reg0, map_min_y, map_max_y),
        (call_script, "script_recurse_party_around_pos0", ":party", ":radius", ":zero_for_land"),
        
      (else_try),
        (party_set_position, ":party", pos1),
        (party_get_current_terrain, ":terrain_check", ":party"),
        
        (eq,  ":zero_for_land", 0),
        (try_begin),
          (eq, ":terrain_check", rt_bridge),  #used as water in BW
          (call_script, "script_recurse_party_around_pos0", ":party", ":radius", ":zero_for_land"),
        (try_end),
        
      (else_try),
        # (neq, ":terrain_check", rt_water),
        # (neq, ":terrain_check", rt_river),
        (neq, ":terrain_check", rt_bridge),  #used as navigable water in BW
        (call_script, "script_recurse_party_around_pos0", ":party", ":radius", ":zero_for_land"),
      (try_end),]),
  
  # script_troop_default_division MOTO chief
  # Input: troop_id, flag 0 for expanded divisions
  # Output: reg0 default division
  ("troop_default_division", [(store_script_param, ":troop_no", 1),
      (store_script_param, ":flag_0_for_expanded", 2),
      
      (assign, ":target_division", grc_infantry),
      (troop_get_inventory_capacity, ":inv_cap", ":troop_no"),
      
      (try_begin),
        (troop_is_guarantee_horse, ":troop_no"),
        (assign, ":has_horse", 0),
        (try_for_range, reg0, 0, ":inv_cap"),
          (troop_get_inventory_slot, ":item", ":troop_no", reg0),
          (gt, ":item", 0),
          (item_get_type, reg1, ":item"),
          (eq, reg1, itp_type_horse),
          (assign, ":has_horse", 1),
        (try_end),
        (neq, ":has_horse", 0),
        (assign, ":target_division", grc_cavalry),
        
        (try_begin),
          (eq, ":flag_0_for_expanded", 0),
          (try_for_range, reg0, 0, ":inv_cap"),
            (troop_get_inventory_slot, ":item", ":troop_no", reg0),
            (call_script, "script_cf_is_weapon_ranged", ":item", 1),
            (assign, ":target_division", sdt_harcher),
          (try_end),
        (try_end),
        
      (else_try),
        (troop_is_guarantee_ranged, ":troop_no"),
        (assign, ":has_ranged", 0),
        (try_for_range, reg0, 0, ":inv_cap"),
          (troop_get_inventory_slot, ":item", ":troop_no", reg0),
          (call_script, "script_cf_is_weapon_ranged", ":item", 1),
          # (item_get_max_ammo, reg1, ":item"), returns 1 for slings, etc.
          # (neg|is_between, reg1, 1, minimum_ranged_ammo), #more than two to
          # throw on a charge?
          (item_get_type, reg1, ":item"),
          (try_begin),
            (this_or_next | eq, reg1, itp_type_thrown),
            (eq, reg1, itp_type_pistol),
            (eq, ":flag_0_for_expanded", 0),
            (neq, ":target_division", grc_archers),
            (assign, ":target_division", sdt_skirmisher),
          (else_try),
            (assign, ":target_division", grc_archers),
          (try_end),
          (assign, ":has_ranged", 1),
        (try_end),
        (neq, ":has_ranged", 0),
        
      (else_try),
        (eq, ":flag_0_for_expanded", 0),
        (try_for_range, reg0, 0, ":inv_cap"),
          (troop_get_inventory_slot, ":item", ":troop_no", reg0),
          (call_script, "script_cf_is_thrusting_weapon", ":item"),
          (item_get_type, reg1, ":item"),
          (eq, reg1, itp_type_polearm),
          (assign, ":target_division", sdt_polearm),
        (try_end),
      (try_end),
      
      (assign, reg0, ":target_division"),]),
  
  # script_get_distance_to_terrain_or_water MOTO chief
  # Input: position to test
  # Output: reg0
  ("get_distance_to_terrain_or_water", [(store_script_param, ":position", 1),
      (position_get_z, ":dist_to_water", ":position"),
      (assign, reg0, 1), #Water is commonly set to -100 cm in scenes
      (convert_to_fixed_point, reg0),
      (val_add, ":dist_to_water", reg0),
      (try_begin),
        (scene_prop_get_num_instances, reg0, "spr_dyn_ship_substrate"),
        (gt, reg0, 0),  #at sea
        (store_add, reg0, "$Amplitude_x", "$Amplitude_y"),
        (convert_to_fixed_point, reg0),
        (val_div, reg0, 5000),
        (val_sub, ":dist_to_water", reg0),
      (try_end),
      (position_get_distance_to_terrain, reg0, ":position"),
      (val_min, reg0, ":dist_to_water"),]),
  
  ####player lair chief
  ("cf_player_has_tools",
    [(troop_get_inventory_capacity,":capacity","trp_player"),
      (assign,":has_tools",0),
      (assign,":has_timber",0),
      (try_for_range,":slot",0,":capacity"),
        (troop_get_inventory_slot,":cur_item","trp_player",":slot"),
        (eq,":cur_item","itm_tools"),
        (val_add,":has_tools",1),
      (try_end),
      (try_for_range,":slot",0,":capacity"),
        (troop_get_inventory_slot,":cur_item","trp_player",":slot"),
        (eq,":cur_item","itm_timber"),
        (val_add,":has_timber",1),
      (try_end),
      (try_begin),
        (lt,":has_tools",5),
        (display_message,"@You need five tool kits to improve your refuge!"),
      (try_end),
      (try_begin),
        (lt,":has_timber",5),
        (display_message,"@You need five timber pallets to improve your refuge!"),
      (try_end),
      (ge,":has_tools",5),
      (ge,":has_timber",5),]),
  
  ("game_get_lair_garrison_limit",
    [(store_script_param_1, ":party"),
      (assign, ":limit", 50), #chief cambiado tamano base party
      
      (try_begin),
        (party_slot_eq, ":party", slot_lair_improve, 4), #Refuge
        (val_add, ":limit", 200),
      (else_try),
        (this_or_next | party_slot_eq, ":party", slot_lair_improve, 3),
        (party_slot_eq, ":party", slot_lair_improve, 2), #hideout
        (val_add, ":limit", 70),
      (try_end),
      
      (assign, reg0, ":limit"),
      (set_trigger_result, reg0),]),
  
  
  ("texto_description_staff",
    [(str_clear,s2),
      (str_clear,s3),
      (str_clear,s10),
      #captain
      (try_begin),
        (eq, "$show_face", 0), #face description
        (str_store_string,s3,"@To hire workers, you need to select them from the list. If the list is empty, you may need to increase your renown or improve your refuge for them to come and offer their services."),
      (else_try),
        (eq, "$show_face", 1), #face description
        (str_store_string,s2,"@Name: Marcus."),
        (try_begin),
          (eq, "$captain_hired_on", 1), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This captain is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Captain^Initial Cost: 500^Weekly cost: 55^Notes: helps manage your refuge and develop the improvements you need."),
        (try_end),
        # (str_store_string,s3,"@Occupation: Captain^Initial Cost: 500^Weekly
        # cost: 55^Notes: helps manage your refuge and develop the improvements
        # you need."),
      (else_try),
        (eq, "$show_face", 2), #face description
        (str_store_string,s2,"@Name: Father Beda."),
        (try_begin),
          (eq, "$priest_hired_on", 1), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This priest is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Priest^Initial Cost: 250^Weekly cost: 30^Notes: encourages the practice of Christianity among your troops."),
        (try_end),
        #(str_store_string,s3,"@Occupation: Priest^Initial Cost: 250^Weekly
        #cost: 30^Notes: encourages the practice of Christianity among your
        #troops."),
      (else_try),
        (eq, "$show_face", 3), #face description
        (str_store_string,s2,"@Name: Godi Olafr."),
        (try_begin),
          (eq, "$priest_hired_on", 2), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This priest is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Godi^Initial Cost: 250 ^Weekly cost: 30^Notes: encourages the practice of paganism among your troops."),
        (try_end),
        # (str_store_string,s3,"@Occupation: Godi^Initial Cost: 250 ^Weekly
        # cost: 30^Notes: encourages the practice of paganism among your
        # troops."),
      (else_try),
        (eq, "$show_face", 4), #face description
        (str_store_string,s2,"@Name: Skald Bragi."),
        (try_begin),
          (eq, "$bard_hired_on", 1), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This bard is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Skald^Initial Cost: 200^Weekly cost: 40^Notes: fills your halls with inspiring Nordic music."),
        (try_end),
        #       (str_store_string,s3,"@Occupation: Skald^Initial Cost: 200^Weekly
        #       cost: 40^Notes: fills your halls with inspiring Nordic music."),
      (else_try),
        (eq, "$show_face", 5), #face description
        (str_store_string,s2,"@Name: Bard Siwi."),
        (try_begin),
          (eq, "$bard_hired_on", 2), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This bard is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Bard^Initial Cost: 200 ^Weekly cost: 40^Notes: fills your halls with inspiring English music."),
        (try_end),
        #  (str_store_string,s3,"@Occupation: Bard^Initial Cost: 200 ^Weekly
        #  cost: 40^Notes: fills your halls with inspiring English music."),
      (else_try),
        (eq, "$show_face", 6), #face description
        (str_store_string,s2,"@Name: Casius."),
        (try_begin),
          (eq, "$tavernkeeper_hired_on", 1), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This man is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Bottler^Initial Cost: 100^Weekly cost: 10^Notes: necessary complement for your mead hall."),
        (try_end),
        #  (str_store_string,s3,"@Occupation: Bottler^Initial Cost: 100^Weekly
        #  cost: 10^Notes: necessary complement for your mead hall."),
      (else_try),
        (eq, "$show_face", 7), #face description
        (str_store_string,s2,"@Name: Suaibsech."),
        (try_begin),
          (eq, "$whore_hired_on", 1), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This woman is already serving you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Happy Widow^Initial Cost: 300^Weekly cost: 25^Notes: The best way to make your men happy."),
        (try_end),
        #  (str_store_string,s3,"@Occupation: Happy Widow^Initial Cost:
        #  300^Weekly cost: 25^Notes: The best way to make your men happy."),
      (else_try),
        (eq, "$show_face", 8), #face description
        (str_store_string,s2,"@Name: Tancoystl."),
        (try_begin),
          (eq, "$whore_hired_on", 2), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This woman is already serving you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Happy Widow^Initial Cost: 300^Weekly cost: 25^Notes: The best way to make your men happy."),
        (try_end),
        #  (str_store_string,s3,"@Occupation: Happy Widow^Initial Cost:
        #  300^Weekly cost: 25^Notes: The best way to make your men happy."),
      (else_try),
        (eq, "$show_face", 9), #face description
        (str_store_string,s2,"@Name: Gulla."),
        (try_begin),
          (eq, "$whore_hired_on", 3), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This woman is already serving you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Happy Widow^Initial Cost: 150^Weekly cost: 15^Notes: The best way to make your men happy."),
        (try_end),
        # (str_store_string,s3,"@Occupation: Happy Widow^Initial Cost:
        # 150^Weekly cost: 15^Notes: The best way to make your men happy."),
      (else_try),
        (eq, "$show_face", 10), #face description
        (str_store_string,s2,"@Name: Eadgyd."),
        (try_begin),
          (eq, "$whore_hired_on", 4), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This woman is already serving you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Happy Widow^Initial Cost: 150^Weekly cost: 15^Notes: The best way to make your men happy."),
        (try_end),
        #        (str_store_string,s3,"@Occupation: Happy Widow^Initial Cost:
        #        150^Weekly cost: 15^Notes: The best way to make your men happy."),
      (else_try),
        (eq, "$show_face", 11), #face description
        (str_store_string,s2,"@Name: Harald."),
        (try_begin),
          (eq, "$trainer_hired_on", 1), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This trainer is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Trainer^Initial Cost: 450^Weekly cost: 45^Notes: trains your troops."),
        (try_end),
        #  (str_store_string,s3,"@Occupation: Trainer^Initial Cost: 450^Weekly
        #  cost: 45^Notes: trains your troops."),
      (else_try),
        (eq, "$show_face", 12), #face description
        (str_store_string,s2,"@Name: Ulfberht."),
        (try_begin),
          (eq, "$smith_hired_on", 1), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This smith is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Smith^Initial Cost: 400^Weekly cost: 40^Notes: makes new weapons for your refuge."),
        (try_end),
        #     (str_store_string,s3,"@Occupation: Smith^Initial Cost: 400^Weekly
        #     cost: 40^Notes: makes new weapons for your refuge."),
      (else_try),
        (eq, "$show_face", 13), #face description
        (str_store_string,s2,"@Name: The Hungry Hammer."),
        (try_begin),
          (eq, "$armorer_hired_on", 1), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This armorer is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Armorer^Initial Cost: 400^Weekly cost: 40^Notes: His nickname comes from his voracious appetite for iron, manufacturing all kinds of things."),
        (try_end),
        #  (str_store_string,s3,"@Occupation: Armorer^Initial Cost: 400^Weekly
        #  cost: 40^Notes: His nickname comes from his voracious appetite for
        #  iron, manufacturing all kinds of things."),
      (else_try),
        (eq, "$show_face", 14), #face description
        (str_store_string,s2,"@Name: Aurelius."),
        (try_begin),
          (eq, "$barber_hired_on", 1), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This physician is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Physician^Initial Cost: 250^Weekly cost: 15^Notes: patches light wounds."),
        (try_end),
        #  (str_store_string,s3,"@Occupation: Physician^Initial Cost: 250^Weekly
        #  cost: 15^Notes: patches light wounds."),
      (else_try),
        (eq, "$show_face", 15), #face description
        (str_store_string,s2,"@Name: Morgan."),
        (try_begin),
          (eq, "$barber_hired_on", 2), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This physician is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Physician^Initial Cost: 250^Weekly cost: 15^Notes: patches light wounds."),
        (try_end),
        #   (str_store_string,s3,"@Occupation: Physician^Initial Cost: 250^Weekly
        #   cost: 15^Notes: patches light wounds."),
      (else_try),
        (eq, "$show_face", 16), #face description
        (str_store_string,s2,"@Name: Alexander."),
        (try_begin),
          (eq, "$barber_hired_on", 3), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This physician is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Physician^Initial Cost: 350^Weekly cost: 30^Notes: a highly trained barber who patches light and heavy wounds."),
        (try_end),
        #   (str_store_string,s3,"@Occupation: Physician^Initial Cost: 350^Weekly
        #   cost: 30^Notes: an highly trained barber who patches light and heavy
        #   wounds."),
      (else_try),
        (eq, "$show_face", 17), #face description
        (str_store_string,s2,"@Name: Aillil."),
        (try_begin),
          (eq, "$barber_hired_on", 4), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This physician is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Physician^Initial Cost: 250^Weekly cost: 30^Notes: patches light wounds."),
        (try_end),
        # (str_store_string,s3,"@Occupation: Physician^Initial Cost: 250^Weekly
        # cost: 30^Notes: patches light wounds."),
      (else_try),
        (eq, "$show_face", 18), #face description
        (str_store_string,s2,"@Name: Wamba."),
        (try_begin),
          (eq, "$cook_hired_on", 1), #contratado chief, hired
          (str_store_string,s3,"@Occupation: This cook is already working for you."),
        (else_try),
          (str_store_string,s3,"@Occupation: Cook^Initial Cost: 350^Weekly cost: 30^Notes: makes feasts for you and your men."),
        (try_end),
        #  (str_store_string,s3,"@Occupation: Cook^Initial Cost: 350^Weekly cost:
        #  30^Notes: makes feasts for you and your men."),
      (try_end),
      (str_store_string,s10,"@{s2}^^{s3}"),]),
  
  # script_player_set_skill_level
  # asigna una skill segun se asigne
  # Input: skill, skill_level
  # Output: none
  ("player_set_skill_level", #usable for troops too chief
    [(store_script_param_1, ":skill"),
      (store_script_param_2, ":new_skill"),
      (store_skill_level, ":player_sklevel", ":skill", "trp_player"),
      (val_sub, ":new_skill", ":player_sklevel"),
      (troop_raise_skill, "trp_player",":skill",":new_skill"),]),
  
  # script_diplomacy_start_alliance_between_kingdoms
  # diplomacy alliance chief
  # Input: arg1 = kingdom_1, arg2 = kingdom_2, arg3 =
  # Output: none
  ("diplomacy_start_alliance_between_kingdoms", #
    [(store_script_param, ":kingdom_a", 1),
      (store_script_param, ":kingdom_b", 2),
      (store_script_param, ":initializing_war_peace_cond", 3), #set to 1 if not the start of the game
      
      (store_relation, ":relation", ":kingdom_a", ":kingdom_b"),
      (val_max, ":relation", 30),
      (set_relation, ":kingdom_a", ":kingdom_b", ":relation"),
      (call_script, "script_exchange_prisoners_between_factions", ":kingdom_a", ":kingdom_b"),
      
      (try_begin),
        (eq, "$players_kingdom", ":kingdom_a"),
        (store_relation, ":player_relation", ":kingdom_b", "fac_player_faction"),  #keeps track of "real" player relation
        (val_add, ":player_relation", 10),  #VC-3960 will make at least cooperative if no bad blood
        (try_begin),
          (faction_slot_eq, "$players_kingdom", slot_faction_leader, "trp_player"),
          (val_max, ":player_relation", 0), #VC-3971 ensure peace ensues
        (try_end),
        (call_script, "script_set_player_relation_with_faction", ":kingdom_b", ":player_relation"),
        (call_script, "script_event_kingdom_make_peace_with_kingdom", ":kingdom_b", "fac_player_supporters_faction"), #event cancels certain quests
      (else_try),
        (eq, "$players_kingdom", ":kingdom_b"),
        (store_relation, ":player_relation", ":kingdom_a", "fac_player_faction"),  #keeps track of "real" player relation
        (val_add, ":player_relation", 10),  #VC-3960 will make at least cooperative if no bad blood
        (try_begin),
          (faction_slot_eq, "$players_kingdom", slot_faction_leader, "trp_player"),
          (val_max, ":player_relation", 0), #VC-3971 ensure peace ensues
        (try_end),
        (call_script, "script_set_player_relation_with_faction", ":kingdom_a", ":player_relation"),
        (call_script, "script_event_kingdom_make_peace_with_kingdom", ":kingdom_a", "fac_player_supporters_faction"), #event cancels certain quests
      (try_end),
      
      (try_for_range, ":cur_center", centers_begin, centers_end),
        (store_faction_of_party, ":faction_no", ":cur_center"),
        (this_or_next | eq, ":faction_no", ":kingdom_a"),
        (eq, ":faction_no", ":kingdom_b"),
        (party_get_slot, ":besieger_party", ":cur_center", slot_center_is_besieged_by),
        (ge, ":besieger_party", 0), #town is under siege
        (party_is_active, ":besieger_party"),
        (store_faction_of_party, ":besieger_party_faction_no", ":besieger_party"),
        (this_or_next | eq, ":besieger_party_faction_no", ":kingdom_a"),
        (eq, ":besieger_party_faction_no", ":kingdom_b"),
        (call_script, "script_lift_siege", ":cur_center", 0),
      (try_end),
      
      (try_begin),
        (this_or_next | eq, "$players_kingdom", ":kingdom_a"),
        (eq, "$players_kingdom", ":kingdom_b"),
        
        (ge, "$g_player_besiege_town", 0),
        (party_is_active, "$g_player_besiege_town"),
        
        (store_faction_of_party, ":besieged_center_faction_no", "$g_player_besiege_town"),
        
        (this_or_next | eq, ":besieged_center_faction_no", ":kingdom_a"),
        (eq, ":besieged_center_faction_no", ":kingdom_b"),
        
        (call_script, "script_lift_siege", "$g_player_besiege_town", 0),
        (assign, "$g_player_besiege_town", -1),
      (try_end),
      
      (try_begin),
        (eq, ":initializing_war_peace_cond", 1),
        (str_store_faction_name_link, s1, ":kingdom_a"),
        (str_store_faction_name_link, s2, ":kingdom_b"),
        (display_log_message, "@The {s1} and the {s2} have entered into an alliance with each other."),
        (call_script, "script_add_notification_menu", "mnu_notification_alliance_declared", ":kingdom_a", ":kingdom_b"), #stability penalty for early peace is in the menu
        (call_script, "script_event_kingdom_make_peace_with_kingdom", ":kingdom_a", ":kingdom_b"), #cancels quests
        (call_script, "script_event_kingdom_make_peace_with_kingdom", ":kingdom_b", ":kingdom_a"), #cancels quests
        (assign, "$g_recalculate_ais", 1),
      (try_end),
      
      (try_begin), #add truce
        (store_add, ":truce_slot", ":kingdom_a", slot_faction_truce_days_with_factions_begin),
        (val_sub, ":truce_slot", kingdoms_begin),
        (faction_set_slot, ":kingdom_b", ":truce_slot", alliance_time),
        (store_add, ":truce_slot", ":kingdom_b", slot_faction_truce_days_with_factions_begin),
        (val_sub, ":truce_slot", kingdoms_begin),
        (faction_set_slot, ":kingdom_a", ":truce_slot", alliance_time),
        (store_add, ":slot_war_damage_inflicted_on_b", ":kingdom_b", slot_faction_war_damage_inflicted_on_factions_begin),
        (val_sub, ":slot_war_damage_inflicted_on_b", kingdoms_begin),
        #(faction_get_slot, ":damage_inflicted_by_a", ":kingdom_a",
        #":slot_war_damage_inflicted_on_b"),
        (faction_set_slot, ":kingdom_a", ":slot_war_damage_inflicted_on_b", 0),
        (store_add, ":slot_war_damage_inflicted_on_a", ":kingdom_a", slot_faction_war_damage_inflicted_on_factions_begin),
        (val_sub, ":slot_war_damage_inflicted_on_a", kingdoms_begin),
        #(faction_get_slot, ":damage_inflicted_by_b", ":kingdom_b",
        #":slot_war_damage_inflicted_on_a"),
        (faction_set_slot, ":kingdom_b", ":slot_war_damage_inflicted_on_a", 0),
      (try_end),
      
      
      # same enemies in 0 and -1 ratio.  This save alliances and truces
      (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        (neq, ":kingdom_a", ":faction_no"),
        (neq, ":kingdom_b", ":faction_no"),
        (call_script, "script_diplomacy_faction_get_diplomatic_status_with_faction",":kingdom_a", ":faction_no"),
        #result: -1 faction_1 has a casus belli against faction_2.  1, faction_1
        #has a truce with faction_2, -2, the two factions are at war
        (eq, reg0, -2),
        (call_script, "script_diplomacy_faction_get_diplomatic_status_with_faction",":kingdom_b", ":faction_no"),
        (ge, reg0, -1),
        (lt, reg0, 1), #but respect others
        # MOTO build explanation string chief
        (assign, "$g_last_acting_faction", ":kingdom_b"),
        (assign, "$g_last_target_faction", ":faction_no"),
        (str_store_faction_name, s15, ":kingdom_b"),
        (str_store_faction_name, s16, ":faction_no"),
        (str_store_string, s64, "@The {s15} complies with the new alliance by attacking the {s16}."),
        # MOTO build explanation string end
        (call_script, "script_diplomacy_start_war_between_kingdoms", ":kingdom_b", ":faction_no", logent_faction_declares_war_to_fulfil_alliance),  	#MOTO chief pass log entries
      (try_end),
      (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        (neq, ":kingdom_a", ":faction_no"),
        (neq, ":kingdom_b", ":faction_no"),
        (call_script, "script_diplomacy_faction_get_diplomatic_status_with_faction",":kingdom_b", ":faction_no"),
        #result: -1 faction_1 has a casus belli against faction_2.  1, faction_1
        #has a truce with faction_2, -2, the two factions are at war
        (eq, reg0, -2),
        (call_script, "script_diplomacy_faction_get_diplomatic_status_with_faction",":kingdom_a", ":faction_no"),
        (ge, reg0, -1),
        (lt, reg0, 1), #but respect others
        # MOTO build explanation string chief
        (assign, "$g_last_acting_faction", ":kingdom_a"),
        (assign, "$g_last_target_faction", ":faction_no"),
        (str_store_faction_name, s15, ":kingdom_a"),
        (str_store_faction_name, s16, ":faction_no"),
        (str_store_string, s64, "@The {s15} complies with the new alliance by attacking the {s16}."),
        # MOTO build explanation string end
        (call_script, "script_diplomacy_start_war_between_kingdoms", ":kingdom_a", ":faction_no", logent_faction_declares_war_to_fulfil_alliance),  	#MOTO chief pass log entries
      (try_end),]),
  
  #script_companions_camp_conversation
  # companions conversations chief
  ("companions_camp_conversation",
    [(try_for_range,":current_npc",companions_begin,companions_end),
        (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 0),
      (try_end),
      
      (try_for_range,":current_npc",companions_begin,companions_end),
        (main_party_has_troop, ":current_npc"),
        (troop_get_slot, ":relation", ":current_npc", slot_troop_player_relation),
        (neq,"$temp",1),
        (try_begin),
          (eq, ":current_npc", "trp_npc1"), #caio
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #world wonderful
          (assign,"$temp",1), #done
        (else_try),
          (eq, ":current_npc", "trp_npc1"), #caio
          (main_party_has_troop,"trp_npc5"), #Morgant
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 1), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #world wonderful
          (assign,"$temp",1),
        (else_try),
          (eq, ":current_npc", "trp_npc2"), #egil
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #liderazgo
          (assign,"$temp",1), #done
        (else_try),
          (eq, ":current_npc", "trp_npc3"), #Brunhild
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #problem with his parent and kennemer
          (assign,"$temp",1), #done
        (else_try),#####-----------
          (eq, ":current_npc", "trp_npc3"), #Brunhild
          (main_party_has_troop,"trp_npc2"), #egil
          # (lt, ":relation_to_king", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 1), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #discusion de pareja
          (assign,"$temp",1),
        (else_try),
          (eq, ":current_npc", "trp_npc4"), #Donnachdh
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #charla sobre Irlanda y la guerra endemica de los clanes
          (assign,"$temp",1), #done
        (else_try),
          (eq, ":current_npc", "trp_npc4"), #Donnachdh
          (main_party_has_troop,"trp_npc7"), #Dwywei
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 1), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #charla sobre Irlanda y la guerra endemica de los clanes
          (assign,"$temp",1),
        (else_try),
          (eq, ":current_npc", "trp_npc5"), #Morgant
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #le habla de su infancia en Cornubia
          (assign,"$temp",1), #done
        (else_try),
          (eq, ":current_npc", "trp_npc6"), #Bodo
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #enamoramiento
          (assign,"$temp",1), #done
        (else_try),
          (eq, ":current_npc", "trp_npc6"), #Bodo
          (ge, ":relation", 30), #good relation
          (troop_get_type, ":is_female", "trp_player"),
          (val_mod, ":is_female", 2),
          (eq, ":is_female", 1),
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 1), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #amor imposible
          (assign,"$temp",1), #done
          ##      (else_try),
          ##        (eq, ":current_npc", "trp_npc6"), #Bodo # finally no romance
          ##        (ge, ":relation", 60), #good relation
          ##        (lt,"$romance_target",0), #romance variable
          ##            (troop_get_type, ":is_female", "trp_player"),
          ##            (val_mod, ":is_female", 2),
          ##            (eq, ":is_female", 1),
          ##        (troop_slot_eq, ":current_npc", camp_companion_romance, 0), #si no es
          ##        0 no continua
          ##	(troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1),
          ##	#Inicia romance si player is female
          ##         (assign,"$temp",1),
        (else_try),
          (eq, ":current_npc", "trp_npc7"), #Dwywei
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #Habla sobre Pictland y su pasado.
          (assign,"$temp",1), #done
        (else_try), #####-----------
          (eq, ":current_npc", "trp_npc7"), #Dwywei
          (ge, ":relation", 40), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 1), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), ##le habla sobre si mismo, ocn claro interes en el player male.
          (assign,"$temp",1), #done
        (else_try),
          (eq, ":current_npc", "trp_npc7"), #Dwywei
          (ge, ":relation", 60), #good relation
          (lt,"$romance_target",0), #romance variable
          (troop_get_type, ":is_female", "trp_player"),
          (val_mod, ":is_female", 2),
          (neq, ":is_female", 1),
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 2), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #Inicia Romance si player es male.
          (assign,"$temp",1), # done
        (else_try),
          (eq, ":current_npc", "trp_npc8"), #Reginhard
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #le habla sobre su tiempo en Dorestad y su romance con Adda, la vendedora.
          (assign,"$temp",1),
          ##      (else_try),
          ##        (eq, ":current_npc", "trp_npc8"), #Reginhard
          ##        (ge, ":relation", 40), #good relation
          ##        (troop_slot_eq, ":current_npc", camp_2companions_over, 0), #si no es
          ##        0 no continua
          ##	(troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1),
          ##	# congraciarse con Adda y su hijo.
          ##         (assign,"$temp",1),
        (else_try),
          (eq, ":current_npc", "trp_npc9"), #Clovis
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #Habla sobre Frankia y los vikingos
          (assign,"$temp",1),
        (else_try), #####-----------
          (eq, ":current_npc", "trp_npc9"), #Clovis
          (main_party_has_troop,"trp_npc13"), #Helgi
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 1), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #se insultan y casi llegan a las manos.  Discuten quien son mejores guerreros,
          #los francos de clovis o los svia, suecos, de Helgi.  El player tendra q
          #intervenir
          (assign,"$temp",1),
        (else_try),
          (eq, ":current_npc", "trp_npc10"), #Ceawlin
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #Habla sobre Frankia y los vikingos
          (assign,"$temp",1),
        (else_try),
          (eq, ":current_npc", "trp_npc11"), #Solveig
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #hablan sobre las skaljmo, Solveig muy segura de si misma siempre, flaquea al
          #reconocer que no hay muchas y cada vez menos.
          (assign,"$temp",1), #done
        (else_try),
          (eq, ":current_npc", "trp_npc11"), #Solveig
          (ge, ":relation", 40), #good relation
          (troop_get_type, ":is_female", "trp_player"),
          (val_mod, ":is_female", 2),
          (neq, ":is_female", 1), #done
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 1), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #hablan sobre las skaljmo, Solveig muy segura de si misma siempre, flaquea al
          #reconocer que no hay muchas y cada vez menos.
          (assign,"$temp",1),
          ##      (else_try),
          ##        (eq, ":current_npc", "trp_npc11"), #Solveig #Finally no romance
          ##        (ge, ":relation", 50), #good relation
          ##        (lt,"$romance_target",0), #romance variable
          ##            (troop_get_type, ":is_female", "trp_player"),
          ##            (val_mod, ":is_female", 2),
          ##            (neq, ":is_female", 1),
          ##        (troop_slot_eq, ":current_npc", camp_companion_romance, 0), #si no es
          ##        0 no continua
          ##	(troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1),
          ##	#Inicia romance si player is male
          ##         (assign,"$temp",1),
        (else_try),
          (eq, ":current_npc", "trp_npc12"), #Asbjorn
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #hablan de mitologia nordica
          (assign,"$temp",1), #done
          ##      (else_try),
          ##        (eq, ":current_npc", "trp_npc12"), #Asbjorn
          ##        (ge, ":relation", 40), #good relation
          ##        (troop_slot_eq, ":current_npc", camp_2companions_over, 0), #si no es
          ##        0 no continua
          ##	(troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1),
          ##	#hablan sobre los daneses y sus costumbres
          ##         (assign,"$temp",1),
        (else_try),
          (eq, ":current_npc", "trp_npc13"), #Helgi
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #Le habla sobre los suecos en el este, los rus y los fieneses.
          (assign,"$temp",1), #done
        (else_try),
          (eq, ":current_npc", "trp_npc13"), #Helgi
          (ge, ":relation", 40), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 1), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #Le habla sobre si mismo con interes
          (assign,"$temp",1), #done
        (else_try),
          (eq, ":current_npc", "trp_npc13"), #Helgi
          (ge, ":relation", 60), #good relation
          (lt,"$romance_target",0), #romance variable
          (troop_get_type, ":is_female", "trp_player"),
          (val_mod, ":is_female", 2), #done
          (eq, ":is_female", 1),
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 2), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #Inicia romance si player es female
          (assign,"$temp",1),
        (else_try),
          (eq, ":current_npc", "trp_npc14"), #ailchu
          (main_party_has_troop,"trp_npc3"), #Brunhild
          (ge, ":relation", 10), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #tiene un amigo imaginario, habla con su amigo y el player puede intervenir
          #poco.  Quizas aprovechar para un tutorial sobre tacticas de asedio
          (assign,"$temp",1), #done
        (else_try),
          (eq, ":current_npc", "trp_npc15"), #Agathinos
          (ge, ":relation", 10), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #tiene un amigo imaginario, habla con su amigo y el player puede intervenir
          #poco.  Quizas aprovechar para un tutorial sobre tacticas de asedio
          (assign,"$temp",1), #done
        (else_try),
          (eq, ":current_npc", "trp_npc16"), #Beda
          (ge, ":relation", 20), #good relation
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 0), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #habla sobre los tiempos oscuros, de ragnar sus hijos y como el destino de
          #wessex es combatirlos y liberar englaland de su presencia.
          (assign,"$temp",1),
        (else_try), #done
          (eq, ":current_npc", "trp_npc16"), #beda
          (main_party_has_troop,"trp_npc15"), #aghatinos
          (main_party_has_troop,"trp_npc3"), #Brunhild
          (troop_slot_eq, ":current_npc", slot_troop_companion_camp_conversed, 1), #si no es 0 no continua
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 1), #charla metafisica
          (assign,"$temp",1),
        (else_try),
          (troop_set_slot, ":current_npc", slot_troop_companion_camp_conversation, 0),
        (try_end),
      (try_end),]),
  
  # script_faction_to_cb7
  # Input: fac_kingdom_*
  # Output: cb7_* to reg0
  ("faction_to_cb7",[(store_script_param, ":faction", 1),
      
      (faction_get_slot, ":culture", ":faction", slot_faction_culture),
      (try_begin),
        (eq, ":culture", "fac_culture_norse"),
        (assign, reg0, cb7_norseman),
      (else_try),
        (eq, ":culture", "fac_culture_welsh"),
        (assign, reg0, cb7_briton),
      (else_try),
        (eq, ":culture", "fac_culture_irish"),
        (assign, reg0, cb7_irish),
      (else_try),
        (eq, ":culture", "fac_culture_scotch"),
        (assign, reg0, cb7_scotopict),
      (else_try),
        (assign, reg0, cb7_anglesaxon),
      (try_end),]),
  
  # script_game_character_screen_requested
  # called by the engine
  ("game_character_screen_requested",[(try_begin),
        (eq,"$on_map",1),
        #(map_free),
        (set_trigger_result,1),
        (start_presentation,"prsnt_character_screen_main"),
      (else_try),
        (set_trigger_result,0),
      (try_end),]),
  
  # # script_create_button_with_outline
  # # Component: Helper
  # ("create_button_with_outline",[
  # (create_text_overlay, reg1, s7, tf_with_outline|tf_center_justify),
  # (create_button_overlay, reg2, s7, tf_with_outline|tf_center_justify),
  # (position_set_x, pos1, 500),
  # (position_set_y, pos1, "$temp1"),
  # (val_sub, "$temp1", "$temp2"),
  # (overlay_set_position, reg1, pos1),
  # (position_set_x, pos1, 485),
  # (overlay_set_position, reg2, pos1),
  # (overlay_set_size, reg1, pos2),
  # (overlay_set_size, reg2, pos2),
  # (overlay_set_alpha, reg2, 0x03),
  # (overlay_set_hilight_alpha, reg2, 0x03),
  # ]),
  
  # script_prsnt_vc_menu_helper
  # Component: Helper
  ("prsnt_vc_menu_helper",[(overlay_set_color, reg1, 0x000000), #Black se ve bien
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos2, 900),
      (position_set_y, pos2, 900),
      (overlay_set_size, reg1, pos2),
      (copy_position, pos2, pos1),
      (position_get_x, ":x", pos2),
      (val_add, ":x", 285),
      (position_set_x, pos2, ":x"),
      (position_get_y, ":y", pos2),
      (val_add, ":y", 3),
      (position_set_y, pos2, ":y"),
      (create_check_box_overlay, reg2, "mesh_checkbox_off", "mesh_checkbox_on"),
      (overlay_set_position, reg2, pos2),
      (position_set_x, pos2, 900),
      (position_set_y, pos2, 900),
      (overlay_set_size, reg2, pos2),
      #gap
      (position_get_y, ":y", pos1),
      (val_sub, ":y", 30),
      (position_set_y, pos1, ":y"),]),
  
  # script_prsnt_vc_menu_helper_2
  # Component: Helper
  ("prsnt_vc_menu_helper_2",[(overlay_set_color, reg1, 0x000000), #Black se ve bien
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos2, 900),
      (position_set_y, pos2, 900),
      (overlay_set_size, reg1, pos2),
      (copy_position, pos2, pos1),
      (position_get_x, ":x", pos2),
      (val_add, ":x", 285),
      (position_set_x, pos2, ":x"),
      (create_combo_button_overlay, reg2),
      (overlay_set_position, reg2, pos2),
      (position_set_x, pos2, 700),
      (position_set_y, pos2, 700),
      (overlay_set_size, reg2, pos2),
      #gap
      (position_get_y, ":y", pos1),
      (val_sub, ":y", 40),
      (position_set_y, pos1, ":y"),]),
  
  ("prsnt_vc_menu_helper_3",[(overlay_set_color, reg1, 0x000000), #Black se ve bien
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos2, 900),
      (position_set_y, pos2, 900),
      (overlay_set_size, reg1, pos2),
      (copy_position, pos2, pos1),
      (position_get_x, ":x", pos2),
      (val_add, ":x", 285),
      (position_set_x, pos2, ":x"),
      (create_slider_overlay, reg2, 0, 1000),
      (overlay_set_position, reg2, pos2),
      (position_set_x, pos2, 700),
      (position_set_y, pos2, 700),
      (overlay_set_size, reg2, pos2),
      #gap
      (position_get_y, ":y", pos1),
      (val_sub, ":y", 40),
      (position_set_y, pos1, ":y"),]),
  
  ("prsnt_vc_menu_helper_gap",[#sub gap
      (position_get_y, ":y", pos1),
      (val_add, ":y", 15),
      (position_set_y, pos1, ":y"),
      #line
      (create_mesh_overlay, reg1, "mesh_white_plane"),
      (overlay_set_color, reg1, 0x000000),
      (overlay_set_alpha, reg1, 0xA0),
      (position_set_x, pos3, 24000),
      (position_set_y, pos3, 50),
      (overlay_set_size, reg1, pos3),
      (overlay_set_position, reg1, pos1),
      #gap
      (position_get_y, ":y", pos1),
      (val_sub, ":y", 45),
      (position_set_y, pos1, ":y"),]),
  
  # script_save_cam_first_person_mode
  # Component: Helper
  ("save_cam_first_person_mode",[#VC-2097
      (try_begin),
        (assign, "$cam_first_person_mode", 0),
        (is_camera_in_first_person),
        (assign, "$cam_first_person_mode", 1),
      (end_try),]),
  
  # script_return_to_cam_first_person_mode_1_sec
  # Component: Helper
  ("return_to_cam_first_person_mode_1_sec",[#VC-2097
      (try_begin),
        (eq, "$cam_first_person_mode", 0),
        (mission_cam_set_mode, 0, 1000, 0),
      (else_try),
        (mission_cam_set_mode, 0, 0, 0),
        (set_camera_in_first_person, "$cam_first_person_mode"),
      (end_try),]),
  
  # script_get_next_lower_imod_for_item
  # Component: Helper
  ("get_next_lower_imod_for_item",[(store_script_param_1, ":item_id"),
      (store_script_param_2, ":current_imod"),
      
      (item_get_type, ":item_type", ":item_id"),
      (call_script, "script_get_item_modifier_effects", ":item_type", ":current_imod"),
      (assign, ":start_item_value", reg5),
      (assign, ":compare_item_value", 0),
      (assign, ":target_imod", ":current_imod"),
      
      (try_for_range, ":test_imod", imod_plain, imod_large_bag + 1),
        (this_or_next | eq, ":test_imod", imod_plain),
        (item_has_modifier, ":item_id", ":test_imod"), #Fails if <item_modifier_no> is not a valid modifier for <item_kind_no>
        (neq, ":test_imod", ":current_imod"),
        (call_script, "script_get_item_modifier_effects", ":item_type", ":test_imod"),
        (is_between, reg5, ":compare_item_value", ":start_item_value"),
        (assign, ":compare_item_value", reg5),
        (assign, ":target_imod", ":test_imod"),
      (end_try),
      (assign, reg0, ":target_imod"),]),
  
  # script_get_next_higher_imod_for_item
  # Component: Helper
  ("get_next_higher_imod_for_item",[(store_script_param_1, ":item_id"),
      (store_script_param_2, ":current_imod"),
      
      (item_get_type, ":item_type", ":item_id"),
      (call_script, "script_get_item_modifier_effects", ":item_type", ":current_imod"),
      (assign, ":start_item_value", reg5),
      (assign, ":compare_item_value", 1000),
      (assign, ":target_imod", ":current_imod"),
      
      (try_for_range, ":test_imod", imod_plain, imod_large_bag + 1),
        (this_or_next | eq, ":test_imod", imod_plain),
        (item_has_modifier, ":item_id", ":test_imod"), #Fails if <item_modifier_no> is not a valid modifier for <item_kind_no>
        (neq, ":test_imod", imod_swaybacked),	# for some reason item_has_modifier and arrows dont fail for imod_swaybacked
        (neq, ":test_imod", ":current_imod"),
        (call_script, "script_get_item_modifier_effects", ":item_type", ":test_imod"),
        (is_between, reg5, ":start_item_value", ":compare_item_value"),
        (assign, ":compare_item_value", reg5),
        (assign, ":target_imod", ":test_imod"),
      (end_try),
      (assign, reg0, ":target_imod"),]),
  
  # script_make_color
  # Component: Helper
  ("make_color",[(store_mul, ":red", 0x10000, "$current_red"),
      (store_mul, ":green", 0x100, "$current_green"),
      (store_mul, ":blue", 0x1, "$current_blue"),
      (assign, "$current_color", 0x000000),
      (val_add, "$current_color", ":red"),
      (val_add, "$current_color", ":green"),
      (val_add, "$current_color", ":blue"),]),
  
  # script_remove_lair
  # Input: lair party ID
  ("remove_lair",[(store_script_param, ":lair", 1),
      
      (try_for_range, ":bandit_spawn_point", laired_spawn_points_begin, laired_spawn_points_end),
        (party_slot_eq, ":bandit_spawn_point", slot_party_lair_party, ":lair"),
        (party_set_slot, ":bandit_spawn_point", slot_party_lair_party, 0),
        
        (try_for_parties, ":spawned_party"),
          (gt, ":spawned_party", "p_spawn_points_end"),
          (party_slot_eq, ":spawned_party", slot_party_spawn_point, ":bandit_spawn_point"),
          (party_set_ai_behavior, ":spawned_party", ai_bhvr_patrol_party),
          (party_set_ai_object, ":spawned_party", ":bandit_spawn_point"),
        (try_end),
      (try_end),
      
      (remove_party, ":lair"),]),
  
  
  
  ###############
  # GORE scripts
  
  # "script_cf_goredec"
  # Description: CAN FAIL.  Tests if should try a decapitation
  # Input: inflicted_agent_id, hit_bone, missile_item_kind_no, damage
  # Output: none (CAN FAIL)
  ("cf_goredec",
    [(store_script_param_1, ":inflicted_agent_id"),
      (store_script_param_2, ":hit_bone"),
      (store_script_param, ":missile_item_kind_no", 3),
      (store_script_param, ":damage", 4),
      (store_script_param, ":weapon_id", 5),
      
      (assign, ":continue", 0),
      # Can't be: player, hero or horse nor female
      (agent_is_non_player, ":inflicted_agent_id"),
      (agent_get_troop_id, ":troop_inflicted", ":inflicted_agent_id"),
      (neg | troop_is_hero,":troop_inflicted"),
      (neq, ":troop_inflicted", "trp_sea_raider_leader"),#non berserker
      (neq, ":troop_inflicted", "trp_looter_leader"),#non berserker
      (neq, ":troop_inflicted", "trp_norse_level2_companion"),#non huskarl
      (neq, ":troop_inflicted", "trp_norse_standard_bearer"),#norse
      (store_current_scene,":current_scene"),
      (neq,":current_scene","scn_mainquest_fleet"), #no in this scene: sven bull neck traition.
      (agent_is_human, ":inflicted_agent_id"),
      (troop_get_type, ":is_female", ":troop_inflicted"),
      (val_mod, ":is_female", 2),
      (neq, ":is_female", 1),
      
      # Debugging
      (try_begin),
        (eq, debug_goredec, 2),
        (display_message, "@{!}DEBUG: cf_goredec ignoring weapon, hp and bone"),
        (assign, ":continue", 1),
        
      (else_try),
        # test weapon: cutting damage from a weapon (no missiles)
        (gt, ":weapon_id", 0),
        (eq, ":missile_item_kind_no", -1),
        (item_get_swing_damage_type, ":damage_type", ":weapon_id"),
        (eq, ":damage_type", cut),
        
        # test if agent is dying from the hit
        (store_agent_hit_points, ":inflicted_hp", ":inflicted_agent_id", 1),
        (store_sub, ":inflicted_new_hp", ":inflicted_hp", ":damage"),
        (le, ":inflicted_new_hp", 0),
        
        # Hitboxes near neck
        (this_or_next | eq, ":hit_bone", hb_head),
        (this_or_next | eq, ":hit_bone", hb_thorax),
        (this_or_next | eq, ":hit_bone", hb_shoulder_l),
        (this_or_next | eq, ":hit_bone", hb_upperarm_l),
        (this_or_next | eq, ":hit_bone", hb_upperarm_r),
        (eq, ":hit_bone", hb_shoulder_r),
        
        (assign, ":continue", 1),
      (try_end),
      
      (eq, ":continue", 1),
      
      (try_begin),
        (eq, debug_goredec, 1),
        (assign, reg1, ":hit_bone"),
        (display_message, "@{!}DEBUG: cf_goredec OK on bone: {reg1}"),
      (try_end),]),
  
  # "script_goredec_probability"
  # Description: checks conditions for the decapitation chance (%)
  # Input: inflicted_agent_id, attacker_agent_id, weapon_id,
  # Output: none (CAN FAIL)
  ("cf_goredec_probability",
    [(store_script_param_1, ":inflicted_agent_id"),
      (store_script_param_2, ":attacker_agent_id"),
      (store_script_param, ":weapon_id", 3),
      
      (agent_is_human, ":inflicted_agent_id"),
      (agent_is_human, ":attacker_agent_id"),
      (gt, ":weapon_id", 0),
      ### Probability ###
      #BASE: 5
      #IF PLAYER
      #	BASE: +5
      #	IF MOUNTED +30
      #	IF STR>15 : +10
      #	IF PS>7 : +10
      #IF BOT
      #	IF MOUNTED +10
      #IF AXE +5
      #IF HAS HELMET -5
      #MIN CHANCE: 5
      (assign, ":base_chance", 5),
      
      (try_begin),
        
        # Mounted bot
        (agent_get_horse, ":horse_id", ":attacker_agent_id"),
        (try_begin),
          (agent_is_non_player, ":attacker_agent_id"),
          
          (try_begin),
            (neq, ":horse_id", -1),
            (val_add, ":base_chance", 10),
          (try_end),
          
          #Player bonus
        (else_try),
          (val_add, ":base_chance", "$goredec_player_bonus"),
          (try_begin),
            (neq, ":horse_id", -1),
            (val_add, ":base_chance", 30),
          (try_end),
        (try_end),
        
        # Axe
        (try_begin),
          (this_or_next | eq, ":weapon_id", "itm_club_troll"),
          (this_or_next | is_between, ":weapon_id", "itm_hatchet", "itm_staff"),
          (is_between, ":weapon_id", "itm_work_axe", "itm_work_pitchfork"),
          #				(this_or_next|item_has_capability, ":weapon_id", itcf_carry_axe_back),
          #				(item_has_capability, ":weapon_id", itcf_carry_axe_left_hip),
          (val_add, ":base_chance", 5),
        (try_end),
        
        # Helmet
        (try_begin),
          (agent_get_item_slot, ":head_gear", ":inflicted_agent_id", ek_head),
          (ge, ":head_gear", 1),
          (item_get_head_armor, ":armor", ":head_gear"),
          (gt, ":armor", 20),
          (val_sub, ":base_chance", 5),
        (try_end),
        
        (val_max, ":base_chance", 5),
      (try_end),
      
      (store_random_in_range, ":rand", 0, 101),
      
      (val_div, ":base_chance", 2),#VC-3296
      # Debugging
      (try_begin),
        (eq, debug_goredec, 1),
        (assign, reg1, ":base_chance"),
        (assign, reg2, ":rand"),
        (display_message, "@{!}DEBUG: Decapitation: chance {reg1}%, rand {reg2}%"),
      (else_try),
        (eq, debug_goredec, 2),
        (assign, ":base_chance", 100),
      (try_end),
      
      (ge, ":base_chance", ":rand"),]),
  
  
  # "script_goredec_player_bonus"
  # Description: player bonus based on skills and if he is mounted
  # Input: none
  # Output: $goredec_player_bonus
  ("goredec_player_bonus",
    [(get_player_agent_no, ":player_agent"),
      
      (assign, "$goredec_player_bonus", 5),
      
      (agent_get_troop_id, ":player_troop", ":player_agent"),
      (store_attribute_level, ":player_str", ":player_troop", ca_strength),
      (store_skill_level, ":player_power_strike", "skl_power_strike", ":player_troop"),
      
      (try_begin),
        (ge, ":player_str", 15),
        (val_add, "$goredec_player_bonus", 10),
      (try_end),
      (try_begin),
        (ge, ":player_power_strike", 7),
        (val_add, "$goredec_player_bonus", 10),
      (try_end),
      
      # Debugging
      (try_begin),
        (ge, debug_goredec, 1),
        (assign, reg1, "$goredec_player_bonus"),
        (display_message, "@Player bonus for decap: {reg1}"),
      (try_end),]),
  
  
  # "script_goredec_special_effects"
  # Description: for decapitation -> blood, helmet, spawn head
  # Input: inflicted_agent_id, head_position
  # Output: none
  ("goredec_special_effects",
    [(store_script_param_1, ":inflicted_agent_id"),
      (agent_get_bone_position, pos1, ":inflicted_agent_id", hb_head, 1),
      
      # Checks if agent was using a helmet
      (try_begin),
        (agent_get_item_slot, ":head_gear", ":inflicted_agent_id", ek_head),
        (ge, ":head_gear", 1),
        (assign, ":spawn_for_timer", 60),
        
        # helmet on the ground
        (copy_position, pos2, pos1),
        (position_move_x, pos2, 20, 0),
        (position_move_z, pos2, -30, 0),
        (store_random_in_range, ":rot_x", 10, 40),
        (store_random_in_range, ":rot_z", 15, 75),
        (position_rotate_x, pos2, ":rot_x", 1),
        (position_rotate_z, pos2, ":rot_z", 1),
        (position_set_z_to_ground_level, pos2),
        (position_move_y, pos2, -5, 1),
        (set_spawn_position, pos2),
        (spawn_item, ":head_gear", 0, ":spawn_for_timer"),
        
        (agent_unequip_item, ":inflicted_agent_id", ":head_gear"),
      (try_end),
      
      # equip invisible head on agent
      (agent_equip_item, ":inflicted_agent_id", "itm_decap_head"),
      
      # blood
      (copy_position, pos2, pos1),
      (set_spawn_position, pos2),
      (particle_system_burst, "psys_decap_blood", pos2, 5),
      
      # fake head
      (spawn_scene_prop, "spr_head"),
      (assign, ":head_id", reg0),
      
      (prop_instance_enable_physics, ":head_id", 1),
      
      # makes sure the agent dies
      (agent_set_hit_points,":inflicted_agent_id", 0, 1),
      
      # force kill for the report
      (agent_set_slot, ":inflicted_agent_id", slot_agent_vc_wounded, -1),]),
  
  
  # # "script_goredec_debug_log"
  # # Description
  # # Input: none
  # # Output: none
  # ("goredec_debug_log",
    # [(store_script_param_1, ":inflicted_agent_id"),
      # (store_script_param_2, ":attacker_agent_id"),
      
      # # Log for testing
      # (try_begin),
        # (ge, debug_goredec, 1),
        # (str_store_agent_name, s10, ":inflicted_agent_id"),
        # (str_store_agent_name, s11, ":attacker_agent_id"),
        # (val_add, "$log_number_decapitations", 1),
        # (assign, reg50, "$log_number_decapitations"),
        # (display_message, "@{!}DEBUG: Decap: {s10} by {s11}. Total: {reg50}", 0xFF0000),
      # (try_end),]),
  
  # # "script_goredec_debug_speed_agents"
  # # Description
  # # Input: none
  # # Output: none
  # ("goredec_debug_speed_agents",
    # [(store_script_param_1, ":combat_speed"),
      # (store_script_param_2, ":affect_player"),
      
      # (assign, reg1, ":combat_speed"),
      # (display_message, "@INFO:   Changing movement speed to {reg1}%", 0x00FF33),
      
      # (try_for_agents, ":agent_id"),
        # (agent_is_alive, ":agent_id"),
        # (agent_is_active, ":agent_id"),
        
        # (try_begin),
          # (eq, ":affect_player", 1),
          # (agent_set_speed_modifier, ":agent_id", ":combat_speed"),
        # (else_try),
          # (agent_is_non_player, ":agent_id"),
          # (agent_set_speed_modifier, ":agent_id", ":combat_speed"),
        # (try_end),
      # (try_end),]),
  
  ###############
  # "script_view_goal_metric"
  ("view_goal_metric",
    [(store_script_param, reg2, 1),
      (store_script_param, ":y", 2),
      (store_script_param, ":slot", 3),
      (store_script_param, ":y_dec", 4),
      (store_script_param, ":type", 5),
      
      (quest_get_slot, reg0, "qst_vc_menu", ":type"),
      (store_mul, reg1, ":y_dec", ":slot"),
      (val_sub, ":y", reg1),
      (assign, ":font_normal", 1200),
      
      (create_text_overlay, reg1, s1, tf_left_align),
      (position_set_x, pos1, 50),(position_set_y, pos1, ":y"),
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos1, ":font_normal"),(position_set_y, pos1, ":font_normal"),
      (overlay_set_size, reg1, pos1),
      
      (assign, ":x_value", 240),
      (create_text_overlay, reg1, "str_reg0", tf_right_align),
      
      # Exceptions
      (assign, ":min_progressbar", -1),
      (try_begin),
        (eq, ":type", slot_set_goal_marry),
        (try_begin),
          (eq, reg0, 0),# dont want to marry
          (assign, reg2, 0),# dont care if is married
          (overlay_set_text, reg1, "str_no"),
        (else_try),
          (overlay_set_text, reg1, "str_yes"),
        (try_end),
      (else_try),
        (eq, ":type", slot_set_goal_reputation),
        #bar goes from -100 to goal value.  reg0 is goal, reg2 is current
        (try_begin),
          (call_script, "script_cf_goal_reputation_completed"),
          (assign, reg0, 100),
          (assign, reg2, 100),
        (else_try),
          (assign, ":min_progressbar", -101),
          (try_begin),
            (lt, reg0, 0),
            (val_mul, reg0, -1),
            (val_mul, reg2, -1),
          (try_end),
        (try_end),
      (try_end),
      
      (position_set_x, pos1, ":x_value"),(position_set_y, pos1, ":y"),
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos1, ":font_normal"),(position_set_y, pos1, ":font_normal"),
      (overlay_set_size, reg1, pos1),
      
      (val_sub, ":y", 60),
      (create_progress_overlay, reg1, ":min_progressbar", reg0),
      (overlay_set_val, reg1, reg2),
      (position_set_x, pos1, 175),(position_set_y, pos1, ":y"),
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos1, 750),(position_set_y, pos1, 1600),
      (overlay_set_size, reg1, pos1),
      
      (troop_set_slot, "trp_temp_array_a", ":slot", reg1),
      (troop_set_slot, "trp_temp_array_b", ":slot", reg2),
      (troop_set_slot, "trp_temp_array_c", ":slot", reg3),
      
      (val_sub, ":y", 10),
      (create_mesh_overlay, reg1, "mesh_white_plane"),
      (position_set_x, pos1, 15000),(position_set_y, pos1, 115),(overlay_set_size, reg1, pos1),
      (position_set_x, pos1, 0),(position_set_y, pos1, ":y"),(overlay_set_position, reg1, pos1),
      (overlay_set_color, reg1, 0x000000),]),
  
  # "script_set_goal_check_success_event"
  # SET GOAL success event
  # Output: reg0 (0..100% completed metrics)
  ("set_goal_check_success_event",
    [(assign, reg0, 0),
      
      (try_begin),
        (quest_get_slot, ":quest_type", "qst_vc_menu", slot_set_goal_type),
        (assign, "$goal_type", ":quest_type"),
        
        (ge, ":quest_type", goal_custom),
        
        (call_script, "script_set_goal_check_metrics_progress"),
        (assign, ":completed_metrics", reg0),
        (store_current_day, ":date"),
        (store_mod, ":mod_date", ":date", 7),
        
        (try_begin),
          (le, ":completed_metrics", 50),
          (try_begin),
            (eq, ":mod_date", 0),
            (display_message, "str_set_goal_half_way_there"),
          (try_end),
          
        (else_try),
          (le, ":completed_metrics", 90),
          (try_begin),
            (eq, ":mod_date", 0),
            (display_message, "str_set_goal_almost_there", 0x0000FF),
          (try_end),
          
        (else_try),
          
          #metrics OK
          #setting history for goal
          (quest_get_slot, ":quest_type", "qst_vc_menu", slot_set_goal_type),
          (store_add, ":slot_set_goal_type", ":quest_type", slot_set_goal_completed_custom - 1),
          
          (quest_set_slot, "qst_vc_menu", slot_set_goal_type, goal_empty),
          (quest_set_slot, "qst_vc_menu", ":slot_set_goal_type", goal_complete),
          
          (call_script, "script_set_goal_rewards"),
          (assign, "$goal_type", goal_empty),
          
          (try_begin),
            (eq, debug_set_goal, 1),
            (assign, reg0, ":quest_type"),
            (display_message, "@{!}DEBUG: set_goal_check_success_event completing goal {reg0} ", 0xff0000),
            
            (try_for_range, ":i", slot_set_goal_completed_custom, slot_set_goal_completed_expand + 1),
              (quest_get_slot, reg0, "qst_vc_menu", ":i"),
              (assign, reg1, ":i"),
              (display_message, "@{!}DEBUG: goal slot {reg1} is now {reg0} ", 0xff0000),
            (try_end),
          (try_end),
          # Success menu
          (jump_to_menu, "mnu_set_goal_message_complete_goal"),
        (try_end),
      (try_end),]),
  
  # "script_set_goal_rewards"
  # Rewards for each goal
  ("set_goal_rewards",
    [(str_clear, s1),
      
      (try_begin),
        (eq, "$goal_type", goal_custom),
        (str_store_string, s1, "str_set_goal_custom_reward"),
        
      (else_try),
        (eq, "$goal_type", goal_raider),
        (str_store_string, s1, "str_set_goal_raider_reward"),
        (party_force_add_members, "p_main_party", "trp_mountain_bandit", 5),
        
      (else_try),
        (eq, "$goal_type", goal_warrior),
        (str_store_string, s1, "str_set_goal_warrior_reward"),
        (troop_raise_proficiency_linear, "trp_player", wpt_one_handed_weapon, 30),
        
      (else_try),
        (eq, "$goal_type", goal_lord),
        (call_script, "script_set_goal_lord_reward"),
        
      (else_try),
        (eq, "$goal_type", goal_king),
        (call_script, "script_set_goal_king_reward"),
        
      (else_try),
        (eq, "$goal_type", goal_expand),
        (call_script, "script_set_goal_expand_reward"),
        
      (else_try),
        (eq, debug_set_goal, 1),
        (display_message, "@{!}DEBUG: error set_goal_rewards", 0xff0000),
      (try_end),
      
      (display_message, "@{s1}", color_good_news),]),
  
  # "script_set_goal_lord_reward"
  ("set_goal_lord_reward",
    [(try_begin),
        (store_free_inventory_capacity, ":free_slots"),
        (ge, ":free_slots", 1),
        (troop_add_item, "trp_player", "itm_cow1"),
      (try_end),
      
      (try_begin),
        (eq, "$g_player_faith", 1),
        (party_force_add_members, "p_main_party", "trp_briton_priest", 1),
        (assign, reg0, 1),
      (else_try),
        (party_force_add_members, "p_main_party", "trp_norse_priest", 1),
        (assign, reg0, 0),
      (try_end),
      (str_store_string, s1, "str_set_goal_lord_reward"),
      
      (try_for_range, ":lord", lords_begin, lords_end),
        (store_faction_of_troop, ":fac_lord", ":lord"),
        (eq, ":fac_lord", "$players_kingdom"),
        (call_script, "script_troop_get_relation_with_troop", "trp_player", ":lord"),
        (ge, reg0, 0),
        (call_script, "script_change_player_relation_with_troop", ":lord", 10),
        
        (try_begin),
          (eq, debug_set_goal, 1),
          (str_store_troop_name, s10, ":lord"),
          (display_message, "@{!}DEBUG: raising +10 relation with {s10}", 0xff0000),
        (try_end),
      (try_end),]),
  
  # "script_set_goal_king_reward"
  ("set_goal_king_reward",
    [(try_begin),
        (store_free_inventory_capacity, ":free_slots"),
        (ge, ":free_slots", 1),
        (troop_add_item, "trp_player", "itm_book_wound_treatment_reference"),
      (try_end),
      
      (party_force_add_members, "p_main_party", "trp_frisian_cav", 5),
      
      (str_store_string, s1, "str_set_goal_king_reward"),]),
  
  # "script_set_goal_expand_reward"
  ("set_goal_expand_reward",
    [(try_begin),
        (store_free_inventory_capacity, ":free_slots"),
        (ge, ":free_slots", 1),
        (troop_add_item, "trp_player", "itm_noble_sword_5", imod_tempered),
      (try_end),
      
      (str_store_string, s1, "str_set_goal_expand_reward"),
      
      (try_for_range, ":fac", npc_kingdoms_begin, npc_kingdoms_end),
        (faction_slot_eq, ":fac", slot_faction_state, sfs_active),
        (store_relation, ":relation", ":fac", "fac_player_faction"),
        
        (ge, ":relation", 25),
        (call_script, "script_change_player_relation_with_faction", ":fac", 10),
        
        (try_begin),
          (gt, debug_set_goal, 1),
          (str_store_faction_name, s10, ":fac"),
          (display_message, "@{!}DEBUG: raising +10 relation with {s10}", 0xff0000),
        (try_end),
      (try_end),]),
  
  # "script_set_goal_check_metrics_progress"
  # Checks if goals were reached or not
  # Output: reg0 (% of completed goals, 0 ..  100)
  ("set_goal_check_metrics_progress",
    [(assign, ":count", 0),
      (try_begin),
        (call_script, "script_cf_goal_special_case_lord_king"),
        (val_add, ":count", 1),
      (try_end),
      (try_begin),
        (call_script, "script_cf_goal_armysize_completed"),
        (val_add, ":count", 1),
      (try_end),
      (try_begin),
        (call_script, "script_cf_goal_wealth_completed"),
        (val_add, ":count", 1),
      (try_end),
      (try_begin),
        (call_script, "script_cf_goal_rtr_completed"),
        (val_add, ":count", 1),
      (try_end),
      (try_begin),
        (call_script, "script_cf_goal_renown_completed"),
        (val_add, ":count", 1),
      (try_end),
      (try_begin),
        (call_script, "script_cf_goal_reputation_completed"),
        (val_add, ":count", 1),
      (try_end),
      (try_begin),
        (call_script, "script_cf_goal_married_completed"),
        (val_add, ":count", 1),
      (try_end),
      
      (try_begin),
        (le, ":count", 3),
        (assign, ":count", 25),
      (else_try),
        (le, ":count", 5),
        (assign, ":count", 50),
      (else_try),
        (le, ":count", 6),
        (assign, ":count", 90),
      (else_try),
        (assign, ":count", 100),
      (try_end),
      
      (assign, reg0, ":count"),
      (try_begin),
        (gt, debug_set_goal, 1),
        (display_message, "@{!}DEBUG: metric progress at {reg0} %", 0xff0000),
      (try_end),]),
  
  # "script_cf_goal_armysize_completed"
  # Output: True if complete, False if not
  ("cf_goal_armysize_completed",
    [(assign, ":ok_armysize", 0),
      
      (try_begin),
        (quest_get_slot, ":quest_army", "qst_vc_menu", slot_set_goal_army),
        (store_party_size_wo_prisoners, ":current_army"),
        (ge, ":current_army", ":quest_army"),
        (assign, ":ok_armysize", 1),
      (try_end),
      
      (try_begin),
        (gt, debug_set_goal, 1),
        (assign, reg10, ":quest_army"),
        (assign, reg11, ":current_army"),
        (assign, reg12, ":ok_armysize"),
        (display_message, "@{!}DEBUG: armysizeCompleted {reg10} <= {reg11}, test {reg12}", 0xff0000),
      (try_end),
      
      (eq, ":ok_armysize", 1),]),
  
  # "script_cf_goal_wealth_completed"
  # Output: True if complete, False if not
  ("cf_goal_wealth_completed",
    [(assign, ":ok_wealth", 0),
      
      (try_begin),
        (quest_get_slot, ":quest_wealth", "qst_vc_menu", slot_set_goal_wealth),
        (store_troop_gold, ":current_wealth", "trp_player"),
        (ge, ":current_wealth", ":quest_wealth"),
        (assign, ":ok_wealth", 1),
      (try_end),
      
      (try_begin),
        (gt, debug_set_goal, 1),
        (assign, reg10, ":quest_wealth"),
        (assign, reg11, ":current_wealth"),
        (assign, reg12, ":ok_wealth"),
        (display_message, "@{!}DEBUG: wealthCompleted {reg10} <= {reg11}, test {reg12}", 0xff0000),
      (try_end),
      
      (eq, ":ok_wealth", 1),]),
  
  # "script_cf_goal_rtr_completed"
  # Output: True if complete, False if not
  ("cf_goal_rtr_completed",
    [(assign, ":ok_rtr", 0),
      
      (try_begin),
        (quest_get_slot, ":quest_rtr", "qst_vc_menu", slot_set_goal_rtr),
        (assign, ":current_rtr", "$player_right_to_rule"),
        (ge, ":current_rtr", ":quest_rtr"),
        (assign, ":ok_rtr", 1),
      (try_end),
      
      (try_begin),
        (gt, debug_set_goal, 1),
        (assign, reg10, ":quest_rtr"),
        (assign, reg11, ":current_rtr"),
        (assign, reg12, ":ok_rtr"),
        (display_message, "@{!}DEBUG: rtr {reg10} <= {reg11}, test {reg12}", 0xff0000),
      (try_end),
      
      (eq, ":ok_rtr", 1),]),
  
  # "script_cf_goal_renown_completed"
  # Output: True if complete, False if not
  ("cf_goal_renown_completed",
    [(assign, ":ok_renown", 0),
      
      (try_begin),
        (quest_get_slot, ":quest_renown", "qst_vc_menu", slot_set_goal_renown),
        (troop_get_slot, ":current_renown", "trp_player", slot_troop_renown),
        (ge, ":current_renown", ":quest_renown"),
        (assign, ":ok_renown", 1),
      (try_end),
      
      (try_begin),
        (gt, debug_set_goal, 1),
        (assign, reg10, ":quest_renown"),
        (assign, reg11, ":current_renown"),
        (assign, reg12, ':ok_renown'),
        (display_message, "@{!}DEBUG: renown {reg10} <= {reg11}, test {reg12}", 0xff0000),
      (try_end),
      
      (eq, ":ok_renown", 1),]),
  
  # "script_cf_goal_reputation_completed"
  # Output: True if complete, False if not
  ("cf_goal_reputation_completed",
    [(assign, ":ok_reputation", 0),
      
      (try_begin),
        (quest_get_slot, ":quest_reputation", "qst_vc_menu", slot_set_goal_reputation),
        (assign, ":current_reputation", "$player_honor"),
        (ge, ":quest_reputation", 0),
        
        (try_begin),
          (ge, ":current_reputation", ":quest_reputation"),
          (assign, ":ok_reputation", 1),
        (try_end),
        
      (else_try),
        (le, ":current_reputation", ":quest_reputation"),
        (assign, ":ok_reputation", 1),
      (try_end),
      
      (try_begin),
        (gt, debug_set_goal, 1),
        (assign, reg10, ":quest_reputation"),
        (assign, reg11, ":current_reputation"),
        (assign, reg12, ":ok_reputation"),
        (display_message, "@{!}DEBUG: reputation - quest: {reg10}, current: {reg11}, test: {reg12}", 0xff0000),
      (try_end),
      
      (eq, ":ok_reputation", 1),]),
  
  # "script_cf_goal_married_completed"
  # Output: True if complete, False if not
  ("cf_goal_married_completed",
    [(assign, ":ok_married", 0),
      
      (try_begin),
        (quest_get_slot, ":quest_marry", "qst_vc_menu", slot_set_goal_marry),
        (troop_get_slot, ":current_marry_to", "trp_player", slot_troop_spouse),
        (eq, ":quest_marry", 0),# dont want to marry
        (assign, ":ok_married", 1),# dont care if is married
      (else_try),
        (ge, ":current_marry_to", 1), #married to someone
        (assign, ":ok_married", 1),
      (try_end),
      
      (try_begin),
        (gt, debug_set_goal, 1),
        (assign, reg10, ":quest_marry"),
        (assign, reg11, ":current_marry_to"),
        (assign, reg12, ":ok_married"),
        (display_message, "@{!}DEBUG: married - quest: {reg10}, spouse: {reg11}, test: {reg12}", 0xff0000),
      (try_end),
      
      (eq, ":ok_married", 1),]),
  
  # "script_cf_goal_special_case_lord_king"
  ("cf_goal_special_case_lord_king",
    [(assign, ":completed", 1),
      
      (try_begin),
        (eq, "$goal_type", goal_king),
        (neq, "$players_kingdom", "fac_player_supporters_faction"),
        (assign, ":completed", 0),
      (else_try),
        (eq, "$goal_type", goal_lord),
        (this_or_next | eq, "$players_kingdom", 0),
        (eq, "$player_has_homage", 0), #is a mercenary
        (assign, ":completed", 0),
      (try_end),
      
      (try_begin),
        (gt, debug_set_goal, 1),
        (assign, reg10, "$players_kingdom"),
        (assign, reg11, "$goal_type"),
        (assign, reg12, ":completed"),
        (display_message, "@{!}DEBUG: lord or king - player faction: {reg10}, goal: {reg11}, test: {reg12}", 0xff0000),
      (try_end),
      
      (eq, ":completed", 1),]),
  
  # "script_goal_slider_values"
  # Raises max value for the slider if current value is already higher than
  # default max
  # Input: slider_min, slider_max
  # Output: reg5 as the max value
  ("goal_slider_values",
    [(store_script_param_1, ":slider_min"), #500 2001 60
      (store_script_param_2, ":max_value"),  #2000 2000 2000
      
      (assign, reg5, ":max_value"),
      (try_begin),
        (eq, "$goal_type", goal_custom),
        (le, reg5, ":slider_min"),
        (store_mul, reg5, ":slider_min", 2),
      (try_end),]),
  
  # "script_button_open_companions_prsnt"
  # Add here any special rules to allow or disable this presentation
  ("button_open_companions_prsnt",
    [(try_begin),
        (start_presentation,"prsnt_show_companions"),
      (try_end),]),
  
  # "script_button_open_set_goal_prsnt"
  ("button_open_set_goal_prsnt",
    [(try_begin),
        # not allowed on storymode
        (neq, "$campaign_type", camp_storyline),
        (assign, "$set_goal_new_game_trigger", 1),
        
        (try_begin),
          (call_script, "script_set_goal_check_success_event"),
          (eq, reg0, 100),
        (else_try),
          (quest_get_slot, reg0, "qst_vc_menu", slot_set_goal_type),
          (this_or_next | eq, reg0, goal_empty),
          (eq, reg0, 0),
          (quest_set_slot, "qst_vc_menu", slot_set_goal_type, goal_empty),
          (assign, "$goal_type", goal_empty),
          (start_presentation,"prsnt_set_goal"),
          
        (else_try),
          (start_presentation,"prsnt_view_current_goal"),
        (try_end),
      (try_end),]),
  
  # "script_player_honor_to_s3" -> "$player_honor"
  ("player_honor_to_s3",
    [(store_script_param, ":reputation", 1),
      
      (str_clear, s3),
      (try_begin),
        (lt, ":reputation", -90),
        (str_store_string,s3,"@Ruthless"),
      (else_try),
        (lt, ":reputation", -70),
        (str_store_string,s3,"@Vile"),
      (else_try),
        (lt, ":reputation", -50),
        (str_store_string,s3,"@Notorious"),
      (else_try),
        (lt, ":reputation", -20),
        (str_store_string,s3,"@Unkind"),
      (else_try),
        (lt, ":reputation", 20),
        (str_store_string,s3,"@Unknown"),
      (else_try),
        (is_between, ":reputation", 20, 50),
        (str_store_string,s3,"@Kind"),
      (else_try),
        (is_between, ":reputation", 50, 70),
        (str_store_string,s3,"@Admirable"),
      (else_try),
        (is_between, ":reputation", 70, 90),
        (str_store_string,s3,"@Noble"),
      (else_try),
        (ge, ":reputation", 90),
        (str_store_string,s3,"@Heroic"),
      (else_try),
        (str_store_string,s3,"str_none"),
      (try_end),]),
  
  # script_cf_player_use_second_outfit
  # Component: Second outfit VC-2404
  ("cf_player_use_second_outfit",
    [(assign, ":continue", 0),
      (try_for_range, ":slot", ek_item_0, ek_food + 1),
        (neq, ":slot", ek_horse),
        (troop_get_inventory_slot, ":cur_item", "trp_pseudo_troop_end", ":slot"),
        (gt, ":cur_item", 0),
        (assign, ":continue", 1),
      (end_try),
      
      (try_begin),#VC-3338
        (check_quest_active, "qst_hunt_down_fugitive"),
        (neg | check_quest_succeeded, "qst_hunt_down_fugitive"),
        (neg | check_quest_failed, "qst_hunt_down_fugitive"),
        (quest_slot_eq, "qst_hunt_down_fugitive", slot_quest_target_center, "$current_town"),
        (assign, ":continue", 0),
      (else_try),
        (check_quest_active, "qst_blank_quest_14"),
        (neg | check_quest_succeeded, "qst_blank_quest_14"),
        (neg | check_quest_failed, "qst_blank_quest_14"),
        (quest_slot_eq, "qst_blank_quest_14", slot_quest_target_center, "$current_town"),
        (assign, ":continue", 0),
      (try_end),
      
      (eq, ":continue", 1),]),
  
  # script_init_second_outfit
  # Component: Second outfit VC-2404
  ("init_second_outfit",
    [(store_script_param, ":mission_template", 1),
      (store_script_param, ":entry", 2),
      (try_begin),
        (mission_tpl_entry_set_override_flags, ":mission_template", ":entry", af_override_all_but_horse | af_override_foot),
        (mission_tpl_entry_clear_override_items, ":mission_template", ":entry"),
        
        (try_for_range, ":slot", ek_item_0, ek_body),
          (troop_get_inventory_slot, ":cur_item", "trp_pseudo_troop_end", ":slot"),
          (gt, ":cur_item", 0),
          (try_begin),
            (neq, ":mission_template", "mt_visit_town_castle"),
            (mission_tpl_entry_add_override_item, ":mission_template", ":entry", ":cur_item"),
            
          (else_try),
            (is_between, ":cur_item", weapons_begin, weapons_end),
            (item_has_property, ":cur_item", itp_type_one_handed_wpn),
            (item_get_weapon_length, ":weap_len", ":cur_item"),
            (lt, ":weap_len", 40),  #regular seaxes are considered "tools"
            (mission_tpl_entry_add_override_item, ":mission_template", ":entry", ":cur_item"),
          (try_end),
        (end_try),
        
        (try_for_range, ":slot", ek_body, ek_food + 1),
          (neq, ":slot", ek_horse),
          (troop_get_inventory_slot, ":cur_item", "trp_pseudo_troop_end", ":slot"),
          (gt, ":cur_item", 0),
          (mission_tpl_entry_add_override_item, ":mission_template", ":entry", ":cur_item"),
        (end_try),
      (end_try),]),
  
  # script_copy_player_troop
  # Component: Second outfit VC-2404
  ("copy_player_troop",
    [(try_begin),
        (eq,"$character_gender", tf_female),
        (try_begin),
          (eq,"$character_height",cb3_alto),
          (troop_set_type,"trp_pseudo_troop_end", tf_alta),
        (else_try),
          (eq,"$character_height",cb3_bajo),
          (troop_set_type,"trp_pseudo_troop_end", tf_baja),
        (else_try),
          (troop_set_type,"trp_pseudo_troop_end", "$character_gender"),
        (try_end),
      (else_try),
        (try_begin),
          (eq,"$character_height",cb3_alto),
          (troop_set_type,"trp_pseudo_troop_end", tf_alto),
        (else_try),
          (eq,"$character_height",cb3_bajo),
          (troop_set_type,"trp_pseudo_troop_end", tf_bajo),
        (else_try),
          (troop_set_type,"trp_pseudo_troop_end", "$character_gender"),
        (try_end),
      (try_end),
      (str_store_troop_face_keys, s1, "trp_player"),
      (troop_set_face_keys, "trp_pseudo_troop_end", s1),]),
  
  # script_troop_detail_layout
  # Troop Detail VC-2379
  # Description
  ("troop_detail_layout",
    [# Done
      (set_container_overlay, -1),
      (create_game_button_overlay, "$g_presentation_leave_button", "str_done"),
      (position_set_x, pos1, Screen_Width - 210),(position_set_y, pos1, 60),
      (overlay_set_position, "$g_presentation_leave_button", pos1),
      
      # Gear/Stats button
      (str_clear, s1),
      
      (try_begin),
        (eq, "$temp", 1),
        (str_store_string, s1, "@Inventory"),
      (else_try),
        (str_store_string, s1, "@Show stats"),
      (try_end),
      
      (create_game_button_overlay, "$g_presentation_obj_1", s1),
      (position_set_x, pos1, Screen_Width - 425),(position_set_y, pos1, 60),
      (overlay_set_position, "$g_presentation_obj_1", pos1),
      
      # Show item tooltip
      (create_check_box_overlay, "$checkbox_show_item_details", "mesh_checkbox_off", "mesh_checkbox_on"),
      (position_set_x, pos1, 915),(position_set_y, pos1, 675),
      (overlay_set_position, "$checkbox_show_item_details", pos1),
      (overlay_set_val, "$checkbox_show_item_details", "$checkbox_show_item_details_val"),
      
      (create_text_overlay, "$checkbox_show_item_details_label", "@Show item details  ", tf_right_align),
      (position_set_x, pos1, 915),(position_set_y, pos1, 675 - 5),
      (overlay_set_position, "$checkbox_show_item_details_label", pos1),
      (position_set_x, pos1, font_normal),(position_set_y, pos1, font_normal),
      (overlay_set_size, "$checkbox_show_item_details_label", pos1),
      (overlay_set_color, "$checkbox_show_item_details_label", 0x0000FF),
      
      (try_begin),
        (eq, "$temp", 1),
        (overlay_set_display, "$checkbox_show_item_details_label", 0),
        (overlay_set_display, "$checkbox_show_item_details", 0),
      (try_end),
      
      # title
      (create_text_overlay, reg1, "@Troop detail", tf_center_justify),
      (position_set_x, pos1, 500),(position_set_y, pos1, Screen_Height - 85),
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos1, font_title),(position_set_y, pos1, font_title),
      (overlay_set_size, reg1, pos1),
      
      (create_text_overlay, reg1, "@Troop detail", tf_center_justify),
      (position_set_x, pos1, 500 + 1),(position_set_y, pos1, Screen_Height - 85),
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos1, font_title),(position_set_y, pos1, font_title),
      (overlay_set_size, reg1, pos1),
      
      (create_text_overlay, reg1, "@Troop detail", tf_center_justify),
      (position_set_x, pos1, 500 + 2),(position_set_y, pos1, Screen_Height - 85 + 1),
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos1, font_title),(position_set_y, pos1, font_title),
      (overlay_set_size, reg1, pos1),]),
  
  # "script_troop_detail_draw_troop"
  # Input: troop_id
  # Output: none
  ("troop_detail_draw_troop",
    [(store_script_param_1, ":troop_id"),
      
      (create_mesh_overlay, reg1, "mesh_white_plane"),
      (position_set_x, pos1, 10 * 1000),
      (position_set_y, pos1, 24 * 1000),
      (overlay_set_size, reg1, pos1),
      (position_set_x, pos1, 90),
      (position_set_y, pos1, 150),
      (overlay_set_position, reg1, pos1),
      (overlay_set_color, reg1, 0xE6D1A7),
      
      (try_begin),
        (eq, "$temp", 1),
        (store_mul, ":cur_troop", ":troop_id", 2),#with weapons
        (create_mesh_overlay_with_tableau_material,
          "$default_troop_portrait",
          -1,
          "tableau_troop_tree_pic",
        ":cur_troop"),
      (else_try),
        (create_mesh_overlay_with_tableau_material,
          "$default_troop_portrait",
          -1,
          "tableau_troop_detail_dummy_pic",
        ":troop_id"),
      (try_end),
      
      (position_set_x, pos1,  Troop_Tree_Tableau_Width * 0.8),
      (position_set_y, pos1, Troop_Tree_Tableau_Height * 0.8),
      (overlay_set_size, "$default_troop_portrait", pos1),
      (position_set_x, pos1, 0),(position_set_y, pos1, 150),
      (overlay_set_position, "$default_troop_portrait", pos1),]),
  
  
  # "script_troop_detail_draw_weapons"
  # Input: troop_id
  # Output: none
  ("troop_detail_draw_weapons",
    [(store_script_param_1, ":troop_id"),
      
      (call_script, "script_troop_detail_draw_weapons_aux", ":troop_id", ek_item_0),
      (call_script, "script_troop_detail_draw_weapons_aux", ":troop_id", ek_item_1),
      (call_script, "script_troop_detail_draw_weapons_aux", ":troop_id", ek_item_2),
      (call_script, "script_troop_detail_draw_weapons_aux", ":troop_id", ek_item_3),]),
  
  # "script_troop_detail_draw_weapons_aux"
  # Input: troop_id, wp_number
  # Output: none
  ("troop_detail_draw_weapons_aux",
    [(store_script_param_1, ":troop_id"),
      (store_script_param_2, ":wp_number"),
      
      (troop_get_inventory_slot, ":wp", ":troop_id", ":wp_number"),
      
      (try_begin),
        (neq, ":wp", -1),
        
        (assign, ":y", 500),
        (store_mul, ":dec_y", 100, ":wp_number"),
        (val_sub, ":y", ":dec_y"),
        (store_add, ":y2", ":y", 40),
        
        (create_mesh_overlay, reg1, "mesh_mp_inventory_choose"),
        (position_set_x, pos1, 300),(position_set_y, pos1, ":y"),
        (overlay_set_position, reg1, pos1),
        
        (create_mesh_overlay_with_item_id, reg2, ":wp"),
        (position_set_x, pos1, 300 + 40),(position_set_y, pos1, ":y2"),
        (overlay_set_position, reg2, pos1),
        
        (position_set_x, pos2, 600),(position_set_y, pos2, 600),
        (overlay_set_size, reg1, pos2),
        (overlay_set_alpha, reg1, 0xFF),
        (overlay_set_size, reg2, pos2),
      (try_end),]),
  
  # "script_troop_detail_stats"
  # Input: troop_id
  # Output: none
  ("troop_detail_stats",
    [(store_script_param_1, ":troop_id"),
      
      (try_begin),
        (eq, debug_troop_detail, 1),
        (assign, reg0, "$temp2"),
        (display_message, "@{!}DEBUG: STATS screen"),
      (try_end),
      
      (create_mesh_overlay, ":stats_area", "mesh_white_plane"),
      (position_set_x, pos1, 24 * 1000),(position_set_y, pos1, 24 * 1000),
      (overlay_set_size, ":stats_area", pos1),
      (position_set_x, pos1, 450),(position_set_y, pos1, 150),
      (overlay_set_position, ":stats_area", pos1),
      (overlay_set_color, ":stats_area", 0xE6D1A7),
      
      (str_store_troop_name, s1, ":troop_id"),
      
      (store_attribute_level, ":troop_str", ":troop_id", ca_strength),
      (store_attribute_level, ":troop_agi", ":troop_id", ca_agility),
      
      (store_skill_level, ":troop_powerstrike", "skl_power_strike", ":troop_id"),
      (store_skill_level, ":troop_athletics", "skl_athletics", ":troop_id"),
      (store_skill_level, ":troop_riding", "skl_riding", ":troop_id"),
      (store_skill_level, ":troop_powerthrow", "skl_power_throw", ":troop_id"),
      (store_skill_level, ":troop_powerdraw", "skl_power_draw", ":troop_id"),
      
      (store_proficiency_level, ":troop_onehanded", ":troop_id", wpt_one_handed_weapon),
      (store_proficiency_level, ":troop_twohanded", ":troop_id", wpt_two_handed_weapon),
      (store_proficiency_level, ":troop_polearm", ":troop_id", wpt_polearm),
      (store_proficiency_level, ":troop_archery", ":troop_id", wpt_archery),
      (store_proficiency_level, ":troop_crossbow", ":troop_id", wpt_crossbow),
      (store_proficiency_level, ":troop_throwing", ":troop_id", wpt_throwing),
      (store_proficiency_level, ":troop_slings", ":troop_id", wpt_firearm),
      
      # Check items for weapon skills of the troops
      (assign, ":has_onehand", 0),
      (assign, ":has_twohand", 0),
      (assign, ":has_polearm", 0),
      (assign, ":has_crossbow", 0),
      (assign, ":has_archery", 0),
      (assign, ":has_sling", 0),
      (assign, ":has_throw", 0),
      
      (try_for_range, ":i", 0, 64),
        (troop_get_inventory_slot, ":item", ":troop_id", ":i"),
        (neq, ":item", -1),
        (item_get_type, ":type", ":item"),
        
        (try_begin),
          (eq, ":type", itp_type_bow),
          (assign, ":has_archery", 1),
        (else_try),
          (eq, ":type", itp_type_crossbow),
          (assign, ":has_crossbow", 1),
        (else_try),
          (eq, ":type", itp_type_pistol),
          (assign, ":has_sling", 1),
        (else_try),# throwable spears are both
          (try_begin),
            (eq, ":type", itp_type_thrown),
            (assign, ":has_throw", 1),
          (try_end),
          
          (eq, ":type", itp_type_polearm),
          (assign, ":has_polearm", 1),
        (else_try),
          (eq, ":type", itp_type_one_handed_wpn),
          (assign, ":has_onehand", 1),
        (else_try),
          (eq, ":type", itp_type_two_handed_wpn),
          (assign, ":has_twohand", 1),
        (try_end),
      (try_end),
      
      (store_troop_health, ":troop_hp", ":troop_id", 1),
      (store_character_level, ":troop_level", ":troop_id"),
      
      (str_store_string, s1, "@{s1}^"),
      (assign, reg1, ":troop_level"),
      (str_store_string, s1, "@{s1}Level: {reg1}^"),
      (assign, reg1, ":troop_hp"),
      (str_store_string, s1, "@{s1}Hit Points: {reg1}"),
      
      (create_text_overlay, reg1, s1, tf_left_align | tf_double_space),
      (position_set_x, pos1, 465),(position_set_y, pos1, 530),
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos1, font_normal),(position_set_y, pos1, font_normal),
      (overlay_set_size, reg1, pos1),
      
      (str_clear, s1),(str_clear, s2),
      (assign, reg1, ":troop_str"),
      (str_store_string, s1, "@{s1}Atributes: ^   Strength:^"),
      (str_store_string, s2, "@{s2}^{reg1}^"),
      (assign, reg1, ":troop_agi"),
      (str_store_string, s1,   "@{s1}   Agility:"),
      (str_store_string, s2, "@{s2}{reg1}"),
      
      (create_text_overlay, reg1, s1, tf_left_align | tf_double_space),
      (position_set_x, pos1, 465),(position_set_y, pos1, 405),
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos1, font_normal),(position_set_y, pos1, font_normal),
      (overlay_set_size, reg1, pos1),
      
      (create_text_overlay, reg1, s2, tf_center_justify | tf_double_space),
      (position_set_x, pos1, 465 + 145),(position_set_y, pos1, 405),
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos1, font_normal),(position_set_y, pos1, font_normal),
      (overlay_set_size, reg1, pos1),
      
      (str_clear, s1),(str_clear, s2),
      (assign, reg1, ":troop_powerstrike"),
      (str_store_string, s1, "@{s1}Main skills: ^"),
      (str_store_string, s1, "@{s1}   Power Strike:^"),
      (str_store_string, s2, "@{s2}{reg1}^"),
      (assign, reg1, ":troop_athletics"),
      (str_store_string, s1, "@{s1}   Athletics:^"),
      (str_store_string, s2, "@{s2}{reg1}^"),
      (assign, reg1, ":troop_riding"),
      (str_store_string, s1, "@{s1}   Riding:^"),
      
      (try_begin),
        (troop_is_mounted, ":troop_id"),
        (str_store_string, s2, "@{s2}{reg1}^"),
      (else_try),
        (str_store_string, s2, "@{s2}-^"),
      (try_end),
      
      (assign, reg1, ":troop_powerthrow"),
      (str_store_string, s1, "@{s1}   Power Throw:^"),
      
      (try_begin),
        (eq, ":has_throw", 1),
        (str_store_string, s2, "@{s2}{reg1}^"),
      (else_try),
        (str_store_string, s2, "@{s2}-^"),
      (try_end),
      
      (assign, reg1, ":troop_powerdraw"),
      (str_store_string, s1, "@{s1}   Power Draw:^^"),
      
      (try_begin),
        (eq, ":has_archery", 1),
        (str_store_string, s2, "@{s2}{reg1}^^"),
      (else_try),
        (str_store_string, s2, "@{s2}-^^"),
      (try_end),
      
      (create_text_overlay, reg1, s1, tf_left_align | tf_double_space),
      (position_set_x, pos1, 465),(position_set_y, pos1, 170),
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos1, font_normal),(position_set_y, pos1, font_normal),
      (overlay_set_size, reg1, pos1),
      
      (create_text_overlay, reg1, s2, tf_center_justify | tf_double_space),
      (position_set_x, pos1, 645),(position_set_y, pos1, 170),
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos1, font_normal),(position_set_y, pos1, font_normal),
      (overlay_set_size, reg1, pos1),
      
      (str_clear, s1),(str_clear, s2),
      (str_store_string, s1, "@{s1}Weapons:^"),
      (assign, reg1, ":troop_onehanded"),
      (str_store_string, s1, "@{s1}   One Handed:^"),
      
      (try_begin),
        (eq, ":has_onehand", 1),
        (str_store_string, s2, "@{s2}{reg1}^"),
      (else_try),
        (str_store_string, s2, "@{s2}-^"),
      (try_end),
      
      (assign, reg1, ":troop_twohanded"),
      (str_store_string, s1, "@{s1}   Two Handed:^"),
      
      (try_begin),
        (eq, ":has_twohand", 1),
        (str_store_string, s2, "@{s2}{reg1}^"),
      (else_try),
        (str_store_string, s2, "@{s2}-^"),
      (try_end),
      
      (assign, reg1, ":troop_polearm"),
      (str_store_string, s1, "@{s1}   Polearms:^"),
      
      (try_begin),
        (eq, ":has_polearm", 1),
        (str_store_string, s2, "@{s2}{reg1}^"),
      (else_try),
        (str_store_string, s2, "@{s2}-^"),
      (try_end),
      
      (assign, reg1, ":troop_archery"),
      (str_store_string, s1, "@{s1}   Archery:^"),
      
      (try_begin),
        (eq, ":has_archery", 1),
        (str_store_string, s2, "@{s2}{reg1}^"),
      (else_try),
        (str_store_string, s2, "@{s2}-^"),
      (try_end),
      
      (assign, reg1, ":troop_crossbow"),
      (str_store_string, s1, "@{s1}   Crossbow:^"),
      
      (try_begin),
        (eq, ":has_crossbow", 1),
        (str_store_string, s2, "@{s2}{reg1}^"),
      (else_try),
        (str_store_string, s2, "@{s2}-^"),
      (try_end),
      
      (assign, reg1, ":troop_throwing"),
      (str_store_string, s1, "@{s1}   Throwing:^"),
      
      (try_begin),
        (eq, ":has_throw", 1),
        (str_store_string, s2, "@{s2}{reg1}^"),
      (else_try),
        (str_store_string, s2, "@{s2}-^"),
      (try_end),
      
      (assign, reg1, ":troop_slings"),
      (str_store_string, s1, "@{s1}   Slings:"),
      
      (try_begin),
        (eq, ":has_sling", 1),
        (str_store_string, s2, "@{s2}{reg1}"),
      (else_try),
        (str_store_string, s2, "@{s2}-"),
      (try_end),
      
      (create_text_overlay, reg1, s1, tf_left_align | tf_double_space),
      (position_set_x, pos1, 465 + 240),(position_set_y, pos1, 275),
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos1, font_normal),(position_set_y, pos1, font_normal),
      (overlay_set_size, reg1, pos1),
      
      (create_text_overlay, reg1, s2, tf_center_justify | tf_double_space),
      (position_set_x, pos1, 465 + 240 + 192),(position_set_y, pos1, 275),
      (overlay_set_position, reg1, pos1),
      (position_set_x, pos1, font_normal),(position_set_y, pos1, font_normal),
      (overlay_set_size, reg1, pos1),]),
  
  # Uses trp_temp_troop to gather the items
  # "script_troop_detail_inventory"
  # Output: $temp2 with the quantity of items on inventory
  # Output: trp_temp_array_a, trp_temp_array_b, trp_temp_array_c
  ("troop_detail_inventory",
    [# Container 1: selection
      (create_text_overlay, ":gear_container", "str_empty_string", tf_scrollable_style_2),
      (position_set_x, pos1, 450),(position_set_y, pos1, 150),
      (overlay_set_position, ":gear_container", pos1),
      (position_set_x, pos1, 480),(position_set_y, pos1, 480),
      (overlay_set_area_size, ":gear_container", pos1),
      (set_container_overlay, ":gear_container"),
      
      (troop_sort_inventory, "trp_temp_troop"),
      (troop_get_inventory_capacity, ":num_slots", "trp_temp_troop"),
      (store_free_inventory_capacity, ":num_free_slots", "trp_temp_troop"),
      (store_sub, ":num_items", ":num_slots", ":num_free_slots"),
      (val_sub, ":num_items", 10),
      
      (store_div, ":y_max", ":num_items", 4),
      
      (assign, ":box_incr", 115),
      (val_max, ":y_max", 1),
      (val_mul, ":y_max", ":box_incr"),
      
      (assign, ":x_item", 60),
      (store_add, ":y_item", ":y_max", 60),
      (assign, ":count", 0),
      (assign, ":x_box", 0),
      (assign, ":y_box", ":y_max"),
      
      # 0-70 for body armor, 71-140 for helmet, 141-210 for boots, 211-299 rest
      # 300+ for imod of each item
      (assign, ":limit_temp_c", 300),
      (store_mul, reg0, ":limit_temp_c", 2),
      
      (try_for_range, ":slot", 0, reg0),
        (troop_set_slot, "trp_temp_array_a", ":slot", -1),
        (troop_set_slot, "trp_temp_array_b", ":slot", -1),
        (troop_set_slot, "trp_temp_array_c", ":slot", -1),
      (try_end),
      
      (assign, ":armor_slot", 0),
      (assign, ":helmet_slot", 71),
      (assign, ":boots_slot", 141),
      (assign, ":rest_slot", 211),
      (assign, ":imod_slot_add", 300),
      
      (try_for_range, ":slot", 0, ":num_slots"),
        (troop_get_inventory_slot, ":item", "trp_temp_troop", ":slot"),
        (neq, ":item", -1),
        (troop_get_inventory_slot_modifier, ":item_imod", "trp_temp_troop", ":slot"),
        
        (item_get_type, ":type", ":item"),
        
        (try_begin),
          (eq, ":type", itp_type_body_armor),
          (troop_set_slot, "trp_temp_array_c", ":armor_slot", ":item"),
          (store_add, reg0, ":armor_slot", ":imod_slot_add"),
          (troop_set_slot, "trp_temp_array_c", reg0, ":item_imod"),
          (val_add, ":armor_slot", 1),
        (else_try),
          (eq, ":type", itp_type_head_armor),
          (troop_set_slot, "trp_temp_array_c", ":helmet_slot", ":item"),
          (store_add, reg0, ":helmet_slot", ":imod_slot_add"),
          (troop_set_slot, "trp_temp_array_c", reg0, ":item_imod"),
          (val_add, ":helmet_slot", 1),
        (else_try),
          (eq, ":type", itp_type_foot_armor),
          (troop_set_slot, "trp_temp_array_c", ":boots_slot", ":item"),
          (store_add, reg0, ":boots_slot", ":imod_slot_add"),
          (troop_set_slot, "trp_temp_array_c", reg0, ":item_imod"),
          (val_add, ":boots_slot", 1),
        (else_try),
          (troop_set_slot, "trp_temp_array_c", ":rest_slot", ":item"),
          (store_add, reg0, ":rest_slot", ":imod_slot_add"),
          (troop_set_slot, "trp_temp_array_c", reg0, ":item_imod"),
          (val_add, ":rest_slot", 1),
        (try_end),
      (try_end),
      
      (try_for_range, ":slot", 0, ":limit_temp_c"),
        (troop_get_slot, ":item", "trp_temp_array_c", ":slot"),
        
        (try_begin),
          (neq, ":item", -1),
          (store_add, reg0, ":slot", 300),
          (troop_get_slot, ":item_imod", "trp_temp_array_c", reg0),
          
          (val_add, ":count", 1),
          
          (create_mesh_overlay, reg1, "mesh_mp_inventory_choose"),
          (position_set_x, pos1, ":x_box"),(position_set_y, pos1, ":y_box"),
          (overlay_set_position, reg1, pos1),
          (position_set_x, pos1, 900),(position_set_y, pos1, 900),
          (overlay_set_size, reg1, pos1),
          (overlay_set_alpha, reg1, 0xFF),
          
          (create_mesh_overlay_with_item_id, reg1, ":item"),
          (position_set_x, pos1, ":x_item"),(position_set_y, pos1, ":y_item"),
          (overlay_set_position, reg1, pos1),
          (position_set_x, pos1, 1250),(position_set_y, pos1, 1250),
          (overlay_set_size, reg1, pos1),
          
          (val_add, ":x_item", 115),
          (val_add, ":x_box", 115),
          
          (try_begin),# next row items
            (store_mod, ":mod", ":count", 4),
            (eq, ":mod", 0),
            (val_sub, ":y_item", 115),
            (val_sub, ":y_box", 115),
            (assign, ":x_item", 60),
            (assign, ":x_box", 0),
          (try_end),
          
          #tooltip
          (store_add, reg0, ":count", ":imod_slot_add"),
          (troop_set_slot, "trp_temp_array_a", ":count", reg1),
          (troop_set_slot, "trp_temp_array_b", ":count", ":item"),
          (troop_set_slot, "trp_temp_array_b", reg0, ":item_imod"),
        (try_end),
      (try_end),
      
      (assign, "$temp2", ":count"),
      (set_container_overlay, -1),
      
      # Text about troop inventory
      (try_begin),
        (create_text_overlay, reg1,
          "@(Click on the troop to rotate it)^^Click on pieces of gear ^(helmet, armor, gloves and boots)^ to display them.",
        tf_center_justify),
        (position_set_x, pos1, 245),(position_set_y, pos1, 27),
        (overlay_set_position, reg1, pos1),
        (position_set_x, pos1, font_normal),(position_set_y, pos1, font_normal),
        (overlay_set_size, reg1, pos1),
        (overlay_set_color, reg1, 0x0000FF),
      (try_end),
      
      (try_begin),
        (eq, debug_troop_detail, 1),
        (assign, reg0, "$temp2"),
        (display_message, "@{!}DEBUG: number items {reg0}"),
      (try_end),]),
  
  # "script_troop_detail_inventory_tooltip"
  ("troop_detail_inventory_tooltip",
    [(try_begin),
        (store_trigger_param_1, ":object"),
        (store_trigger_param_2, ":enter_leave"),
        
        (eq, "$checkbox_show_item_details_val", 1),
        (eq, "$temp", 2),
        
        (try_begin),
          (eq, ":enter_leave", 1),
          (close_item_details),
          
        (else_try),
          (assign, ":end_loop", "$temp2"),
          (val_add, ":end_loop", 1),
          
          (try_for_range, ":i", 1, ":end_loop"),
            (troop_get_slot, reg1, "trp_temp_array_a", ":i"),
            (eq, ":object", reg1),
            (troop_get_slot, reg2, "trp_temp_array_b", ":i"),
            
            (store_add, reg3, ":i", 300),
            (troop_get_slot, reg4, "trp_temp_array_b", reg3),
            (overlay_get_position, pos1, ":object"),
            (show_item_details_with_modifier, reg2, reg4, pos1, 100),
            (assign, ":end_loop", 0),
          (try_end),
        (try_end),
      (try_end),]),
  
  # "script_troop_detail_update_dummy"
  # Input: troop_id, $temp2: quantity of items, trp_temp_array_a,
  # trp_temp_array_b
  # Output: reg10 : should update presentation
  ("troop_detail_update_dummy",
    [(store_script_param_1, ":troop_id"),
      (store_script_param_2, ":object"),
      
      (try_begin),
        # Rotate
        (eq, ":object", "$default_troop_portrait"),
        (val_add, "$troop_detail_dummy_angle", 1),
        (try_begin),
          (ge, "$troop_detail_dummy_angle", 4),
          (assign, "$troop_detail_dummy_angle", 0),
        (try_end),
        (assign, ":redraw_troop", 1),
        
      (else_try),
        # Change gear
        (assign, ":end_loop", "$temp2"),
        (val_add, ":end_loop", 1),
        (assign, ":new_item", -1),
        (assign, reg10, 0),
        
        (try_for_range, ":i", 1, ":end_loop"),
          (troop_get_slot, reg1, "trp_temp_array_a", ":i"),
          (eq, ":object", reg1),
          (troop_get_slot, ":new_item", "trp_temp_array_b", ":i"),
          (store_add, reg0, ":i", 300),
          (troop_get_slot, ":new_item_imod", "trp_temp_array_b", reg0),
          
          (assign, ":end_loop", 0),
          
          (item_get_type, ":type", ":new_item"),
          
          (try_begin),
            (eq, ":type", itp_type_body_armor),
            (call_script, "script_troop_detail_update_dummy_gear_aux", ":troop_id", ek_body, ":new_item", ":new_item_imod"),
            
          (else_try),
            (eq, ":type", itp_type_head_armor),
            (call_script, "script_troop_detail_update_dummy_gear_aux", ":troop_id", ek_head, ":new_item", ":new_item_imod"),
            
          (else_try),
            (eq, ":type", itp_type_foot_armor),
            (call_script, "script_troop_detail_update_dummy_gear_aux", ":troop_id", ek_foot, ":new_item", ":new_item_imod"),
            
          (else_try),
            (eq, ":type", itp_type_hand_armor),
            (call_script, "script_troop_detail_update_dummy_gear_aux", ":troop_id", ek_gloves, ":new_item", ":new_item_imod"),
          (try_end),
          (troop_sort_inventory, ":troop_id"),
          
        (try_end),
        (assign, ":redraw_troop", reg10),
      (try_end),
      
      (try_begin),
        (eq, ":redraw_troop", 1),
        (start_presentation, "prsnt_troop_detail"),
      (try_end),]),
  
  # "script_troop_detail_update_dummy_gear_aux"
  # Input: troop_id, body_part, new_item
  # Output: reg10 with redraw_troop
  ("troop_detail_update_dummy_gear_aux",
    [(store_script_param_1, ":troop_id"),
      (store_script_param_2, ":body_part"),
      (store_script_param, ":new_item", 3),
      (store_script_param, ":new_item_imod", 4),
      
      (assign, ":redraw_troop", 0),
      
      (try_begin),
        (troop_get_inventory_slot, ":old_item", ":troop_id", ":body_part"),
        (neq, ":old_item", ":new_item"),
        (troop_get_inventory_slot_modifier, ":old_item_imod", ":troop_id", ":body_part"),
        (troop_remove_item, ":troop_id", ":new_item"),
        (troop_set_inventory_slot, ":troop_id", ":body_part", ":new_item"),
        (troop_set_inventory_slot_modifier, ":troop_id", ":body_part", ":new_item_imod"),
        (assign, ":redraw_troop", 1),
        
        (neq, ":old_item", -1),
        (troop_add_item, ":troop_id", ":old_item", ":old_item_imod"),
        
      (try_end),
      
      (assign, reg10, ":redraw_troop"),]),
  
  # Changes between STATS and INVENTORY screens
  # "script_troop_detail_change_screen"
  # Input: none
  # Output: none
  ("troop_detail_change_screen",
    [(store_script_param_1, ":troop_id"),
      
      (try_begin),
        (eq, "$temp", 1),
        (assign, "$temp", 2),
        
        # Clone dummy gear
        (troop_clear_inventory, "trp_temp_troop"),
        (troop_get_inventory_capacity, ":slots", "trp_temp_troop"),
        (try_for_range, ":i", 0, ":slots"),
          (troop_set_inventory_slot, "trp_temp_troop", ":i", -1),
          (troop_set_inventory_slot_modifier, "trp_temp_troop", ":i", 0),
        (try_end),
        
        (assign, ":clone_slot", 10),
        (troop_set_auto_equip, "trp_temp_troop", 0),
        
        (try_for_range, ":i", 0, ":slots"),
          (troop_get_inventory_slot, ":item_id", ":troop_id", ":i"),
          (neq, ":item_id", -1),
          (troop_get_inventory_slot_modifier, ":item_imod", ":troop_id", ":i"),
          (troop_set_inventory_slot, "trp_temp_troop",  ":clone_slot", ":item_id"),
          (troop_set_inventory_slot_modifier, "trp_temp_troop", ":clone_slot", ":item_imod"),
          
          (val_add, ":clone_slot", 1),
        (try_end),
        (assign, "$troop_detail_dummy_angle", 0),
        
      (else_try),
        (assign, "$temp", 1),
      (try_end),
      
      (start_presentation, "prsnt_troop_detail"),]),
  
  
  # script_add_troop_to_cur_tableau_for_troop_detail_dummy
  # Used on Troop Detail to display the dummy troop (player can switch helmet,
  # chest, boots)
  # Input: troop_no (x4 as it has the rotation angle too)
  ("add_troop_to_cur_tableau_for_troop_detail_dummy",
    [(store_script_param, ":troop_no",1),
      (assign, ":side", "$troop_detail_dummy_angle"),
      (val_mul, ":side", 90), #to degrees
      
      (assign, reg0, ":side"),
      
      (set_fixed_point_multiplier, 100),
      
      (cur_tableau_clear_override_items),
      #(cur_tableau_set_override_flags, af_override_weapons),
      
      (init_position, pos2),
      (position_rotate_z, pos2, ":side"),
      (cur_tableau_set_camera_parameters, 1, 6, 6, 10, 10000),
      
      (init_position, pos5),
      (assign, ":cam_height", 105),
      (assign, ":camera_distance", 450),
      (assign, ":camera_yaw", 15),
      (assign, ":camera_pitch", -18),
      (assign, ":animation", anim_stand_man),
      
      (position_set_z, pos5, ":cam_height"),
      
      # camera looks towards -z axis
      (position_rotate_x, pos5, -90),
      (position_rotate_z, pos5, 180),
      
      # now apply yaw and pitch
      (position_rotate_y, pos5, ":camera_yaw"),
      (position_rotate_x, pos5, ":camera_pitch"),
      (position_move_z, pos5, ":camera_distance", 0),
      (position_move_x, pos5, 5, 0),
      
      (call_script, "script_manage_legs_in_cur_tableau", ":troop_no"),
      
      (store_mul, ":random_seed", ":troop_no", 126233),
      (val_mod, ":random_seed", 1000),
      (val_add, ":random_seed", 1),
      (cur_tableau_add_troop, ":troop_no", pos2, ":animation", ":random_seed"),
      
      (cur_tableau_set_camera_position, pos5),
      
      (copy_position, pos8, pos5),
      (position_rotate_x, pos8, -90), #y axis aligned with camera now.  z is up
      (position_rotate_z, pos8, 30),
      (position_rotate_x, pos8, -60),
      (cur_tableau_add_sun_light, pos8, 175,150,125),]),
  
  
  # script_advanced_agent_set_speed_modifier
  # Component: Helper (VC-2404)
  # Input:
  # Output:
  ("advanced_agent_set_speed_modifier",
    [(store_script_param, ":agent", 1),
      (store_script_param, ":percent", 2),
      (try_begin),
        (game_in_multiplayer_mode),
        (server_get_combat_speed, ":combat_speed"),
        (call_script,"script_mp_get_agent_speed_modifiers",":agent",":percent"),
        (assign,":percent",reg0),
      (else_try),
        (options_get_combat_speed, ":combat_speed"),		#0 = slowest, 1 = slower, 2 = normal, 3 = faster, 4 = fastest
      (end_try),
      (val_sub, ":combat_speed", 4),						# -4 ...  0
      (val_mul, ":combat_speed", -7),						# 0 ...  28
      (store_sub, ":speed_modifier", 100, ":combat_speed"),	# 72 ...  100
      
      (val_mul, ":speed_modifier", ":percent"),
      (val_div, ":speed_modifier", 100),
      (agent_set_speed_modifier,":agent", ":speed_modifier"),
      (set_fixed_point_multiplier, 100),
      (agent_set_horse_speed_factor, ":agent", ":speed_modifier"),]),
  
  # script_add_jetty_system
  # Component: Ships and sea (map)
  ("add_jetty_system",
    [(try_begin),
        (party_slot_eq, "p_jetty_1", slot_party_port_party, 0),	#avoid doing it twice
        (try_for_range, ":cur_jetty", "p_jetty_1", "p_jetty_4"),
          (set_spawn_radius, 0),
          (spawn_around_party, ":cur_jetty", "pt_jetty_port"),
          (assign, ":curr_port", reg0),
          (party_set_slot, ":cur_jetty", slot_party_port_party, ":curr_port"),
          (party_set_slot, ":curr_port", slot_party_port_party, ":cur_jetty"),
          (party_get_position, pos1, ":cur_jetty"),
          (call_script, "script_get_next_water_position", 1),
          (party_set_position, ":curr_port", pos2),
          (call_script, "script_get_closest_center", ":cur_jetty"),
          (str_store_party_name, s2, reg0),
          (party_set_name, ":curr_port", "@Landing near {s2}"),
        (try_end),
      (try_end),]),
  
  # script_add_jetty_system_2
  # VC-3494
  # Component: Ships and sea (map)
  ("add_jetty_system_2",
    [(try_begin),
        (party_slot_eq, "p_jetty_4", slot_party_port_party, 0),	#avoid doing it twice
        (try_for_range, ":cur_jetty", "p_jetty_4", "p_wales_spawn_point"),
          (set_spawn_radius, 0),
          (spawn_around_party, ":cur_jetty", "pt_jetty_port"),
          (assign, ":curr_port", reg0),
          (party_set_slot, ":cur_jetty", slot_party_port_party, ":curr_port"),
          (party_set_slot, ":curr_port", slot_party_port_party, ":cur_jetty"),
          (party_get_position, pos1, ":cur_jetty"),
          (call_script, "script_get_next_water_position", 1),
          (party_set_position, ":curr_port", pos2),
          (call_script, "script_get_closest_center", ":cur_jetty"),
          (str_store_party_name, s2, reg0),
          (party_set_name, ":curr_port", "@Landing near {s2}"),
        (try_end),
      (try_end),]),
  
  # script_print_debug_log
  # Component: Debug
  ("print_debug_log",
    [(try_begin),
        (neq, "$savegame_id", 0),
        (neg | str_is_empty, s30),
        (set_show_messages, 1),
        (display_message, "str_log_created"),
        (set_show_messages, 0),
        (assign, reg1, "$savegame_id"),
        (assign, reg2, warband_version),
        (assign, reg3, vc_version),
        (assign, reg4, "$game_started_in_version"),
        (assign, reg5, "$campaign_type"),
        (assign, reg6, "$recruitment_on"),
        (assign, reg7, "$easy_levelling"),
        (assign, reg8, "$easy_wage"),
        (display_message, "str_debug_log_print1"),
        (display_message, "str_debug_log_print2"),
        (set_show_messages, 1),
      (try_end),]),
  
  # "script_setup_debug_log"
  # Component: Debug
  # Load into memory s30 with the log content
  # Output: s30
  ("setup_debug_log",
    [(str_store_troop_name_plural, s30, "trp_pseudo_troop_01"),
      (try_begin),
        (neg | str_is_empty, s30),
        (str_store_troop_name_plural, s31, "trp_pseudo_troop_02"),
        (str_store_troop_name_plural, s32, "trp_pseudo_troop_03"),
        (str_store_string, s30, "str_debug_log_1_header"),
        (str_store_string, s31, "str_debug_log_3_header"),
        (str_store_string, s32, "str_debug_log_4_header"),
        (str_store_string, s30, "@{!}{s30}{s31}{s32}"),
        (str_clear, s31),
        (str_clear, s32),
      (end_try),]),
  
  # script_cf_casulties_complication_1
  # Component: Helper
  # This script is recovering death in case they have been killed for wrong
  # reason
  # Input: killer_agent_no,
  ("cf_casulties_complication_1",
    [(store_script_param, ":killer_agent_no", 1),
      (store_script_param, ":hit_bone", 2),
      (assign, ":killed", 0),
      (try_begin),
        (agent_is_active,":killer_agent_no"),
        (agent_get_wielded_item, ":wielded_item", ":killer_agent_no", 0),
        (gt, ":wielded_item", 0),
        (this_or_next | is_between,":wielded_item", "itm_long_bow", "itm_torch"),
        (is_between, ":wielded_item", "itm_darts", "itm_stones"),
        (eq, ":hit_bone", hb_head),
        (assign, ":killed", 1),
        # (str_store_item_name, s33, ":wielded_item"),
        # (display_message, "@{s32} is killed by {s34} with {s33}"),
      (try_end),
      (eq, ":killed", 1),
      #(display_message, "@{!}TEST: complication_1_called and applied"),
  ]),
  
  # script_cf_casulties_complication_2
  # Component: Helper
  ("cf_casulties_complication_2",
    [(store_script_param, ":killer_agent_no", 1),
      (assign, ":cont", 0),
      (try_begin),
        (agent_is_active,":killer_agent_no"),
        (agent_get_wielded_item, ":wielded_item", ":killer_agent_no", 0),
        (gt, ":wielded_item", 0),
        (item_get_type, ":item_type", ":wielded_item"),
        (try_begin),
          (is_between, ":wielded_item","itm_light_spear1", "itm_standard"),
          (agent_get_action_dir,":swing",":killer_agent_no"),
          (is_between,":swing",1,3),
          (assign, ":cont", 1),
        (else_try),
          (eq, ":item_type", itp_type_polearm),
          (neg | is_between, ":wielded_item","itm_light_spear1", "itm_standard"),
          (assign, ":damage_type", 0),
          (item_get_thrust_damage_type, ":damage_type", ":wielded_item"),
          (eq,":damage_type",blunt),
          (assign, ":cont", 1),
        (else_try),
          (this_or_next | eq, ":wielded_item", "itm_stones"),
          (eq, ":wielded_item", "itm_stones_siege"),
          (assign, ":cont", 1),
        (else_try),
          (this_or_next | eq, ":wielded_item", "itm_club_hard"),
          (eq, ":wielded_item", "itm_wooden_stick"),
          (assign, ":cont", 1),
        (try_end),
      (try_end),
      (eq, ":cont", 1),
      #(display_message, "@{!}TEST: complication_2_called and applied"),
  ]),
  
  # script_bandits_go_to_lairs
  # Having bandits go to lairs and back
  ("bandits_go_to_lairs",
    [(store_script_param, ":spawned_party", 1),
      (try_begin),
        # CONDITIONS
        (gt, ":spawned_party", "p_spawn_points_end"),
        (party_get_slot, ":home", ":spawned_party", slot_party_spawn_point),
        (is_between, ":home", spawn_points_begin, spawn_points_end),
        #(neg|party_slot_ge, ":spawned_party", slot_party_ai_embarking_port, 1),#is
        #already on the way to a port(embarking)
        (assign, ":continue", 1),
        (get_party_ai_object, ":ai_object", ":spawned_party"),
        (try_begin),
          (call_script, "script_cf_is_port_party", ":ai_object"),#is already on the way to a port(landing)
          (party_slot_eq, ":spawned_party", slot_party_on_water, 1),
          (assign, ":continue", 0),
        (try_end),
        (try_begin),
          (party_is_in_any_town, ":spawned_party"),
          (party_get_cur_town, ":cur_town", ":spawned_party"),
          (call_script, "script_cf_is_port_party", ":cur_town"),#is already in a port(waiting for landing)
          (assign, ":continue", 0),
        (try_end),
        (eq, ":continue", 1),
        
        # CONSEQUENCES
        (assign, ":spawn_point", ":home"),
        
        #if a lair exists, use that
        (party_get_slot, reg0, ":spawn_point", slot_party_lair_party),
        (try_begin),
          (gt, reg0, "p_spawn_points_end"),
          (assign, ":home", reg0),
        (try_end),
        
        #lost port parties (don't know why it happens, but it does)
        (try_begin),
          (gt, ":ai_object", -1),
          (party_slot_eq, ":spawned_party", slot_party_on_water, 1),
          (party_get_template_id, ":party_template_id", ":ai_object"),
          (eq, ":party_template_id", "pt_port"),
          (party_slot_eq, ":spawned_party", slot_party_ai_object_backup, -1),
          (party_set_ai_behavior, ":spawned_party", ai_bhvr_travel_to_party),
          (party_set_ai_object, ":spawned_party", ":home"),
          (party_set_slot, ":spawned_party", slot_party_ai_object, ":home"),
          #message
          (try_begin),
            (ge, "$vc_debug_mode", 1),
            (str_store_party_name, s1, ":spawned_party"),
            (assign, reg7, ":spawned_party"),
            (str_store_party_name, s7, ":ai_object"),
            (display_message, "@{!}INFO lost port party: spawned_party={s1}({reg7}) ai_object={s7}", color_bad_news),
          (end_try),
          
          #water parties that have wandered onto land
        (else_try),
          (is_between, ":spawn_point", "p_channel_spawn_point", "p_testing_spawn_point"),
          (party_slot_eq, ":spawned_party", slot_party_on_water, 0),
          (party_set_ai_behavior, ":spawned_party", ai_bhvr_travel_to_party),
          (party_set_ai_object, ":spawned_party", ":home"),
          (party_set_slot, ":spawned_party", slot_party_ai_object, ":home"),
          (try_begin),
            (ge, "$vc_debug_mode", 2),
            (str_store_party_name, s1, ":spawned_party"),
            (display_message, "@{!}INFO: {s1} is a water party that has wandered onto land and is heading back now"),
          (end_try),
          
          #land parties that have wandered onto water
        (else_try),
          (neg | is_between, ":spawn_point", "p_channel_spawn_point", "p_testing_spawn_point"),
          (party_slot_eq, ":spawned_party", slot_party_on_water, 1),
          (party_set_ai_behavior, ":spawned_party", ai_bhvr_travel_to_party),
          (party_set_ai_object, ":spawned_party", ":home"),
          (party_set_slot, ":spawned_party", slot_party_ai_object, ":home"),
          
          #If is far away, then head home
        (else_try),
          (store_distance_to_party_from_party, ":distance", ":spawned_party", ":home"),
          (gt, ":distance", 75),
          (party_set_ai_behavior, ":spawned_party", ai_bhvr_travel_to_party),
          (party_set_ai_object, ":spawned_party", ":home"),
          (party_set_slot, ":spawned_party", slot_party_ai_object, ":home"),
          
          #If home, then start patrolling again
        (else_try),
          (get_party_ai_behavior, ":behavior", ":spawned_party"),
          (this_or_next | lt, ":distance", 3),
          (party_is_in_any_town, ":spawned_party"),
          (eq, ":behavior", ai_bhvr_travel_to_party),
          (party_set_ai_behavior, ":spawned_party", ai_bhvr_patrol_party),
          (party_set_ai_object, ":spawned_party", ":home"),
          (party_set_slot, ":spawned_party", slot_party_ai_object, ":home"),
          # (party_set_ai_patrol_radius, ":spawned_party", 45), this trigger
          # effectively acts as a radius
          
          #catch new lairs and drop lost ones
        (else_try),
          (eq, ":behavior", ai_bhvr_patrol_party),
          (neq, ":ai_object", ":home"),
          (party_set_ai_object, ":spawned_party", ":home"),
          (party_set_slot, ":spawned_party", slot_party_ai_object, ":home"),
          
          #save game conversion
        (else_try),
          (eq, ":behavior", ai_bhvr_travel_to_point),
          (party_set_ai_behavior, ":spawned_party", ai_bhvr_travel_to_party),
          (party_set_ai_object, ":spawned_party", ":home"),
          (party_set_slot, ":spawned_party", slot_party_ai_object, ":home"),
        (try_end),
      (end_try),]),
  
  # script_cf_party_is_valid
  # Component: Ships and sea (map)
  # This script checks if a party is valid.
  ("cf_party_is_valid",
    [(store_script_param, ":tested_party", 1),
      (assign, ":continue", 0),
      (try_for_parties, ":cur_party"),
        (eq, ":cur_party", ":tested_party"),
        (assign, ":continue", 1),
      (try_end),
      (eq, ":continue", 1),]),
  
  # script_current_battle_size
  # Component: Helper
  ("current_battle_size", [(options_get_battle_size, reg0),
      (val_mul, reg0, 600),
      (val_div, reg0, 1000),
      (val_add, reg0, 150), #150-750, as in module.ini
  ]),
  
  # script_cf_additional_dog_conditions
  # Component: Dog
  # This script fixes VC-3126
  ("cf_additional_dog_conditions",
    [(assign, ":continue", 1),
      (try_begin),
        (check_quest_active, "qst_hunt_down_fugitive"),
        (neg | check_quest_succeeded, "qst_hunt_down_fugitive"),
        (neg | check_quest_failed, "qst_hunt_down_fugitive"),
        (quest_slot_eq, "qst_hunt_down_fugitive", slot_quest_target_center, "$current_town"),
        (assign, ":continue", 0),
      (else_try),
        (check_quest_active, "qst_blank_quest_10"),
        (neg | check_quest_succeeded, "qst_blank_quest_10"),
        (neg | check_quest_failed, "qst_blank_quest_10"),
        (quest_slot_eq, "qst_blank_quest_10", slot_quest_target_center, "$current_town"),
        (assign, ":continue", 0),
      (else_try),
        (check_quest_active, "qst_blank_quest_13"),
        (neg | check_quest_succeeded, "qst_blank_quest_13"),
        (neg | check_quest_failed, "qst_blank_quest_13"),
        (quest_slot_eq, "qst_blank_quest_13", slot_quest_target_center, "$current_town"),
        (assign, ":continue", 0),
      (try_end),
      (eq, ":continue", 1),]),
  
  # script_condition_check_merging_ships
  # Component: Ships and sea (map)
  # This script fixes VC-3126
  # Output: reg0
  ("condition_check_merging_ships",
    [(store_script_param, ":merge_party", 1),
      
      (assign, ":num_port_ships", 0),
      (try_for_range, ":cur_slot", slot_party_player_ships_type_begin, slot_party_player_ships_type_end),
        (party_slot_ge, ":merge_party", ":cur_slot", 1),
        (val_add, ":num_port_ships", 1),
      (end_try),
      (assign, ":num_party_ships", 0),
      (try_for_range, ":cur_slot", slot_party_player_ships_type_begin, slot_party_player_ships_type_end),
        (party_slot_ge, "p_main_party", ":cur_slot", 1),
        (val_add, ":num_party_ships", 1),
      (end_try),
      (store_add, ":num_ships_total", ":num_port_ships", ":num_party_ships"),
      (store_skill_level, ":cur_sea_king_skill", "skl_sea_king", "trp_player"),
      (val_mul, ":cur_sea_king_skill", 2),	#new skill adjust
      (val_min, ":cur_sea_king_skill", 7),	#new skill adjust
      (try_begin),
        # skill to low
        (gt, ":num_ships_total", ":cur_sea_king_skill"),
        (assign, reg0, 1),
      (else_try),
        # more then 7 ships (player shouldn't see this in theory)
        (gt, ":num_ships_total", 7),
        (assign, reg0, 2),
      (else_try),
        # merging possible
        (assign, reg0, 3),
      (end_try),]),
  
  # script_merge_ships_from_party_to_party
  # Component: Ships and sea (map)
  # This script fixes VC-3126
  ("merge_ships_from_party_to_party",
    [(store_script_param, ":origin_party", 1),
      (store_script_param, ":destination_party", 2),
      
      (try_for_range, ":cur_ship_type_slot", slot_party_player_ships_type_begin, slot_party_player_ships_type_end),
        (party_slot_ge, ":origin_party", ":cur_ship_type_slot", 1),
        (store_add, ":cur_ship_name_slot", ":cur_ship_type_slot", 10),
        (store_add, ":cur_ship_cond_slot", ":cur_ship_type_slot", 20),
        (store_add, ":cur_ship_prop_slot", ":cur_ship_type_slot", 30),
        #get ship
        (party_get_slot, ":cur_ship_type", ":origin_party", ":cur_ship_type_slot"),
        (party_get_slot, ":cur_ship_name", ":origin_party", ":cur_ship_name_slot"),
        (party_get_slot, ":cur_ship_cond", ":origin_party", ":cur_ship_cond_slot"),
        (party_get_slot, ":cur_ship_prop", ":origin_party", ":cur_ship_prop_slot"),
        #delete it from origin
        (party_set_slot, ":origin_party", ":cur_ship_type_slot", 0),
        (party_set_slot, ":origin_party", ":cur_ship_name_slot", 0),
        (party_set_slot, ":origin_party", ":cur_ship_cond_slot", 0),
        (party_set_slot, ":origin_party", ":cur_ship_prop_slot", 0),
        #give it to destination
        (assign, ":cur_transaction_complete", 0),
        (try_for_range, ":cur_ship_type_slot_2", slot_party_player_ships_type_begin, slot_party_player_ships_type_end),
          (eq, ":cur_transaction_complete", 0),
          (party_slot_eq, ":destination_party", ":cur_ship_type_slot_2", 0),
          (assign, ":cur_transaction_complete", 1),
          (store_add, ":cur_ship_name_slot_2", ":cur_ship_type_slot_2", 10),
          (store_add, ":cur_ship_cond_slot_2", ":cur_ship_type_slot_2", 20),
          (store_add, ":cur_ship_prop_slot_2", ":cur_ship_type_slot_2", 30),
          (party_set_slot, ":destination_party", ":cur_ship_type_slot_2", ":cur_ship_type"),
          (party_set_slot, ":destination_party", ":cur_ship_name_slot_2", ":cur_ship_name"),
          (party_set_slot, ":destination_party", ":cur_ship_cond_slot_2", ":cur_ship_cond"),
          (party_set_slot, ":destination_party", ":cur_ship_prop_slot_2", ":cur_ship_prop"),
        (end_try),
      (end_try),]),
  
  # script_center_data_logger
  ("center_data_logger", [(assign, reg4, 0),
      (assign, ":num_towns", 0),
      (try_for_range, ":town", villages_begin, villages_end),
        (str_store_party_name, s0, ":town"),
        (party_get_slot, reg0, ":town", slot_center_accumulated_rents),
        (party_get_slot, reg1, ":town", slot_center_accumulated_tariffs),
        (party_get_slot, reg2, ":town", slot_town_wealth),
        (party_get_slot, reg3, ":town", slot_town_prosperity),
        (display_message, "@{!}{s0} rents {reg0} tariffs {reg1} wealth {reg2} prosperity {reg3}"),
        (val_add, ":num_towns", 1),
        (val_add, reg4, reg3),
      (try_end),
      
      (val_div, reg4, ":num_towns"),
      (display_message, "@{!}Average village prosperity {reg4}"),
      
      (assign, reg4, 0),
      (assign, reg7, 0),
      (assign, reg12, 0),
      (assign, reg13, 0),
      (assign, ":num_towns", 0),
      (try_for_range, ":town", towns_begin, towns_end),
        (str_store_party_name, s0, ":town"),
        (display_message, "@{!}^{s0}^Bound Centers"),
        
        (try_for_range, ":village", villages_begin, villages_end),
          (party_slot_eq, ":village", slot_village_bound_center, ":town"),
          (str_store_party_name, s1, ":village"),
          (display_message, "@{!}{s1}"),
        (try_end),
        
        (display_message, "@{!}Food Supply"),
        (try_for_range, ":village", villages_begin, villages_end),
          (party_slot_eq, ":village", slot_village_market_town, ":town"),
          (str_store_party_name, s1, ":village"),
          (display_message, "@{!}{s1}"),
        (try_end),
        
        (call_script, "script_center_get_food_store_limit", ":town"),
        (assign, reg6, reg0),
        (party_get_slot, reg0, ":town", slot_center_accumulated_rents),
        (party_get_slot, reg1, ":town", slot_center_accumulated_tariffs),
        (party_get_slot, reg2, ":town", slot_town_wealth),
        (party_get_slot, reg3, ":town", slot_town_prosperity),
        (party_get_slot, reg5, ":town", slot_party_food_store),
        (val_mul, reg5, 100),
        (val_div, reg5, reg6),
        (party_get_slot, reg8, ":town", slot_town_farmer_visit_starts),
        (party_get_slot, reg9, ":town", slot_town_farmer_visits),
        (party_get_slot, reg10, ":town", slot_town_siege_count),
        (store_party_size_wo_prisoners, reg11, ":town"),
        (display_message, "@{!}rents {reg0} tariffs {reg1} wealth {reg2} prosperity {reg3} food {reg5} farmer starts {reg8} visits {reg9} sieges {reg10} garrison {reg11}"),
        (val_add, ":num_towns", 1),
        (val_add, reg4, reg3),
        (val_add, reg7, reg5),
        (val_add, reg12, reg11),
        (val_add, reg13, reg9),
      (try_end),
      
      (val_div, reg4, ":num_towns"),
      (val_div, reg7, ":num_towns"),
      (val_div, reg12, ":num_towns"),
      (val_div, reg13, ":num_towns"),
      (display_message, "@{!}Average town garrison {reg12} prosperity {reg4} visits {reg13} food {reg7} percent"),
      
      (assign, reg4, 0),
      (assign, reg7, 0),
      (assign, reg12, 0),
      (assign, reg13, 0),
      (assign, ":num_towns", 0),
      (try_for_range, ":town", castles_begin, castles_end),
        (str_store_party_name, s0, ":town"),
        (display_message, "@{!}^{s0}^Bound Centers"),
        
        (try_for_range, ":village", villages_begin, villages_end),
          (party_slot_eq, ":village", slot_village_bound_center, ":town"),
          (str_store_party_name, s1, ":village"),
          (display_message, "@{!}{s1}"),
        (try_end),
        
        (display_message, "@{!}Food Supply"),
        (try_for_range, ":village", villages_begin, villages_end),
          (party_slot_eq, ":village", slot_village_market_town, ":town"),
          (str_store_party_name, s1, ":village"),
          (display_message, "@{!}{s1}"),
        (try_end),
        
        (call_script, "script_center_get_food_store_limit", ":town"),
        (assign, reg6, reg0),
        (party_get_slot, reg0, ":town", slot_center_accumulated_rents),
        (party_get_slot, reg1, ":town", slot_center_accumulated_tariffs),
        (party_get_slot, reg2, ":town", slot_town_wealth),
        (party_get_slot, reg3, ":town", slot_town_prosperity),
        (party_get_slot, reg5, ":town", slot_party_food_store),
        (val_mul, reg5, 100),
        (val_div, reg5, reg6),
        (party_get_slot, reg8, ":town", slot_town_farmer_visit_starts),
        (party_get_slot, reg9, ":town", slot_town_farmer_visits),
        (party_get_slot, reg10, ":town", slot_town_siege_count),
        (store_party_size_wo_prisoners, reg11, ":town"),
        (display_message, "@{!}rents {reg0} tariffs {reg1} wealth {reg2} prosperity {reg3} food {reg5} farmer starts {reg8} visits {reg9} sieges {reg10} garrison {reg11}"),
        (val_add, ":num_towns", 1),
        (val_add, reg4, reg3),
        (val_add, reg7, reg5),
        (val_add, reg12, reg11),
        (val_add, reg13, reg9),
      (try_end),
      
      (val_div, reg4, ":num_towns"),
      (val_div, reg7, ":num_towns"),
      (val_div, reg12, ":num_towns"),
      (val_div, reg13, ":num_towns"),
      (display_message, "@{!}Average castle garrison {reg12} prosperity {reg4} visits {reg13} food {reg7} percent"),]),
  
  # script_simulate_battle_with_parties
  # Input: fraction of player party that is at risk
  #        enemy party
  #        fraction of enemy party that is at risk
  #        ally party
  #        flag non-zero to skip player damage
  # Output: strings s8, s9, s10 with casualties
  # Note: player is assumed to be absent
  ("simulate_battle_with_parties", [
      (store_script_param, ":fraction_player_party_at_risk", 1),
      (store_script_param, ":enemy_party", 2),
      (store_script_param, ":fraction_enemy_party_at_risk", 3),
      (store_script_param, ":ally_party", 4),
      (store_script_param, ":spare_player", 5),
      
      (call_script, "script_encounter_calculate_fit"),
      
      (try_begin),
        (le, ":fraction_enemy_party_at_risk", 0),
        (display_message, "@{!}script_simulate_battle_with_parties: requires non-zero denominator"),
        (assign, ":player_party_strength", 0),
        (assign, ":player_and_allies_strength", 0),
      (else_try),
        (call_script, "script_party_calculate_strength", "p_main_party", 1), #skip player
        (assign, ":player_party_strength", reg0),
        (val_div, ":player_party_strength", ":fraction_enemy_party_at_risk"),
        
        (try_begin),
          (le, ":ally_party", 0),
          (try_begin),
            (eq, ":spare_player", 0),
            (assign, ":player_and_allies_strength", ":player_party_strength"),
          (else_try),
            (assign, ":player_and_allies_strength", 0),
          (try_end),
        (else_try),
          (call_script, "script_party_calculate_strength", "p_collective_friends", 1), #skip player
          (assign, ":player_and_allies_strength", reg0),
          (val_div, ":player_and_allies_strength", ":fraction_enemy_party_at_risk"),
          (try_begin),
            (neq, ":spare_player", 0),
            (val_sub, ":player_and_allies_strength", ":player_party_strength"),
          (try_end),
        (try_end),
        
        (store_mul, "$g_strength_contribution_of_player", ":player_party_strength", 100),
        (try_begin),
          (gt, ":player_and_allies_strength", 0),
          (val_div, "$g_strength_contribution_of_player", ":player_and_allies_strength"),
        (else_try),
          (assign, "$g_strength_contribution_of_player", 100),
        (try_end),
      (try_end),
      
      (store_mul, ":min_orig_strength", ":player_and_allies_strength", ":fraction_enemy_party_at_risk"),
      
      (try_begin),
        (le, ":fraction_player_party_at_risk", 0),
        (display_message, "@{!}script_simulate_battle_with_parties: requires non-zero denominator"),
        (assign, ":enemy_party_strength", 0),
      (else_try),
        (call_script, "script_party_calculate_strength", "p_collective_enemy", 0),
        
        (try_begin),
          (gt, ":min_orig_strength", reg0),
          (assign, ":min_orig_strength", reg0),
        (try_end),
        
        (assign, ":enemy_party_strength", reg0),
        (val_div, ":enemy_party_strength", ":fraction_player_party_at_risk"),
      (try_end),
      
      #ensure not all of any army dies (if one party is much larger than other,
      #could wipe out smaller party)
      (val_div, ":min_orig_strength", 3),
      (val_max, ":min_orig_strength", 1),
      
      (try_begin),
        (gt, ":player_and_allies_strength", ":enemy_party_strength"),
        (store_div, ":adjustment", ":player_and_allies_strength", ":min_orig_strength"),
      (else_try),
        (store_div, ":adjustment", ":enemy_party_strength", ":min_orig_strength"),
      (try_end),
      
      (try_begin),
        (is_between, ":adjustment", 2, 60), #don't bother past 20-to-1 odds
        (store_div, reg1, ":enemy_party_strength", ":adjustment"),
        (store_div, reg2, ":player_and_allies_strength", ":adjustment"),
        (lt, ":adjustment", reg1),
        (lt, ":adjustment", reg2),
        (val_div, ":player_party_strength", ":adjustment"),
        (assign, ":player_and_allies_strength", reg2),
        (assign, ":enemy_party_strength", reg1),
      (try_end),
      
      #apportion casualties between player and allies
      (try_begin),
        (le, ":player_and_allies_strength", 0),
        (assign, ":enemy_party_strength_for_p", 0),
      (else_try),
        (store_mul, ":enemy_party_strength_for_p", ":enemy_party_strength", ":player_party_strength"),
        (val_div, ":enemy_party_strength_for_p", ":player_and_allies_strength"),
      (try_end),
      
      #inflict casualties
      (try_begin),
        (gt, ":enemy_party_strength_for_p", 0),
        (eq, ":spare_player", 0),
        (gt, ":player_party_strength", 0),
        (inflict_casualties_to_party_group, "p_main_party", ":enemy_party_strength_for_p", "p_temp_casualties"),
        (call_script, "script_print_casualties_to_s0", "p_temp_casualties", 0),
        (str_store_string_reg, s8, s0),
      (else_try),
        (str_clear, s8),
      (try_end),
      
      (try_begin),
        (gt, ":player_and_allies_strength", 0),
        (gt, ":enemy_party", 0),
        (gt, ":enemy_party_strength", 0),
        (inflict_casualties_to_party_group, ":enemy_party", ":player_and_allies_strength", "p_temp_casualties"),
        
        #copy casualties to p_total_enemy_casualties
        #ozan begin
        (party_get_num_companion_stacks, ":num_stacks", "p_temp_casualties"),
        (try_for_range, ":stack_no", 0, ":num_stacks"),
          (party_stack_get_troop_id, ":stack_troop", "p_temp_casualties", ":stack_no"),
          (try_begin),
            (party_stack_get_size, ":stack_size", "p_temp_casualties", ":stack_no"),
            (gt, ":stack_size", 0),
            (party_add_members, "p_total_enemy_casualties", ":stack_troop", ":stack_size"), #addition_to_p_total_enemy_casualties
            (party_stack_get_num_wounded, ":stack_wounded_size", "p_temp_casualties", ":stack_no"),
            (gt, ":stack_wounded_size", 0),
            (party_wound_members, "p_total_enemy_casualties", ":stack_troop", ":stack_wounded_size"),
          (try_end),
        (try_end),
        #ozan end
        
        (call_script, "script_print_casualties_to_s0", "p_temp_casualties", 0),
        (str_store_string_reg, s10, s0),
      (else_try),
        (str_clear, s10),
      (try_end),
      
      (party_collect_attachments_to_party, ":enemy_party", "p_collective_enemy"),
      (val_sub, ":enemy_party_strength", ":enemy_party_strength_for_p"),
      
      (try_begin),
        (gt, ":enemy_party_strength", 0),
        (gt, ":ally_party", 0),
        (gt, ":player_and_allies_strength", 0),
        (inflict_casualties_to_party_group, ":ally_party", ":enemy_party_strength", "p_temp_casualties"),
        (call_script, "script_print_casualties_to_s0", "p_temp_casualties", 0),
        (str_store_string_reg, s9, s0),
      (else_try),
        (str_clear, s9),
      (try_end),
      
      (call_script, "script_collect_friendly_parties"),
  ]),
  
  # script_sell_prisoners
  # JuJu70
  # Adventurers sell prisoners when in town
  ("sell_prisoners",
    [(store_script_param_1, ":troop_no"),
      (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
      (party_get_num_prisoner_stacks, ":num_stacks",":party_no"),
      (try_begin),
        (ge,":num_stacks",0),
        (assign, ":total_value", 0),
        (try_for_range_backwards, ":stack_no", 0, ":num_stacks"),
          (party_prisoner_stack_get_troop_id, ":stack_troop",":party_no",":stack_no"),
          (neg|troop_is_hero, ":stack_troop"),
          (store_character_level, ":troop_level",":stack_troop"),
          (assign, ":ransom_amount", ":troop_level"),
          (val_add, ":ransom_amount", 10),
          (val_mul, ":ransom_amount", ":ransom_amount"),
          (val_div, ":ransom_amount", 6),
          (party_prisoner_stack_get_size, ":stack_size",":party_no",":stack_no"),
          (val_mul,":ransom_amount",":stack_size"),
          (val_add,":total_value",":ransom_amount"),
          (party_remove_prisoners,":party_no",":stack_troop",":stack_size"),
        (try_end),
      (try_end),
      (troop_get_slot, ":wealth", ":troop_no", slot_troop_wealth),
      #(assign, reg24, ":wealth"),
      (val_add, ":wealth", ":total_value"),
      #(assign, reg25, ":wealth"),
      (troop_set_slot, ":troop_no", slot_troop_wealth, ":wealth"),
      #(display_message, "@Had {reg24} before and have {reg25} now"),
  ]),
  
  # script_equip_new_noble
  # JuJu70
  # Equip an adventurer when he becomes a noble
  ("equip_new_noble",
    [	(store_script_param_1, ":troop_no"),
      #(troop_clear_inventory, ":troop_no"),
      (troop_get_slot, ":religion",":troop_no", slot_troop_religion),
      (try_begin),
        (eq, ":religion", 1),
        (store_random_in_range, ":new_item1", "itm_noble_sword_7", "itm_irish_long_sword1"),
      (else_try),
        (store_random_in_range, ":new_item1", "itm_noble_sword", "itm_old_swordv"),
      (try_end),
      (troop_add_item, ":troop_no", ":new_item1"),
      (store_random_in_range, ":new_item2", "itm_heavy_spear3", "itm_new_mace"),
      (troop_add_item, ":troop_no", ":new_item2"),
      (troop_add_item, ":troop_no", "itm_javelin_skirmishesel"),
      (store_random_in_range, ":new_item3", "itm_tab_shield_round_01_device", "itm_tab_shield_small_round_c"),
      (troop_add_item, ":troop_no", ":new_item3"),
      #Head armor
      (troop_get_inventory_slot,":cur_item1",":troop_no",ek_head),
      (try_begin),
        (gt, ":cur_item1", 0),
        (item_get_head_armor , ":armor", ":cur_item1"),
        (le, ":armor", 40),
        (try_begin),
          (eq, ":religion", 1),
          (store_random_in_range, ":helmet1", "itm_briton_helm35", "itm_briton_helm38"),
        (else_try),
          (store_random_in_range, ":helmet1", "itm_viking_helm21", "itm_vikingold_elitehelm1"),
        (try_end),
        (troop_remove_item,":troop_no", ":cur_item1"),
        (troop_add_item, ":troop_no", ":helmet1"),
      (else_try),
        (le, ":cur_item1", 0),
        (try_begin),
          (eq, ":religion", 1),
          (store_random_in_range, ":helmet1", "itm_briton_helm35", "itm_briton_helm38"),
        (else_try),
          (store_random_in_range, ":helmet1", "itm_viking_helm21", "itm_vikingold_elitehelm1"),
        (try_end),
        (troop_add_item, ":troop_no", ":helmet1"),
      (try_end),
      #Body armor
      (troop_get_inventory_slot,":cur_item1",":troop_no",ek_body),
      (try_begin),
        (gt, ":cur_item1", 0),
        (item_get_body_armor, ":armor", ":cur_item1"),
        (le, ":armor", 40),
        (store_random_in_range, ":mail1", "itm_byrnie11", "itm_orm_byrnie"),
        (troop_remove_item,":troop_no", ":cur_item1"),
        (troop_add_item, ":troop_no", ":mail1"),
      (else_try),
        (le, ":cur_item1", 0),
        (store_random_in_range, ":mail1", "itm_byrnie11", "itm_orm_byrnie"),
        (troop_add_item, ":troop_no", ":mail1"),
      (try_end),
      #Leg armor
      (troop_get_inventory_slot,":cur_item1",":troop_no",ek_foot),
      (try_begin),
        (gt, ":cur_item1", 0),
        (item_get_leg_armor , ":armor", ":cur_item1"),
        (le, ":armor", 24),
        (store_random_in_range, ":shoe", "itm_carbatinae_11qs", "itm_bare_foot_man"),
        (troop_remove_item,":troop_no", ":cur_item1"),
        (troop_add_item, ":troop_no", ":shoe"),
      (else_try),
        (le, ":cur_item1", 0),
        (store_random_in_range, ":shoe", "itm_carbatinae_11qs", "itm_bare_foot_man"),
        (troop_add_item, ":troop_no", ":shoe"),
      (try_end),
      (troop_add_item, ":troop_no", "itm_leather_gloves"),
      #Horse
      (troop_get_inventory_slot,":cur_item2",":troop_no",ek_horse),
      (try_begin),
        (le, ":cur_item2", 0),
        (store_random_in_range, ":new_horse", "itm_common_horse", "itm_cow1"),
        (troop_add_item, ":troop_no", ":new_horse"),
      (try_end),
      (troop_equip_items, ":troop_no"),
      (store_skill_level, ":leader", "skl_leadership", ":troop_no"),
      (store_sub, ":diff", ":leader", 5),
      (try_begin),
        (gt, ":diff", 0),
      (else_try),
        (val_mul, ":diff", -1),
        (troop_raise_skill, ":troop_no", "skl_leadership", ":diff"),
      (try_end),
  ]),
  
  # script_cf_troop_religion_matches_players
  # Input: troop ID
  ("cf_troop_religion_matches_players", [
      (store_script_param_1, ":stack_troop"),
      (assign, ":ret_val", 1),
      
      (try_begin),
        (is_between, ":stack_troop", companions_begin, companions_end),
        (troop_get_slot, ":religion", ":stack_troop", slot_troop_religion),
        (try_begin),
          (eq, "$g_player_faith", 2), #player es pagano
          (eq,":religion", 1),
          (assign, ":ret_val", 0),
        (else_try),
          (eq, "$g_player_faith", 1), #player es cristiano
          (eq,":religion", 2),
          (assign, ":ret_val", 0),
        (try_end),
        
        #regular troops
      (else_try),
        (eq, "$g_player_faith", 2), #player es pagano, problemas con tropas cristianas
        (this_or_next | is_between, ":stack_troop", christian_troops_begin, christian_troops_end),
        (this_or_next | is_between, ":stack_troop", christian_troops2_begin, christian_troops2_end),
        (this_or_next | is_between, ":stack_troop", christian_troops3_begin, christian_troops3_end),
        (is_between, ":stack_troop", christian_troops4_begin, christian_troops4_end),
        (assign, ":ret_val", 0),
      (else_try),
        (eq, "$g_player_faith", 1), #player es cristiano
        (this_or_next | is_between, ":stack_troop", pagan_troops_begin, pagan_troops_end),
        (this_or_next | is_between, ":stack_troop", pagan_troops2_begin, pagan_troops2_end),
        (is_between, ":stack_troop", pagan_troops3_begin, pagan_troops3_end),
        (assign, ":ret_val", 0),
      (try_end),
      
      (eq, ":ret_val", 1),
  ]),
  
  # script_cf_process_training_fail
  # Input: training center, upgrade path
  # Output: none
  ("cf_process_training_fail", [
    (store_script_param_1, ":party_no"),
    (store_script_param_2, ":path"),
      
    (assign, ":num_troops_to_train", 0),
    (store_skill_level, ":trainer_level", skl_trainer, "trp_player"),
    
    (try_begin),
      (le, ":trainer_level", 1),
      (store_add, ":num_troops_to_train", ":trainer_level", 4),
    (else_try),
      (store_add, ":num_troops_to_train", ":trainer_level", 3),
    (try_end),
    
    (try_begin),
      (store_attribute_level, ":int", "trp_player", ca_intelligence),
      (ge, ":int", 18),
      (val_add, ":num_troops_to_train", 1),
    (try_end),
    
    (party_get_num_companion_stacks, ":num_stacks", ":party_no"),
    
    (assign, ":trained", 0),
    (try_for_range_backwards, ":i_stack", 0, ":num_stacks"),    #MOTO go from bottom of stack (which player can easily manipulate)
      (eq, ":trained", 0),
      (party_stack_get_troop_id, ":troop_id", ":party_no", ":i_stack"),
      (neg|troop_is_hero, ":troop_id"),
      
      (troop_get_upgrade_troop, ":upgrade_troop", ":troop_id" , ":path"),
      (try_begin),
        (le, ":upgrade_troop", 0),
        (troop_get_upgrade_troop, ":upgrade_troop", ":troop_id", 0),
      (try_end),
      
      #only proceed if troop is upgradable
      (gt, ":upgrade_troop", 0),
      
      (store_character_level, ":troop_level", ":troop_id"),
      #only proceed if troop level is basic
      (le, ":troop_level", AI_Poor_Troop_Level-4),  #train up to L3
      
      (try_begin),
        (le, ":troop_level", 12), #chief cambia MOTO change from 22 to 15 (level of recruits)
        (val_add, ":num_troops_to_train", 2), #more recruits are trained during improved training
      (try_end),
    
      (str_store_party_name, s10, ":party_no"),
      (party_count_members_of_type,":cur_number",":party_no",":troop_id"),
      (val_min, ":num_troops_to_train", ":cur_number"),
      
      (call_script, "script_game_get_upgrade_cost", ":troop_id"),
      (val_add, reg0, 10),    #MOTO per upgrade
      
      (store_mul, ":upgrade_cost", ":num_troops_to_train", reg0),    #MOTO make PER upgrade
      
      (store_troop_gold, ":gold", "trp_player"),
      (try_begin),
        (lt, ":gold", ":upgrade_cost"),
        (store_div, ":money_limit", ":gold", reg0),
        (val_min, ":num_troops_to_train", ":money_limit"),
        (store_mul, ":upgrade_cost", ":num_troops_to_train", reg0),
        (display_message, "str_you_dont_have_enough_money_to_continue_training_troops_at_s10"),
      (try_end),
      
      (gt, ":num_troops_to_train", 0),    #MOTO avoid needless processing
      
      (party_remove_members,":party_no",":troop_id",":num_troops_to_train"),
      (party_add_members, ":party_no", ":upgrade_troop", ":num_troops_to_train"),
      
      (troop_remove_gold,"trp_player",":upgrade_cost"),
      
      (assign, reg5, ":num_troops_to_train"),
      (store_sub, reg6, reg5, 1),
      (str_store_troop_name, s6, ":troop_id"),
      (str_store_troop_name, s7, ":upgrade_troop"),
      (str_store_troop_name_plural, s8, ":troop_id"),
      (str_store_troop_name_plural, s9, ":upgrade_troop"),
      (str_store_string, s11, "str_at_s10_has_promoted_reg5_reg6s8_to_s9s6_to_s7"),
      (display_message, "@The trainer {s11}."),
      (assign, ":trained", 1),
    (try_end),
    
    (eq, ":trained", 0), #no one left to train?
  ]),
  
  # script_make_lair
  # Input: none
  # Output: none
  ("make_lair", [
    (party_relocate_near_party, "p_yourlair", "p_main_party"),
    (call_script, "script_give_center_to_lord", "p_yourlair", "trp_player", 0),
    (enable_party, "p_yourlair"),
    (assign, "$lair_on", 1),
    (party_set_slot, "p_yourlair", slot_lair_improve, 0),
    #personal
    (assign, "$captain_ok", hire_captainno),
    (assign, "$hire_priest1", hire_priest3),
    (assign, "$bard_type", hire_bard3),
    (assign, "$hire_tavernkeeper1", hire_tavernkeeper2),
    (assign, "$hire_whore1", hire_whore5),
    (assign, "$hire_trainer1", hire_trainer2),
    (assign, "$hire_barber1", hire_barber5),
    (assign, "$hire_smith1", hire_smith2),
    (assign, "$hire_armorer1", hire_armorer2),
    (assign, "$hire_cook1", hire_cook2),
    ###no hired
    (assign, "$captain_hired_on", 0), # no contratado chief, hired
    (assign, "$priest_hired_on", 0), #contratado chief, hired
    (assign, "$bard_hired_on", 0), # no contratado chief, hired
    (assign, "$tavernkeeper_hired_on", 0), # no contratado chief, hired
    (assign, "$whore_hired_on", 0), #contratado chief, hired
    (assign, "$trainer_hired_on", 0), #contratado chief, hired
    
    (assign, "$smith_hired_on", 0), #contratado chief, hired
    (assign, "$armorer_hired_on", 0), #contratado chief, hired
    (assign, "$barber_hired_on", 0), #contratado chief, hired
    (assign, "$cook_hired_on", 0), #contratado chief, hired
  ]),
  
  # script_player_set_faction_relation_ceiling
  # Input: faction
  # Output: none
  # VC-3960 add consistency; originally in script_diplomacy_start_war_between_kingdoms
  ("player_set_faction_relation_ceiling", [
    (store_script_param, ":faction_no", 1),
    (store_script_param, ":ceiling", 2),
    (store_script_param, ":notify", 3),
      
    (store_relation, ":relation", "fac_player_faction", ":faction_no"),
    
    (try_begin),
      (this_or_next|neq, ":notify", 0),
      (eq, "$cheat_mode", 1),
      
      (store_sub, ":difference", ":ceiling", ":relation"),
      (try_begin),
        (lt, ":difference", 0),
        (call_script, "script_change_player_relation_with_faction", ":faction_no", ":difference"),
      (try_end),
      
    (else_try),
      (val_min, ":relation", ":ceiling"),
      (call_script, "script_set_player_relation_with_faction", ":faction_no", ":relation"),
    (try_end),
  ]),
  
  # script_give_troop_next_banner
  # Input: none
  # Output: reg0 contains banner prop ID
  ("give_troop_next_banner", [
    (store_script_param, ":troop_no", 1),
    
    (assign, ":cur_banner_prop", "spr_banner_default"),
    
    (assign, ":end_cond", banner_scene_props_end),
    (try_for_range, reg0, banner_scene_props_begin, ":end_cond"),
      (assign, ":end_loop", active_npcs_end),
      (try_for_range, ":kingdom_hero", active_npcs_including_player_begin, ":end_loop"),
        (try_begin),
          (eq, ":kingdom_hero", "trp_kingdom_heroes_including_player_begin"),
          (assign, ":kingdom_hero", "trp_player"),
        (try_end),
        
        (troop_slot_eq, ":kingdom_hero", slot_troop_banner_scene_prop, reg0),
        (assign, ":end_loop", 0),
      (try_end),
      
      (eq, ":end_loop", active_npcs_end), #no one has this banner?
      (assign, ":cur_banner_prop", reg0),
      (assign, ":end_cond", 0), #break
    (try_end),
    
    (troop_set_slot, ":troop_no", slot_troop_banner_scene_prop, ":cur_banner_prop"),
    
    (troop_get_slot, ":cur_party", ":troop_no", slot_troop_leaded_party),
    (try_begin),
      (gt, ":cur_party", 0),
      (store_sub, ":cur_banner_icon", ":cur_banner_prop", banner_scene_props_begin),
      (val_add, ":cur_banner_icon", banner_map_icons_begin),
      (party_set_banner_icon, ":cur_party", ":cur_banner_icon"),
    (try_end),
    
    (assign, reg0, ":cur_banner_prop"),
  ]),
]




vc_sea_scripts = [
  
  # script_map_sea_ai_1
  # Component: Ships and sea (map)
  # This script is called every 33.3h in a simple trigger
  # it is fixing 06.01.15 problem in VC-1307 and VC-2123
  ("map_sea_ai_1",
    [(try_for_parties, ":party_no"),
        (party_slot_eq, ":party_no", slot_party_on_water, 0),
        (gt, ":party_no", "p_spawn_points_end"),
        #scenario : ai_object = port party
        (party_get_slot, ":ai_object", ":party_no", slot_party_ai_object),
        
        #VC-2123 begins (NEW!!!)
        (try_begin),
          (get_party_ai_object, ":real_ai_object", ":party_no"),
          (gt, ":real_ai_object", 0),
          (neq, ":real_ai_object", ":ai_object"),
          (try_begin),
            (is_between, ":ai_object", centers_begin, centers_end),
            # Error message:
            (try_begin),
              (ge, "$vc_debug_mode", 2),
              (str_store_party_name, s1, ":real_ai_object"),
              (str_store_party_name, s2, ":ai_object"),
              (str_store_party_name, s7, ":party_no"),
              (display_message, "@{!}ERROR: In AI of {s7}: real_ai_object = {s1}, slot_ai_object = {s2}", color_terrible_news),
            (end_try),
            # Fix:
            (neg | party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
            (party_set_ai_object, ":party_no", ":ai_object"),
          (else_try),
            (eq, ":ai_object", -1),
            (call_script, "script_cf_is_port_party", ":real_ai_object"),
            # Error message:
            (try_begin),
              (ge, "$vc_debug_mode", 2),
              (str_store_party_name, s1, ":real_ai_object"),
              (str_store_party_name, s7, ":party_no"),
              (display_message, "@{!}ERROR: In AI of {s7}: real_ai_object = {s1}, slot_ai_object = -1", color_terrible_news),
            (end_try),
            # Fix:
            (neg | party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
            (party_set_slot, ":party_no", slot_party_ai_object, ":real_ai_object"),
            (assign, ":ai_object", ":real_ai_object"),
          (end_try),
        (end_try),
        #VC-2123 ends
        
        # (get_party_ai_object, ":ai_object", ":party_no"), #try fixing VC-1850 (wont
        # fix)
        (get_party_ai_behavior, ":ai_behavior", ":party_no"),
        (neq, ":ai_behavior", ai_bhvr_hold),
        (neq, ":ai_behavior", ai_bhvr_patrol_location),
        (neq, ":ai_behavior", ai_bhvr_travel_to_point),
        (call_script, "script_cf_is_port_party", ":ai_object"),
        
        (try_begin),
          #avoiding problems:
          (neg | party_slot_ge, ":party_no", slot_party_ai_object_backup, 0),
          (party_get_slot, ":port_town", ":ai_object", slot_party_port_party),
          #(is_between, ":port_town", towns_begin, towns_end),
          (gt, ":port_town", 0),
          (party_set_slot, ":party_no", slot_party_ai_object_backup, ":port_town"),
        (end_try),
        (party_get_slot, ":ai_object", ":party_no", slot_party_ai_object_backup),
        (party_get_slot, ":ai_state", ":party_no", slot_party_ai_state_backup),
        (party_get_slot, ":ai_behavior", ":party_no", slot_party_ai_behavior_backup),
        (party_set_slot, ":party_no", slot_party_ai_state_backup, -1),
        (party_set_slot, ":party_no", slot_party_ai_object_backup, -1),
        (this_or_next | is_between, ":ai_object", spawn_points_begin, spawn_points_end),
        (party_is_active, ":ai_object"),
        (try_begin),
          (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
          (call_script, "script_party_set_ai_state", ":party_no", ":ai_state", ":ai_object"),
        (else_try),
          (le, ":ai_behavior", 0),
          (ge, "$cheat_mode", 1),
          (party_get_template_id, reg1, ":party_no"),
          (display_message, "@{!}trait trigger: bad backup behavior for party template {reg1}"),
        (else_try),
          #(party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
          (party_set_ai_behavior, ":party_no", ":ai_behavior"),
          (party_set_ai_object, ":party_no", ":ai_object"),
          (party_set_flags, ":party_no", pf_default_behavior, 0),
          (party_set_slot, ":party_no", slot_party_ai_state, ":ai_state"),
          (party_set_slot, ":party_no", slot_party_ai_object, ":ai_object"),
        (try_end),
      (end_try),]),
  
  # script_map_sea_ai_2
  # Component: Ships and sea (map)
  # This script is called every 0.7h in a simple trigger
  # it is the new system for embarking parties (VC-1537)
  ("map_sea_ai_2",
    [# (try_begin),
      # (ge, "$cheat_mode", 1),
      # (display_message, "@new AI landing trigger fire!"),
      # (end_try),
      
      (try_for_parties, ":party_no"),
        
        # 1.GENERAL CONDITIONS
        (gt, ":party_no", "p_spawn_points_end"),
        (party_get_template_id, ":party_template_id", ":party_no"),
        (neq, ":party_template_id", "pt_port"),
        (neq, ":party_template_id", "pt_landet_ships"),
        
        (try_begin),
          
          # 2.REACTIVATE STUCKING PARTIES (fixing VC-2139)
          (try_begin),
            (assign, ":party_stuck_in_port_party", 0),
            (party_is_in_any_town, ":party_no"),
            (party_get_cur_town, ":cur_center", ":party_no"),
            (neg | is_between, ":cur_center", centers_begin, centers_end),
            (party_slot_ge, ":cur_center",slot_party_port_party, 1),	# it has a corresponding party
            (assign, ":party_stuck_in_port_party", 1),
            # message:
            (try_begin),
              (ge, "$vc_debug_mode", 2),
              (party_get_slot, reg7, ":party_no", slot_party_on_water),
              (str_store_party_name, s1, ":cur_center"),
              (str_store_party_name, s7, ":party_no"),
              (display_message, "@{!}INFO: {s7} is in {s1}, on water = {reg7}"),
            (end_try),
            (party_slot_eq, ":party_no", slot_party_on_water, 0),
            (party_set_slot, ":party_no", slot_party_on_water, 1),
          (end_try),
          
          # 3.PARTY CONDITIONS
          (this_or_next | eq, ":party_stuck_in_port_party", 1),
          (party_is_active, ":party_no"),
          (party_slot_eq, ":party_no", slot_party_on_water, 1),
          
          # 4.DO NOT LAND:
          (party_get_template_id, ":party_template_id", ":party_no"),
          (neq, ":party_template_id", "pt_fisher_ship"),
          (neq, ":party_template_id", "pt_traveller_ship"),
          (neq, ":party_template_id", "pt_slave_trader_ship"),
          (neq, ":party_template_id", "pt_sea_king_danish"),
          (neq, ":party_template_id", "pt_sea_king_norweg"),
          (neq, ":party_template_id", "pt_walrus_hunter"),
          (neq, ":party_no", "p_main_party"),
          
          # 5.PARTY POSITIONS CONDITIONS
          (get_party_ai_object, ":ai_object", ":party_no"),
          (call_script, "script_cf_is_port_party", ":ai_object"),
          (store_distance_to_party_from_party, ":distance", ":party_no", ":ai_object"),
          (le, ":distance", 3),
          
          # 6.NEW POSITION
          (party_get_slot, ":port_town", ":ai_object", slot_party_port_party),
          (party_get_position, pos3, ":port_town"),
          (map_get_land_position_around_position, pos4, pos3, 1),
          (party_set_position, ":party_no", pos4),
          
          # 7.NEW PROPERTIES
          (party_set_slot, ":party_no", slot_party_on_water, 0),
          (party_set_flags, ":party_no", pf_is_ship, 0),
          (call_script, "script_update_party_icon", ":party_no"),
          (party_get_slot, ":ai_state", ":party_no", slot_party_ai_state_backup),
          (party_get_slot, ":ai_object", ":party_no", slot_party_ai_object_backup),
          (party_get_slot, ":ai_behavior", ":party_no", slot_party_ai_behavior_backup),
          (party_set_slot, ":party_no", slot_party_ai_state_backup, -1),
          (party_set_slot, ":party_no", slot_party_ai_object_backup, -1),
          
          # 8.NEW BEHAVIOUR
          (ge, ":ai_object", 0),
          (call_script, "script_cf_party_is_valid", ":ai_object"),#fix for VC-2699
          (party_get_template_id, ":party_template", ":ai_object"),#fix for VC-2382: spawnpoints and lairs aint "active"
          (this_or_next | is_between, ":party_template", "pt_steppe_bandit_lair", "pt_bandit_lair_templates_end"),
          (this_or_next | is_between, ":ai_object", centers_begin, centers_end),	#VC-2123 (Towns aint active as long as player is in water)
          (this_or_next | is_between, ":ai_object", spawn_points_begin, spawn_points_end),
          (party_is_active, ":ai_object"),
          (try_begin),
            (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
            (call_script, "script_party_set_ai_state", ":party_no", ":ai_state", ":ai_object"),
          (else_try),
            (le, ":ai_behavior", 0),
            (ge, "$vc_debug_mode", 2),
            (party_get_template_id, reg1, ":party_no"),
            (display_message, "@{!}auto travel trigger: bad backup behavior for party template {reg1}"),
            (eq, 1, 0),
          (else_try),
            #(party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
            (party_set_ai_behavior, ":party_no", ":ai_behavior"),
            (party_set_ai_object, ":party_no", ":ai_object"),
            (party_set_flags, ":party_no", pf_default_behavior, 0),
            (party_set_slot, ":party_no", slot_party_ai_state, ":ai_state"),
            (party_set_slot, ":party_no", slot_party_ai_object, ":ai_object"),
          (try_end),
          
        (else_try),
          # 9.  SPECIAL SITUATION LORD (VC-1679: system for following a marshal who is
          # switching into water)
          
          #conditions
          (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
          (party_slot_eq, ":party_no", slot_party_on_water, 0),
          (party_get_slot, ":embarking_port", ":party_no", slot_party_ai_embarking_port),
          (gt, ":embarking_port", 0),
          (get_party_ai_behavior, ":ai_behavior", ":party_no"),
          (eq, ":ai_behavior", ai_bhvr_travel_to_point),
          (party_get_position, pos1, ":party_no"),
          (party_get_position, pos2, ":embarking_port"),
          (get_distance_between_positions, ":distance", pos1, pos2),
          (le, ":distance", 150),
          
          #consequences
          (party_get_slot, ":port_party", ":embarking_port", slot_party_port_party),
          (party_get_position, pos3, ":port_party"),
          (party_set_position, ":party_no", pos3),	#maybe line 100
          (party_set_slot, ":party_no", slot_party_on_water, 1),
          (party_set_flags, ":party_no", pf_is_ship, 1),
          (call_script, "script_update_party_icon", ":party_no"),
          (party_get_slot, ":ai_state", ":party_no", slot_party_ai_state_backup),
          (party_get_slot, ":ai_object", ":party_no", slot_party_ai_object_backup),
          #(party_get_slot, ":ai_behavior", ":party_no",
          #slot_party_ai_behavior_backup),
          (party_set_slot, ":party_no", slot_party_ai_state_backup, -1),
          (party_set_slot, ":party_no", slot_party_ai_object_backup, -1),
          (party_set_slot, ":party_no", slot_party_ai_embarking_port, -1),
          (party_set_slot, ":party_no", slot_party_ai_object, -1),
          #
          (party_is_active, ":ai_object"),
          (call_script, "script_party_set_ai_state", ":party_no", ":ai_state", ":ai_object"),
        (else_try),
          # 10.  SPECIAL SITUATION: SPAWN POINT ON WATER
          
          #conditions
          (party_slot_eq, ":party_no", slot_party_on_water, 0),
          (party_get_slot, ":embarking_port", ":party_no", slot_party_ai_embarking_port),
          (gt, ":embarking_port", 0),
          (party_get_slot, ":home", ":party_no", slot_party_spawn_point),
          (is_between, ":home", spawn_points_begin, spawn_points_end),# limiting this to motos parties
          (party_get_slot, ":ai_object", ":party_no", slot_party_ai_object_backup),
          (ge, ":ai_object", 0),
          (party_slot_eq, ":ai_object", slot_party_on_water, 1),
          (party_get_position, pos1, ":party_no"),
          (party_get_position, pos2, ":embarking_port"),
          (get_distance_between_positions, ":distance", pos1, pos2),
          (le, ":distance", 150),
          
          #message
          (try_begin),
            (ge, "$vc_debug_mode", 2),
            (str_store_party_name, s1, ":party_no"),
            (display_message, "@{!}INFO: {s1} is embarking while using SPECIAL SITUATION: SPAWN POINT ON WATER"),
          (end_try),
          
          #consequences
          (party_get_slot, ":port_party", ":embarking_port", slot_party_port_party),
          (party_get_position, pos3, ":port_party"),
          (party_set_position, ":party_no", pos3),
          (party_set_slot, ":party_no", slot_party_on_water, 1),
          (party_set_flags, ":party_no", pf_is_ship, 1),
          (call_script, "script_update_party_icon", ":party_no"),
          (party_get_slot, ":ai_state", ":party_no", slot_party_ai_state_backup),
          (party_get_slot, ":ai_object", ":party_no", slot_party_ai_object_backup),
          (party_get_slot, ":ai_behavior", ":party_no", slot_party_ai_behavior_backup),
          #(party_set_slot, ":party_no", slot_party_ai_state_backup, -1),
          (party_set_slot, ":party_no", slot_party_ai_object_backup, -1),
          (party_set_slot, ":party_no", slot_party_ai_embarking_port, -1),
          (party_set_ai_behavior, ":party_no", ":ai_behavior"),
          (party_set_ai_object, ":party_no", ":ai_object"),
          # (party_set_slot, ":party_no", slot_party_ai_state, -1),# outcommented to
          # try fixing VC-1824
          (party_set_slot, ":party_no", slot_party_ai_object, ":ai_object"),
        (try_end),
      (try_end),]),
  
  # script_map_sea_ai_3
  # Component: Ships and sea (map)
  # This script is called every 3h in a simple trigger
  # it is "having bandits go to lairs and back" + "heuristic/workaround trigger
  # to fix the problems which occurred to sea travel with the new executables"
  ("map_sea_ai_3",
    [#(display_message, "@{!}map_sea_ai_3..."),
      (try_for_parties, ":party_no"),
        # 1.CONDITIONS
        (gt, ":party_no", "p_spawn_points_end"),
        (party_get_template_id, ":party_template_id", ":party_no"),
        (neq, ":party_template_id", "pt_port"),
        (neq, ":party_template_id", "pt_landet_ships"),
        (party_is_active, ":party_no"),
        
        # 2.PIGGYBACKING
        (call_script, "script_bandits_go_to_lairs", ":party_no"),
        
        #3.1 GETTING DATA
        (get_party_ai_object, ":ai_object", ":party_no"),
        (get_party_ai_behavior, ":ai_behavior", ":party_no"),
        
        # 3.PARTIES ON SEA
        (try_begin),
          (party_slot_eq, ":party_no", slot_party_on_water, 1),
          
          (party_set_flags, ":party_no", pf_is_ship, 1),#fixing flags (fixing VC-1537 for old save games)
          
          #3.2 SCENARIOS (fixing hanging ships mentioned in VC-1216)
          (try_begin),
            # #scenario 0: Dont change AI while attacking or flee
            # (this_or_next|eq, ":ai_behavior", ai_bhvr_attack_party),
            # ( eq, ":ai_behavior", ai_bhvr_driven_by_party),
            # (display_message, "@{!}I dont change AI while attacking or flee"),
            # (assign, ":block", 1),
            # (else_try),
            #scenario 1: ai_object = port party
            (neq, ":ai_behavior", ai_bhvr_hold),
            (neq, ":ai_behavior", ai_bhvr_patrol_location),
            (neq, ":ai_behavior", ai_bhvr_travel_to_point),
            (call_script, "script_cf_is_port_party", ":ai_object"),
            (assign, ":block", 1),
          (else_try),
            #scenario 2: ai_object on land
            (neq, ":ai_object", -1),
            (neq, ":ai_behavior", ai_bhvr_hold),
            (neq, ":ai_behavior", ai_bhvr_patrol_location),
            (neq, ":ai_behavior", ai_bhvr_travel_to_point),
            (party_slot_eq, ":ai_object", slot_party_on_water, 0),
            (assign, ":block", 0),
            (call_script, "script_get_closest_port", ":ai_object"),
            (party_get_slot, ":port_party", reg0, slot_party_port_party),
          (else_try),
            #scenario x: parties who shouldn't be on the sea
            #(this_or_next|is_between, ":party_template_id", "pt_looters",
            #"pt_merchant_caravan"),
            (this_or_next | is_between, ":party_template_id", "pt_looters", "pt_deserters"),	#Deserters are now allowed to go on sea since they have a AI now
            (this_or_next | eq, ":party_template_id", "pt_sacerdotes_party"),
            (this_or_next | eq, ":party_template_id", "pt_routed_warriors"),
            (eq, ":party_template_id", "pt_paganos_party"),
            (assign, ":block", 0),
            (call_script, "script_get_closest_port", ":party_no"),
            (party_get_slot, ":port_party", reg0, slot_party_port_party),
          (else_try),
            #scenario 3: target_position on land
            (eq, ":ai_behavior", ai_bhvr_travel_to_point),
            (party_get_ai_target_position, pos1, ":party_no"),
            (party_set_position, "p_temp_party", pos1),
            (party_get_current_terrain, ":terrain_type", "p_temp_party"),
            (neq, ":terrain_type", 0),
            (neq, ":terrain_type", 7),
            (neq, ":terrain_type", 8),
            (assign, ":block", 0),
            (call_script, "script_get_closest_port", "p_temp_party"),
            (party_get_slot, ":port_party", reg0, slot_party_port_party),
          (else_try),
            #scenario 4: rest
            (assign, ":block", 1),
          (end_try),
          (eq, ":block", 0),
          
          #3.3 EXECUTING CONSEQUENCES
          (party_get_slot, ":ai_state", ":party_no", slot_party_ai_state),
          (party_set_slot, ":party_no", slot_party_ai_state_backup, ":ai_state"),
          (party_set_slot, ":party_no", slot_party_ai_object_backup, ":ai_object"),
          (party_set_slot, ":party_no", slot_party_ai_behavior_backup, ":ai_behavior"),
          (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
          (party_set_ai_object, ":party_no", ":port_party"),
          # (party_set_slot, ":party_no", slot_party_ai_state, 0), # outcommented to
          # try fixing VC-1824
          (party_set_slot, ":party_no", slot_party_ai_object, ":port_party"),
          
          # 4.PARTIES ON LAND
        (else_try),
          (party_slot_eq, ":party_no", slot_party_on_water, 0),
          
          (try_begin),
            #4.1 SPECIAL SCENARIO: AI OBJECT ON WATER
            # Note: This was created for the new system of parties which travel to
            # their spawnpoints on sea But as long as all spawnpoints are
            # on rt_bridge it should also work without it
            
            #conditions
            (neq, ":ai_object", -1),
            (neq, ":ai_behavior", ai_bhvr_hold),
            (neq, ":ai_behavior", ai_bhvr_patrol_location),
            (neq, ":ai_behavior", ai_bhvr_travel_to_point),
            (get_party_ai_object, ":ai_object", ":party_no"),
            (party_get_slot, ":home", ":party_no", slot_party_spawn_point),
            (is_between, ":home", spawn_points_begin, spawn_points_end),# limiting this to motos parties
            (party_slot_eq, ":ai_object", slot_party_on_water, 1),
            
            #message
            (try_begin),
              (ge, "$vc_debug_mode", 2),
              (str_store_party_name, s1, ":party_no"),
              (display_message, "@{!}INFO: {s1} is in SPECIAL SCENARIO: AI OBJECT ON WATER"),
            (end_try),
            
            #consequences
            (get_party_ai_behavior, ":ai_behavior", ":party_no"),
            #(party_set_slot, ":party_no", slot_party_ai_state_backup, -1),
            (party_set_slot, ":party_no", slot_party_ai_object_backup, ":ai_object"),
            (party_set_slot, ":party_no", slot_party_ai_behavior_backup, ":ai_behavior"),
            (call_script, "script_get_closest_port", ":party_no"),
            (party_set_slot, ":party_no", slot_party_ai_embarking_port, reg0),
            (party_get_position, pos1, reg0),
            (party_set_ai_target_position, ":party_no", pos1),
            (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_point),
            
          (else_try),
            # 4.2 SPECIAL SCENARIO LORD (VC-1679)
            
            #conditions
            (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
            (party_get_slot, ":ai_state", ":party_no", slot_party_ai_state),
            (eq, ":ai_state", spai_accompanying_army),
            (party_get_slot, ":ai_object", ":party_no", slot_party_ai_object),
            (gt, ":ai_object", -1),
            (party_is_active, ":ai_object"),
            (party_slot_eq, ":ai_object", slot_party_on_water, 1),
            
            #consequences
            (get_party_ai_behavior, ":ai_behavior", ":party_no"),
            (party_set_slot, ":party_no", slot_party_ai_state_backup, ":ai_state"),
            (party_set_slot, ":party_no", slot_party_ai_object_backup, ":ai_object"),
            (party_set_slot, ":party_no", slot_party_ai_behavior_backup, ":ai_behavior"),
            (call_script, "script_get_closest_port", ":party_no"),
            (party_set_slot, ":party_no", slot_party_ai_embarking_port, reg0),
            (party_get_position, pos1, reg0),
            (party_set_ai_target_position, ":party_no", pos1),
            (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_point),
          (end_try),
        (end_try),
        
      (end_try),]),
  
  # script_print_party_ship_info_to_s0
  # Component: Ships and sea (map)
  ("print_party_ship_info_to_s0",
    [(store_script_param_1, ":party"),
      (str_clear, s0),
      (try_for_range, ":slot_number_minus_one", 0, 7),
        (store_add, ":calculated_slot", ":slot_number_minus_one", slot_party_1_ship_type),
        (party_get_slot, "$ship_menu_current_ship_type", ":party", ":calculated_slot"),
        (gt, "$ship_menu_current_ship_type", 0),	#!
        (store_add, ":calculated_slot", ":slot_number_minus_one", slot_party_1_ship_name),
        (party_get_slot, "$ship_menu_current_ship_name", ":party", ":calculated_slot"),
        (store_add, ":calculated_slot", ":slot_number_minus_one", slot_party_1_ship_quality),
        (party_get_slot, "$ship_menu_current_ship_quality", ":party", ":calculated_slot"),
        (store_add, ":calculated_slot", ":slot_number_minus_one", slot_party_1_ship_propertys),
        (party_get_slot, "$ship_menu_current_ship_propertys", ":party", ":calculated_slot"),
        
        (call_script,  "script_get_ship_properties", "$ship_menu_current_ship_type"),
        (str_store_string, s1, reg7), #type_name
        (try_begin),# ship name
          (is_between, "$ship_menu_current_ship_name", "trp_pseudo_troop_01", "trp_pseudo_troop_end"),
          (str_store_troop_name, s2, "$ship_menu_current_ship_name"),
        (else_try),
          (str_store_string, s2, "$ship_menu_current_ship_name"),
        (end_try),
        
        (try_begin),
          (gt, "$ship_menu_current_ship_quality", 80),
          (str_store_string, s3, "@Very Good"),
        (else_try),
          (gt, "$ship_menu_current_ship_quality", 60),
          (str_store_string, s3, "@Good"),
        (else_try),
          (gt, "$ship_menu_current_ship_quality", 40),
          (str_store_string, s3, "@Acceptable"),
        (else_try),
          (gt, "$ship_menu_current_ship_quality", 20),
          (str_store_string, s3, "@Bad"),
        (else_try),
          (gt, "$ship_menu_current_ship_quality", 5),
          (str_store_string, s3, "@Very Bad"),
        (else_try),
          (str_store_string, s3, "@Dangerous"),
        (end_try),
        (call_script, "script_decode_value", "$ship_menu_current_ship_propertys"),
        (assign, ":ship_wood", reg1),
        (try_begin),
          (eq, ":ship_wood", 1),
          (str_store_string, s4, "str_ship_wood_1"),
        (else_try),
          (eq, ":ship_wood", 2),
          (str_store_string, s4, "str_ship_wood_2"),
        (else_try),
          (str_store_string, s4, "str_ship_wood_3"),
        (end_try),
        (str_store_string, s0, "@{s0}^> {s1}, '{s2}', {s3}, {s4}"),
      (end_try),]),
  
  # script_get_closest_port
  # Component: Ships and sea (map)
  # Input: target party
  # Output: reg0 ID of party with port
  #         reg1 distance
  ("get_closest_port",
    [(store_script_param, ":target_party", 1),
      (assign, ":score_to_beat", 99999),
      (assign, reg1, ":score_to_beat"),
      
      # 1.islands
      (try_begin),
        (store_distance_to_party_from_party, ":distance1", ":target_party", "p_castle_59"),
        (store_distance_to_party_from_party, ":distance2", ":target_party", "p_village_59"),
        (this_or_next | le, ":distance1", 15),
        (le, ":distance2", 15),
        (assign, reg0, "p_jetty_1"),
      (else_try),
        (store_distance_to_party_from_party, ":distance1", ":target_party", "p_castle_60"),
        (store_distance_to_party_from_party, ":distance2", ":target_party", "p_village_60"),
        (this_or_next | le, ":distance1", 15),
        (le, ":distance2", 15),
        (assign, reg0, "p_jetty_2"),
      (else_try),
        (store_distance_to_party_from_party, ":distance1", ":target_party", "p_castle_61"),
        (store_distance_to_party_from_party, ":distance2", ":target_party", "p_village_61"),
        (this_or_next | le, ":distance1", 15),
        (le, ":distance2", 15),
        (assign, reg0, "p_jetty_3"),
      (else_try),
        #2.Ireland
        (is_between, ":target_party", centers_begin, centers_end),
        (this_or_next | party_slot_eq, ":target_party", slot_center_culture, "fac_culture_irish"),
        (this_or_next | eq, ":target_party", "p_town_7"),	#Dublin and villages
        (this_or_next | eq, ":target_party", "p_village_104"),
        (this_or_next | eq, ":target_party", "p_village_105"),
        (this_or_next | eq, ":target_party", "p_castle_62"),	#Veisafjordr and village
        (eq, ":target_party", "p_village_62"),
        (try_for_range, ":town_no", towns_begin, towns_end),
          (this_or_next | eq, ":town_no", "p_town_7"),	#Dublin
          (this_or_next | eq, ":town_no", "p_town_20"),	#Rath Celtair
          (eq, ":town_no", "p_town_25"),	#Aileach
          (store_distance_to_party_from_party, ":distance", ":target_party", ":town_no"),
          (lt, ":distance", ":score_to_beat"),
          (assign, reg0, ":town_no"),
          (assign, ":score_to_beat", ":distance"),
        (try_end),
      (else_try),
        #3.England
        (is_between, ":target_party", centers_begin, centers_end),
        (this_or_next | party_slot_eq, ":target_party", slot_center_culture, "fac_culture_saxon"),
        (this_or_next | party_slot_eq, ":target_party", slot_center_culture, "fac_culture_angle"),
        (this_or_next | party_slot_eq, ":target_party", slot_center_culture, "fac_culture_welsh"),
        (this_or_next | party_slot_eq, ":target_party", slot_center_culture, "fac_culture_scotch"),
        (this_or_next | eq, ":target_party", "p_castle_57"),	#Caer Reghed and village are norse culture
        (eq, ":target_party", "p_village_57"),
        (try_for_range, ":town_no", towns_begin, towns_end),
          (party_slot_eq, ":town_no", slot_town_port, 1),
          (this_or_next | party_slot_eq, ":town_no", slot_center_culture, "fac_culture_saxon"),
          (this_or_next | party_slot_eq, ":town_no", slot_center_culture, "fac_culture_angle"),
          (this_or_next | party_slot_eq, ":town_no", slot_center_culture, "fac_culture_welsh"),
          (party_slot_eq, ":town_no", slot_center_culture, "fac_culture_scotch"),
          (store_distance_to_party_from_party, ":distance", ":target_party", ":town_no"),
          (lt, ":distance", ":score_to_beat"),
          (assign, reg0, ":town_no"),
          (assign, ":score_to_beat", ":distance"),
        (try_end),
        (try_begin), #VC-3494
          (store_distance_to_party_from_party, ":distance", ":target_party", "p_jetty_4"),
          (lt, ":distance", ":score_to_beat"),
          (assign, reg0, "p_jetty_4"),
          (assign, ":score_to_beat", ":distance"),
        (try_end),
      (else_try),
        #4.Rest
        (try_for_range, ":town_no", towns_begin, towns_end),
          (party_slot_eq, ":town_no", slot_town_port, 1),
          (store_distance_to_party_from_party, ":distance", ":target_party", ":town_no"),
          (lt, ":distance", ":score_to_beat"),
          (assign, reg0, ":town_no"),
          (assign, ":score_to_beat", ":distance"),
        (try_end),
        (try_for_range, ":jetty_no", "p_jetty_5", "p_wales_spawn_point"), #VC-3494
          (store_distance_to_party_from_party, ":distance", ":target_party", ":jetty_no"),
          (lt, ":distance", ":score_to_beat"),
          (assign, reg0, ":jetty_no"),
          (assign, ":score_to_beat", ":distance"),
        (try_end),
      (try_end),
      
      (assign, reg1, ":score_to_beat"),]),
  
  # script_get_wave_properties
  # Component: Ships and sea (scene)
  ("get_wave_properties",
    [(try_begin),
        (eq, "$beaufort", 12),
        (store_random_in_range, reg1, 30000, 40000),	#Amplitude X (mykrometer)
        (store_random_in_range, reg2, 55000, 70000),	#Amplitude Y
        (store_random_in_range, reg3, 25000, 30000),	#Lamda X (cm)
        (store_random_in_range, reg4, 25000, 30000),	#Lamda Y (cm)
        (store_random_in_range, reg6,  1200,  1300),	#WaveSpeed Y
        (val_div, reg6, 3),
      (else_try),
        (eq, "$beaufort", 11),
        (store_random_in_range, reg1, 30000, 35000),
        (store_random_in_range, reg2, 47000, 55000),
        (store_random_in_range, reg3, 20000, 25000),
        (store_random_in_range, reg4, 20000, 25000),
        (store_random_in_range, reg6,   950,  1150),
        (val_div, reg6, 3),
      (else_try),
        (eq, "$beaufort", 10),
        (store_random_in_range, reg1, 20000, 30000),
        (store_random_in_range, reg2, 40000, 47000),
        (store_random_in_range, reg3, 17000, 20000),
        (store_random_in_range, reg4, 17000, 20000),
        (store_random_in_range, reg6,   850,   950),
        (val_div, reg6, 2),
      (else_try),
        (eq, "$beaufort", 9),
        (store_random_in_range, reg1,  8000, 10000),
        (store_random_in_range, reg2, 35000, 40000), #y
        (store_random_in_range, reg3, 15000, 17000),
        (store_random_in_range, reg4, 15000, 17000), #y
        (store_random_in_range, reg6,   750,   850),
        (val_div, reg6, 2),
      (else_try),
        (eq, "$beaufort", 8),
        (store_random_in_range, reg1,  8000, 10000),
        (store_random_in_range, reg2, 30000, 35000), #y
        (store_random_in_range, reg3, 13500, 15000),
        (store_random_in_range, reg4, 13500, 15000), #y
        (store_random_in_range, reg6,   650,   750),
        (val_div, reg6, 2),
      (else_try),
        (eq, "$beaufort", 7),
        (store_random_in_range, reg1,  8000, 10000),
        (store_random_in_range, reg2, 23000, 30000), #y
        (store_random_in_range, reg3, 10000, 13500),
        (store_random_in_range, reg4, 10000, 13500), #y
        (store_random_in_range, reg6,   500,   600),
        (val_div, reg6, 2),
      (else_try),
        (eq, "$beaufort", 6),
        (store_random_in_range, reg1,  8000, 10000),
        (store_random_in_range, reg2, 15000, 23000), #y
        (store_random_in_range, reg3,  7500, 10000),
        (store_random_in_range, reg4,  7500, 10000), #y
        (store_random_in_range, reg6,   400,   450),
        (val_div, reg6, 2),
      (else_try),
        (eq, "$beaufort", 5),
        (store_random_in_range, reg1,  8000, 10000),
        (store_random_in_range, reg2,  8000, 15000), #y
        (store_random_in_range, reg3,  5000,  7500),
        (store_random_in_range, reg4,  5000,  7500), #y
        (store_random_in_range, reg6,   350,   430),
        (val_div, reg6, 2),
      (else_try),
        (eq, "$beaufort", 4),#
        (store_random_in_range, reg1,  4000,  6000),
        (store_random_in_range, reg2,  6000,  8000), #y
        (store_random_in_range, reg3,  4500,  5500), #lamda x
        (store_random_in_range, reg4,  4500,  5500), #lamda y
        (store_random_in_range, reg6,   250,   350),
      (else_try),
        (eq, "$beaufort", 3),
        (store_random_in_range, reg1,  1500,  4000),
        (store_random_in_range, reg2,  1500,  4000), #y
        (store_random_in_range, reg3,  2500,  3500),
        (store_random_in_range, reg4,  2500,  3500), #y
        (store_random_in_range, reg6,   150,   250),
      (else_try),
        (eq, "$beaufort", 2),
        (store_random_in_range, reg1,   500,  1500),
        (store_random_in_range, reg2,   500,  1500),
        (store_random_in_range, reg3,  1500,  2500),
        (store_random_in_range, reg4,  1500,  2500),
        (store_random_in_range, reg6,   120,   150),
      (else_try),
        (eq, "$beaufort", 1),
        (store_random_in_range, reg1,   200,   500),	#was 100, 500),
        (store_random_in_range, reg2,   200,   500),	#was 100, 500),
        (store_random_in_range, reg3,  1000,  1500),	#was 600, 1500),
        (store_random_in_range, reg4,  1000,  1500),	#was 600, 1500),
        (store_random_in_range, reg6,   100,   120),
      (else_try),
        (eq, "$beaufort", 0),
        (store_random_in_range, reg1,   100,   200),	#was 0, 100),
        (store_random_in_range, reg2,   100,   200),	#was 0, 100),
        (store_random_in_range, reg3,  1000,  1200),	#was 500, 600),
        (store_random_in_range, reg4,  1000,  1200),	#was 500, 600),
        (store_random_in_range, reg6,    80,   150),
      (end_try),]),
  
  # script_prepare_party_ships_for_team
  # Component: Ships and sea (map)
  # Input: ":team", ":amount_of_agents"
  ("prepare_party_ships_for_team",
    [(store_script_param, ":party", 1),
      (store_script_param, ":team", 2),
      (try_begin),
        (eq, ":team", 0),
        (assign, ":quest", "qst_team_0_ships"),
      (else_try),
        (assign, ":quest", "qst_team_1_ships"),
      (end_try),
      (try_for_range, ":counter", 0, 7),
        (store_add, ":curr_type_slot1", ":counter", slot_party_player_ships_type_begin),
        (store_add, ":curr_cond_slot1", ":counter", slot_party_player_ships_quality_begin),
        (store_add, ":curr_prop_slot1", ":counter", slot_party_player_ships_propertys_begin),
        (party_get_slot, ":ship_type", ":party", ":curr_type_slot1"),
        (party_get_slot, ":ship_cond", ":party", ":curr_cond_slot1"),
        (party_get_slot, ":ship_prop", ":party", ":curr_prop_slot1"),
        (store_add, ":curr_type_slot2", slot_quest_1_ship_type, ":counter"),
        (store_add, ":curr_cond_slot2", slot_quest_1_ship_cond, ":counter"),
        (store_add, ":curr_prop_slot2", slot_quest_1_ship_prop, ":counter"),
        (quest_set_slot, ":quest", ":curr_type_slot2", ":ship_type"),
        (quest_set_slot, ":quest", ":curr_cond_slot2", ":ship_cond"),
        (quest_set_slot, ":quest", ":curr_prop_slot2", ":ship_prop"),
      (end_try),]),
  
  # script_randomize_ships_for_team
  # Component: Ships and sea (map)
  # Input: ":team", ":amount_of_agents"
  ("randomize_ships_for_team", #in the moment thats only used in custom battle
    [(store_script_param, ":team", 1),
      (store_script_param, ":amount_of_agents", 2),
      (assign, ":number_of_busse", 0),
      (assign, ":number_of_skei", 0),
      (assign, ":number_of_karvi", 0),
      
      (try_begin),
        (eq, ":team", 0),
        (assign, ":quest", "qst_team_0_ships"),
      (else_try),
        (assign, ":quest", "qst_team_1_ships"),
      (end_try),
      
      (try_for_range, ":counter", 0, 7),
        (try_begin),
          (gt, ":amount_of_agents", 120),
          (lt, ":number_of_busse", 1),
          (assign, ":ship_type", 1),
          (val_add, ":number_of_busse", 1),
        (else_try),
          (gt, ":amount_of_agents", 100),
          (lt, ":number_of_skei", 2),
          (assign, ":ship_type", 2),
          (val_add, ":number_of_skei", 1),
        (else_try),
          (gt, ":amount_of_agents", 60),
          (lt, ":number_of_karvi", 4),
          (assign, ":ship_type", 3),
          (val_add, ":number_of_karvi", 1),
        (else_try),
          (gt, ":amount_of_agents", 29),
          (assign, ":ship_type", 4),
          # (else_try),
          # (gt, ":amount_of_agents", 7),
          # (assign, ":ship_type", 5),
        (else_try),
          (gt, ":amount_of_agents", 0),
          (assign, ":ship_type", 6),
        (else_try),
          (assign, ":ship_type", 0),
        (end_try),
        (store_random_in_range, ":ship_cond", 75, 101),
        (store_random_in_range, ":wood", 1, 4),
        (store_random_in_range, ":finish", 0, 9),
        (store_random_in_range, ":sail", 1, 10),
        (call_script, "script_encode_values_to_reg0", ":wood",  ":sail", ":finish",0, 0),
        (assign, ":ship_prop", reg0),
        (store_add, ":curr_type_slot", slot_quest_1_ship_type, ":counter"),
        (store_add, ":curr_cond_slot", slot_quest_1_ship_cond, ":counter"),
        (store_add, ":curr_prop_slot", slot_quest_1_ship_prop, ":counter"),
        (quest_set_slot, ":quest", ":curr_type_slot", ":ship_type"),
        (quest_set_slot, ":quest", ":curr_cond_slot", ":ship_cond"),
        (quest_set_slot, ":quest", ":curr_prop_slot", ":ship_prop"),
        (try_begin),
          (gt, ":ship_type", 0),
          (call_script, "script_get_ship_properties", ":ship_type"),
          (val_sub, ":amount_of_agents", reg6),
        (end_try),
      (end_try),]),
  
  # script_randomize_ships_for_team_war_party
  # Component: Ships and sea (map)
  # Input: ":team", ":amount_of_agents"
  ("randomize_ships_for_team_war_party",
    [(store_script_param, ":team", 1),
      (store_script_param, ":amount_of_agents", 2),
      
      (try_begin),
        (eq, ":team", 0),
        (assign, ":quest", "qst_team_0_ships"),
      (else_try),
        (assign, ":quest", "qst_team_1_ships"),
      (end_try),
      
      (try_for_range, ":counter", 0, 7),
        (try_begin),
          (gt, ":amount_of_agents", 110),
          (assign, ":ship_type", ship_type_busse),
        (else_try),
          (gt, ":amount_of_agents", 78),
          (assign, ":ship_type", ship_type_skei),
        (else_try),
          (gt, ":amount_of_agents", 54),
          (assign, ":ship_type", ship_type_karvi),
        (else_try),
          (gt, ":amount_of_agents", 14),
          (assign, ":ship_type", ship_type_snekkja),
        (else_try),
          (gt, ":amount_of_agents", 0),
          (assign, ":ship_type", ship_type_byrding),
        (else_try),
          (assign, ":ship_type", 0),
        (end_try),
        (store_random_in_range, ":ship_cond", 75, 101),
        (store_random_in_range, ":wood", 1, 4),
        (store_random_in_range, ":finish", 0, 9),
        (store_random_in_range, ":sail", 1, 10),
        (call_script, "script_encode_values_to_reg0", ":wood",  ":sail", ":finish",0, 0),
        (assign, ":ship_prop", reg0),
        (store_add, ":curr_type_slot", slot_quest_1_ship_type, ":counter"),
        (store_add, ":curr_cond_slot", slot_quest_1_ship_cond, ":counter"),
        (store_add, ":curr_prop_slot", slot_quest_1_ship_prop, ":counter"),
        (quest_set_slot, ":quest", ":curr_type_slot", ":ship_type"),
        (quest_set_slot, ":quest", ":curr_cond_slot", ":ship_cond"),
        (quest_set_slot, ":quest", ":curr_prop_slot", ":ship_prop"),
        (try_begin),
          (gt, ":ship_type", 0),
          (call_script, "script_get_ship_properties", ":ship_type"),
          (val_sub, ":amount_of_agents", reg6),
        (end_try),
      (end_try),]),
  
  # script_randomize_ships_for_team_civilian
  # Component: Ships and sea (map)
  # Input: ":team", ":amount_of_agents"
  ("randomize_ships_for_team_civilian",
    [(store_script_param, ":team", 1),
      (store_script_param, ":amount_of_agents", 2),
      
      (try_begin),
        (eq, ":team", 0),
        (assign, ":quest", "qst_team_0_ships"),
      (else_try),
        (assign, ":quest", "qst_team_1_ships"),
      (end_try),
      
      (try_for_range, ":counter", 0, 7),
        (assign, ":cargo", 0),
        (try_begin),
          (gt, ":amount_of_agents", 29),
          (assign, ":ship_type", ship_type_karvi),
        (else_try),
          (gt, ":amount_of_agents", 14),
          (assign, ":ship_type", ship_type_knorr),
          (assign, ":cargo", 1),
        (else_try),
          (gt, ":amount_of_agents", 0),
          (assign, ":ship_type", ship_type_byrding),
        (else_try),
          (assign, ":ship_type", 0),
        (end_try),
        (store_random_in_range, ":ship_cond", 60, 80),
        (store_random_in_range, ":wood", 1, 4),
        (store_random_in_range, ":finish", 0, 9),
        (store_random_in_range, ":sail", 1, 10),
        (call_script, "script_encode_values_to_reg0", ":wood",  ":sail", ":finish", 0, ":cargo"),
        (assign, ":ship_prop", reg0),
        (store_add, ":curr_type_slot", slot_quest_1_ship_type, ":counter"),
        (store_add, ":curr_cond_slot", slot_quest_1_ship_cond, ":counter"),
        (store_add, ":curr_prop_slot", slot_quest_1_ship_prop, ":counter"),
        (quest_set_slot, ":quest", ":curr_type_slot", ":ship_type"),
        (quest_set_slot, ":quest", ":curr_cond_slot", ":ship_cond"),
        (quest_set_slot, ":quest", ":curr_prop_slot", ":ship_prop"),
        (try_begin),
          (gt, ":ship_type", 0),
          (call_script, "script_get_ship_properties", ":ship_type"),
          (val_sub, ":amount_of_agents", reg6),
        (end_try),
      (end_try),]),
  
  # script_randomize_ships_for_team_old
  # Component: Ships and sea (map)
  # Input: ":team", ":amount_of_agents"
  ("randomize_ships_for_team_old",
    [(store_script_param, ":team", 1),
      (store_script_param, ":amount_of_agents", 2),
      
      (try_begin),
        (eq, ":team", 0),
        (assign, ":quest", "qst_team_0_ships"),
      (else_try),
        (assign, ":quest", "qst_team_1_ships"),
      (end_try),
      
      (try_for_range, ":counter", 0, 7),
        (try_begin),
          (gt, ":amount_of_agents", 60),
          (assign, ":ship_type", 1),
        (else_try),
          (gt, ":amount_of_agents", 39),
          (assign, ":ship_type", 2),
        (else_try),
          (gt, ":amount_of_agents", 27),
          (assign, ":ship_type", 3),
          #(store_random_in_range, ":ship_type", 4, 6), # 4 or 5 = Snekkja or Knorr
        (else_try),
          (gt, ":amount_of_agents", 11),
          (assign, ":ship_type", 4),
        (else_try),
          (gt, ":amount_of_agents", 7),
          (assign, ":ship_type", 5),
        (else_try),
          (gt, ":amount_of_agents", 0),
          (assign, ":ship_type", 6),
        (else_try),
          (assign, ":ship_type", 0),
        (end_try),
        (store_random_in_range, ":ship_cond", 75, 101),
        (store_random_in_range, ":wood", 1, 4),
        (store_random_in_range, ":finish", 0, 9),
        (store_random_in_range, ":sail", 1, 10),
        (call_script, "script_encode_values_to_reg0", ":wood", ":sail", ":finish",0, 0),
        (assign, ":ship_prop", reg0),
        (store_add, ":curr_type_slot", slot_quest_1_ship_type, ":counter"),
        (store_add, ":curr_cond_slot", slot_quest_1_ship_cond, ":counter"),
        (store_add, ":curr_prop_slot", slot_quest_1_ship_prop, ":counter"),
        (quest_set_slot, ":quest", ":curr_type_slot", ":ship_type"),
        (quest_set_slot, ":quest", ":curr_cond_slot", ":ship_cond"),
        (quest_set_slot, ":quest", ":curr_prop_slot", ":ship_prop"),
        (try_begin),
          (gt, ":ship_type", 0),
          (call_script, "script_get_ship_properties", ":ship_type"),
          (val_sub, ":amount_of_agents", reg6),
        (end_try),
      (end_try),]),
  
  # script_spawn_sea_battle_visitors
  # Component: Ships and sea (scene)
  ("spawn_sea_battle_visitors",
    [(store_script_param, ":team", 1),
      (store_script_param, ":amount_of_troops", 2),
      
      (try_begin),
        (eq, ":team", 0),
        (assign, ":begin_slot", slot_quest_team_0_spawn_troop1_type),
        (store_add, ":end_slot", slot_quest_team_0_spawn_troop5_type, 1),
        (assign, ":entry_no", 2),
      (else_try),
        (assign, ":begin_slot", slot_quest_team_1_spawn_troop1_type),
        (store_add, ":end_slot", slot_quest_team_1_spawn_troop5_type, 1),
        (assign, ":entry_no", 3),
      (end_try),
      
      (try_for_range, ":troop_type_slot", ":begin_slot", ":end_slot"),
        (ge, ":amount_of_troops", 1),
        (store_add, ":troop_count_slot", ":troop_type_slot", 5),
        (quest_get_slot, ":troop_count", "qst_sea_battle_spawn", ":troop_count_slot"),
        (ge, ":troop_count", 1),
        (quest_get_slot, ":troop_type", "qst_sea_battle_spawn", ":troop_type_slot"),
        (add_visitors_to_current_scene, ":entry_no", ":troop_type", 1),
        (val_sub, ":troop_count", 1),
        (quest_set_slot, "qst_sea_battle_spawn", ":troop_count_slot", ":troop_count"),
        (val_sub, ":amount_of_troops", 1),
      (end_try),
      
      (try_begin),
        (ge, ":amount_of_troops", 1),
        (call_script, "script_spawn_sea_battle_visitors", ":team", ":amount_of_troops"),
      (end_try),]),
  
  # script_set_ship_materials
  # Component: Ships and sea (scene)
  # Input: ":ship_instance", ":property_code"
  ("set_ship_materials",
    [(store_script_param, ":ship_instance", 1),
      (store_script_param, ":property_code", 2),
      
      (assign, reg1, ":property_code"),
      #(display_message, "@property_code: {reg1}"),
      
      (call_script, "script_decode_value", ":property_code"),
      
      (assign, ":ship_cargo", reg5),	# ship cargo
      # (assign, :ship_...", reg4), # ship dragon head
      (assign, ":ship_paint", reg3),
      (assign, ":ship_sail", reg2),
      (assign, ":ship_wood", reg1),
      #(display_message, "@wood: {reg1}, sail: {reg2}, paint: {reg3} "),
      
      # 1.  SHIP
      (try_begin),
        (eq, ":ship_wood", 1),
        (is_between, ":ship_paint", 1, 3),	#1-2 tar = basic
        (str_store_string, s0, "@{!}vc_ships_keel_tar"),
        (str_store_string, s1, "@{!}vc_ships_hull_tar"),
        (str_store_string, s2, "@{!}vc_ships_floor_tar"),
        (str_store_string, s3, "@{!}vc_ships_various_tar"),
      (else_try),
        (eq, ":ship_wood", 1),
        (is_between, ":ship_paint", 3, 5),	#3-4 varnish = basic
        (str_store_string, s0, "@{!}vc_ships_keel_varnish"),
        (str_store_string, s1, "@{!}vc_ships_hull_varnish"),
        (str_store_string, s2, "@{!}vc_ships_floor_oak"),
        (str_store_string, s3, "@{!}vc_ships_various01"),
      (else_try),
        (eq, ":ship_wood", 1),				# rest = simple oak = basic
        (str_store_string, s0, "@{!}vc_ships_keel"),
        (str_store_string, s1, "@{!}vc_ships_hull_oak"),
        (str_store_string, s2, "@{!}vc_ships_floor_oak"),
        (str_store_string, s3, "@{!}vc_ships_various01"),
      (else_try),
        (eq, ":ship_wood", 2),
        (str_store_string, s0, "@{!}vc_ships_keel"),
        (str_store_string, s1, "@{!}vc_ships_hull_pine"),
        (str_store_string, s2, "@{!}vc_ships_floor_pine"),
        (str_store_string, s3, "@{!}vc_ships_various01"),
      (else_try),
        #(eq, ":ship_wood", 3),
        (str_store_string, s0, "@{!}vc_ships_keel"),
        (str_store_string, s1, "@{!}vc_ships_hull_beech"),
        (str_store_string, s2, "@{!}vc_ships_floor_beech"),
        (str_store_string, s3, "@{!}vc_ships_various01"),
      (end_try),
      
      (try_begin),
        (eq, ":ship_wood", 1),
        (try_begin),
          (eq, ":ship_paint", 2),
          (str_store_string, s1, "@{!}vc_ships_hull_painted_c"),
        (else_try),
          (eq, ":ship_paint", 4),
          (str_store_string, s1, "@{!}vc_ships_hull_painted_f"),
        (else_try),
          (eq, ":ship_paint", 5),
          (str_store_string, s1, "@{!}vc_ships_hull_painted_a"),
        (else_try),
          (eq, ":ship_paint", 6),
          (str_store_string, s1, "@{!}vc_ships_hull_painted_b"),
        (else_try),
          (eq, ":ship_paint", 7),
          (str_store_string, s1, "@{!}vc_ships_hull_painted_d"),
        (else_try),
          (eq, ":ship_paint", 8),
          (str_store_string, s1, "@{!}vc_ships_hull_painted_e"),
        (end_try),
      (end_try),
      
      
      (str_store_string, s4, "@{!}vc_ships_various01"),
      
      #(str_store_string, s5, s4),
      
      (scene_prop_get_slot, ":main_instance", ":ship_instance", scene_prop_main_instance),
      
      (prop_instance_set_material, ":main_instance", 0, s0),
      (prop_instance_set_material, ":main_instance", 1, s1),
      (prop_instance_set_material, ":main_instance", 2, s2),
      (prop_instance_set_material, ":main_instance", 3, s3),
      (prop_instance_set_material, ":main_instance", 4, s4),
      #(prop_instance_set_material, ":main_instance", 5, s5),
      #(prop_instance_set_material, ":main_instance", 6, "@{!}water_kill"),
      
      # 2.  CARGO
      (scene_prop_get_slot, ":ship_type", ":ship_instance", scene_prop_ship_type),
      (try_begin),
        (eq, ":ship_type", ship_type_knorr),
        (neq, ":ship_cargo", 1),
        (scene_prop_get_slot, ":cargo_instance", ":ship_instance", scene_prop_cargo_1),
        (prop_instance_set_material, ":cargo_instance", 0, s2),
      (end_try),
      
      # 3.  SAIL
      (try_begin),
        (eq, ":ship_sail", 0),
        (str_store_string, s1, "@{!}vc_ships_sails01c"),
      (else_try),
        (eq, ":ship_sail", 1),
        (str_store_string, s1, "@{!}vc_ships_sails01b"),
      (else_try),
        (eq, ":ship_sail", 2),
        (str_store_string, s1, "@{!}vc_ships_sails01a"),
      (else_try),
        (eq, ":ship_sail", 3),
        (str_store_string, s1, "@{!}vc_ships_sails01d"),
      (else_try),
        (eq, ":ship_sail", 4),
        (str_store_string, s1, "@{!}vc_ships_sails01e"),
      (else_try),
        (eq, ":ship_sail", 5),
        (str_store_string, s1, "@{!}vc_ships_sails01f"),
      (else_try),
        (eq, ":ship_sail", 6),
        (str_store_string, s1, "@{!}vc_ships_sails02a"),
      (else_try),
        (eq, ":ship_sail", 7),
        (str_store_string, s1, "@{!}vc_ships_sails02b"),
      (else_try),
        (eq, ":ship_sail", 8),
        (str_store_string, s1, "@{!}vc_ships_sails02e"),
      (else_try),
        #(eq, ":ship_sail", 9),
        (str_store_string, s1, "@{!}vc_ships_sails02d"),
      (end_try),
      
      (scene_prop_get_slot, ":boom_instance", ":ship_instance", scene_prop_boom_instance),
      (prop_instance_set_material, ":boom_instance", 2, s1),
  ]),
  
  # script_refill_sea_king_partys
  # Component: Ships and sea (map)
  # Input: ":desired_faction"
  ("refill_sea_king_partys",
    [(store_script_param, ":desired_faction", 1),
      
      (store_num_parties_of_template, reg22, "$pin_party_template"),
      (try_begin),
        (lt,reg22, "$pin_limit"),
        (assign, ":party", -1),
        (try_for_parties, ":test_party"),
          (party_get_template_id, ":party_template_id", ":test_party"),
          (eq, ":party_template_id", "pt_port"),
          (store_faction_of_party, ":party_faction", ":test_party"),
          (eq, ":party_faction", ":desired_faction"),
          (assign, ":party", ":test_party"),
        (try_end),
        (try_begin),
          (neq, ":party", -1),
          (set_spawn_radius, 0),
          (spawn_around_party, ":party", "$pin_party_template"),
          (assign, ":new_party", reg0),
          (party_set_faction, ":new_party", ":desired_faction"),
          (party_set_slot, ":new_party", slot_party_home_center, ":party"),
          (party_set_slot, ":new_party", slot_party_ai_state, spai_patrolling_around_center),
          (party_set_ai_patrol_radius, ":new_party", 1),
          (party_set_slot, ":new_party", slot_party_ai_object, ":party"),
          (party_set_ai_behavior, ":new_party", ai_bhvr_patrol_location),
          (party_set_ai_object, ":new_party", ":party"),
          #(party_set_flags, ":new_party", pf_default_behavior, 0),
          # (str_store_party_name, s1, ":party"),
          # (display_message, "@Sea King created at {s1}."),
        (end_try),
      (try_end),]),
  
  # script_refill_sea_party_template
  # Component: Ships and sea (map)
  ("refill_sea_party_template",
    [(store_num_parties_of_template, reg(22), "$pin_party_template"),
      (try_begin),
        (lt,reg(22),"$pin_limit"),
        (store_random_in_range, ":rand", 1, 15),
        (try_begin),
          (gt, ":rand", 6),
          (call_script, "script_store_random_eastern_port_to_reg0"),
          (assign, ":home_port_town", reg0),
        (else_try),
          (call_script, "script_store_random_western_port_to_reg0"),
          (assign, ":home_port_town", reg0),
        (end_try),
        (set_spawn_radius, 0),
        (try_begin),
          (eq, "$pin_party_template", "pt_fisher_ship"),
          (party_get_slot, ":port_party", ":home_port_town", slot_party_port_party),
          (spawn_around_party, ":port_party", "$pin_party_template"),
          (assign, ":new_party", reg0),
          (store_faction_of_party, ":party_faction", ":home_port_town"),
          (party_set_faction, ":new_party", ":party_faction"),
          (party_set_slot, ":new_party", slot_party_home_center, ":port_party"),
          (party_set_slot, ":new_party", slot_party_ai_state, spai_patrolling_around_center),
          (party_set_ai_patrol_radius, ":new_party", 1),
          (party_set_slot, ":new_party", slot_party_ai_object, ":port_party"),
          (party_set_ai_behavior, ":new_party", ai_bhvr_patrol_location),
          (party_set_ai_object, ":new_party", ":port_party"),
          (party_set_slot, ":new_party", slot_party_on_water, 1),
          #(party_set_flags, ":new_party", pf_default_behavior, 0),
          #(party_set_slot, ":port_party", slot_village_farmer_party, ":new_party"),
          # (str_store_party_name, s1, ":port_party"),
          # (display_message, "@Fishers created at {s1}."),
        (else_try),
          (this_or_next | eq, "$pin_party_template", "pt_traveller_ship"),
          (eq, "$pin_party_template", "pt_slave_trader_ship"),
          (party_get_slot, ":port_party", ":home_port_town", slot_party_port_party),	#new
          (spawn_around_party, ":port_party", "$pin_party_template"),
          (assign, ":new_party", reg0),
          (party_set_slot, ":new_party", slot_party_home_center, ":home_port_town"),
          (party_set_slot, ":new_party", slot_party_ai_state, spai_trading_with_town),
          (try_begin),
            # (eq, ":home_port_town", "p_town_12"), #from lundenwic only to Dorestad
            # (assign, ":target_port_town", "p_town_11"),
            # (else_try),
            # (eq, ":home_port_town", "p_town_25"),
            # (assign, ":target_port_town", "p_town_6"), #From Ailach only to Dun
            # Breatann
            # (else_try),
            # (eq, ":home_port_town", "p_town_20"),
            # (assign, ":target_port_town", "p_town_6"), #From RathC.  only to Dun
            # Breatann
            # (else_try),
            # (eq, ":home_port_town", "p_town_7"), #From Dublin only to Ynys Mon
            # (assign, ":target_port_town", "p_town_13"),
            # (else_try),
            (call_script, "script_cf_party_is_eastern_port", ":home_port_town"),
            (call_script, "script_store_random_eastern_port_to_reg0"),
            (assign, ":target_port_town", reg0),
          (else_try),
            (call_script, "script_store_random_western_port_to_reg0"),
            (assign, ":target_port_town", reg0),
          (end_try),
          (party_get_slot, ":target_port_party", ":target_port_town", slot_party_port_party),	#new
          (party_set_slot, ":new_party", slot_party_ai_object, ":target_port_party"),
          (party_set_ai_behavior, ":new_party", ai_bhvr_travel_to_party),
          (party_set_ai_object, ":new_party", ":target_port_party"),
          #new:
          (party_set_slot, ":new_party", slot_party_on_water, 1),
          (party_set_flags, ":new_party", pf_is_ship, 1),
          # (str_store_party_name, s1, ":party"),
          # (display_message, "@Traveler/Slavetrader created at {s1}."),
          (try_begin),
            (eq, "$pin_party_template", "pt_slave_trader_ship"),
            (party_get_num_companions, ":number", ":new_party"),
            (store_random_in_range, ":rand", 0, 270),
            (val_mul, ":number", ":rand"),
            (val_div, ":number", 100),
            (try_begin),
              (eq, "$game_started_with_content_update", 1),
              (party_add_prisoners,":new_party", "trp_slave" , ":number"),
            (else_try),
              (party_add_prisoners,":new_party", "trp_farmer" , ":number"),#returns number added in reg0 #old trp_sailors, but sailors troop are only for
              #woden ric quest Phaiak
            (try_end),
          (try_end),
        (try_end),
        (call_script,"script_refill_sea_party_template"),	# to really fill it
      (try_end),]),
  
  # script_get_random_port_party
  # Component: Ships and sea (map)
  # ("get_random_port_party",
    # [(store_random_in_range, ":random", 0, "$g_number_of_map_ports"),
      # (val_add, ":random", 1),	#correction
      # (assign, ":counter", 0),
      # (try_for_parties, ":party"),
        # (party_get_template_id, ":party_template_id", ":party"),
        # (eq, ":party_template_id", "pt_port"),
        # (val_add, ":counter", 1),
        # (eq, ":counter", ":random"),
        # (assign, reg1, ":party"),
      # (try_end),]),
  
  # script_cf_party_is_eastern_port
  # Component: Ships and sea (map)
  ("cf_party_is_eastern_port",
    [(store_script_param, ":party_no", 1),
      (this_or_next | eq, ":party_no", "p_town_3"),
      (this_or_next | eq, ":party_no", "p_town_4"),
      (this_or_next | eq, ":party_no", "p_town_5"),
      (this_or_next | eq, ":party_no", "p_town_11"),
      (this_or_next | eq, ":party_no", "p_town_12"),
      (this_or_next | eq, ":party_no", "p_town_14"),
      (this_or_next | eq, ":party_no", "p_town_19"),
      (eq, ":party_no", "p_town_27"),]),
  
  # script_cf_party_is_western_port
  # Component: Ships and sea (map)
  ("cf_party_is_western_port",
    [(store_script_param, ":party_no", 1),
      (this_or_next | eq, ":party_no", "p_town_6"),
      (this_or_next | eq, ":party_no", "p_town_7"),
      (this_or_next | eq, ":party_no", "p_town_9"),
      (this_or_next | eq, ":party_no", "p_town_13"),
      (this_or_next | eq, ":party_no", "p_town_20"),
      (eq, ":party_no", "p_town_25"),]),
  
  # script_store_random_eastern_port_to_reg0
  # Component: Ships and sea (map)
  ("store_random_eastern_port_to_reg0",
    [(assign, reg0, "p_town_3"),
      (assign, reg1, "p_town_4"),
      (assign, reg2, "p_town_5"),
      (assign, reg3, "p_town_11"),
      (assign, reg4, "p_town_12"),
      (assign, reg5, "p_town_14"),
      (assign, reg6, "p_town_19"),
      (assign, reg7, "p_town_27"),
      (shuffle_range, 0, 8),]),
  
  # script_store_random_western_port_to_reg0
  # Component: Ships and sea (map)
  ("store_random_western_port_to_reg0",
    [(assign, reg0, "p_town_6"),
      (assign, reg1, "p_town_7"),
      (assign, reg2, "p_town_9"),
      (assign, reg3, "p_town_13"),
      (assign, reg4, "p_town_20"),
      (assign, reg5, "p_town_25"),
      (shuffle_range, 0, 6),]),
  
  # script_encode_values_to_reg0
  # Component: Ships and sea
  # Input: ":ship_wood", ":ship_sail", ":ship_paint", ":ship_dragon_head", ":ship_cargo"
  ("encode_values_to_reg0",
    [(store_script_param, ":ship_wood", 1),
      (store_script_param, ":ship_sail", 2),
      (store_script_param, ":ship_paint", 3),
      (store_script_param, ":ship_dragon_head", 4),
      (store_script_param, ":ship_cargo", 5),
      
      (val_mul, ":ship_sail", 10),			# ship sail
      (val_mul, ":ship_paint", 100),		# ship paint
      (val_mul, ":ship_dragon_head", 1000),	# ship dragon head
      (val_mul, ":ship_cargo", 10000),		# ship cargo
      
      (assign, reg0, ":ship_wood"),
      (val_add, reg0, ":ship_sail"),
      (val_add, reg0, ":ship_paint"),
      (val_add, reg0, ":ship_dragon_head"),
      (val_add, reg0, ":ship_cargo"),]),
  
  # script_decode_value
  # Component: Ships and sea
  ("decode_value",
    [(store_script_param, ":value", 1),
      
      # (set_fixed_point_multiplier, 1),
      
      (store_div, reg5, ":value", 10000),	# ship cargo
      (store_div, reg4, ":value", 1000),	# ship dragon head
      (store_div, reg3, ":value", 100),		# ship paint
      (store_div, reg2, ":value", 10),		# ship sail
      (store_div, reg1, ":value", 1),		# ship wood
      
      (store_mul, ":sub", reg2, 10),
      (val_sub, reg1, ":sub"),
      (store_mul, ":sub", reg3, 10),
      (val_sub, reg2, ":sub"),
      (store_mul, ":sub", reg4, 10),
      (val_sub, reg3, ":sub"),
      (store_mul, ":sub", reg5, 10),
      (val_sub, reg4, ":sub"),
  ]),
  
  # script_switch_to_land_consequences
  # Component: Ships and sea (map)
  ("switch_to_land_consequences",
    [#(display_message, "@switch_to_land_consequences"),
      
      (party_set_slot, "p_main_party", slot_party_on_water, 0),
      (assign, "$g_player_icon_state", pis_normal),
      (party_set_flags, "p_main_party", pf_is_ship, 0),
      
      (try_for_range, ":cur_center", walled_centers_begin, walled_centers_end),
        (party_slot_eq, ":cur_center",slot_town_port, 1),
        (party_set_flags, ":cur_center", pf_no_label, 0),
        (party_get_slot, ":curr_port", ":cur_center", slot_party_port_party),
        (party_set_flags, ":curr_port", pf_no_label, 1),
      (try_end),
      (party_set_flags, "p_landing_point", pf_no_label, 1),
      
      # new for VC-1878
      (try_begin),
        (party_slot_eq, "p_yourlair",slot_town_port, 1),
        (party_set_flags, "p_yourlair", pf_no_label, 0),
        (party_get_slot, ":curr_port", "p_yourlair", slot_party_port_party),
        (party_set_flags, ":curr_port", pf_no_label, 1),
      (try_end),
      
      # new for VC-1644
      (try_begin),
        (gt, "$cattle_in_ship", 0),
        (call_script, "script_create_cattle_herd", "p_main_party", "$cattle_in_ship"),
        # new for VC-2184:
        (check_quest_active,"qst_move_cattle_herd"),
        (quest_set_slot, "qst_move_cattle_herd", slot_quest_target_party, reg0),
      (try_end),
      # new for VC-1593
      (try_begin),
        (check_quest_active, "qst_escort_merchant_caravan"),
        (quest_get_slot, ":quest_target_party", "qst_escort_merchant_caravan", slot_quest_target_party),
        (party_is_active, ":quest_target_party"),
        (eq, "$escort_merchant_caravan_mode", 0),
        (try_begin),
          (store_distance_to_party_from_party, ":distance", "p_main_party", ":quest_target_party"),
          (le, ":distance", 7),
          (party_set_slot, ":quest_target_party", slot_party_on_water, 0),
          (party_set_flags, ":quest_target_party", pf_is_ship, 0),
          (call_script, "script_update_party_icon", ":quest_target_party"),
          (party_get_position, pos1, "p_main_party"),
          (party_set_position, ":quest_target_party", pos1),
        (else_try),
          (assign, "$escort_merchant_caravan_mode", 1),
          (display_message, "@You have lost the traders because you landed without them."),
        (try_end),
      (try_end),
      # new end
  ]),
  
  # script_switch_to_water_consequences
  # Component: Ships and sea (map)
  ("switch_to_water_consequences",
    [(party_set_slot, "p_main_party", slot_party_on_water, 1),
      (assign, "$g_player_icon_state", pis_ship),
      (party_set_flags, "p_main_party", pf_is_ship, 1),
      
      (try_for_range, ":cur_center", walled_centers_begin, walled_centers_end),
        (party_slot_eq, ":cur_center",slot_town_port, 1),
        (party_set_flags, ":cur_center", pf_no_label, 1),
        (store_faction_of_party, ":cur_faction", ":cur_center"),
        (party_get_slot, ":curr_port", ":cur_center", slot_party_port_party),
        (party_set_flags, ":curr_port", pf_no_label, 0),
        (party_set_faction, ":curr_port", ":cur_faction"),
      (try_end),
      (party_set_flags, "p_landing_point", pf_no_label, 0),
      
      # new for VC-1878
      (try_begin),
        (party_slot_eq, "p_yourlair",slot_town_port, 1),
        (party_set_flags, "p_yourlair", pf_no_label, 1),
        (store_faction_of_party, ":cur_faction", "p_yourlair"),
        (party_get_slot, ":curr_port", "p_yourlair", slot_party_port_party),
        (party_set_faction, ":curr_port", ":cur_faction"),
        (str_store_party_name, s1, "p_yourlair"),
        (party_set_name, ":curr_port", s1),
        (party_set_flags, ":curr_port", pf_no_label, 0),
      (try_end),
      
      # new for VC-1644
      (assign, "$cattle_in_ship", 0),
      (try_for_parties, ":cur_party"),
        (party_slot_eq, ":cur_party", slot_party_type, spt_cattle_herd),
        (party_slot_eq, ":cur_party", slot_cattle_driven_by_player, 1),
        (party_count_companions_of_type, ":num_cattle", ":cur_party", "trp_cattle"),
        (val_add, "$cattle_in_ship", ":num_cattle"),
        (remove_party, ":cur_party"),
      (try_end),
      # new for VC-1593
      (try_begin),
        (check_quest_active, "qst_escort_merchant_caravan"),
        (quest_get_slot, ":quest_target_party", "qst_escort_merchant_caravan", slot_quest_target_party),
        (party_is_active, ":quest_target_party"),
        (eq, "$escort_merchant_caravan_mode", 0),
        (try_begin),
          (store_distance_to_party_from_party, ":distance", "p_main_party", ":quest_target_party"),
          (le, ":distance", 7),
          (party_set_slot, ":quest_target_party", slot_party_on_water, 1),
          (party_set_flags, ":quest_target_party", pf_is_ship, 1),
          (call_script, "script_update_party_icon", ":quest_target_party"),
          (party_get_position, pos1, "p_main_party"),
          (party_set_position, ":quest_target_party", pos1),
        (else_try),
          (assign, "$escort_merchant_caravan_mode", 1),
          (display_message, "@You have lost the traders because you embarked without them."),
        (try_end),
      (try_end),
      # new end
  ]),
  
  # script_cf_crew_fit_in_ships
  # Component: Ships and sea (map)
  # This script fails if player party does not fit into the ships
  # Input: ":ship_location_party"
  # Output: reg1 = ":num_men", reg2 = ":max_crew_of_all_ships", reg3 = missing
  # room
  ("cf_crew_fit_in_ships",
    [(store_script_param, ":ship_location_party", 1),
      #(display_message, "@cf_crew_fit_in_ships"),
      (assign, ":max_crew_of_all_ships", 0),
      (try_for_range, ":curr_ship_type_slot", slot_party_player_ships_type_begin, slot_party_player_ships_type_end),
        (party_get_slot, ":ship_type", ":ship_location_party", ":curr_ship_type_slot"),
        (gt,  ":ship_type", 0),
        (call_script, "script_get_ship_properties", ":ship_type"),
        (val_add, ":max_crew_of_all_ships", reg6),		# reg6= max_crew of ship
      (end_try),
      (party_get_num_companions, ":num_men", "p_main_party"),
      (try_begin),
        (gt, ":num_men", ":max_crew_of_all_ships"),
        (store_sub, reg3, ":num_men", ":max_crew_of_all_ships"),
        #(display_message, "str_cant_set_sail"),
      (end_try),
      (assign, reg1, ":num_men"),
      (assign, reg2, ":max_crew_of_all_ships"),
      (le, ":num_men", ":max_crew_of_all_ships"),]),
  
  # script_get_next_water_position
  # Component: Ships and sea (map)
  # Input: pos1
  # Output: pos2
  ("get_next_water_position",
    [(store_script_param, ":radius", 1),
      #(display_message, "@get_next_water_position"),
      (try_begin),
        (lt, ":radius", 100),
        (map_get_water_position_around_position, pos2, pos1, ":radius"),
      (else_try),
        (lt, ":radius", 100),
        (val_add, ":radius", 1),
        (call_script, "script_get_next_water_position", ":radius"),
      (else_try),
        (display_message, "@{!}Error: get_next_water_position"),
      (end_try),]),
  
  # script_get_next_land_position
  # Component: Ships and sea (map)
  # Input: pos1
  # Output: pos2
  ("get_next_land_position",
    [(store_script_param, ":radius", 1),
      
      (try_begin),
        (map_get_land_position_around_position, pos2, pos1, ":radius"),
        (party_set_position, "p_temp_party", pos2),
        (party_get_current_terrain, ":terrain_type", "p_temp_party"),
        (neq, ":terrain_type", 0),
        (neq, ":terrain_type", 1), #cliffs
        (neq, ":terrain_type", 7),
        (neq, ":terrain_type", 8),
      (else_try),
        (assign, reg1, ":radius"),
        (try_begin),
          (ge, "$cheat_mode", 1),
          (display_message, "@{!}get_next_land_position failed with radius: {reg1}"),
        (end_try),
        (lt, ":radius", 100),
        (val_add, ":radius", 1),
        (call_script, "script_get_next_land_position", ":radius"),
      (else_try),
        (display_message, "@{!}Error: get_next_water_position"),
      (end_try),]),
  
  # script_setup_random_sea_scene
  # Component: Ships and sea
  ("setup_random_sea_scene",
    [(set_fixed_point_multiplier, 100),
      
      (party_get_position, pos1, "p_main_party"),
      # (position_get_z, ":z", pos1),
      # (assign, reg8, ":z"),
      # (display_message, "@z = {reg8}"),
      (try_begin),
        #(gt, ":z", 75),
        (map_get_land_position_around_position, pos2, pos1, 10),
        (party_set_position, "p_temp_party", pos2),
        (party_get_current_terrain, ":terrain_type", "p_temp_party"),
        (neq, ":terrain_type", 0),
        (neq, ":terrain_type", 7),
        (neq, ":terrain_type", 8),
        # making outer terrain look better
        (get_global_haze_amount, ":haze"),
        (val_min, ":haze", 45),
        (set_global_haze_amount, ":haze"),
        (try_begin),
          (position_get_x, ":x", pos1),
          (position_get_y, ":y", pos1),
          (gt, ":y", 4000),
          (gt, ":x", 17000),	#norway
          (store_random_in_range, ":scene_to_use", "scn_sea_battle_fjord_1", "scn_sea_battle_cliffs_1"),	# 2 fjord scenes
        (else_try),
          (party_get_position, pos2, "p_temp_party"),
          (position_get_z, ":z", pos2),
          # (assign, reg8, ":z"),
          # (display_message, "@z = {reg8}"),
          (gt, ":z", 170),
          #(eq, ":terrain_type", 1), #cliffs
          (store_random_in_range, ":scene_to_use", "scn_sea_battle_cliffs_1", "scn_town_3_coastal"),	# 2 cliff scenes
        (else_try),
          (store_random_in_range, ":scene_to_use", "scn_sea_battle_hills_1", "scn_sea_battle_fjord_1"),	# 2 hill scenes
        (end_try),
      (else_try),
        (assign, ":scene_to_use", "scn_sea_battle"),
      (end_try),
      (jump_to_scene, ":scene_to_use"),]),
  
  # script_calculate_party_speed_water
  # Component: Ships and sea (map)
  # Input: ":party_no"
  ("calculate_party_speed_water",
    [(store_script_param, ":party_no", 1),
      
      (try_begin),
        (eq, ":party_no", "p_main_party"), # PLAYER (Getting speed of slowest ship ...)
        (assign, ":ship_speed", 20),	# = max speed
        (try_for_range, ":curr_slot", slot_party_1_ship_type, slot_party_8_ship_type),
          (party_get_slot, ":ship_type", ":party_no", ":curr_slot"),
          (gt, ":ship_type", 0),
          (call_script, "script_get_ship_properties", ":ship_type"),
          # new begin
          (assign, ":curr_ship_speed", reg1),
          (store_add, ":cond_slot", ":curr_slot", 20),
          (party_get_slot, ":ship_cond", ":party_no", ":cond_slot"),
          (val_mul, ":curr_ship_speed", ":ship_cond"),
          (val_div, ":curr_ship_speed", 100),
          (lt, ":curr_ship_speed", ":ship_speed"),
          # new end
          (assign, ":ship_speed", reg1),
        (end_try),
      (else_try),
        (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
        (assign, ":ship_speed", 12),
      (else_try),
        (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_caravan),
        (assign, ":ship_speed", 7),
      (else_try),
        (store_faction_of_party, ":party_faction", ":party_no"),
        (this_or_next | eq, ":party_faction", "fac_outlaws"),
        (this_or_next | eq, ":party_faction", "fac_manhunters"),
        (this_or_next | eq, ":party_faction", "fac_mountain_bandits"),
        (this_or_next | eq, ":party_faction", "fac_forest_bandits"),
        (eq, ":party_faction", "fac_deserters"),
        (assign, ":ship_speed", 13),
      (else_try),
        (party_get_template_id, ":party_template", ":party_no"),
        (this_or_next | eq,":party_template","pt_sea_king_norweg"),
        (eq,":party_template","pt_sea_king_danish"),
        (assign, ":ship_speed", 14),
      (else_try),
        (assign, ":ship_speed", 8),
      (try_end),
      
      #new:
      (store_mul, ":wind_speed", "$beaufort", 3),
      (val_min, ":wind_speed", 18),
      (store_add, ":speed_multiplier", ":ship_speed", ":wind_speed"),
      (val_max, ":speed_multiplier", 5), 	# basic speed
      
      #navigation skill
      (try_begin),
        (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
        (party_stack_get_troop_id, ":party_leader", ":party_no", 0),
        (troop_is_hero, ":party_leader"),
        (store_skill_level, ":cur_navigation_skill", "skl_navigation", ":party_leader"),
        # (try_begin),
        # (ge, "$cheat_mode", 1),
        # (str_store_party_name, s1, ":party_no"),
        # (assign, reg1, ":cur_navigation_skill"),
        # (display_message, "@{s1} is travelling on sea with a navigation skill of
        # {reg1}."),
        # (end_try),
      (else_try),
        (eq, ":party_no", "p_main_party"),
        (party_get_skill_level, ":cur_navigation_skill", "p_main_party", "skl_navigation"),
      (else_try),
        #(party_get_skill_level, ":cur_navigation_skill", ":party_no",
        #"skl_navigation"),
        (assign, ":cur_navigation_skill", 0),
      (end_try),
      (store_mul, ":skill_speed_bonus", ":cur_navigation_skill", ":speed_multiplier"),
      (val_mul, ":skill_speed_bonus", 5),	# new: 5% for every skill point (was 3)
      (val_div, ":skill_speed_bonus", 100),
      
      #profesional sailors
      (assign, reg5, 0),					#for presentation
      (assign, reg6, 0),					#for presentation
      (assign, ":troop_speed_bonus", 0),
      (try_begin),
        (eq, ":party_no", "p_main_party"),
        # new changes for VC-805:
        (store_party_size_wo_prisoners, ":party_size", "p_main_party"),
        (gt, ":party_size", 0),
        (party_count_companions_of_type, ":num_sailors", "p_main_party", "trp_regular_sailors"),
        
        (assign, ":num_norse_troops", 0),
        #(assign, ":num_viking_troops", 0),
        (try_for_range, ":troop", "trp_norse_elitearcher", "trp_norse_priest"),
          (store_character_level, ":level", ":troop"),
          (ge, ":level", 23),
          (party_count_companions_of_type, ":count", "p_main_party", ":troop"),
          (gt, ":count", 0),
          (val_add, ":num_norse_troops", ":count"),
        (end_try),
        (try_for_range, ":troop", "trp_steppe_bandit", "trp_sea_raider_leader2"),
          (party_count_companions_of_type, ":count", "p_main_party", ":troop"),
          (gt, ":count", 0),
          (val_add, ":num_norse_troops", ":count"),
        (end_try),
        (store_add, reg5, ":num_norse_troops", ":num_sailors"),	#for presentation
        
        (val_mul, ":num_sailors", 80),
        (store_div, ":sailor_speed_percent", ":num_sailors", ":party_size"),
        (val_mul, ":num_norse_troops", 40),
        (store_div, ":norse_speed_percent", ":num_norse_troops", ":party_size"),
        (store_add, ":troop_speed_percent", ":norse_speed_percent", ":sailor_speed_percent"),
        
        (val_min, ":troop_speed_percent", 40),	# troop speed bonus limit cap of 40% VC-2124
        (assign, reg6, ":troop_speed_percent"),					#for presentation
        (store_mul, ":troop_speed_bonus", ":troop_speed_percent", ":speed_multiplier"),
        (val_div, ":troop_speed_bonus", 100),
      (end_try),
      
      #Adding the boni
      (val_add, ":speed_multiplier", ":skill_speed_bonus"),
      (val_add, ":speed_multiplier", ":troop_speed_bonus"),
      
      (assign, ":multiplier", 190), 			# 190% = final balancing #was 170
      (try_begin),
        (eq, ":party_no", "p_main_party"),
        (assign, ":multiplier", 180), 			# -10% for player #was 150
      (end_try),
      (store_random_in_range, ":add", -2, 3),	# random effect is for stopping parties to stick together/ being unable to
      # catch each other
      (val_mul, ":add", 2), 					# -4, -2, 0, 2, 4 %
      (val_add, ":multiplier", ":add"),
      (val_mul, ":speed_multiplier", ":multiplier"),
      (val_div, ":speed_multiplier", 100),
      
      (assign, reg1, ":speed_multiplier"),
      (try_begin),
        (neg | is_between, reg1, 0, 200),
        (ge, "$vc_debug_mode", 1),
        (display_message, "@{!}WARNING: speed_multiplier = {reg1}"),
      (end_try),]),
  
  # script_visit_town_port
  # Component: Ships and sea
  # ("visit_town_port",
    # [(assign, "$talk_context", 0),
      # (try_begin),
        # (call_script, "script_cf_enter_center_location_bandit_check"),
      # (else_try),
        
        # (try_begin),
          # (assign, ":port_scene", "scn_port"),
        # (try_end),
        
        # (modify_visitors_at_site, ":port_scene"),
        # (reset_visitors),
        # (assign, "$g_mt_mode", tcm_default),
        # (set_visitor, 13, "trp_port_crew"),
        # (party_get_slot, reg0, "$current_town", slot_town_shipwright),
        # (set_visitor, 14, reg0),
        # (try_begin),
          # (party_slot_eq, "$current_town", slot_center_has_slavemarket, 1),
          # (set_visitor, 15, "trp_port_pirate"),
        # (try_end),
        # # Setup peasant walkers
        # (try_for_range, ":visiterator", 32, 40),
          # (store_random_in_range, ":sex", 0, 100),
          # (try_begin),
            # (lt,":sex",50),
            # (set_visitor, ":visiterator", "trp_village_walker_5",),
          # (else_try),
            # (set_visitor, ":visiterator", "trp_village_walker_6",),
          # (try_end),
        # (try_end),
        
        # (call_script, "script_init_town_walkers"),
        # (set_jump_mission,"mt_port_visit"),
        # (try_begin),
          # (eq, "$town_entered", 0),
          # (assign, "$town_entered", 1),
          # (eq, "$town_nighttime", 0),
        # (try_end),
        # (set_jump_entry, 1),
        # (jump_to_scene, ":port_scene"),
        # (change_screen_mission,0),
      # (try_end),]),
  
  # script_move_ship_instance_from_slot_to_slot
  # Component: Ships and sea (map)
  # Input: ":party1", ":party2"
  # Output: ship_types _names and _quaitys
  ("move_ship_instance_from_slot_to_slot",
    [(set_fixed_point_multiplier, 100),
      (store_script_param, ":origin_slot_no", 1),
      (store_script_param, ":destination_slot_no", 2),
      
      (store_add, ":dest_entry_no", 50, ":destination_slot_no"),
      (entry_point_get_position, pos1, ":dest_entry_no"),
      (scene_prop_get_num_instances, ":number_of_ships", "spr_dyn_ship_substrate"),
      (try_for_range,":ship_number", 0, ":number_of_ships"),
        (scene_prop_get_instance, ":ship_instance", "spr_dyn_ship_substrate", ":ship_number"),
        (scene_prop_slot_eq, ":ship_instance", scene_prop_ship_number, ":origin_slot_no"),
        # correcting position
        (scene_prop_get_slot, ":ship_type", ":ship_instance", scene_prop_ship_type),
        (call_script, "script_get_ship_properties", ":ship_type"),
        (position_move_x, pos1, reg5),				# ship radius
        (position_get_rotation_around_y, ":y_rotation", pos1),
        (val_mul, ":y_rotation", -1),
        (position_rotate_y, pos1, ":y_rotation"),
        (position_rotate_z, pos1, 90),				# correkt angle
        (set_spawn_position, pos1),
        # correcting position end
        (prop_instance_stop_animating, ":ship_instance"),
        (prop_instance_set_position, ":ship_instance", pos1),
        (scene_prop_set_slot, ":ship_instance", scene_prop_ship_number, ":destination_slot_no"),
      (end_try),]),
  
  # script_move_ship_instance_from_slot_to_nowhere
  # Component: Ships and sea (map)
  # Input: ":party1"
  # Output: ship_types _names and _quaitys
  ("move_ship_instance_from_slot_to_nowhere",
    [(set_fixed_point_multiplier, 100),
      (store_script_param, ":origin_slot_no", 1),
      
      (scene_prop_get_num_instances, ":number_of_ships", "spr_dyn_ship_substrate"),
      (try_for_range,":ship_number", 0, ":number_of_ships"),
        (scene_prop_get_instance, ":ship_instance", "spr_dyn_ship_substrate", ":ship_number"),
        (scene_prop_slot_eq, ":ship_instance", scene_prop_ship_number, ":origin_slot_no"),
        # correcting position
        (init_position, pos1),
        (position_set_x, pos1, -100000),
        (position_set_y, pos1, -100000),
        # correcting position end
        (prop_instance_stop_animating, ":ship_instance"),
        (prop_instance_set_position, ":ship_instance", pos1),
        (scene_prop_set_slot, ":ship_instance", scene_prop_ship_number, -1),
      (end_try),]),
  
  # script_give_player_ships_from_party_to_party
  # Component: Ships and sea (map)
  # Input: ":party1", ":party2"
  # Output: ship_types _names and _quaitys
  ("give_player_ships_from_party_to_party",
    [(store_script_param, ":party1", 1),
      (store_script_param, ":party2", 2),
      (try_for_range, ":current_slot", slot_party_player_ships_type_begin, slot_party_player_ships_type_end),
        (party_get_slot, ":slot_value", ":party1", ":current_slot"),
        (party_set_slot, ":party2", ":current_slot", ":slot_value"),
        (party_set_slot, ":party1", ":current_slot", 0),
      (end_try),
      (try_for_range, ":current_slot", slot_party_player_ships_name_begin, slot_party_player_ships_name_end),
        (party_get_slot, ":slot_value", ":party1", ":current_slot"),
        (party_set_slot, ":party2", ":current_slot", ":slot_value"),
        (party_set_slot, ":party1", ":current_slot", 0),
      (end_try),
      (try_for_range, ":current_slot", slot_party_player_ships_quality_begin, slot_party_player_ships_quality_end),
        (party_get_slot, ":slot_value", ":party1", ":current_slot"),
        (party_set_slot, ":party2", ":current_slot", ":slot_value"),
        (party_set_slot, ":party1", ":current_slot", 0),
      (end_try),
      (try_for_range, ":current_slot", slot_party_player_ships_propertys_begin, slot_party_player_ships_propertys_end),
        (party_get_slot, ":slot_value", ":party1", ":current_slot"),
        (party_set_slot, ":party2", ":current_slot", ":slot_value"),
        (party_set_slot, ":party1", ":current_slot", 0),
      (end_try),]),
  
  # script_give_ships_to_towns
  # Component: Ships and sea (map)
  # This script starts a loop for all towns.  It gives random amount of ship,
  # with random ship types.  Rest of ship slots is set to 0.
  # Output: ship_types _names and _quaitys for all towns (random)
  ("give_ships_to_towns",
    [   (try_begin),
        (try_for_range, ":curr_town", walled_centers_begin, walled_centers_end),
          (party_slot_eq,":curr_town",slot_town_port, 1),	#!!!!
          (store_random_in_range, ":number_of_ships_in_town", 0, 4), # 0-3
          (try_begin),
            (gt, ":number_of_ships_in_town", 0),
            (try_for_range, ":current_slot", 0, ":number_of_ships_in_town"),
              # ship type
              (try_begin),
                (this_or_next | eq, ":curr_town", "p_town_4"), # Ribe # better ships for norse factions
                (this_or_next | eq, ":curr_town", "p_town_5"), # Tunsberg
                (this_or_next | eq, ":curr_town", "p_town_11"), # Dorestad
                (eq, ":curr_town", "p_castle_52"),			 # Haithabu
                (store_random_in_range, ":ship_type", 1, 6),	# skei - Knorr (busse not ready yet)
              (else_try),
                (store_random_in_range, ":ship_type", 3, 7),	# Karvi - Byrding # only weaker/smaller ships in all other ports
              (end_try),
              (store_add, ":calculated_slot", ":current_slot", slot_party_8_ship_type), # because ship8 is the first ship_slot that is not owned by player
              (party_set_slot, ":curr_town", ":calculated_slot", ":ship_type"),
              # ship name
              (store_random_in_range, ":ship_name", "str_ship_name_1", "str_ship_names_end"),
              (store_add, ":calculated_slot", ":current_slot", slot_party_8_ship_name),
              (party_set_slot, ":curr_town", ":calculated_slot", ":ship_name"),
              # ship condition
              (store_random_in_range, ":ship_condition", 21, 101),
              (store_add, ":calculated_slot", ":current_slot", slot_party_8_ship_quality),
              (party_set_slot, ":curr_town", ":calculated_slot", ":ship_condition"),
              
              (store_random_in_range, ":ship_wood", 1, 4),	#1-3
              (store_random_in_range, ":ship_sail", 1, 10),	#0-9 #0=customized sail
              (store_random_in_range, ":ship_paint", 0, 9),	#0-8
              (store_random_in_range, ":ship_dragon_head", 0, 3),	#0-2
              (store_random_in_range, ":ship_cargo", 0, 3),	#0-2
              (call_script, "script_encode_values_to_reg0", ":ship_wood", ":ship_sail", ":ship_paint", ":ship_dragon_head", ":ship_cargo"),
              (assign, ":ship_properties", reg0),
              
              (store_add, ":calculated_slot", ":current_slot", slot_party_8_ship_propertys),
              (party_set_slot, ":curr_town", ":calculated_slot", ":ship_properties"),
            (try_end),
          (try_end),
          (try_begin),
            (val_add, ":number_of_ships_in_town", 1),
            (lt, ":number_of_ships_in_town", 3),
            (try_for_range, ":current_slot", ":number_of_ships_in_town", 3),
              (store_add, ":calculated_slot", ":current_slot", slot_party_8_ship_type),
              (party_set_slot, ":curr_town", ":calculated_slot", 0),
            (try_end),
          (try_end),
        (try_end),
      (try_end),]),
  
  # script_calculate_movement_to_pos1
  # Component: Ships and sea (scene)
  # This scriptgives the next ship Position and also does some other stuff
  # Input: ":ship_number", pos1
  # Output: pos1
  ("calculate_movement_to_pos1",
    [   ### GET DATA
      (try_begin),
        (store_script_param, ":ship_number", 1),
        (scene_prop_get_instance, ":ship_instance", "spr_dyn_ship_substrate", ":ship_number"),
        #Commands:
        (scene_prop_get_slot, ":sail", ":ship_instance", 6),
        (scene_prop_get_slot, ":rowing", ":ship_instance", 7),
        (scene_prop_get_slot, ":rudder", ":ship_instance", 8),
        #Last Situation:
        (scene_prop_get_slot, ":ship_speed", ":ship_instance", 9),
        (scene_prop_get_slot, ":ship_turn", ":ship_instance", 10),
        #Ship properties
        (scene_prop_get_slot, ":ship_max_speed", ":ship_instance", scene_prop_max_speed),
        (scene_prop_get_slot, ":ship_max_x_rotation", ":ship_instance", scene_prop_max_x_rotation),
        (scene_prop_get_slot, ":ship_quality", ":ship_instance", scene_prop_quality),
        (scene_prop_get_slot, ":ship_type", ":ship_instance", scene_prop_ship_type),
      (try_end),
      
      ### SAIL CALCULATION
      (assign, ":sail_boost", 0),
      (try_begin),
        (eq, ":sail", 1),					# whole function only if sail is set
        (gt, "$wind_strenght", 0),			# only if there is wind
        
        (prop_instance_get_position, pos2, "$wind_spr"),
        (get_angle_between_positions, ":angle_diff", pos1, pos2),
        
        (try_begin),
          (is_between, ":angle_diff", -4500, 4500),
          (assign, ":sail_boost", 100),		# 100%
        (else_try),
          (is_between, ":angle_diff", -9000, 9000),
          (assign, ":sail_boost", 90),
        (else_try),
          (is_between, ":angle_diff", -13500, 13500),
          (assign, ":sail_boost", 80),
        (else_try),
          (assign, ":sail_boost", 0),
        (try_end),
        
        (val_mul, ":sail_boost", "$wind_strenght"),
        (val_div, ":sail_boost", 100),
      (try_end),
      
      ### ROWING CALCULATION
      (call_script, "script_get_ship_properties", ":ship_type"),
      (store_div, ":rower_for_max_speed", reg6, 2),					# = max_crew / 2
      (store_div, ":guaranteed_rower", ":rower_for_max_speed", 3),	# in order to give small crews a minimum of speed, they will be handled like
      # they have 1/4 of the crew for max speeed
      (try_begin),
        (assign, ":rowing_boost", ":rowing"),
        (scene_prop_get_slot, ":num_of_rower", ":ship_instance", scene_prop_crew_number),
        (try_begin),
          (gt, ":num_of_rower", ":rower_for_max_speed"),
          (assign, ":num_of_rower", ":rower_for_max_speed"),
        (else_try),
          (is_between, ":num_of_rower", 1, ":guaranteed_rower"),	# To get a minimum of speed even for small crew
          (assign, ":num_of_rower", ":guaranteed_rower"),
        (end_try),
        (val_mul, ":rowing_boost", ":num_of_rower"),
        (val_div, ":rowing_boost", ":rower_for_max_speed"),
      (try_end),
      
      ### COMBINING ROWING_BOOST WITH SAIL_BOOST TO SOLL_SPPED
      (try_begin),
        (assign, ":soll_speed", 0),
        (val_mul, ":rowing_boost", 60),		# Rowing can make up to 60% of max speed
        (val_div, ":rowing_boost", 100),
        # (val_mul, ":sail_boost", 75), # Sail can make up to 75% of max speed
        # (val_div, ":sail_boost", 100),
        (val_add, ":soll_speed", ":rowing_boost"),
        (val_add, ":soll_speed", ":sail_boost"),
        (try_begin),
          (gt, ":soll_speed", 100),
          (assign, ":soll_speed", 100),
        (else_try),
          (lt, ":soll_speed", -100),
          (assign, ":soll_speed", -100),
        (end_try),
      (end_try),
      
      ### SPEED CALCULATION
      (try_begin),
        (val_add, ":soll_speed", 100),	# to make calculation easy, will be substracted later
        (val_add, ":ship_speed", 100),	#
        (assign, ":diff", 0),
        (try_begin),
          (le, ":ship_speed", ":soll_speed"),
          (store_sub, ":diff",  ":soll_speed", ":ship_speed"),
          (try_begin),
            (gt, ":diff", 20),
            (val_div, ":diff", 20),
          (else_try),
            (assign, ":diff", 1),
          (try_end),
          (val_sub, ":ship_speed", 100),
          (val_add, ":ship_speed", ":diff"),
        (else_try),
          (ge, ":ship_speed", ":soll_speed"),
          (assign, ":diff", 1),
          (val_sub, ":ship_speed", 100),
          (val_sub, ":ship_speed", ":diff"),
        (try_end),
        
        (try_begin),						#  Correcting errors
          (gt, ":ship_speed", 100),
          (assign, ":ship_speed", 100),
        (else_try),
          (lt, ":ship_speed", -100),
          (assign, ":ship_speed", -100),
        (end_try),
      (try_end),
      
      ### TURN CALCULATION
      (try_begin),
        (lt, ":ship_turn", ":rudder"),
        (val_add, ":ship_turn", 10),
      (else_try),
        (gt, ":ship_turn", ":rudder"),
        (val_add, ":ship_turn", -10),
      (try_end),
      
      ### Check Direction changes
      (assign, ":direction", 1),
      (try_begin),		# Turn 0, when the speed is 0
        (lt, ":ship_speed", 0),
        (assign, ":direction", -1),
      (try_end),
      
      ### WAKE
      (try_begin),
        (neg | is_between, ":ship_speed", -15, 20),
        (prop_instance_get_position, pos14, ":ship_instance"),
        # correct additional angles
        (assign, ":power", 4),
        (val_mul, ":power", ":ship_speed"),
        (val_mul, ":power", ":direction"),
        (val_div, ":power", 100),				# transforming % to number
        (assign, ":power", 1),	#test
        (particle_system_burst, "psys_heck_water", pos14, ":power"),
        (position_rotate_z, pos14, 180),
        (particle_system_burst, "psys_heck_water", pos14, ":power"),
        #
        (prop_instance_get_position, pos14, ":ship_instance"),
        (scene_prop_get_slot, ":ship_distance_to_front", ":ship_instance", scene_prop_distance_to_front),
        (val_sub, ":ship_distance_to_front", 25),
        (position_move_x, pos14, ":ship_distance_to_front"),
        (position_rotate_z, pos14, -17),
        (particle_system_burst, "psys_front_water_2", pos14, ":power"),
        (position_rotate_z, pos14, 17),
        (position_rotate_z, pos14, 180),
        (position_rotate_z, pos14, 17),
        (particle_system_burst, "psys_front_water_2", pos14, ":power"),
      (try_end),
      
      ### VALUE CALCULATION
      (val_max, ":ship_quality", 75),	# To avoid super slow ships
      (try_begin),
        # x-distance drive
        (assign, ":x_drive_distance", ":ship_speed"),			# here ship_speed has the function of the new speed
        (val_mul, ":x_drive_distance", 52),					# Speed multkiplikator 1knot = 51.4 m/s
        (val_mul, ":x_drive_distance", ":ship_max_speed"),	# max speed in knots
        (val_mul, ":x_drive_distance", ":ship_quality"),		# low quality makes ship slower
        (val_div, ":x_drive_distance", 10000),				# transforming % to number (100% rowing x 100% quality / 10000 = 1)
        # y-distance drive
        (assign, ":y_drive_distance", ":ship_turn"),			# here ship_turn has the function of the new turn
        (val_mul, ":y_drive_distance", ":ship_quality"),
        (val_mul, ":y_drive_distance", 5),					# max y-turn #TEESTING (LOWERED, was 42)
        (val_div, ":y_drive_distance", 10000),					# transforming % to number (has to be last calculation
        # z-turn angle
        (assign, ":z_turn", ":ship_turn"),				# here ship_turn has the function of the new turn
        #(val_mul, ":z_turn", ":ship_quality"),
        #(val_mul, ":z_turn", 15), # max z-turn (rotate) multkiplikator
        ##TEESTING (was 8)
        (try_begin),
          (eq, ":sail", 1),
          (val_mul, ":z_turn",  9),
        (else_try),
          (val_mul, ":z_turn", 17),					# ship turns faster if it uses oars
        (end_try),
        (val_mul, ":z_turn", ":direction"),
        (val_div, ":z_turn", 100),						# transforming % to number (has to be last calculation
        ### x-turn angle
        (assign, ":x_turn", ":ship_turn"),				# here last_pitch has the function of the new turn
        (val_mul, ":x_turn", ":ship_max_x_rotation"),	# max x-pitch (rotate) multkiplicator
        (val_mul, ":x_turn", ":ship_speed"),
        (val_mul, ":x_turn", ":direction"), 			# to prevnt negativ value
        (val_div, ":x_turn", 100),
        
        ### CHANGES FOR D-DAY SCENE
        (try_begin),
          (store_current_scene, ":curr_scene"),
          (eq, ":curr_scene", "scn_d_day"),
          (assign, ":x_drive_distance", 150),
        (end_try),
        
        ### changes because of frequence of main trigger
        (val_div, ":x_drive_distance", 4),				# Depends on frequence of main trigger
        (val_div, ":y_drive_distance", 4),				# Depends on frequence of main trigger
        (val_div, ":z_turn", 4),					# Depends on frequence of main trigger
        
      (try_end),
      
      
      
      ### POSITION CALCULATION
      (try_begin),
        (set_fixed_point_multiplier, 100),
        (position_move_x, pos1, ":x_drive_distance"),
        (position_move_y, pos1, ":y_drive_distance"),
        (position_rotate_z, pos1, ":z_turn"),
        #(position_rotate_x_floating , pos1, ":x_turn"),
      (try_end),
      
      (try_begin),
        ### OUTPUT
        (is_between, ":ship_speed", -100, 101),
        (is_between, ":ship_turn", -100, 101),
        (scene_prop_set_slot, ":ship_instance", 9, ":ship_speed"),
        (scene_prop_set_slot, ":ship_instance", 10, ":ship_turn"),
      (try_end),]),
  
  # script_calculate_rocking_to_pos1
  # Component: Ships and sea (scene)
  # This script can move objects in a scene according to the water-wave shader
  ("calculate_rocking_to_pos1",
    [(set_fixed_point_multiplier, 100),
      (try_begin),
        # Y DATA
        (position_get_y, ":y_pos", pos1),
        (store_mul, reg1, "$WaveNumber_y", ":y_pos"),
        (val_div, reg1, 10000),	# 2 for WaveNumber and 2 for fixed point
        (store_add, ":future_wave_timer", "$wave_timer", "$wave_timer_diff"),	# Because timer will have 12,5 more then
        (store_mul, ":omega_mul_timer", "$omega_y", ":future_wave_timer"),
        (val_div, ":omega_mul_timer", 10),
        (store_sub, ":radian_value", reg1, ":omega_mul_timer"),
        (store_mul, ":degree_value", ":radian_value", 18000),
        (val_div, ":degree_value", 314),
        (store_sin, ":y_sinus", ":degree_value"),
        (store_mul, ":z_amplitude_y", ":y_sinus", "$Amplitude_y"),
        (val_div, ":z_amplitude_y", 10000),
        (store_cos, "$y_cosinus", ":degree_value"),
        (store_div, reg1, "$Amplitude_y", 200),	# /100 /2
        (store_mul, reg2, "$lamda_y", 16), #old: 25 (*100 /4)
        (store_mul, ":max_angle", 90, reg1),
        (val_mul, ":max_angle", 100),	#Because I changed Lamda from meter to cm
        (val_div, ":max_angle", reg2),
        (store_mul, ":x_angle", "$y_cosinus", ":max_angle"),
        
        # X DATA
        (position_get_x, ":x_pos", pos1),
        (store_mul, reg1, "$WaveNumber_x", ":x_pos"),
        (val_div, reg1, 10000),	# 2 for WaveNumber and 2 for fixed point
        (store_sub, ":radian_value", reg1, ":omega_mul_timer"),
        (store_mul, ":degree_value", ":radian_value", 18000),
        (val_div, ":degree_value", 314),
        (store_sin, ":x_sinus", ":degree_value"),
        (store_mul, ":z_amplitude_x", ":x_sinus", "$Amplitude_x"),
        (val_div, ":z_amplitude_x", 10000),
        (store_cos, ":x_cosinus", ":degree_value"),
        (store_div, reg1, "$Amplitude_x", 200),	# /100 /2
        (store_mul, reg2, "$lamda_x", 16), #old: 25 (*100 /4)
        (store_mul, ":max_angle", -90, reg1),
        (val_mul, ":max_angle", 100),	#Because I changed Lamda from meter to cm
        (val_div, ":max_angle", reg2),
        (store_mul, ":y_angle", ":x_cosinus", ":max_angle"),
        
        # ROTATION
        (position_get_rotation_around_x, ":rot", pos1),
        (val_mul, ":rot", -1),
        (position_rotate_x_floating, pos1, ":rot"),
        (position_get_rotation_around_y, ":rot", pos1),
        (val_mul, ":rot", -1),
        (position_rotate_y_floating, pos1, ":rot"),
        
        (init_position, pos8),
        (get_angle_between_positions, ":z_rot", pos8, pos1),
        (position_copy_rotation, pos1, pos8),
        (position_rotate_x_floating, pos1, ":x_angle"),
        (position_rotate_y_floating, pos1, ":y_angle"),
        (position_rotate_z_floating, pos1, ":z_rot"),
        
        
        # X+Y DATA
        (store_add, ":z_amplitude", ":z_amplitude_x", ":z_amplitude_y"),
        (store_add, ":origin", "$Amplitude_x", "$Amplitude_y"),
        (try_begin),
          (lt, ":origin", 100),
          (assign, ":origin", 0),
        (end_try),
        (val_div, ":origin", 100),
        (val_add, ":z_amplitude", ":origin"),
        #(val_sub, ":z_amplitude", 33),
        (position_set_z, pos1, ":z_amplitude"),
      (end_try),]),
  
  # script_set_wave_shader
  # Component: Ships and sea (scene)
  # This script is controlling the sea (water-wave shader)
  ("set_wave_shader",
    [(store_div, "$WaveNumber_x", 3141592 * 2, "$lamda_x"),
      (store_div, "$WaveNumber_y", 3141592 * 2, "$lamda_y"),
      (store_mul, "$omega_y", "$WaveNumber_y", "$wavespeed_y"),
      (val_div, "$omega_y", 10000),	# 2 + 2 new for wavespeed
      # no "$omega_x" yet
      (try_begin),
        (neg | multiplayer_is_dedicated_server),
        (set_fixed_point_multiplier,10000),
        (set_shader_param_float4, "@vWaveInfo", "$Amplitude_x", "$Amplitude_y", "$WaveNumber_x", "$WaveNumber_y"),
        (store_add, ":Origin_z", "$Amplitude_x", "$Amplitude_y"),
        (try_begin),
          (lt, ":Origin_z", 10000),
          (assign, ":Origin_z", 0),
        (end_try),
        (set_shader_param_float4, "@vWaveOrigin", 0, 0, ":Origin_z", 0),
        (set_fixed_point_multiplier,100),
      (end_try),]),
  
  # script_set_wave_timer
  # Component: Ships and sea (scene)
  ("set_wave_timer",
    [(store_mul, "$minus_omega_mul_timer", "$omega_y", "$wave_timer"),
      (val_mul, "$minus_omega_mul_timer", -1),
      (set_fixed_point_multiplier, 10000),
      (set_shader_param_float, "@vTimer", "$minus_omega_mul_timer"),
      (set_fixed_point_multiplier, 100),]),
  
  # script_calculate_wank_to_pos1
  # Component: Ships and sea (scene)
  # Input: ":ship_number", pos1
  # Output: pos1
  # ("calculate_wank_to_pos1",
    # [(try_begin),
        
        # ### GET DATA
        # (store_script_param, ":ship_number", 1),
        # (scene_prop_get_instance, ":ship_instance", "spr_dyn_ship_substrate", ":ship_number"),
        # (scene_prop_get_slot, ":wank_state", ":ship_instance", 11),#Letzter Stand
        # (scene_prop_get_slot, ":ship_max_y_rotation", ":ship_instance", scene_prop_max_y_rotation),
        # (scene_prop_get_slot, ":ship_distance_to_front", ":ship_instance", scene_prop_distance_to_front),
        
        # ### CONDITIONS
        # #(call_script, "script_check_aground", ":ship_number"),
        # #(ge, reg12, 200),
        # #(ge, reg6, 200),
        
        # ### WANK CALCULATION
        
        # (try_begin),
          # (is_between, ":wank_state", -100, 100),
          # (val_add, ":wank_state", 25),
          # (assign, ":wank", ":wank_state"),
        # (else_try),
          # (is_between, ":wank_state", 100, 300),
          # (val_add, ":wank_state", 25),
          # (assign, ":wank", ":wank_state"),
          # (val_add, ":wank", -200),
          # (val_mul, ":wank", -1),
        # (else_try),
          # (eq, ":wank_state", 300),
          # (assign, ":wank_state", -75),
          # (assign, ":wank", -75),
        # (else_try),
          # (display_debug_message,"@Wank_ERROR!"),
          # (assign, ":wank", 0),
        # (try_end),
        
        # ### How strong?  (Depends on speed)
        # (try_begin),		# wank slow, when the speed is slow
          # (scene_prop_get_slot, ":ship_speed", ":ship_instance", 9),
          # (ge, ":ship_speed", 0),
          # (assign, ":wank_copy", ":wank"),
          # (val_mul, ":wank", ":ship_speed"),
          # (val_div, ":wank", 100),
          # (val_mul, ":wank", 2),
          # (val_add, ":wank", ":wank_copy"),
          # (val_div, ":wank", 3),
        # (try_end),
        
        # ### WATER SPLASH
        # (prop_instance_get_position, pos4, ":ship_instance"),
        # (position_move_x, pos4, ":ship_distance_to_front"),
        # (try_begin),
          # (eq, ":wank_state", 100),
          # (assign, ":power", 10),				# max power for splash
          # (val_mul, ":power", ":wank"),
          # (val_div, ":power", 75),				# because max wank is 75
          # (position_rotate_z, pos4, 45),
          # (particle_system_burst, "psys_front_water", pos4, ":power"),
          # (position_rotate_z, pos4, -90),
          # (particle_system_burst, "psys_front_water", pos4, ":power"),
          # (play_sound_at_position, "snd_front_water_end", pos4),
          # # (try_begin),
          # # (eq, "$player_ship_number", ":ship_number"),
          # # (play_sound, "snd_front_water", 1),
          # # (play_sound_at_position, "snd_front_water", pos4, 1),
          # # (try_end),
        # (else_try),
          # (eq, ":wank_state", 125),
          # (play_sound_at_position, "snd_front_water_begin", pos4),
        # (try_end),
        
        # ### OUTPUT
        # (try_begin),
          # (set_fixed_point_multiplier, 100),
          # (val_mul, ":wank", ":ship_max_y_rotation"),			# max wank in angle
          # (position_rotate_y_floating, pos1, ":wank"),			# do it!
          # (scene_prop_set_slot, ":ship_instance", 11, ":wank_state"),
        # (try_end),
      # (try_end),]),
  
  # script_ship_AI
  # Component: Ships and sea (scene)
  # This script says the AI ships what to do
  # Input: ":ship1_number",
  ("ship_AI",
    [(try_begin),
        ### CONDITION
        (store_script_param, ":ship1_number", 1),
        
        (assign,":stop",1),
        (try_begin),
          (game_in_multiplayer_mode),
          (scene_prop_get_instance,":ship1_instance","spr_dyn_ship_substrate",":ship1_number"),
          (try_begin),
            #(scene_prop_slot_eq,":ship1_instance",scene_prop_lead_player,-1),
            (scene_prop_get_slot,":agent",":ship1_instance",scene_prop_lead_player),
            (this_or_next | neg | agent_is_active,":agent"),
            (this_or_next | neg | agent_is_alive,":agent"),
            (agent_is_non_player,":agent"),
            (assign,":stop",0),
          (try_end),
        (else_try),
          (this_or_next | eq, "$block_player_ship_control", 1),
          (neq, ":ship1_number", "$player_ship_number"),			# No AI for Player Ship
          (assign,":stop",0),
        (try_end),
        (eq,":stop",0),
        
        ### GET DATA
        (scene_prop_get_instance, ":ship1_instance", "spr_dyn_ship_substrate", ":ship1_number"),
        (scene_prop_get_team, ":team_ship1", ":ship1_instance"),
        (scene_prop_get_slot, ":shp1_left_boardet_ship", ":ship1_instance", scene_prop_boarding_left),
        (scene_prop_get_slot, ":shp1_right_boardet_ship", ":ship1_instance", scene_prop_boarding_right),
        (scene_prop_get_slot, ":shp1_boarding_progress", ":ship1_instance", scene_prop_boarding_progress),
        
        ### CHANGES FOR COASTAL BATTEL
        (try_begin),
          (eq, "$coastal_battle", 1),
          (assign, ":ship_target", 0),		# this is to make coastal battle easily possible (has to be zero)
        (try_end),
        
        ### PREVENTING that reinforcement hangs in the air
        (try_begin),
          (scene_prop_get_slot, ":timer", ":ship1_instance", scene_prop_timer),
          (gt, ":timer", 0),
          (val_sub, ":timer", 1),
          (scene_prop_set_slot, ":ship1_instance", scene_prop_timer, ":timer"),
        (try_end),
        
        ### NEW: ORDERS
        (try_begin),
          (assign, ":stand_ground_command", 0),
          (neg | game_in_multiplayer_mode),
          (neq, "$coastal_battle", 1),
          (get_player_agent_no, ":player_agent"),
          (agent_get_team, ":player_team", ":player_agent"),
          (eq, ":team_ship1", ":player_team"),
          (team_get_movement_order, ":cur_order", ":player_team", 8),
          (eq, ":cur_order", mordr_stand_ground),
          (assign, ":stand_ground_command", 1),
        (end_try),
        
        
        (try_begin),
          ### CONDITIONS
          (ge, ":team_ship1", 0),
          (neq, "$block_ship_ai", 1),
          (le, ":timer", 0),
          (eq, ":stand_ground_command", 0),	#new
          (eq, ":shp1_left_boardet_ship", 0),
          (eq, ":shp1_right_boardet_ship", 0),
          (eq, ":shp1_boarding_progress", 0),
          (prop_instance_get_position, pos11, ":ship1_instance"),
          
          ### CALCULATE NUMBER OF ROWER
          (assign, ":num_of_rower", 0),
          (try_for_agents,":agent"),
            (agent_is_alive,":agent"),
            (agent_is_human,":agent"),
            (agent_get_slot, ":crewmate_of_ship_instance", ":agent", slot_agent_on_ship),
            (eq, ":crewmate_of_ship_instance", ":ship1_instance"),
            (val_add, ":num_of_rower", 1),
          (end_try),
          (try_begin),
            (gt, ":num_of_rower", 20),
            (assign, ":slow_rowing", 50),
          (else_try),
            (assign, ":slow_rowing", 100),
          (end_try),
          
          ### CALCULATE TARGET
          (try_begin),
            (eq, "$coastal_battle", 0),		# means sea battle
            (assign, ":last_distance", 100000),
            (assign, ":ship_target", -1),
            #(scene_prop_get_num_instances, ":number_of_ships",
            #"spr_dyn_ship_substrate"),
            (try_for_range,":ship2_number", 0, "$number_of_ships_global"),
              (scene_prop_get_instance, ":ship2_instance", "spr_dyn_ship_substrate", ":ship2_number"),
              (scene_prop_get_team, ":team_ship2", ":ship2_instance"),
              (neg | eq, ":team_ship2", ":team_ship1"),
              (neg | eq, ":team_ship2", -2),
              (scene_prop_get_slot, ":ship2_left_boardet_ship", ":ship2_instance", 14),
              (scene_prop_get_slot, ":ship2_right_boardet_ship", ":ship2_instance", 15),
              (this_or_next | eq, ":ship2_left_boardet_ship", 0),
              (eq, ":ship2_right_boardet_ship", 0),
              (prop_instance_get_position, pos12, ":ship2_instance"),
              (get_distance_between_positions, ":distance", pos11, pos12),
              (try_begin),
                (lt, ":distance", ":last_distance"),
                (assign, ":last_distance", ":distance"),
                (assign, ":ship_target", ":ship2_instance"),
              (try_end),
            (try_end),
          (try_end),
          
          ### CALCULATE COMMANDS
          (try_begin),
            (eq, ":ship_target", -1),
            (assign, ":rudder", 0),
            (assign, ":rowing", 0),
            (assign, ":sail", 0),
          (else_try),
            (try_begin),
              (eq, "$coastal_battle", 1),	# this is for coastal assault!
              (scene_prop_get_slot, ":team_ship_number", ":ship1_instance", scene_prop_ship_number),
              (store_add, ":entry_no", 50, ":team_ship_number"),
              (entry_point_get_position, pos12, ":entry_no"),
              #(entry_point_get_position, pos12, 50),
            (else_try),
              (eq, "$coastal_battle", 0),
              (prop_instance_get_position, pos12, ":ship_target"),
            (end_try),
            #(prop_instance_get_position, pos11, ":ship1_instance"),
            (position_move_y, pos11, 100),
            (get_distance_between_positions, ":left_dist", pos12, pos11),
            (position_move_y, pos11, -200),
            (get_distance_between_positions, ":right_dist", pos12, pos11),
            (position_move_y, pos11, 100),
            (position_move_x, pos11, 100),
            (get_distance_between_positions, ":front_dist", pos12, pos11),
            (position_move_x, pos11, -200),
            (get_distance_between_positions, ":heck_dist", pos12, pos11),
            
            (try_begin),
              (le, ":front_dist", ":heck_dist"),	# means: target is in front of ship
              (try_begin),
                (le, ":right_dist", ":left_dist"),
                (store_sub, ":difference", ":left_dist", ":right_dist"),
                (try_begin),
                  (ge, ":difference", 50),
                  (assign, ":rudder", -100),
                  (assign, ":rowing", ":slow_rowing"),
                (else_try),
                  (ge, ":difference", 15),
                  (assign, ":rudder", -50),
                  (assign, ":rowing", 100),
                (else_try),
                  (assign, ":rudder", 0),
                  (assign, ":rowing", 100),
                (end_try),
              (else_try),
                (le, ":left_dist", ":right_dist"),
                (store_sub, ":difference", ":right_dist", ":left_dist"),
                (try_begin),
                  (ge, ":difference", 50),
                  (assign, ":rudder", 100),
                  (assign, ":rowing", ":slow_rowing"),
                (else_try),
                  (ge, ":difference", 15),
                  (assign, ":rudder", 50),
                  (assign, ":rowing", 100),
                (else_try),
                  (assign, ":rudder", 0),
                  (assign, ":rowing", 100),
                (end_try),
              (end_try),
            (else_try),
              (le, ":heck_dist", ":front_dist"),	# means: target is behind ship
              (try_begin),
                (le, ":right_dist", ":left_dist"),
                (assign, ":rudder", -100),
                (assign, ":rowing", ":slow_rowing"),
              (else_try),
                (le, ":left_dist", ":right_dist"),
                (assign, ":rudder", 100),
                (assign, ":rowing", ":slow_rowing"),
              (end_try),
            (end_try),
            
            # SAIL CALCULATION
            (assign, ":sail", 0),
            (try_begin),
              (prop_instance_get_position, pos2, "$wind_spr"),
              (get_angle_between_positions, ":angle_diff", pos1, pos2),
              (is_between, ":angle_diff", -13500, 13500),
              (gt, ":last_distance", 5000), #50m
              (gt, "$wind_strenght", 50),
              (assign, ":sail", 1),
              (assign, ":rowing", 0),
            (end_try),
          (end_try),
          
          ### OUTPUT
          (scene_prop_set_slot, ":ship1_instance", scene_prop_sail, ":sail"),
          
          (try_begin),
            (game_in_multiplayer_mode),
            ###set sail visibility to 0
            (scene_prop_get_slot,":ship_sail",":ship1_instance",scene_prop_boom_instance),
            (get_max_players, ":num_players"),
            (try_for_range,":i",1,":num_players"),
              (player_is_active,":i"),
              #(try_for_players,":i",1),
              (multiplayer_send_3_int_to_player,":i", multiplayer_event_ship, me_ship_return_ship_sail, ":ship_sail", ":sail"),
            (try_end),
          (try_end),
          
          (scene_prop_set_slot, ":ship1_instance", scene_prop_rowing, ":rowing"),
          (scene_prop_set_slot, ":ship1_instance", scene_prop_rudder, ":rudder"),
          (scene_prop_set_slot, ":ship1_instance", scene_prop_boarding_wanted, 0),
        (else_try),
          ### else_try means: The ship is empty OR fights are on the ship OR ship_AI
          ### is blocked OR
          (scene_prop_set_slot, ":ship1_instance", scene_prop_sail, 0),
          (scene_prop_set_slot, ":ship1_instance", scene_prop_rowing, 0),
          (scene_prop_set_slot, ":ship1_instance", scene_prop_rudder, 0),
          (scene_prop_set_slot, ":ship1_instance", scene_prop_boarding_wanted, 0),
          # a change for sails in d-day scenes:
          (try_begin),
            (store_current_scene, ":cur_scene"),
            (this_or_next | eq, ":cur_scene", "scn_d_day"),
            (eq, ":cur_scene", "scn_d_day_douara"),
            (scene_prop_set_slot, ":ship1_instance", scene_prop_sail, 0),
          (try_end),
        (try_end),
      (try_end),]),
  
  # script_AI_decide_stop_boarding
  # Component: Ships and sea (scene)
  # Input: ":ship1_number",
  ("AI_decide_stop_boarding",
    [(try_begin),
        ### GET DATA
        (store_script_param, ":ship1_number", 1),
        #(neg|eq, ":ship1_number", "$player_ship_number"), # No AI for Player Ship
        #(NOT)
        (scene_prop_get_instance, ":ship1_instance", "spr_dyn_ship_substrate", ":ship1_number"),
        (scene_prop_get_team, ":team_ship1", ":ship1_instance"),
        (try_begin),
          (ge, ":team_ship1", 0),
          (scene_prop_get_slot, ":boarding_wanted", ":ship1_instance", 12),
          (scene_prop_get_slot, ":left_boardet_ship", ":ship1_instance", 14),
          (scene_prop_get_slot, ":right_boardet_ship", ":ship1_instance", 15),
          (scene_prop_get_slot, ":boarding_progress", ":ship1_instance", 16),
          (scene_prop_get_team, ":team_ship", ":ship1_instance"),
          (try_begin),
            
            (try_begin),
              (eq, ":boarding_progress", 0),					# means NO boarding progress
              (try_begin),
                ## 1.  CHECKING LEFT SHIP
                (neq, ":left_boardet_ship", 0),
                (scene_prop_get_team, ":left_boardet_ship_team", ":left_boardet_ship"),
                (scene_prop_get_slot, ":left_boardet_ship_boarding_progress", ":left_boardet_ship", scene_prop_boarding_progress),
                (eq, ":left_boardet_ship_boarding_progress", 0),
                (this_or_next | eq, ":left_boardet_ship_team", ":team_ship"),
                (eq, ":left_boardet_ship_team", -2),
                (assign, ":boarding_wanted", -1),
                
                (try_begin),
                  (call_script, "script_boarding_for_ship3", ":ship1_instance", ":left_boardet_ship"),	# Getting extra data
                  (eq, reg0, 1),
                  (assign, ":boarding_wanted", 0),
                (try_end),
                
              (try_end),
              
              (try_begin),
                ## 2.  CHECKING RIGHT SHIP
                (neq, ":right_boardet_ship", 0),
                (scene_prop_get_team, ":right_boardet_ship_team", ":right_boardet_ship"),
                (scene_prop_get_slot, ":right_boardet_ship_boarding_progress", ":right_boardet_ship", scene_prop_boarding_progress),
                (eq, ":right_boardet_ship_boarding_progress", 0),
                (this_or_next | eq, ":right_boardet_ship_team", ":team_ship"),
                (eq, ":right_boardet_ship_team", -2),
                (assign, ":boarding_wanted", -1),
                
                (try_begin),
                  (call_script, "script_boarding_for_ship3", ":ship1_instance", ":right_boardet_ship"),	# Getting extra data
                  (eq, reg0, 1),
                  (assign, ":boarding_wanted", 0),
                (try_end),
                
              (try_end),
            (try_end),
          (try_end),
          ### OUTPUT
          (scene_prop_set_slot, ":ship1_instance", scene_prop_boarding_wanted, ":boarding_wanted"),
        (else_try),
          ### else_try means: The ship is empty or fights are on the ship
          (scene_prop_set_slot, ":ship1_instance", scene_prop_sail, 0),
          (scene_prop_set_slot, ":ship1_instance", scene_prop_rowing, 0),
          (scene_prop_set_slot, ":ship1_instance", scene_prop_rudder, 0),
          (scene_prop_set_slot, ":ship1_instance", scene_prop_boarding_wanted, 0),
        (try_end),
      (try_end),]),
  
  # script_calculate_collisionreaction_to_pos1
  # Component: Ships and sea (scene)
  # This script says how to react on collisions
  # Input: ":collided_scene_prop_instance", pos1
  # Output: pos1
  ("calculate_collisionreaction_to_pos1",
    [(try_begin),
        ### GET DATA
        (store_script_param, ":collided_scene_prop_instance", 1),
        (store_script_param, ":ship_instance", 2),
        
        (prop_instance_get_position, pos2, ":collided_scene_prop_instance"),
        ### CALCULATION
        (position_get_x, ":x1", pos1),
        (position_get_y, ":y1", pos1),
        (position_get_x, ":x2", pos2),
        (position_get_y, ":y2", pos2),
        (val_sub, ":x1", ":x2"),
        (val_sub, ":y1", ":y2"),
        (assign, ":x1_copy", ":x1"),
        (assign, ":y1_copy", ":y1"),
        (try_begin),
          (lt, ":x1_copy", 0),
          (val_mul, ":x1_copy", -1),
        (end_try),
        (try_begin),
          (lt, ":y1_copy", 0),
          (val_mul, ":y1_copy", -1),
        (end_try),
        (try_begin),
          (ge, ":x1_copy", ":y1_copy"),
          (assign, ":divisor", ":x1_copy"),
        (else_try),
          (assign, ":divisor", ":y1_copy"),
        (try_end),
        (val_div, ":divisor", 10),
        (try_begin),
          (eq, ":divisor", 0),		# avoid division with zero
          (assign, ":divisor", 1),
        (try_end),
        (val_div, ":x1", ":divisor"),
        (val_div, ":y1", ":divisor"),
        
        
        (scene_prop_get_slot, ":boarding_progress_ship2", ":collided_scene_prop_instance", scene_prop_boarding_progress),
        (scene_prop_get_slot, ":boarding_progress_ship1", ":ship_instance", scene_prop_boarding_progress),
        (try_begin),
          (gt, ":boarding_progress_ship2", 0),
          (assign, ":multiplicator", 100),
        (else_try),
          (neq, ":boarding_progress_ship1", 0),
          (scene_prop_get_slot, ":multiplicator", ":ship_instance", scene_prop_last_speed),
          (val_max, ":multiplicator", 5),
        (else_try),
          (assign, ":multiplicator", 50),
        (try_end),
        
        (val_mul, ":x1", ":multiplicator"),
        (val_mul, ":y1", ":multiplicator"),
        (val_div, ":x1", 10),
        (val_div, ":y1", 10),
        
        (position_move_x, pos1, ":x1", 1),
        (position_move_y, pos1, ":y1", 1),
      (try_end),]),
  
  # script_calculate_embark_to_pos1
  # Component: Ships and sea (scene)
  # Input: ":aground_site", pos1
  # Output: pos1
  ("calculate_embark_to_pos1",
    [(try_begin),
        ### GET DATA
        (store_script_param, ":aground_site", 1),
        ### CALCULATION
        (try_begin),
          (eq, ":aground_site", 12),
          (position_move_x, pos1, -50),
        (else_try),
          (eq, ":aground_site", 6),
          (position_move_x, pos1, 50),
        (else_try),
          (eq, ":aground_site", 3),
          (position_move_y, pos1, -50),
        (else_try),
          (eq, ":aground_site", 9),
          (position_move_y, pos1, 50),
        (else_try),
          (eq, ":aground_site", 1),
          (position_move_y, pos1, -20),
          (position_move_x, pos1, -20),
        (else_try),
          (eq, ":aground_site", 11),
          (position_move_y, pos1, 20),
          (position_move_x, pos1, -20),
          
        (else_try),
          (eq, ":aground_site", 51),			# max x+ boarder reached
          (position_move_x, pos1, -50, 1),
        (else_try),
          (eq, ":aground_site", 52),			# max y+ boarder reached
          (position_move_y, pos1, -50, 1),
        (else_try),
          (eq, ":aground_site", 53),			# max x- boarder reached
          (position_move_x, pos1, 50, 1),
        (else_try),
          (eq, ":aground_site", 54),			# max y- boarder reached
          (position_move_y, pos1, 50, 1),
          
        (try_end),
      (try_end),]),
  
  # script_calculate_meetingpoint_to_pos1
  # Component: Ships and sea (scene)
  # Input: "ship1_number", ":ship2_instance", pos1
  # Output: pos1
  ("calculate_meetingpoint_to_pos1",
    [(try_begin),
        ### GET DATA
        (store_script_param, ":ship1_number", 1),
        (scene_prop_get_instance, ":ship1_instance", "spr_dyn_ship_substrate", ":ship1_number"),
        (store_script_param, ":ship2_instance", 2),
        (prop_instance_get_position, pos2, ":ship2_instance"),
        (scene_prop_get_slot, ":ship1_radius", ":ship1_instance", scene_prop_radius),
        (scene_prop_get_slot, ":ship2_radius", ":ship2_instance", scene_prop_radius),
        
        ### GET ship2_number
        (try_for_range,":ship_number", 0, "$number_of_ships_global"),
          (scene_prop_get_instance, ":ship_instance" , "spr_dyn_ship_substrate", ":ship_number"),
          (eq, ":ship_instance", ":ship2_instance"),
          (assign, ":ship2_number", ":ship_number"),
        (end_try),
        
        (scene_prop_get_slot, ":boarding_progress", ":ship1_instance", 16),
        
        ### PART 1 ANGLE-CALCULATION
        (position_get_rotation_around_z, ":angle1", pos1),
        (position_get_rotation_around_z, ":angle2", pos2),
        (assign, ":opposite", 1),
        
        (try_begin),
          (ge, ":angle1", ":angle2"),
          (store_sub, ":angle_diff", ":angle1", ":angle2"),
          (assign,":flip", -1),
        (else_try),
          (store_sub, ":angle_diff", ":angle2", ":angle1"),
          (assign,":flip", 1),
        (end_try),
        (try_begin),
          (ge, ":angle_diff", 180),
          (val_add, ":angle_diff", -360),
          (val_mul, ":angle_diff", -1),
          (val_mul,":flip", -1),
        (end_try),
        (try_begin),
          (ge, ":angle_diff", 90),
          (assign, ":opposite", -1),
          (val_add, ":angle_diff", -180),
          (val_mul, ":angle_diff", -1),
          (val_mul,":flip", -1),
        (end_try),
        (val_mul,":angle_diff", ":flip"),
        (val_div,":angle_diff", 2),
        
        ### PART 2 POSITION-CALCULATION
        (prop_instance_get_position, pos2, ":ship2_instance"),
        (prop_instance_get_position, pos1, ":ship1_instance"),
        
        (position_get_x, ":x1", pos1),
        (position_get_y, ":y1", pos1),
        (position_get_x, ":x2", pos2),
        (position_get_y, ":y2", pos2),
        (val_sub, ":x1", ":x2"),
        (val_sub, ":y1", ":y2"),
        (val_div, ":x1", -2), #!
        (val_div, ":y1", -2),
        
        (copy_position, pos3, pos1),
        (position_move_x, pos3, ":x1", 1),
        (position_move_y, pos3, ":y1", 1),
        (position_rotate_z, pos3, ":angle_diff"),
        (copy_position, pos4, pos3),
        
        #(val_add, ":ship1_radius", ":ship2_radius"), # this is the wanted distance
        #between both ships to board
        (store_add, ":distance", ":ship1_radius", ":ship2_radius"),
        (val_div, ":distance", 2),
        (position_move_y, pos4, ":distance"),		#left= positive
        (val_mul, ":distance", -1),
        (position_move_y, pos3, ":distance"),		#right = negative
        
        ### PART 3 POSITION-DECISION
        (try_begin),
          (gt, ":ship1_instance", ":ship2_instance"),
          (copy_position, pos7, pos1),
          (position_move_y, pos7, -100),
          (get_distance_between_positions, ":right_dist", pos7, pos2),
          (position_move_y, pos7, 200),
          (get_distance_between_positions, ":left_dist", pos7, pos2),
          (try_begin),
            (le, ":right_dist", ":left_dist"),		# means: ship2 is on the right side of ship1
            (assign, ":decision", 1),		#left
          (else_try),
            (assign, ":decision", -1),	#right
          (end_try),
        (else_try),
          (copy_position, pos7, pos2),
          (position_move_y, pos7, -100),
          (get_distance_between_positions, ":right_dist", pos7, pos1),
          (position_move_y, pos7, 200),
          (get_distance_between_positions, ":left_dist", pos7, pos1),
          (try_begin),
            (le, ":right_dist", ":left_dist"),		# means: ship1 is on the right side of ship2
            (assign, ":decision", -1),				# right
          (else_try),
            (assign, ":decision", 1),					# left
          (end_try),
          (val_mul, ":decision", ":opposite"),		# chage decision when ships are opposite
        (end_try),
        
        ### PART 4 EXECUTE POSITION-DECISION
        (try_begin),
          (eq, ":decision", -1),	# execute decision right
          (position_get_x, ":x2", pos3),
          (position_get_y, ":y2", pos3),
          (try_begin),
            (le, ":boarding_progress", 15),
            (val_div, ":distance", 4),			# means: 1/4 in 0,25 seconds
            (position_move_y, pos1, ":distance"),	# moving already in first steps full y to avoid clipping
            #(display_message,"@{!}right_position"),
          (else_try),
            (eq, ":boarding_progress", 100),
            (scene_prop_set_slot, ":ship1_instance", 14, ":ship2_instance"),
            #(scene_prop_set_slot, ":ship1_instance", 15, 0), # because
            #(display_message,"@{!}right_position_end"),
          (try_end),
        (else_try),
          (eq, ":decision", 1),	# execute decision left
          (position_get_x, ":x2", pos4),
          (position_get_y, ":y2", pos4),
          (try_begin),
            (le, ":boarding_progress", 15),
            (val_div, ":distance", 4),			# means: 1/4 in 0,25 seconds
            (val_mul, ":distance", -1),			# to make it positive again
            (position_move_y, pos1, ":distance"),	# moving already in first steps full y to avoid clipping
            #(display_message,"@{!}left_position"),
          (else_try),
            (eq, ":boarding_progress", 100),
            (scene_prop_set_slot, ":ship1_instance", 15, ":ship2_instance"),
            #(scene_prop_set_slot, ":ship1_instance", 14, 0), # because
            #(display_message,"@{!}left_position_end"),
          (try_end),
        (else_try),
          (display_message,"@{!}ERROR!_KEINE_ENTSCHEIDUNG!"),
        (end_try),
        
        ### PART 5 SECOND POSITION-CALCULATION
        (store_mul, ":quad_boarding_progress", ":boarding_progress", ":boarding_progress"),
        (store_mul, ":quad_boarding_progress_mul2", ":quad_boarding_progress", 2),
        (val_min, ":quad_boarding_progress", 10000),
        (val_min, ":quad_boarding_progress_mul2", 10100),
        
        (position_get_x, ":x1", pos1),
        (position_get_y, ":y1", pos1),
        (val_sub, ":x1", ":x2"),
        (val_sub, ":y1", ":y2"),
        (val_div, ":x1", -1),
        (val_div, ":y1", -1),
        (val_mul, ":x1", ":quad_boarding_progress"),
        (val_div, ":x1", 10000),
        (val_mul, ":y1", ":quad_boarding_progress"),
        (val_div, ":y1", 10000),
        (val_mul, ":angle_diff", ":quad_boarding_progress_mul2"),
        (val_div, ":angle_diff", 10000),
        
        (position_move_x, pos1, ":x1", 1),
        (position_move_y, pos1, ":y1", 1),
        (position_rotate_z, pos1, ":angle_diff"),
        
        ### MAKE BOARDING PROGRESS EQUAL + OUTPUT
        (try_begin),
          (gt, ":ship2_number", ":ship1_number"),
          (scene_prop_set_slot, ":ship2_instance", scene_prop_boarding_progress, ":boarding_progress"),
          
          # (assign, reg8, ":boarding_progress"),
          # (display_message, "@{!}meeting: boarding_progress: {reg8}"),
          
          (try_begin),
            (lt, ":boarding_progress", boarding_progress_peak_0),
            (val_add, ":boarding_progress", 5),# this value determines the speed of the progress
            (val_min, ":boarding_progress", boarding_progress_peak_0),
          (else_try),
            (lt, ":boarding_progress", boarding_progress_peak),
            (val_add, ":boarding_progress", 4),# this value determines the speed of the progress
            (val_min, ":boarding_progress", boarding_progress_peak),
          (else_try),
            (lt, ":boarding_progress", 100),
            (val_add, ":boarding_progress", 100 - boarding_progress_peak),
          (else_try),
            #(assign, ":boarding_progress", 0),
          (try_end),
          (scene_prop_set_slot, ":ship1_instance", scene_prop_boarding_progress, ":boarding_progress"),
        (else_try),
          (lt, ":ship2_number", ":ship1_number"),
          (eq, ":boarding_progress", 100),
          (scene_prop_get_slot, ":gtbw_ship_of_ship1", ":ship1_instance", scene_prop_going_to_boarding_with),
          (eq, ":gtbw_ship_of_ship1", ":ship2_instance"),
          (assign, reg9, ":ship2_number"),
          (assign, reg8, ":ship1_number"),
          #(display_message,
          #"@{!}SHIP_{reg8}_INFO:_After_finishing_free_boarding_I_have_deleted_my_gtbws_entry_and_the_entry_of_SHIP_{reg9}!"),
          (scene_prop_set_slot, ":ship1_instance", scene_prop_going_to_boarding_with, 0),
          (scene_prop_set_slot, ":ship2_instance", scene_prop_going_to_boarding_with, 0),
          (scene_prop_set_slot, ":ship1_instance", scene_prop_boarding_progress, 0),
          (scene_prop_set_slot, ":ship2_instance", scene_prop_boarding_progress, 0),
        (end_try),
        
      (try_end),]),
  
  # script_calculate_dockingpoint_to_pos1
  # Component: Ships and sea (scene)
  # Input: "ship1_number", ":ship2_instance", pos1
  # Output: pos1
  ("calculate_dockingpoint_to_pos1",
    [(try_begin),
        ### GET DATA
        (store_script_param, ":ship1_number", 1),
        (scene_prop_get_instance, ":ship1_instance", "spr_dyn_ship_substrate", ":ship1_number"),
        (scene_prop_get_slot, ":boarding_progress", ":ship1_instance", 16),
        (store_script_param, ":ship2_instance", 2),
        (scene_prop_get_slot, ":ship2_left_boardet_ship", ":ship2_instance", 14),
        (scene_prop_get_slot, ":ship2_right_boardet_ship", ":ship2_instance", 15),
        (scene_prop_get_slot, ":ship1_radius", ":ship1_instance", scene_prop_radius),
        (scene_prop_get_slot, ":ship2_radius", ":ship2_instance", scene_prop_radius),
        (val_add, ":ship1_radius", ":ship2_radius"),
        (prop_instance_get_position, pos2, ":ship2_instance"),
        (copy_position, pos3, pos2),
        
        ### EASY CORRECTION
        # (try_begin),
        # (eq, ":boarding_progress", 0),
        # (assign, ":boarding_progress", 2),
        # (end_try),
        
        ### CALCULATE DOCKINGPOINT (pos2)
        (try_begin),
          (eq, ":ship2_left_boardet_ship", 0),		# means: left is free
          (position_move_y, pos2, ":ship1_radius"),
          (assign, ":side", -1),
        (else_try),
          (eq, ":ship2_right_boardet_ship", 0),		# means: rigt is free
          (val_mul, ":ship1_radius", -1),
          (position_move_y, pos2, ":ship1_radius"),
          (assign, ":side", 1),
        (else_try),
          (display_debug_message,"@{!}Error_1_in_calculate_dockingpoint"),
        (end_try),
        
        ### POSITION-Check
        (position_move_y, pos3, -100),
        (get_distance_between_positions, ":right_dist", pos3, pos1),
        (position_move_y, pos3, 200),
        (get_distance_between_positions, ":left_dist", pos3, pos1),
        (try_begin),
          (le, ":right_dist", ":left_dist"),						# means: ship1 is on the right side of ship2
          (eq, ":side", 1),										# means: ship1 wants on the right side of ship2
          (assign, ":condition_fullfilled", 1),
        (else_try),
          (le, ":left_dist", ":right_dist"),
          (eq, ":side", -1),										# means: ship1 wants on the right side of ship2
          (assign, ":condition_fullfilled", 1),
        (end_try),
      (end_try),
      
      (try_begin),
        (eq, ":condition_fullfilled", 1),		## IMPORTANT CONDITION (Ship on the right(correct) side?)
        
        ### PART 1 ANGLE-CALCULATION
        (position_get_rotation_around_z, ":angle1", pos1),
        (position_get_rotation_around_z, ":angle2", pos2),
        (try_begin),
          (ge, ":angle1", ":angle2"),
          (val_sub, ":angle1", ":angle2"),
          (assign, ":angle_diff", ":angle1"),
          (assign,":flip", -1),								# was negativ
        (else_try),
          (val_sub, ":angle2", ":angle1"),
          (assign, ":angle_diff", ":angle2"),
          (assign,":flip", 1),
        (end_try),
        (try_begin),
          (ge, ":angle_diff", 180),
          (val_add, ":angle_diff", -360),
          (val_mul, ":angle_diff", -1),
          (val_mul,":flip", -1),
        (end_try),
        (try_begin),
          (ge, ":angle_diff", 90),
          (val_add, ":angle_diff", -180),
          (val_mul, ":angle_diff", -1),
          (val_mul,":flip", -1),
          (val_mul, ":side", -1),
        (end_try),
        (val_mul,":angle_diff", ":flip"),
        
        ### PART 2 POSITION-CALCULATION
        (store_mul, ":quad_boarding_progress", ":boarding_progress", ":boarding_progress"),
        (store_mul, ":quad_boarding_progress_mul2", ":quad_boarding_progress", 2),
        (val_min, ":quad_boarding_progress", 10000),
        (val_min, ":quad_boarding_progress_mul2", 10100),
        
        (position_get_x, ":x1", pos1),
        (position_get_y, ":y1", pos1),
        (position_get_x, ":x2", pos2),
        (position_get_y, ":y2", pos2),
        (val_sub, ":x1", ":x2"),
        (val_sub, ":y1", ":y2"),
        (val_mul, ":x1", -1),
        (val_mul, ":y1", -1),
        (val_mul, ":x1", ":quad_boarding_progress"),
        (val_div, ":x1", 10000),
        (val_mul, ":y1", ":quad_boarding_progress"),
        (val_div, ":y1", 10000),
        (val_mul, ":angle_diff", ":quad_boarding_progress_mul2"),
        (val_div, ":angle_diff", 10000),
        
        (position_move_x, pos1, ":x1", 1),
        (position_move_y, pos1, ":y1", 1),
        (position_rotate_z, pos1, ":angle_diff"),
        
        ### BOARDING ENTRIES
        (try_begin),
          (eq, ":boarding_progress", 100),
          
          (try_begin),	# THIS SHIP
            (eq, ":side", 1),
            (scene_prop_set_slot, ":ship1_instance", scene_prop_boarding_left, ":ship2_instance"),
          (else_try),
            (eq, ":side", -1),
            (scene_prop_set_slot, ":ship1_instance", scene_prop_boarding_right, ":ship2_instance"),
          (end_try),
          #(scene_prop_set_slot, ":ship1_instance", scene_prop_timer, 14),
          
          (try_begin),	# OTHER SHIP
            (eq, ":ship2_left_boardet_ship", 0),		# means: left is free
            (scene_prop_set_slot, ":ship2_instance", scene_prop_boarding_left, ":ship1_instance"), 	# make boarding enrie
          (else_try),
            (eq, ":ship2_right_boardet_ship", 0),		# means: rigt is free
            (scene_prop_set_slot, ":ship2_instance", scene_prop_boarding_right, ":ship1_instance"), 	# make boarding enrie
          (else_try),
            (display_message,"@{!}Error_2_in_calculate_dockingpoint"),
          (end_try),
          #(scene_prop_set_slot, ":ship2_instance", scene_prop_timer, 14),
          
          (scene_prop_set_slot, ":ship1_instance", scene_prop_going_to_boarding_with, 0),
          (try_begin),
            # each ship deletes only his own entry
            (scene_prop_get_slot, ":gtbw_ship_of_ship2", ":ship2_instance", scene_prop_going_to_boarding_with),
            (eq, ":gtbw_ship_of_ship2", ":ship1_instance"),
            (scene_prop_set_slot, ":ship2_instance", scene_prop_going_to_boarding_with, 0),
          (end_try),
          
        (end_try),
        
        
        ### OUTPUT
        # (assign, reg8, ":boarding_progress"),
        # (display_message, "@{!}boarding_progress: {reg8}"),
        (try_begin),
          (lt, ":boarding_progress", boarding_progress_peak_0),
          (val_add, ":boarding_progress", 4),# this value determines the speed of the progress
          (val_min, ":boarding_progress", boarding_progress_peak_0),
        (else_try),
          (lt, ":boarding_progress", boarding_progress_peak),
          (val_add, ":boarding_progress", 4),# this value determines the speed of the progress
          (val_min, ":boarding_progress", boarding_progress_peak),
        (else_try),
          (lt, ":boarding_progress", 100),
          (val_add, ":boarding_progress", 100 - boarding_progress_peak),
        (else_try),
          (assign, ":boarding_progress", 0),
        (end_try),
        (scene_prop_set_slot, ":ship1_instance", scene_prop_boarding_progress, ":boarding_progress"),
        
      (else_try),
        # means: if ship1 is not on the correc) side of ship 2
        # A.  CREW
        (try_begin),
          (ge, ":boarding_progress", boarding_progress_peak),
          (call_script, "script_make_crew", ":ship1_instance"),
          (call_script, "script_keep_crew_on_board", ":ship1_instance"),
        (end_try),
        
        # B.  SHIP REACTION
        (call_script, "script_calculate_collisionreaction_to_pos1", ":ship2_instance", ":ship1_instance"),
        
        # C.  ENTRIES
        (scene_prop_set_slot, ":ship1_instance", scene_prop_boarding_progress, 0),
        (scene_prop_set_slot, ":ship1_instance", scene_prop_going_to_boarding_with, 0),
        
        (assign, reg8, ":ship1_number"),
        (try_for_range,":ship_number_x", 0, "$number_of_ships_global"),
          (scene_prop_get_instance, ":ship_instance_x" , "spr_dyn_ship_substrate", ":ship_number_x"),
          (eq, ":ship_instance_x", ":ship2_instance"),
          (assign, reg9, ":ship_number_x"),
        (end_try),
        #(display_message,
        #"@{!}SHIP_{reg8}_INFO:_After_Abort_Docking_with_SHIP_{reg9}_I_have_deleted_my_gtbws_entry!"),
        
        (try_begin),
          # each ship deletes only his own entry
          (scene_prop_get_slot, ":gtbw_ship_of_ship2", ":ship2_instance", scene_prop_going_to_boarding_with),
          (eq, ":gtbw_ship_of_ship2", ":ship1_instance"),
          (scene_prop_set_slot, ":ship2_instance", scene_prop_going_to_boarding_with, 0),
          #(display_message,
          #"@{!}SHIP_{reg8}_INFO:_After_Abort_Docking_I_have_deleted_the_gtbws_entry_of_SHIP_{reg9}_!"),
        (else_try),
          (display_message, "@{!}SHIP_{reg8}_ERROR_:_Want to delete my Entry in SHIP {reg9} , but it isnt my entry.", 0xFFFF0000),
        (end_try),
        
      (try_end),]),
  
  # script_check_collision
  # Component: Ships and sea (scene)
  # This script checks_ship_collisions
  # Input: ":ship_number"
  # Output: ":collided_ship" (reg0), ":undodge"
  # (reg1),":going_to_boarding_ship_instance" (reg2)
  ("check_collision",
    [### GET DATA
      (try_begin),
        #(display_message,"@Checking_Collision"),
        (store_script_param, ":ship1_number", 1),
        (scene_prop_get_instance, ":ship1_instance", "spr_dyn_ship_substrate", ":ship1_number"),
        (scene_prop_get_slot, ":ship1_collision_instance", ":ship1_instance", scene_prop_collision_instance),
        (scene_prop_get_slot, ":going_to_boarding_ship_instance", ":ship1_instance", scene_prop_going_to_boarding_with),
        #(scene_prop_get_slot, ":ship1_right_boardet_ship_instance",
        #":ship1_instance", scene_prop_boarding_right),
        #(scene_prop_get_num_instances, ":number_of_ships",
        #"spr_dyn_ship_substrate"),
        (assign, reg0, -1),
        (assign, reg1, -1),
        (assign, reg2, -1),
      (end_try),
      (try_for_range,":ship2_number", 0, "$number_of_ships_global"),
        (neq, ":ship1_number", ":ship2_number"),
        (scene_prop_get_instance, ":ship2_instance", "spr_dyn_ship_substrate", ":ship2_number"),
        (scene_prop_get_slot, ":ship2_collision_instance", ":ship2_instance", scene_prop_collision_instance),
        (prop_instance_intersects_with_prop_instance, ":ship1_collision_instance", ":ship2_collision_instance"),
        
        (scene_prop_get_slot, ":ship2_left_boardet_ship", ":ship2_instance", scene_prop_boarding_left),
        (scene_prop_get_slot, ":ship2_right_boardet_ship", ":ship2_instance", scene_prop_boarding_right),
        (scene_prop_get_slot, ":ship2_boarding_progress", ":ship2_instance", scene_prop_boarding_progress),
        (scene_prop_get_slot, ":ship2_going_to_boarding_with", ":ship2_instance", scene_prop_going_to_boarding_with),
        
        (try_begin),
          (eq, ":ship2_instance", ":going_to_boarding_ship_instance"),
          (assign, reg2, ":ship2_instance"),
        (else_try),
          (assign, reg0, ":ship2_instance"),
        (try_end),
        (try_begin),		# can ship2_instance dodge???
          (neq, ":ship2_instance", ":going_to_boarding_ship_instance"),	# ignore ship you will board
          (this_or_next | neq, ":ship2_boarding_progress", 0),	# ships in boarding progress cannot dodge
          (this_or_next | neq, ":ship2_left_boardet_ship", 0),	# boarding ships cannot dodge
          (this_or_next | neq, ":ship2_right_boardet_ship", 0),	# boarding ships cannot dodge
          (neq, ":ship2_going_to_boarding_with", 0),			# going to boarding ships cannot dodge
          (assign, reg1, ":ship2_instance"),
        (try_end),
      (try_end),
      
      (scene_prop_get_num_instances, ":number_of_barriers", "spr_barrier_ship"),
      (try_for_range,":ship_barrier_number", 0, ":number_of_barriers"),
        (scene_prop_get_instance, ":curr_ship_barrier_id", "spr_barrier_ship", ":ship_barrier_number"),
        (prop_instance_intersects_with_prop_instance, ":ship1_collision_instance", ":curr_ship_barrier_id"),
        (assign, reg0, ":curr_ship_barrier_id"),
        (assign, reg1, ":curr_ship_barrier_id"),# cannot dodge
      (try_end),]),
  
  # script_make_crew
  # Component: Ships and sea (scene)
  # Input: ":ship_instance"
  ("make_crew",
    [(try_begin),
        (store_script_param, ":ship_instance", 1),
        (assign, ":agent_counter", 0),
        (scene_prop_get_slot, ":ship_main_instance", ":ship_instance", scene_prop_main_instance),
        (scene_prop_get_slot, ":ship_ramp_right_instance", ":ship_instance", scene_prop_ramp_1),
        (scene_prop_get_slot, ":ship_ramp_left_instance", ":ship_instance", scene_prop_ramp_left),
        (scene_prop_get_slot, ":ship_cargo_instance", ":ship_instance", scene_prop_cargo_1),
        (scene_prop_get_slot, ":ship_ramp_2_instance", ":ship_instance", scene_prop_ramp_2),
        #(scene_prop_get_slot, ":ship_number_in_team", ":ship_instance",
        #scene_prop_ship_number), #NEW:ORDERS!!!
        (try_for_agents,":agent"),
          (agent_is_alive,":agent"),
          (agent_is_human,":agent"),
          (agent_is_non_player, ":agent"),
          #(agent_get_team , ":team", ":agent"),
          (this_or_next | scene_prop_has_agent_on_it, ":ship_main_instance", ":agent"),
          (this_or_next | scene_prop_has_agent_on_it, ":ship_ramp_right_instance", ":agent"),
          (this_or_next | scene_prop_has_agent_on_it, ":ship_ramp_left_instance", ":agent"),
          (this_or_next | scene_prop_has_agent_on_it, ":ship_cargo_instance", ":agent"),
          (scene_prop_has_agent_on_it, ":ship_ramp_2_instance", ":agent"),
          (agent_set_slot, ":agent", slot_agent_on_ship, ":ship_instance"),
          (agent_set_slot, ":agent", slot_agent_position_on_ship, ":agent_counter"),
          #(agent_set_division, ":agent", ":ship_number_in_team"), #NEW:ORDERS!!!
          (val_add, ":agent_counter", 1),
        (end_try),
      (end_try),]),
  
  # script_check_player_ship
  # Component: Ships and sea (scene)
  # This script checks which is the ship of the player
  ("check_player_ship",
    [(try_begin),
        (get_player_agent_no, ":agent"),
        (neq, ":agent", -1),
        (agent_is_alive,":agent"),
        (agent_get_team, ":agent_team", ":agent"),
        #(scene_prop_get_num_instances, ":number_of_ships",
        #"spr_dyn_ship_substrate"),
        (assign, ":player_ship_number_backup", "$player_ship_number"),
        (assign, "$player_ship_number", -1),
        #(assign, "$player_ship_instance", -1),
        (try_for_range,":ship_number", 0, "$number_of_ships_global"),
          (scene_prop_get_instance, ":ship_instance", "spr_dyn_ship_substrate", ":ship_number"),
          (scene_prop_get_slot, ":ship_main_instance", ":ship_instance", scene_prop_main_instance),
          (scene_prop_get_slot, ":ship_ramp_right_instance", ":ship_instance", scene_prop_ramp_1),
          (scene_prop_get_slot, ":ship_ramp_left_instance", ":ship_instance", scene_prop_ramp_left),
          (scene_prop_get_slot, ":ship_cargo_1_instance", ":ship_instance", scene_prop_cargo_1),
          (try_begin),
            (assign, ":on_cargo", 0),
            (neq, ":ship_cargo_1_instance", 0),
            (scene_prop_has_agent_on_it, ":ship_cargo_1_instance", ":agent"),
            (assign, ":on_cargo", 1),
          (end_try),
          (this_or_next | scene_prop_has_agent_on_it, ":ship_main_instance", ":agent"),
          (this_or_next | scene_prop_has_agent_on_it, ":ship_ramp_right_instance", ":agent"),
          (this_or_next | scene_prop_has_agent_on_it, ":ship_ramp_left_instance", ":agent"),
          (eq, ":on_cargo", 1),
          (scene_prop_get_team, ":team", ":ship_instance"),
          (eq, ":team", ":agent_team"),
          (assign, "$player_ship_number", ":ship_number"),
          #(assign, "$player_ship_instance", ":ship_instance"),
          (agent_set_slot, ":agent", 23, ":ship_instance"),
          
          (try_begin),#VC-3342
            (neq, ":player_ship_number_backup", "$player_ship_number"),
            (neq, "$player_ship_number", -1),
            (scene_prop_set_slot, "$player_ship_number", scene_prop_rudder, 0),
          (try_end),
        (try_end),
        
        # ACCORDING CAMERA
        (try_begin),
          (eq, "$player_ship_number", -1),
          (eq, "$cam_mode", 1),
          (assign, "$cam_mode", 0),
          (call_script, "script_return_to_cam_first_person_mode_1_sec"),
        (try_end),
        
      (try_end),]),
  
  # script_check_ship_team
  # Component: Ships and sea (scene)
  # This script checks the owner of the ship
  # Input: ":ship_number"
  ("check_ship_team",
    [
      (try_for_range,":ship_number", 0, "$number_of_ships_global"),
        (scene_prop_get_instance, ":ship_instance", "spr_dyn_ship_substrate", ":ship_number"),
        (scene_prop_set_team, ":ship_instance", -2),			# means ship is empty
        
        (scene_prop_get_slot, ":ship_main_instance", ":ship_instance", scene_prop_main_instance),
        (scene_prop_get_slot, ":ship_ramp_right_instance", ":ship_instance", scene_prop_ramp_1),
        (scene_prop_get_slot, ":ship_ramp_left_instance", ":ship_instance", scene_prop_ramp_left),
        (scene_prop_get_slot, ":ship_cargo_1_instance", ":ship_instance", scene_prop_cargo_1),
        (assign, ":agent_counter", 0),
        (try_for_agents,":agent"),
          (agent_is_alive,":agent"),
          (agent_is_human,":agent"),
          #(agent_is_non_player, ":agent"),
          (try_begin),
            (assign, ":on_cargo", 0),
            (neq, ":ship_cargo_1_instance", 0),
            (scene_prop_has_agent_on_it, ":ship_cargo_1_instance", ":agent"),
            (assign, ":on_cargo", 1),
          (end_try),
          (this_or_next | scene_prop_has_agent_on_it, ":ship_main_instance", ":agent"),
          (this_or_next | scene_prop_has_agent_on_it, ":ship_ramp_right_instance", ":agent"),
          (this_or_next | scene_prop_has_agent_on_it, ":ship_ramp_left_instance", ":agent"),
          (eq, ":on_cargo", 1),
          #
          # Refill Ammo of crew 2 times (VC-1806)
          (try_begin),
            (agent_get_slot, ":times_ammo_refilled", ":agent", slot_agent_ammo_refilled),
            (lt, ":times_ammo_refilled", 2),
            (agent_get_ammo, ":ammo", ":agent", 0),
            (le, ":ammo", 0),
            (val_add, ":times_ammo_refilled", 1),
            (agent_refill_ammo, ":agent"),
            (agent_set_slot, ":agent", slot_agent_ammo_refilled, ":times_ammo_refilled"),
          (end_try),
          #
          (agent_get_team  , ":agent_team", ":agent"),
          (val_add, ":agent_counter", 1),
          (try_begin),
            (eq, ":agent_counter", 1),						# give ship same team as first agent
            (scene_prop_set_team, ":ship_instance", ":agent_team"),
          (end_try),
          (scene_prop_get_team, ":ship_team", ":ship_instance"),
          (try_begin),
            (neq, ":ship_team", ":agent_team"),
            (scene_prop_set_team, ":ship_instance", -1),		# means agents of diffrent teams are on the ship
          (end_try),
        (end_try),
        (scene_prop_set_slot, ":ship_instance", scene_prop_crew_number, ":agent_counter"),
      (end_try),]),
  
  # script_check_aground
  # Component: Ships and sea (scene)
  # This script checks collisions with the terrain AND with scene boarders
  # Input: ":ship_number"
  # Output: ":aground_site" (reg0) reg12, reg6
  ("check_aground",
    [   (try_begin),
        (set_fixed_point_multiplier, 100),
        (store_script_param, ":ship_number", 1),
        (scene_prop_get_instance, ":ship_instance", "spr_dyn_ship_substrate", ":ship_number"),
        (scene_prop_get_slot, ":ship_type", ":ship_instance", scene_prop_ship_type),
        (call_script, "script_get_ship_properties", ":ship_type"),
        (assign, ":distance_to_front", reg4),
        (assign, ":radius", reg5),
        (store_mul, ":big_distance", reg4, 1.5),
        (assign, reg0, -1),
        (try_begin),
          (position_get_distance_to_terrain, ":main_distance", pos1),
          (lt, ":main_distance", ":big_distance"),
          
          (copy_position, pos2, pos1),
          
          (copy_position, pos0, pos2),
          (position_move_x, pos0, ":distance_to_front"),
          (position_get_distance_to_terrain, ":12Uhr_distance", pos0),
          (copy_position, pos0, pos2),
          (store_mul, ":x_move", ":distance_to_front", -1),
          (position_move_x, pos0, ":x_move"),
          (position_get_distance_to_terrain, ":6Uhr_distance", pos0),
          (copy_position, pos0, pos2),
          (position_move_y, pos0, ":radius"),
          (position_get_distance_to_terrain, ":3Uhr_distance", pos0),
          (copy_position, pos0, pos2),
          (store_mul, ":y_move", ":radius", -1),
          (position_move_y, pos0, ":y_move"),
          (position_get_distance_to_terrain, ":9Uhr_distance", pos0),
          (copy_position, pos0, pos2),
          (store_mul, ":x_move", ":distance_to_front", 0.8),
          (position_move_x, pos0, ":x_move"),
          (store_mul, ":y_move", ":radius", 0.8),
          (position_move_y, pos0, ":y_move"),
          (position_get_distance_to_terrain, ":1Uhr_distance", pos0),
          (store_mul, ":y_move", ":radius", -1.6),
          (position_move_y, pos0, ":y_move"),
          (position_get_distance_to_terrain, ":11Uhr_distance", pos0),
          
          (assign, reg12, ":12Uhr_distance"),
          (assign, reg6, ":6Uhr_distance"),
          
          (assign, ":min_distance", 40),
          (this_or_next | le, ":12Uhr_distance", ":min_distance"),
          (this_or_next | le, ":11Uhr_distance", ":min_distance"),
          (this_or_next | le, ":9Uhr_distance", ":min_distance"),
          (this_or_next | le, ":6Uhr_distance", ":min_distance"),
          (this_or_next | le, ":3Uhr_distance", ":min_distance"),
          (le, ":1Uhr_distance", ":min_distance"),
          
          (try_begin),
            (le, ":12Uhr_distance", ":11Uhr_distance"),
            (le, ":12Uhr_distance", ":9Uhr_distance"),
            (le, ":12Uhr_distance", ":6Uhr_distance"),
            (le, ":12Uhr_distance", ":3Uhr_distance"),
            (le, ":12Uhr_distance", ":1Uhr_distance"),
            (assign, reg0, 12),
          (else_try),
            (le, ":6Uhr_distance", ":11Uhr_distance"),
            (le, ":6Uhr_distance", ":9Uhr_distance"),
            (le, ":6Uhr_distance", ":3Uhr_distance"),
            (le, ":6Uhr_distance", ":1Uhr_distance"),
            (assign, reg0, 6),
          (else_try),
            (le, ":3Uhr_distance", ":11Uhr_distance"),
            (le, ":3Uhr_distance", ":9Uhr_distance"),
            (le, ":3Uhr_distance", ":1Uhr_distance"),
            (assign, reg0, 3),
          (else_try),
            (le, ":9Uhr_distance", ":11Uhr_distance"),
            (le, ":9Uhr_distance", ":1Uhr_distance"),
            (assign, reg0, 9),
          (else_try),
            (le, ":11Uhr_distance", ":1Uhr_distance"),
            (assign, reg0, 11),
          (else_try),
            (assign, reg0, 1),
          (end_try),
        (else_try),
          (eq,":ship_number", "$player_ship_number"),
          (eq, "$coastal_battle", 0),
          (get_scene_boundaries, pos55, pos56),
          (position_get_x, ":x_min", pos55),
          (position_get_y, ":y_min", pos55),
          (position_get_x, ":x_max", pos56),
          (position_get_y, ":y_max", pos56),
          (val_add, ":x_min", ":big_distance"),
          (val_add, ":y_min", ":big_distance"),
          (val_sub, ":x_max", ":big_distance"),
          (val_sub, ":y_max", ":big_distance"),
          (prop_instance_get_position, pos0, ":ship_instance"),
          (position_get_x, ":x_ship", pos0),
          (position_get_y, ":y_ship", pos0),
          (try_begin),
            (ge, ":x_ship", ":x_max"),
            (assign, reg0, 51),
          (else_try),
            (ge, ":y_ship", ":y_max"),
            (assign, reg0, 52),
          (else_try),
            (le, ":x_ship", ":x_min"),
            (assign, reg0, 53),
          (else_try),
            (le, ":y_ship", ":y_min"),
            (assign, reg0, 54),
          (end_try),
        (end_try),
      (end_try),]),
  
  # script_set_crew_free
  # Component: Ships and sea (scene)
  # Input: ":ship_number"
  ("set_crew_free",
    [(try_begin),
        ### GET DATA
        (store_script_param, ":ship_number", 1),
        (scene_prop_get_instance, ":ship_instance", "spr_dyn_ship_substrate", ":ship_number"),
        ### SET CREW FREE
        (try_for_agents,":agent"),
          (agent_is_alive,":agent"),
          (agent_is_human,":agent"),
          (agent_is_non_player, ":agent"),
          (agent_get_slot, ":crewmate_of_ship_instance", ":agent", slot_agent_on_ship),
          (eq, ":crewmate_of_ship_instance", ":ship_instance"),
          (agent_clear_scripted_mode, ":agent"),
          (agent_ai_set_aggressiveness, ":agent", 9000), #!
          (agent_set_slot, ":agent", slot_agent_on_ship, 0), #!
        (end_try),
      (end_try),]),
  
  # script_give_agent_position_on_ship
  # Component: Ships and sea (scene)
  # Input: ":agent_counter", ":ship_instance"
  # Output: pos4
  ("give_agent_position_on_ship",
    [ (try_begin),
        (set_fixed_point_multiplier, 100),
        (store_script_param, ":agent_counter", 1),
        (store_script_param, ":ship_instance", 2),
        (copy_position, pos4, pos1),
        
        (try_begin),
          (gt, ":agent_counter", 0),	# position "0" is the guy at the rudder, so this code is only for all the
          # other guys
          (scene_prop_get_slot, ":ship_type", ":ship_instance", scene_prop_ship_type),
          (scene_prop_get_slot, ":ship_radius", ":ship_instance", scene_prop_radius),
          (val_sub, ":ship_radius", 80),
          (call_script, "script_get_ship_properties", ":ship_type"),
          (assign, ":men_in_double_line", reg6),
          (try_begin),
            (this_or_next | eq, ":ship_type", ship_type_busse),	# This means ship is big and we have 4 lines (otherwise we have only 2)
            (eq, ":ship_type", ship_type_karvi),
            (val_div, ":men_in_double_line", 2),
            (val_sub, ":men_in_double_line", 2),	# to make inner lines longer then outer lines
            #(val_sub, ":ship_radius", 30), # was 30
          (else_try),
            (position_move_x, pos4, 50),
          (try_end),
          (try_begin),
            (eq, ":ship_type", ship_type_busse),
            (val_sub, ":ship_radius", 50),
          (try_end),
          (assign, ":correction_for_line_3and4", 1),
          (try_begin),
            (gt, ":agent_counter", ":men_in_double_line"),	# this is for starting the 3.  line
            (val_sub, ":agent_counter", ":men_in_double_line"),
            (val_div, ":ship_radius", 2),
            (position_move_x, pos4, 60),	# "Phasenverschiebung"
            (assign, ":correction_for_line_3and4", 2),
          (try_end),
          
          # Well...  here we try this: formation starts in the middle and increases
          # on left and right equally to front and back...
          (assign, ":agent_distance", 120),
          
          (store_mod, ":uneven1", ":agent_counter", 2),
          (val_div, ":agent_counter", 2),
          (try_begin),
            (gt, ":uneven1", 0),
            (val_add, ":agent_counter", 1),
            (val_mul, ":ship_radius", -1),
          (end_try),
          (store_mod, ":uneven2", ":agent_counter", 2),
          (val_div, ":agent_counter", 2),
          (try_begin),
            (gt, ":uneven2", 0),
            (val_add, ":agent_counter", 1),
            (store_mul, ":agent_distance_neg", ":agent_distance", -1),
            (store_mul, ":x_position", ":agent_counter", ":agent_distance_neg"),
            (val_add, ":x_position", ":agent_distance"),	# This is to close the "gap" which otherwise would be here...
          (else_try),
            (store_mul, ":x_position", ":agent_counter", ":agent_distance"),
          (end_try),
          # "Well..." ends
          (try_begin),
            (this_or_next | eq, ":ship_type", ship_type_karvi),		# If hull isnt straight: karvi, knorr, byrding
            (this_or_next | eq, ":ship_type", ship_type_knorr),
            (eq, ":ship_type", ship_type_byrding),
            (assign, ":factor", 15),
          (else_try),
            (this_or_next | eq, ":ship_type", ship_type_busse),
            (eq, ":ship_type", ship_type_skei),
            (assign, ":factor", 4),
          (else_try),
            (this_or_next | eq, ":ship_type", ship_type_snekkja),
            (eq, ":ship_type", 8),				#skei2
            (assign, ":factor", 3),
          (else_try),
            (assign, ":factor", 2),
          (end_try),
          (try_begin),
            (val_mul, ":agent_counter", ":factor"),
            (val_div, ":agent_counter", ":correction_for_line_3and4"),
            (try_begin),
              (gt, ":uneven1", 0),
              (val_mul, ":agent_counter", -1),
            (try_end),
            (val_sub, ":ship_radius", ":agent_counter"),
          (try_end),
          
          (position_move_y, pos4, ":ship_radius"),
          (position_move_x, pos4, ":x_position"),
        (else_try),
          (scene_prop_get_slot, ":ship_length", ":ship_instance", scene_prop_distance_to_front),
          (val_sub, ":ship_length", 350),
          (val_mul, ":ship_length", -1),
          (position_move_x, pos4, ":ship_length"),
        (end_try),
        (position_move_z, pos4, 40, 1),	# because ship model has z=0 at waterline
      (end_try),]),
  
  # script_keep_crew_on_board
  # Component: Ships and sea (scene)
  # This script holds a crew on board
  # Input: ":ship_instance"
  # Output: -
  ("keep_crew_on_board",
    [ (try_begin),
        (store_script_param, ":ship_instance", 1),
        (set_fixed_point_multiplier, 100),
        (try_for_agents,":agent"),
          (agent_is_alive,":agent"),
          (agent_is_human,":agent"),
          (agent_is_non_player, ":agent"),
          (agent_slot_eq, ":agent", slot_agent_on_ship, ":ship_instance"),
          (try_begin),
            # #VC-2003 here we try to give the player room to walk on his ship
            # (neg|game_in_multiplayer_mode),
            # (game_key_is_down, gk_move_forward),
            # (agent_slot_eq, ":player_agent", slot_agent_on_ship, ":ship_instance"),
            # (agent_get_position, pos12, ":agent"),
            # (get_distance_between_positions, ":distance", pos11, pos12),
            # (le, ":distance", 90),
            # (agent_clear_scripted_mode, ":agent"),
            # (else_try),
            (agent_get_slot, ":position_on_ship", ":agent", slot_agent_position_on_ship),
            (call_script, "script_give_agent_position_on_ship", ":position_on_ship", ":ship_instance"),
            #(agent_set_scripted_destination,":agent", pos4, 0, 1), # NOT WORKING!
            (agent_set_scripted_destination,":agent", pos4, 0),
            #(val_add, ":agent_counter", 1), # testing
          (end_try),
        (end_try),
        # (store_random_in_range, ":rand", 1, 50), # testing
        # (eq, ":rand", 1),
        # (assign, reg1, ":agent_counter"), # testing
        # (display_message, "@TEST: agent_set_scripted_destination was called for
        # {reg1} agents."), # testing
      (end_try),]),
  
  # script_boarding_for_ship3
  # Component: Ships and sea (scene)
  # Input: ":ship_instance", ":ship2_instance"
  # Output: reg0 (1=yes, 0=no)
  ("boarding_for_ship3",
    [(try_begin),
        (store_script_param, ":ship_instance", 1),
        (store_script_param, ":ship2_instance", 2),
        (scene_prop_get_slot, ":ship2_left_boardet_ship", ":ship2_instance", 14),
        (scene_prop_get_slot, ":ship2_right_boardet_ship", ":ship2_instance", 15),
        (scene_prop_get_team, ":team_ship", ":ship_instance"),
        (try_begin),				# Getting extra data (partys of ships who are already boarding with ship2
          (neq, ":ship2_right_boardet_ship", 0),
          (neq, ":ship2_right_boardet_ship", ":ship_instance"),
          (scene_prop_get_team, ":team_ship2_right_boardet_ship", ":ship2_right_boardet_ship"),
          (neq, ":team_ship2_right_boardet_ship", ":team_ship"),					# has to be diff team
          (neq, ":team_ship2_right_boardet_ship", -2),
          (assign, reg0, 1),
        (else_try),
          (neq, ":ship2_left_boardet_ship", 0),
          (neq, ":ship2_left_boardet_ship", ":ship_instance"),
          (scene_prop_get_team, ":team_ship2_left_boardet_ship", ":ship2_right_boardet_ship"),
          (neq, ":team_ship2_left_boardet_ship", ":team_ship"),					# has to be diff team
          (neq, ":team_ship2_left_boardet_ship", -2),
          (assign, reg0, 1),
        (else_try),
          (assign, reg0, 0),
        (end_try),
      (end_try),]),
  
  # script_find_spawnpoint_around_entry
  # Component: Ships and sea (scene)
  # Input: ":entry_point_num"
  # Output: pos8
  ("find_spawnpoint_around_entry",
    [(store_script_param, ":entry_point_num", 1),
      (try_begin),
        (entry_point_get_position, pos8, ":entry_point_num"),
        (position_rotate_z, pos8, 90),				# correkt angle
        (position_set_z, pos8, 0),				# high of the ship
        (store_random_in_range, ":x" , -2, 3),
        (store_random_in_range, ":y" , -1, 1),
        (val_mul, ":x" , 2500),
        (val_mul, ":y" , 3500),
        (position_move_x, pos8, ":x", 1),
        (position_move_y, pos8, ":y", 1),
        #(assign, ":block", 0),
        (scene_prop_get_num_instances, ":number_of_ships", "spr_dyn_ship_substrate"),
        (try_for_range,":ship_number", 0, ":number_of_ships"),
          (scene_prop_get_instance, ":ship_instance", "spr_dyn_ship_substrate", ":ship_number"),
          #(neq, ":ship_instance", -1),
          (prop_instance_get_position, pos9, ":ship_instance"),
          (get_distance_between_positions, ":distance", pos8, pos9),
          (try_begin),
            (le, ":distance", 3500),
            (assign, ":block", 1),
          (end_try),
        (end_try),
        (neq, ":block", 1),
      (else_try),
        (call_script, "script_find_spawnpoint_around_entry", ":entry_point_num"),
      (end_try),]),
  
  # script_calculate_ship_value
  # Component: Ships and sea
  ("calculate_ship_value",
    [(store_script_param, ":ship_type", 1),
      (store_script_param, ":ship_condition", 2),
      (store_script_param, ":wood_type", 3),
      (call_script,  "script_get_ship_properties", ":ship_type"),
      (assign, ":max_speed", reg1),
      (assign, ":max_crew", reg6),
      
      (store_mul, ":speed_value", ":max_speed", ":ship_condition"),
      (store_mul, ":crew_value", ":max_crew", 100),
      (store_add, ":ship_value", ":speed_value", ":crew_value"),
      (val_mul, ":ship_value", 4),
      
      (try_begin),
        (eq, ":wood_type", 1),
        (val_mul, ":ship_value", 130),
        (val_div, ":ship_value", 100),
      (else_try),
        (eq, ":wood_type", 2),
        (val_mul, ":ship_value", 115),
        (val_div, ":ship_value", 100),
      (end_try),
      
      #final balancing
      (val_mul, ":ship_value", 90),
      (val_div, ":ship_value", 100),
      
      (assign, reg0, ":ship_value"),]),
  
  # script_get_ship_properties
  # Component: Ships and sea
  # Input: ":ship_type"
  ("get_ship_properties",
    [(try_begin),
        (store_script_param, ":ship_type", 1),
        (try_begin),
          (eq, ":ship_type", ship_type_busse),
          (assign, reg1, 16),		#scene_prop_max_speed
          (assign, reg2, -12),		#scene_prop_max_x_rotation, has to be negativ
          (assign, reg3, 5),		#scene_prop_max_y_rotation
          (assign, reg4, 1940),	#scene_prop_distance_to_front
          (assign, reg5, 375),	#scene_prop_radius
          (assign, reg6, 90),		#max_crew
          (assign, reg7, "str_busse"),	#type_name
        (else_try),
          (eq, ":ship_type", ship_type_skei),	#(hedeby1)
          (assign, reg1, 20),		#scene_prop_max_speed
          #(assign, reg2, -7), #scene_prop_max_x_rotation, has to be negativ
          (assign, reg2, -17),		#scene_prop_max_x_rotation, has to be negativ
          #(assign, reg3, 2), #scene_prop_max_y_rotation
          (assign, reg3, 4),		#scene_prop_max_y_rotation
          (assign, reg4, 2050),	#scene_prop_distance_to_front
          (assign, reg5, 170),	#scene_prop_radius
          (assign, reg6, 55),		#max_crew # was 74
          (assign, reg7, "str_skeid"),	#type_name
        (else_try),
          (eq, ":ship_type", ship_type_karvi),
          (assign, reg1, 12),		#scene_prop_max_speed
          #(assign, reg2, -7), #scene_prop_max_x_rotation, has to be negativ
          (assign, reg2, -12),		#scene_prop_max_x_rotation, has to be negativ
          #(assign, reg3, 2), #scene_prop_max_y_rotation
          (assign, reg3, 5),		#scene_prop_max_y_rotation
          (assign, reg4, 1150),	#scene_prop_distance_to_front
          (assign, reg5, 260),	#scene_prop_radius
          (assign, reg6, 39),		#max_crew (we need someting like 64 later!!!)
          (assign, reg7, "str_karvi"),	#type_name
        (else_try),
          (eq, ":ship_type", ship_type_snekkja),
          (assign, reg1, 17),		#scene_prop_max_speed ## Looking at facts this would have to be 12 (!)
          #(assign, reg2, -7), #scene_prop_max_x_rotation, has to be negativ
          (assign, reg2, -12),		#scene_prop_max_x_rotation, has to be negativ
          #(assign, reg3, 2), #scene_prop_max_y_rotation
          (assign, reg3, 5),		#scene_prop_max_y_rotation
          (assign, reg4, 1180),	#scene_prop_distance_to_front
          (assign, reg5, 165),	#scene_prop_radius
          (assign, reg6, 27),		#max_crew (we need 30 later!!)
          (assign, reg7, "str_snekkja"),	#type_name
        (else_try),
          (eq, ":ship_type", ship_type_knorr),
          (assign, reg1, 7),		#scene_prop_max_speed (historical = 4)
          (assign, reg2, -7),		# scene_prop_max_x_rotation, has to be negativ
          (assign, reg3, 2),		#scene_prop_max_y_rotation
          (assign, reg4, 760),	#scene_prop_distance_to_front #made bigger to fix bug in 1mt (was 550)
          (assign, reg5, 238),	#scene_prop_radius
          (assign, reg6, 11),		#max_crew # was 8!!!
          (assign, reg7, "str_knorr"),	#type_name
        (else_try),
          (eq, ":ship_type", ship_type_byrding),
          (assign, reg1, 9),		#scene_prop_max_speed (was 10)
          (assign, reg2, -7),		# scene_prop_max_x_rotation, has to be negativ
          (assign, reg3, 2),		#scene_prop_max_y_rotation
          (assign, reg4, 790),	#scene_prop_distance_to_front
          (assign, reg5, 190),	#scene_prop_radius
          (assign, reg6, 7),		#max_crew
          (assign, reg7, "str_byrding"),	#type_name
        (else_try),
          (eq, ":ship_type", 7),	### ???
          (assign, reg1, 10),		#scene_prop_max_speed
          (assign, reg2, -7),		# scene_prop_max_x_rotation, has to be negativ
          (assign, reg3, 2),		#scene_prop_max_y_rotation
          (assign, reg4, 625),	#scene_prop_distance_to_front
          (assign, reg5, 190),	#scene_prop_radius
          (assign, reg6, 7),		#max_crew
          (assign, reg7, "str_byrding"),	#type_name
        (else_try),
          (eq, ":ship_type", 8),	### Skeid 2
          (assign, reg1, 10),		#scene_prop_max_speed
          #(assign, reg2, -7), #scene_prop_max_x_rotation, has to be negativ
          (assign, reg2, -12),		#scene_prop_max_x_rotation, has to be negativ
          #(assign, reg3, 2), #scene_prop_max_y_rotation
          (assign, reg3, 5),		#scene_prop_max_y_rotation
          (assign, reg4, 1550),	#scene_prop_distance_to_front
          (assign, reg5, 180),	#scene_prop_radius
          (assign, reg6, 39),		#max_crew
          (assign, reg7, "str_byrding"),	#type_name
        (else_try),
          (display_message, "@{!}Error: I dont have this ship type"),
        (end_try),
      (end_try),
      
      (try_begin),
        (game_in_multiplayer_mode),
        (try_begin),
          (this_or_next | eq, ":ship_type", ship_type_skei),
          (eq, ":ship_type", ship_type_snekkja),
          (val_mul, reg6, 70),
          (val_div, reg6, 100),		# 70%, so we need less agents in MP to fill the narrow ships (VC-2944)
        (end_try),
        
        #Making ships faster in MP (there are too slow otherwise)
        (try_begin),
          (eq, ":ship_type", ship_type_byrding),
          (assign, reg1, 14),
        (else_try),
          (eq, ":ship_type", ship_type_karvi),
          (assign, reg1, 13),
        (else_try),
          (eq, ":ship_type", ship_type_knorr),
          (assign, reg1, 12),
        (end_try),
      (end_try),]),
  
  # script_spawn_ship
  # Component: Ships and sea (scene)
  # Output: reg0
  ("spawn_ship",
    [(try_begin),
        (set_fixed_point_multiplier, 100),
        (store_script_param, ":ship_type", 1),
        (store_script_param, ":ship_property", 2),
        (spawn_scene_prop, "spr_dyn_ship_substrate"),
        (assign, ":actual_ship_instance", reg0),
        #new:
        (val_add, "$number_of_ships_global", 1),
        
        # Test
        (try_for_range, ":curr_slot", scene_prop_sail, scene_prop_slots_end),
          (scene_prop_set_slot, ":actual_ship_instance", ":curr_slot", 0),
        (end_try),
        #(scene_prop_set_visibility, ":actual_ship_instance", 0),
        (try_begin),
          (le, ":ship_type", 0),	### choose random ship type if no special ship type is wanted
          (store_random_in_range, ":ship_type", 2, 6),
          (eq, ":ship_type", 5),
          (assign, ":ship_type", 6),
        (end_try),
        
        (call_script, "script_get_ship_properties", ":ship_type"),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_ship_type, ":ship_type"),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_max_speed, reg1),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_max_x_rotation, reg2),		# has to be negativ
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_max_y_rotation, reg3),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_distance_to_front, reg4),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_radius, reg5),
        
        (store_sub, ":ship_type_minus_one", ":ship_type", 1),
        
        (store_add, ":scn_prop", "spr_dyn_ship_main_busse", ":ship_type_minus_one"),
        (spawn_scene_prop, ":scn_prop"),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_main_instance, reg0), # assigns main instance to the other instance
        
        (store_add, ":scn_prop", "spr_dyn_ship_boom_busse", ":ship_type_minus_one"),
        (spawn_scene_prop, ":scn_prop"),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_boom_instance, reg0), # assigns boom instance to the other instance
        (scene_prop_set_visibility, reg0, 0),
        
        # cargo
        (call_script, "script_decode_value", ":ship_property"),
        (try_begin),
          (eq, ":ship_type", ship_type_knorr),
          (try_begin),
            (eq, reg5, 1),	# ship cargo
            (spawn_scene_prop, "spr_cargo_1_knorr"),
          (else_try),
            (spawn_scene_prop, "spr_cargo_0_knorr"),	#spawn the cover if there is no cargo in it
          (end_try),
          (scene_prop_set_slot, ":actual_ship_instance", scene_prop_cargo_1, reg0),
        (else_try),
          (eq, ":ship_type", ship_type_byrding),
          (eq, reg5, 1),
          (spawn_scene_prop, "spr_cargo_1_byrding"),
          (scene_prop_set_slot, ":actual_ship_instance", scene_prop_cargo_1, reg0),
        (end_try),
        
        #cages
        (try_begin),
          #(neg|game_in_multiplayer_mode),
          #SP cages
          (store_add, ":scn_prop", "spr_dyn_ship_cage_main_busse", ":ship_type_minus_one"),
          (spawn_scene_prop, ":scn_prop"),
          (scene_prop_set_slot, ":actual_ship_instance", scene_prop_cage_main, reg0),
          
          (store_add, ":scn_prop", "spr_dyn_ship_cage_left_busse", ":ship_type_minus_one"),
          (spawn_scene_prop, ":scn_prop"),
          (scene_prop_set_slot, ":actual_ship_instance", scene_prop_cage_left, reg0),
          
          (store_add, ":scn_prop", "spr_dyn_ship_cage_left_2_busse", ":ship_type_minus_one"),
          (spawn_scene_prop, ":scn_prop"),
          (scene_prop_set_slot, ":actual_ship_instance", scene_prop_cage_left_2, reg0),
          
          (store_add, ":scn_prop", "spr_dyn_ship_cage_left_3_busse", ":ship_type_minus_one"),
          (spawn_scene_prop, ":scn_prop"),
          (scene_prop_set_slot, ":actual_ship_instance", scene_prop_cage_left_3, reg0),
          
          (store_add, ":scn_prop", "spr_dyn_ship_cage_right_busse", ":ship_type_minus_one"),
          (spawn_scene_prop, ":scn_prop"),
          (scene_prop_set_slot, ":actual_ship_instance", scene_prop_cage_right, reg0),
          
          (store_add, ":scn_prop", "spr_dyn_ship_cage_right_2_busse", ":ship_type_minus_one"),
          (spawn_scene_prop, ":scn_prop"),
          (scene_prop_set_slot, ":actual_ship_instance", scene_prop_cage_right_2, reg0),
          
          (store_add, ":scn_prop", "spr_dyn_ship_cage_right_3_busse", ":ship_type_minus_one"),
          (spawn_scene_prop, ":scn_prop"),
          (scene_prop_set_slot, ":actual_ship_instance", scene_prop_cage_right_3, reg0),
        (end_try),
        
        (prop_instance_get_position, pos40, reg0),
        (position_set_z, pos40, -1000),
        (set_spawn_position, pos40),
        
        (store_add, ":scn_prop", "spr_dyn_ship_coll_busse", ":ship_type_minus_one"),
        (spawn_scene_prop, ":scn_prop"),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_collision_instance, reg0),
        
        (store_add, ":scn_prop", "spr_dyn_ship_coll2_busse", ":ship_type_minus_one"),
        (spawn_scene_prop, ":scn_prop"),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_collision_2_instance, reg0),
        
        #ramps
        (store_add, ":scn_prop", "spr_dyn_ship_ramp_right_busse", ":ship_type_minus_one"),
        (spawn_scene_prop, ":scn_prop"),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_ramp_1, reg0),
        
        (store_add, ":scn_prop", "spr_dyn_ship_ramp_left_busse", ":ship_type_minus_one"),
        (spawn_scene_prop, ":scn_prop"),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_ramp_left, reg0),
        
        (store_add, ":scn_prop", "spr_dyn_ship_ramp_land_busse", ":ship_type_minus_one"),
        (spawn_scene_prop, ":scn_prop"),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_ramp_2, reg0), # assigns ramp2-instance to the ship-instance
        
        #agent blade
        (try_begin),
          #(neg|game_in_multiplayer_mode), #not in MP
          (spawn_scene_prop, "spr_agent_blade"),
        (end_try),
        
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_boarding_left, 0),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_boarding_right, 0),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_boarding_progress, 0),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_going_to_boarding_with, 0),
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_timer, 5),	#new
        (scene_prop_set_slot, ":actual_ship_instance", scene_prop_timer_2, 20), #NEW 07.12.14
        
        #MATERIALS:
        (call_script, "script_set_ship_materials", ":actual_ship_instance", ":ship_property"),	# NEW!
        
        #OUTPUT:
        (assign, reg0, ":actual_ship_instance"),
        
      (end_try),]),
  
  # script_put_the_Klabautermann_into_the_ships
  # Component: Ships and sea (scene)
  # This is the main script for the movement of all ships in the scene
  
  ("put_the_Klabautermann_into_the_ships",
    [ (try_begin),
        (set_fixed_point_multiplier, 100),
        
        # START OF BIG LOOP
        (try_for_range,":ship_number", 0, "$number_of_ships_global"),
          
          ### GET DATA
          (scene_prop_get_instance, ":ship_instance", "spr_dyn_ship_substrate", ":ship_number"),
          (prop_instance_is_valid, ":ship_instance"),
          (scene_prop_get_team, ":team_ship", ":ship_instance"),
          
          (try_begin),
            #(neq, "$collision_check_disabled", 1),
            (call_script, "script_check_collision", ":ship_number"),
          (else_try),
            (assign, reg0, 0),
            (assign, reg1, 0),
          (end_try),
          (assign, ":ship2_instance", reg0),
          (assign, ":undodge", reg1),
          (scene_prop_get_slot, ":boarding_wanted", ":ship_instance", 12),
          (scene_prop_get_slot, ":landing_wanted", ":ship_instance", 13),
          (scene_prop_get_slot, ":left_boardet_ship", ":ship_instance", scene_prop_boarding_left),
          (scene_prop_get_slot, ":right_boardet_ship", ":ship_instance", scene_prop_boarding_right),
          (scene_prop_get_slot, ":boarding_progress", ":ship_instance", scene_prop_boarding_progress),
          (scene_prop_get_slot, ":going_to_boarding_with_ship", ":ship_instance", scene_prop_going_to_boarding_with),
          
          ### NEW: CECK CRITICAL COLLISION (AVOID CLIPPING)
          (assign, ":critical_collsion", 0),
          (try_begin),
            (gt, ":going_to_boarding_with_ship", 0),
            (scene_prop_get_slot, ":ship1_collision_2_instance", ":ship_instance", scene_prop_collision_2_instance),
            (scene_prop_get_slot, ":ship2_collision_2_instance", ":going_to_boarding_with_ship", scene_prop_collision_2_instance),
            (prop_instance_intersects_with_prop_instance, ":ship1_collision_2_instance", ":ship2_collision_2_instance"),
            (assign, ":critical_collsion", 1),
            #(assign, ":undodge", ":going_to_boarding_with_ship"),
            (ge, "$vc_debug_mode", 2),
            (display_message, "@{!}TEST: CRITICAL COLLISION!"),
          (end_try),
          
          (prop_instance_get_position, pos1, ":ship_instance"),	  								# pos1 will be the next destination
          (assign, reg8, ":ship_number"),		# for info messages
          (assign, ":keep_crew_on_board", 0),	# dont keep crew on board if no other is said in following lines...
          
          ### DELETE X-ROTATION AND Y-ROTATION OF POS1
          (position_get_rotation_around_z, ":z-rotation", pos1),
          (init_position, pos13),
          (position_copy_rotation, pos1, pos13),
          (position_rotate_z, pos1, ":z-rotation"),											# NIVELLIERUNG (modifies pos1)
          
          (try_begin),
            #(neq, "$aground_check_disabled", 1),
            (call_script, "script_check_aground", ":ship_number"),
          (else_try),
            (assign, reg0, -1),
          (end_try),
          
          
          (assign, ":aground_side", reg0),
          
          #########################
          ## A.  CHECK CONDITION ##
          #########################
          
          (try_begin),
            #####################
            ### 1.  FREE MOVE ###
            #####################
            
            ### CONDITIONS
            (eq, ":ship2_instance", -1),		# means NO collision
            (eq, ":aground_side", -1),			# means NO aground
            (eq, ":left_boardet_ship", 0),		# v
            (eq, ":right_boardet_ship", 0),		# means NO boarding
            (eq, ":boarding_progress", 0),		# means NO boarding progress
            (eq, ":going_to_boarding_with_ship", 0),	# means NO going to boarding
            
            ### REACTION
            (call_script, "script_ship_AI", ":ship_number"),	# This script will check by hisself if the ship is the playership
            (call_script, "script_calculate_movement_to_pos1", ":ship_number"),
            (assign, ":keep_crew_on_board", 1),
            
          (else_try),
            ###################
            ### 2.  AGROUND ###
            ###################
            
            ### CONDITIONS
            (ge, ":aground_side", 0),			# means aground
            
            ### REACTION
            (scene_prop_set_slot, ":ship_instance", scene_prop_last_speed, 0), # sets ship_speed to 0
            (assign, ":in_landing_zone", 0),
            (try_for_range, ":entry_number", 51, 60),
              (eq, "$coastal_battle", 1),
              (entry_point_get_position, pos0, ":entry_number"),
              (get_distance_between_positions, ":distance", pos1, pos0),
              (le, ":distance", 3000),	#!  sips will land in a radius of 30 meters around
              (assign, ":in_landing_zone", 1),
            (end_try),
            
            (try_begin),
              (this_or_next | eq, ":in_landing_zone", 1),
              (eq, ":landing_wanted", 1),
              (eq, ":boarding_progress", 0),
              # (display_message, "@{!}SHIP_{reg8}_Aground!"),
              (call_script, "script_set_crew_free", ":ship_number"),
              (assign, ":boarding_progress", -1),
              (assign, "$first_ship_landet", 1),		#!  According Coastal Assault...
              (assign, ":landing_wanted", 1),			# if it isnt already
            (else_try),
              (eq, ":landing_wanted", 0),
              #(gt, ":distance", 5000),
              #(display_message, "@{!}SHIP_{reg8}_Aground!"),
              (call_script, "script_calculate_embark_to_pos1", ":aground_side"),
              (assign, ":keep_crew_on_board", 1),
              (try_begin),
                (eq, ":boarding_progress", -1),
                (assign, ":boarding_progress", 0),
              (try_end),
            (try_end),
            (scene_prop_set_slot, ":ship_instance", 16, ":boarding_progress"),
            (scene_prop_set_slot, ":ship_instance", scene_prop_landing_wanted, ":landing_wanted"),
            (try_begin),
              (game_in_multiplayer_mode),
              (eq, ":boarding_progress", -1),
              (try_for_players,":i",1),
                (multiplayer_send_2_int_to_player,":i", multiplayer_event_ship, me_ship_return_ship_landed, ":ship_instance"),
              (try_end),
            (try_end),
            
          (else_try),
            ####################
            ### 3.  BOARDING ### (boarding was initiated, was in progress to be realised and is now realised)
            ####################
            
            ### CONDITIONS
            (this_or_next | neq, ":left_boardet_ship", 0),	# v
            (neq, ":right_boardet_ship", 0),				# means boarding
            (eq, ":boarding_progress", 0),					# means NO boarding progress
            
            # NEW:REMOVE MOVING ENTRIES
            (scene_prop_set_slot, ":ship_instance", scene_prop_last_speed, 0),
            (scene_prop_set_slot, ":ship_instance", scene_prop_last_turn, 0),
            
            # GETA DATA
            (call_script, "script_AI_decide_stop_boarding", ":ship_number"), #!!!  #outdated?
            
            ### CECK BOARDING ENTRIES
            (try_begin),	# LEFT SHIP
              (neq, ":left_boardet_ship", 0),
              (scene_prop_get_slot, ":ship2_left_boardet_ship", ":left_boardet_ship", scene_prop_boarding_left),
              (scene_prop_get_slot, ":ship2_right_boardet_ship", ":left_boardet_ship", scene_prop_boarding_right),
              (try_begin),
                (neq, ":ship2_left_boardet_ship", ":ship_instance"),
                (neq, ":ship2_right_boardet_ship", ":ship_instance"),
                (assign, ":left_boardet_ship", 0),
              (end_try),
            (end_try),
            
            (try_begin),	# RIGHT SHIP
              (neq, ":right_boardet_ship", 0),
              (scene_prop_get_slot, ":ship2_left_boardet_ship", ":right_boardet_ship", scene_prop_boarding_left),
              (scene_prop_get_slot, ":ship2_right_boardet_ship", ":right_boardet_ship", scene_prop_boarding_right),
              (try_begin),
                (neq, ":ship2_left_boardet_ship", ":ship_instance"),
                (neq, ":ship2_right_boardet_ship", ":ship_instance"),
                (assign, ":right_boardet_ship", 0),
              (end_try),
            (end_try),
            
            ### CHECK IF LEFT SHIP ALLOWS STOP BOARDING
            (try_begin),
              (eq, ":left_boardet_ship", 0),
              (assign, ":left_allowed", 1),
            (else_try),
              (neq, ":left_boardet_ship", 0),
              (scene_prop_get_team, ":team_left_ship", ":left_boardet_ship"),
              (call_script, "script_boarding_for_ship3", ":ship_instance", ":left_boardet_ship"),	# Getting extra data
              (assign, ":boarding_for_ship3", reg0),
              (eq, ":boarding_wanted", -1),		# this_or_next| ???
              (this_or_next | eq, ":team_left_ship", ":team_ship"),
              (eq, ":team_left_ship", -2), #????  was -1
              (eq, ":boarding_for_ship3", 0),
              (assign, ":left_allowed", 1),
            (else_try),
              (assign, ":left_allowed", 0),
            (end_try),
            
            ### CHECK IF RIGHT SHIP ALLOWS STOP BOARDING
            (try_begin),
              (eq, ":right_boardet_ship", 0),
              (assign, ":right_allowed", 1),
            (else_try),
              (neq, ":right_boardet_ship", 0),
              (scene_prop_get_team, ":team_right_ship", ":right_boardet_ship"),
              (call_script, "script_boarding_for_ship3", ":ship_instance", ":right_boardet_ship"),	# Getting extra data
              (assign, ":boarding_for_ship3", reg0),
              (eq, ":boarding_wanted", -1),	# this_or_next| ???
              (this_or_next | eq, ":team_right_ship", ":team_ship"),
              (eq, ":team_right_ship", -2),
              (eq, ":boarding_for_ship3", 0),
              (assign, ":right_allowed", 1),
            (else_try),
              (assign, ":right_allowed", 0),
            (end_try),
            
            ### CHECK IF SOMEBODY WANTS TO DOCK
            (assign, ":docker", 0),
            (try_begin),
              #(scene_prop_get_num_instances, ":number_of_ships",
              #"spr_dyn_ship_substrate"),
              (try_for_range,":ship_number_x", 0, "$number_of_ships_global"),
                (scene_prop_get_instance, ":ship_instance_x", "spr_dyn_ship_substrate", ":ship_number_x"),
                (scene_prop_get_slot, ":gtb_with_ship_instance_x", ":ship_instance_x", scene_prop_going_to_boarding_with),
                (eq, ":gtb_with_ship_instance_x", ":ship_instance"),
                (assign, ":docker", ":ship_instance_x"),
              (end_try),
              
              (scene_prop_get_slot, ":going_to_boarding_with_ship", ":ship_instance", scene_prop_going_to_boarding_with),
              
              (try_begin),
                (neq, ":going_to_boarding_with_ship", 0),			# I want to dock with the docker
                (eq, ":going_to_boarding_with_ship", ":docker"),
                (assign, ":blocked_by_docking", 1),
              (else_try),
                (neq, ":going_to_boarding_with_ship", 0),			# I want to dock, but not with the docker
                (neq, ":docker", 0),
                (neq, ":going_to_boarding_with_ship", ":docker"),
                (assign, ":blocked_by_docking", 1),
                (display_message, "@{!}SHIP_{reg8}_ERROR_1_CHECK_DOCKING", 0xFFFF0000),
              (else_try),
                (eq, ":going_to_boarding_with_ship", 0),			# I dont want to dock and nobody wants to dock me
                (eq, ":docker", 0),
                (assign, ":blocked_by_docking", 0),
              (else_try),
                (eq, ":going_to_boarding_with_ship", 0),			# I dont want to dock but somebody wants to dock me
                (neq, ":docker", 0),
                (assign, ":blocked_by_docking", 1),
                (display_message, "@{!}SHIP_{reg8}_ERROR_2_CHECK_DOCKING", 0xFFFF0000),
              (else_try),
                (eq, ":docker", 0),									# I want to dock somebody, but nobody want to dock me
                (neq, ":going_to_boarding_with_ship", 0),
                (assign, ":blocked_by_docking", 0),
                (display_message, "@{!}SHIP_{reg8}_ERROR_3_CHECK_DOCKING", 0xFFFF0000),
              (else_try),
                (neq, ":docker", 0),								# there is a Docker (Rest)
                (assign, ":blocked_by_docking", 1),
                (display_message, "@{!}SHIP_{reg8}_ERROR_4_CHECK_DOCKING", 0xFFFF0000),
              (else_try),
                #													# All other
                (assign, ":blocked_by_docking", 0),
                (display_message, "@{!}SHIP_{reg8}_ERROR_5_CHECK_DOCKING", 0xFFFF0000),
                ###(display_message, "@{!}SHIP_{reg8}_ERROR_:_Want to delete my Entry in
                ###SHIP {reg9} , but it isnt my entry.", 0xFFFF0000),
              (end_try),
            (end_try),
            
            ### DECISION
            (try_begin),
              ### BOARDING -> STOP
              (eq, ":left_allowed", 1),
              (eq, ":right_allowed", 1),
              (neq, ":blocked_by_docking", 1),
              (this_or_next | eq, ":left_boardet_ship", 0),	# Dont stop boarding if you are in sandwich
              (eq, ":right_boardet_ship", 0),				#
              
              #new condition for VC-1864
              (assign, ":battle_over", 0),
              (try_begin),
                (neg | game_in_multiplayer_mode),
                #(eq, "$g_battle_won", 1),
                (all_enemies_defeated),
                (assign, ":battle_over", 1),
              (end_try),
              (neq, ":battle_over", 1),
              #new condition for VC-1864 ends
              
              #NEW 07.12.14 begins
              (scene_prop_get_slot, ":timer", ":ship_instance", scene_prop_timer_2),
              (try_begin),# TIMER: ships decouple 5 seconds after last agent killed
                (gt, ":timer", 0),
                (val_sub, ":timer", 1),
                (scene_prop_set_slot, ":ship_instance", scene_prop_timer_2, ":timer"),
              (end_try),
              
              ### CREW
              (try_begin),
                (le, ":timer", 19),
                (call_script, "script_make_crew", ":ship_instance"),
                (assign, ":keep_crew_on_board", 1),
                #new: agent blade (VC-1546)
                (eq, ":timer", 19),
                #(neg|game_in_multiplayer_mode),
                (scene_prop_get_instance, ":blade_instance", "spr_agent_blade", ":ship_number"),
                (prop_instance_is_valid, ":blade_instance"),
                (scene_prop_get_slot, ":ship_radius", ":ship_instance", scene_prop_radius),
                (copy_position, pos3, pos1),
                (try_begin),
                  (scene_prop_slot_eq, ":ship_instance", scene_prop_boarding_right, 0),
                  (position_move_y, pos3, ":ship_radius"),		#left= positive
                (else_try),
                  (val_mul, ":ship_radius", -1),
                  (position_move_y, pos3, ":ship_radius"),		#right = negative
                (end_try),
                (position_move_z, pos3, 700),
                (prop_instance_set_position, ":blade_instance", pos3),
                (position_move_z, pos3, -1700),
                (prop_instance_animate_to_position, ":blade_instance", pos3, 475),
                #new: agent blade (VC-1546) ends
              (end_try),
              
              (le, ":timer", 0),
              (scene_prop_set_slot, ":ship_instance", scene_prop_timer_2, 20),
              
              #NEW 07.12.14 end
              
              # (display_message, "@{!}SHIP_{reg8}_Boarding_Stopt!"),
              
              ### REMOVE BOARDING ENTRIES
              (scene_prop_set_slot, ":ship_instance", scene_prop_boarding_left, 0),
              (scene_prop_set_slot, ":ship_instance", scene_prop_boarding_right, 0),
              (scene_prop_set_slot, ":ship_instance", scene_prop_going_to_boarding_with, 0),
              
              ### REMOVE MOVING ENTRIES
              (scene_prop_set_slot, ":ship_instance", scene_prop_last_speed, 0),
              (scene_prop_set_slot, ":ship_instance", scene_prop_last_turn, 0),
              
              
              
              #(else_try),
              ### BOARDING -> CONTINUE
            (end_try),
            
          (else_try),
            #############################
            ### 4.  GOING TO BOARDING ### (boarding was initiated and is now in progress to be realised)
            #############################
            
            ### CONDITIONS
            (scene_prop_get_slot, ":going_to_boarding_with_ship", ":ship_instance", scene_prop_going_to_boarding_with),
            (neq, ":going_to_boarding_with_ship", 0),
            (eq, ":undodge", -1),
            (eq, ":critical_collsion", 0),
            
            
            ### GET DATA
            (scene_prop_get_slot, ":ship2_left_boardet_ship", ":going_to_boarding_with_ship", 14),
            (scene_prop_get_slot, ":ship2_right_boardet_ship", ":going_to_boarding_with_ship", 15),
            
            ### IF SHIP IS SECOND SHIP
            (try_begin),
              (eq, ":ship2_left_boardet_ship", ":ship_instance"),
              (assign, ":ship2_left_boardet_ship", 0),
            (else_try),
              (eq, ":ship2_right_boardet_ship", ":ship_instance"),
              (assign, ":ship2_right_boardet_ship", 0),
            (end_try),
            
            ### CREW AND BOARDING ENTRIES
            (try_begin),
              (lt, ":boarding_progress", boarding_progress_peak),
              (assign, ":keep_crew_on_board", 1),
            (else_try),
              (eq, ":boarding_progress", boarding_progress_peak),
              (call_script, "script_set_crew_free", ":ship_number"),
            (end_try),
            
            ### ERROR DETECTION
            (try_begin),
              (scene_prop_get_slot, ":gtbw_ship_of_gtbw_ship", ":going_to_boarding_with_ship", scene_prop_going_to_boarding_with),
              (neq, ":gtbw_ship_of_gtbw_ship", ":ship_instance"),
              (display_message, "@SHIP_{reg8}_ERROR:_gtbws_not_the_same!", 0xFFFF0000),
            (end_try),
            
            ### DECIDE IF DOCKING OR MEETING
            (try_begin),
              (eq, ":ship2_left_boardet_ship", 0),	# this_or_next| ???
              (eq, ":ship2_right_boardet_ship", 0),
              (call_script, "script_calculate_meetingpoint_to_pos1", ":ship_number", ":going_to_boarding_with_ship"),
            (else_try),
              (call_script, "script_calculate_dockingpoint_to_pos1", ":ship_number", ":going_to_boarding_with_ship"),
            (end_try),
            
            (scene_prop_get_slot, ":going_to_boarding_with_ship", ":ship_instance", scene_prop_going_to_boarding_with),
            
          (else_try),
            ###################################
            ### 5.  ABORT GOING TO BOARDING ###
            ###################################
            
            ### CONDITIONS
            #(gt, ":boarding_progress", 0),
            (neq, ":going_to_boarding_with_ship", 0),
            (try_begin),
              (neq, ":ship2_instance", -1),		# means collision with ship that is NOT in boarding progress with ship1
              (neq, ":undodge", -1),				# means collision with ship that is unable to dodge
              (assign, ":critical_collsion", 2),
            (end_try),
            (neq, ":critical_collsion", 0),
            
            
            ### REACTION
            #(display_message, "@{!}SHIP_{reg8}_INFO:_ABORT_GOING_TO_BOARDING!"),
            
            # A.  CREW
            (try_begin),
              (ge, ":boarding_progress", boarding_progress_peak),
              (call_script, "script_make_crew", ":ship_instance"),
              (assign, ":keep_crew_on_board", 1),
            (end_try),
            (try_begin),
              (scene_prop_get_slot, ":boarding_progress_other_ship", ":going_to_boarding_with_ship", 16),
              (ge, ":boarding_progress_other_ship", 50),
              (call_script, "script_make_crew", ":going_to_boarding_with_ship"),
              (assign, ":keep_crew_on_board", 1),
            (end_try),
            
            # B.  ENTRIES
            (try_for_range,":ship_number_x", 0, "$number_of_ships_global"),
              (scene_prop_get_instance, ":ship_instance_x" , "spr_dyn_ship_substrate", ":ship_number_x"),
              (eq, ":ship_instance_x", ":going_to_boarding_with_ship"),
              (assign, reg9, ":ship_number_x"),
            (end_try),
            
            (assign, ":boarding_progress", 0),
            (scene_prop_set_slot, ":ship_instance", scene_prop_boarding_progress, 0),
            (scene_prop_set_slot, ":ship_instance", scene_prop_going_to_boarding_with, 0),
            (assign, reg8, ":ship_number"),
            #(display_message, "@{!}SHIP_{reg8}_INFO:_After_Abort_going_to_boarding_with
            #SHIP {reg9}_I_have_deleted_my_gtbws_entry!"),
            
            (try_begin),
              # each ship only deletes his own entry
              (scene_prop_get_slot, ":gtbw_ship_of_gtbw_ship", ":going_to_boarding_with_ship", scene_prop_going_to_boarding_with),
              (eq, ":gtbw_ship_of_gtbw_ship", ":ship_instance"),
              (scene_prop_set_slot, ":going_to_boarding_with_ship", scene_prop_going_to_boarding_with, 0),
              (scene_prop_set_slot, ":going_to_boarding_with_ship", scene_prop_boarding_progress, 0),
              #(display_message,
              #"@{!}SHIP_{reg8}_INFO:_After_Abort_going_to_boarding_I_have_deleted_the_gtbws_entry_of_SHIP_{reg9}!"),
            (else_try),
              (display_message, "@{!}SHIP_{reg8}_ERROR_:_Want to delete my Entry in SHIP {reg9} , but it isnt my entry.", 0xFFFF0000),
            (end_try),
            
            (try_begin),
              (eq, ":critical_collsion", 1),
              (call_script, "script_calculate_collisionreaction_to_pos1", ":going_to_boarding_with_ship", ":ship_instance"),
            (else_try),
              (call_script, "script_calculate_collisionreaction_to_pos1", ":undodge", ":ship_instance"),
            (end_try),
            (assign, ":going_to_boarding_with_ship", 0), # important at this position (dont move up)
            
          (else_try),
            ####################
            ### 6.  COLLISION ###
            ####################
            
            ### CONDITIONS
            (neq, ":ship2_instance", -1),	# means collision
            
            ### GET DATA
            (prop_instance_get_scene_prop_kind, ":collided_scene_prop_type", ":ship2_instance"),
            (try_begin),
              (neq, ":collided_scene_prop_type", "spr_barrier_ship"),#new!!!
              (scene_prop_get_team, ":team_ship", ":ship_instance"),
              (scene_prop_get_team, ":team_ship2", ":ship2_instance"),
              (scene_prop_get_slot, ":ship2_boarding_wanted", ":ship2_instance", 12),
              (scene_prop_get_slot, ":ship2_left_boardet_ship", ":ship2_instance", 14),
              (scene_prop_get_slot, ":ship2_right_boardet_ship", ":ship2_instance", 15),
              (scene_prop_get_slot, ":ship2_boarding_progress", ":ship2_instance", 16),
              (call_script, "script_boarding_for_ship3", ":ship_instance", ":ship2_instance"),	# Getting extra data
              (assign, ":boarding_for_ship3", reg0),
            (end_try),
            
            ### GOING TO BOARDING CONDITION
            (try_begin),
              (neq, ":collided_scene_prop_type", "spr_barrier_ship"),	#new!!!
              (scene_prop_get_slot, ":ship2_going_to_boarding_with", ":ship2_instance", scene_prop_going_to_boarding_with),
              (assign, ":blocked_by_docking_2", 1),
              (try_begin),
                (eq, ":ship2_going_to_boarding_with", 0),				# this_or_next|
                #(eq, ":ship2_going_to_boarding_with", ":ship_instance"),
                (assign, ":blocked_by_docking_2", 0),
              (end_try),
            (end_try),
            
            ###	CONDITIONS
            (assign, ":boarding_conditions_fullfilled", 0),
            (try_begin),
              (neq, ":collided_scene_prop_type", "spr_barrier_ship"),	#new!!!
              (neq, ":blocked_by_docking_2", 1),
              (eq, ":ship2_boarding_progress", 0), 					# to avoid problems
              (this_or_next | eq, ":ship2_left_boardet_ship", 0),		# v
              (eq, ":ship2_right_boardet_ship", 0),					# at least one side has to be free
              (neq, ":team_ship", -2),								# no boarding initiation for empty ships
              
              (try_begin),
                # BOARDING ENEMY SHIP ?
                (neq, ":team_ship2", ":team_ship"),					# has to be diff team
                (neq, ":team_ship2", -2),								# ship = empty
                (this_or_next | ge, ":boarding_wanted", 0),				# v
                (ge, ":ship2_boarding_wanted", 0),					# at least one ship has to want it
                (assign, ":boarding_conditions_fullfilled", 1),
              (else_try),
                # BOARDING FRIENDLY SHIP ?
                (this_or_next | eq, ":team_ship", ":team_ship2"),
                (eq, ":team_ship2", -2),							# nobody on ship
                (this_or_next | eq, ":boarding_wanted", 1),
                (this_or_next | eq, ":ship2_boarding_wanted", 1),
                (eq, ":boarding_for_ship3", 1),
                (assign, ":boarding_conditions_fullfilled", 1),
              (end_try),
            (end_try),
            
            (try_begin),
              ### 6.1 COLLISION -> INIATE BOARDING
              # TEST
              # (assign, reg1, ":ship_number"),
              # (assign, reg2, "$player_ship_number"),
              # (assign, reg3, ":boarding_conditions_fullfilled"),
              # (display_message, "@ship {reg1}, player ship {reg2}, boarding {reg3}"),
              #
              (eq, ":boarding_conditions_fullfilled", 1),
              (scene_prop_get_slot, ":last_speed", ":ship_instance", scene_prop_last_speed),
              (scene_prop_set_slot, ":ship_instance", scene_prop_last_speed, 0), # sets ship_speed to 0
              (scene_prop_set_slot, ":ship_instance", 10, 0), # sets ship_turn to 0
              (scene_prop_set_slot, ":ship_instance", 12, 0), 	# sets boarding wanted to 0
              
              #(scene_prop_get_num_instances, ":number_of_ships_x",
              #"spr_dyn_ship_substrate"),
              (try_for_range,":ship_number_x", 0, "$number_of_ships_global"),
                (scene_prop_get_instance, ":ship_instance_x" , "spr_dyn_ship_substrate", ":ship_number_x"),
                (eq, ":ship_instance_x", ":ship2_instance"),
                (assign, ":ship2_number", ":ship_number_x"),
                (assign, reg9, ":ship2_number"),
              (end_try),
              
              
              (try_begin),
                ### 6.1.1 FREE BOARDING (ship1 and ship2 get together)
                (eq, ":ship2_left_boardet_ship", 0),
                (eq, ":ship2_right_boardet_ship", 0),
                (scene_prop_set_slot, ":ship_instance", scene_prop_going_to_boarding_with, ":ship2_instance"),
                (scene_prop_set_slot, ":ship2_instance", scene_prop_going_to_boarding_with, ":ship_instance"),		#
                (scene_prop_set_slot, ":ship_instance", scene_prop_boarding_progress, 2),							# 2 !!!!!!!!!!
                (scene_prop_set_slot, ":ship2_instance", scene_prop_boarding_progress, 2),							# 2 !!!!!!!!!!
                #(display_message,
                #"@{!}SHIP_{reg8}_INFO:_I_change_my_gtbws_entry_and_the_entry_of_SHIP_{reg9}_!_(Free_Boarding)"),
                (call_script, "script_calculate_meetingpoint_to_pos1", ":ship_number", ":ship2_instance"),
              (else_try),
                ### 6.1.2 DOCKING (ship2 already boardet)
                (this_or_next | eq, ":ship2_left_boardet_ship", 0),
                (eq, ":ship2_right_boardet_ship", 0),
                (scene_prop_set_slot, ":ship_instance", scene_prop_going_to_boarding_with, ":ship2_instance"),
                (scene_prop_set_slot, ":ship2_instance", scene_prop_going_to_boarding_with, ":ship_instance"),
                (scene_prop_set_slot, ":ship_instance", scene_prop_boarding_progress, 2),							# 2 !!!!!!!!!!
                #(display_message,
                #"@{!}SHIP_{reg8}_INFO:_I_change_my_gtbws_entry_and_the_entry_of_SHIP_{reg9}_!_(Forced_Boarding)"),
                (call_script, "script_calculate_dockingpoint_to_pos1", ":ship_number", ":ship2_instance"),
              (end_try),
              
              (try_begin),	# play sound
                (try_begin),
                  # (eq, ":ship_number", "$player_ship_number"), off for now, since
                  # player's ship never comes up
                  (play_sound_at_position, "snd_ship_start_boarding", pos1),
                (try_end),
                (gt, ":last_speed", 30),
                (play_sound_at_position, "snd_crash", pos1),
                (ge, "$vc_debug_mode", 2),
                (display_message, "@{!}TEST: I'm playing snd_crash now"),
              (end_try),
              
              (scene_prop_set_slot, ":ship_instance", scene_prop_timer_2, 20),	#new
              
              (assign, ":keep_crew_on_board", 1),
              
            (else_try),
              ### 6.2 COLLISION -> JUST COLLIDE
              #(display_message, "@{!}just_collide!"),
              (call_script, "script_calculate_collisionreaction_to_pos1", ":ship2_instance", ":ship_instance"),
              (scene_prop_get_slot, ":speed", ":ship_instance", scene_prop_last_speed),
              
              (try_begin),
                # reduce speed
                (gt, ":speed", 1),
                (val_div, ":speed", 2),
                (scene_prop_set_slot, ":ship_instance", scene_prop_last_speed, ":speed"),
                # sound for player
                # (eq, ":ship_number", "$player_ship_number"), off for now, since players
                # ship never comes up
                (gt, ":last_speed", 30),
                (play_sound_at_position, "snd_crash", pos1),
                (ge, "$vc_debug_mode", 2),
                (display_message, "@{!}TEST: I'm playing snd_crash now"),
              (end_try),
              
              (assign, ":keep_crew_on_board", 1),
            (end_try),
            
          (end_try),
          
          ##################
          ## B.  REACTION ##
          ##################
          
          ### GET DATA
          (scene_prop_get_instance, ":ship_instance", "spr_dyn_ship_substrate", ":ship_number"),
          (scene_prop_get_slot, ":ship_main_instance", ":ship_instance", scene_prop_main_instance),
          (scene_prop_get_slot, ":ship_boom_instance", ":ship_instance", scene_prop_boom_instance),
          (scene_prop_get_slot, ":ship_cargo_1_instance", ":ship_instance", scene_prop_cargo_1),
          (scene_prop_get_slot, ":ship_collision_instance", ":ship_instance", scene_prop_collision_instance),
          (scene_prop_get_slot, ":ship_collision_2_instance", ":ship_instance", scene_prop_collision_2_instance),
          (scene_prop_get_slot, ":ship_ramp_right", ":ship_instance", scene_prop_ramp_1),
          (scene_prop_get_slot, ":ship_ramp_left", ":ship_instance", scene_prop_ramp_left),
          (scene_prop_get_slot, ":ship_ramp_land", ":ship_instance", scene_prop_ramp_2),
          (scene_prop_get_slot, ":ship_cage_main", ":ship_instance", scene_prop_cage_main),
          (scene_prop_get_slot, ":ship_cage_left", ":ship_instance", scene_prop_cage_left),
          (scene_prop_get_slot, ":ship_cage_left_2", ":ship_instance", scene_prop_cage_left_2),
          (scene_prop_get_slot, ":ship_cage_left_3", ":ship_instance", scene_prop_cage_left_3),
          (scene_prop_get_slot, ":ship_cage_right", ":ship_instance", scene_prop_cage_right),
          (scene_prop_get_slot, ":ship_cage_right_2", ":ship_instance", scene_prop_cage_right_2),
          (scene_prop_get_slot, ":ship_cage_right_3", ":ship_instance", scene_prop_cage_right_3),
          (scene_prop_get_slot, ":last_y_cosinus", ":ship_instance", scene_prop_y_cosinus),
          (scene_prop_get_slot, ":left_boardet_ship", ":ship_instance", scene_prop_boarding_left),
          (scene_prop_get_slot, ":right_boardet_ship", ":ship_instance", scene_prop_boarding_right),
          (scene_prop_get_slot, ":boarding_progress", ":ship_instance", scene_prop_boarding_progress),
          
          ### WANK AND SPLASH
          (call_script, "script_calculate_rocking_to_pos1"),
          (try_begin),
            (store_add, ":Origin_z", "$Amplitude_x", "$Amplitude_y"),
            (gt, ":Origin_z", 10000),	#only if there are high waves...
            (gt, ":last_y_cosinus", 0),
            (lt, "$y_cosinus", ":last_y_cosinus"),
            (scene_prop_get_slot, ":ship_distance_to_front", ":ship_instance", scene_prop_distance_to_front),
            (scene_prop_get_slot, ":ship_speed", ":ship_instance", scene_prop_last_speed),
            (gt, ":ship_speed", 2),
            (store_div, ":burst_strength", ":ship_speed", 5),	#percentage
            (copy_position, pos4, pos1),
            (position_move_x, pos4, ":ship_distance_to_front"),
            (position_rotate_z, pos4, 45),
            (particle_system_burst, "psys_front_water", pos4, ":burst_strength"),
            (position_rotate_z, pos4, -90),
            (particle_system_burst, "psys_front_water", pos4, ":burst_strength"),
            (play_sound_at_position, "snd_front_water_begin", pos4),
          (end_try),
          (scene_prop_set_slot, ":ship_instance", scene_prop_y_cosinus, "$y_cosinus"),
          
          ### DESTROYED SHIPS
          (try_begin),
            (scene_prop_get_slot, ":ship_quality", ":ship_instance", scene_prop_quality),
            (le, ":ship_quality", 5),
            (position_move_z, pos1, -25),
            #(position_rotate_x, pos1, 9),
            (position_rotate_y, pos1, -9),
          (try_end),
          
          ### SAIL
          (scene_prop_get_slot, ":sail", ":ship_instance", 6),
          (try_begin),
            (eq, ":sail", 1),
            (scene_prop_get_visibility, ":visibility", ":ship_boom_instance"),
            (eq, ":visibility", 0),
            (try_begin),
              # (eq, ":ship_number", "$player_ship_number"),
              (prop_instance_play_sound, ":ship_instance", "snd_set_sail"),
            (end_try),
            (scene_prop_set_visibility, ":ship_boom_instance", 100),
          (else_try),
            (eq, ":sail", 0),
            (scene_prop_set_visibility, ":ship_boom_instance", 0),
          (try_end),
          
          # BOOM/SAIL
          (try_begin),						# boom moves with wind
            (prop_instance_get_position, pos11, ":ship_instance"),
            (prop_instance_get_position, pos12, ":ship_boom_instance"),
            (get_angle_between_positions, ":rotation_ship_boom", pos11, pos12),	# current angle between ship and boom (was _old)
            (prop_instance_get_position, pos3, "$wind_spr"),
            (get_angle_between_positions, ":rotation_ship_wind", pos1, pos3),
            (store_sub, ":value", ":rotation_ship_wind", ":rotation_ship_boom"),
            (val_clamp, ":value", -1000, 1001),
            (store_add, ":z_rotation", ":rotation_ship_boom", ":value"),
            (val_clamp, ":z_rotation", -8000, 8001),
            (copy_position, pos2, pos1),
            (position_rotate_z_floating, pos2, ":z_rotation"),
          (try_end),
          
          ### CREW # This script is consuming many resources!  Therefore we try to
          ### reduce calling
          (assign, ":ship_number_x", ":ship_number"),
          (try_for_range, ":unused", 0, 5),	#20 = up to 24 ships
            (ge, ":ship_number_x", 4),
            (val_sub, ":ship_number_x", 4),
          (end_try),
          
          (scene_prop_get_slot, ":timer_3", ":ship_instance", scene_prop_timer_3),
          (try_begin),
            (is_between, ":timer_3", 0, 4),
            (val_add, ":timer_3", 1),
          (else_try),
            (assign, ":timer_3", 0),
          (end_try),
          (scene_prop_set_slot, ":ship_instance", scene_prop_timer_3, ":timer_3"),
          
          (try_begin),
            (eq, ":keep_crew_on_board", 1),
            # (store_current_scene, ":current_scene"),                    #VC-3286
            # (this_or_next | neq, ":current_scene", "scn_doccinga_ca"),    #VC-3286
			(this_or_next | eq, ":ship_number", "$player_ship_number"), #VC-3491
            (eq, ":timer_3", ":ship_number_x"),
            (call_script, "script_keep_crew_on_board", ":ship_instance"),
          (end_try),
          
          ### MOVE THE MAIN-INSTANCES
          (assign, ":animation_duration", 35),		# depends on frequennce of callinng trigger and the wanted smoothnes
          (prop_instance_animate_to_position, ":ship_instance", pos1, ":animation_duration"),
          (prop_instance_animate_to_position, ":ship_main_instance", pos1, ":animation_duration"),
          (prop_instance_animate_to_position, ":ship_boom_instance", pos2, ":animation_duration"),
          (try_begin),
            (neq, ":ship_cargo_1_instance", 0),
            (prop_instance_animate_to_position, ":ship_cargo_1_instance", pos1, ":animation_duration"),
          (end_try),
          (copy_position, pos2, pos1),
          (position_move_z, pos2, -1000),			# collision instances move under the ground tto prevent interaction with
          # agents
          (prop_instance_animate_to_position, ":ship_collision_instance", pos2, ":animation_duration"),
          (prop_instance_animate_to_position, ":ship_collision_2_instance", pos2, ":animation_duration"),
          (copy_position, pos3, pos1),
          (position_move_z, pos3, 700),
          
          ### MOVE THE OTHER INSTANCES
          (scene_prop_get_slot, ":timer", ":ship_instance", scene_prop_timer_2),#NEW 07.12.14
          (val_max, ":timer", 1),
          (store_mul, ":animation_duration_2", ":timer"), #With the new timer the technical instances have more time to move
          
          #pos1 = main ship position
          #pos2 = deep under the ship
          #pos3 = over the ship
          
          # CENTRAL
          (try_begin),
            (eq, ":boarding_progress", -1),
            (prop_instance_set_scale, ":ship_ramp_land", 100, 100, 100),
            (prop_instance_animate_to_position, ":ship_ramp_land", pos1, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_main", pos3, ":animation_duration"),
          (else_try),
            (prop_instance_set_scale, ":ship_ramp_land", 1, 1, 1),
            (prop_instance_animate_to_position, ":ship_ramp_land", pos2, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_main", pos1, ":animation_duration"),
          (try_end),
          
          # LEFT
          (try_begin),
            # 0.Ship is landed
            (eq, ":boarding_progress", -1),
            (prop_instance_animate_to_position, ":ship_ramp_left", pos2, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_left", pos3, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_left_2", pos3, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_left_3", pos3, ":animation_duration"),
            (prop_instance_set_scale, ":ship_ramp_left", 1, 1, 1),
          (else_try),
            # 1.Ship is currently decoupling
            (lt, ":timer", 20),
            (eq, ":boarding_progress", 0),
            (neq, ":left_boardet_ship", 0),
            (prop_instance_animate_to_position, ":ship_ramp_left", pos2, ":animation_duration_2"),
            (prop_instance_animate_to_position, ":ship_cage_left", pos1, ":animation_duration_2"),
            (prop_instance_animate_to_position, ":ship_cage_left_2", pos1, ":animation_duration_2"),
            (prop_instance_animate_to_position, ":ship_cage_left_3", pos1, ":animation_duration_2"),
          (else_try),
            # 2.Currently boarding other ship
            (eq, ":boarding_progress", 0),
            (neq, ":left_boardet_ship", 0),
            (prop_instance_animate_to_position, ":ship_ramp_left", pos1, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_left", pos3, ":animation_duration"),
            (scene_prop_get_slot, ":ship2_type", ":left_boardet_ship", scene_prop_ship_type),
            (try_begin),# exceptions ramps
              (scene_prop_get_slot, ":ship_type", ":ship_instance", scene_prop_ship_type),
              (this_or_next | eq, ":ship_type", ship_type_skei),
              (eq, ":ship_type", ship_type_snekkja),
              (neq, ":ship2_type", ship_type_skei),
              (neq, ":ship2_type", ship_type_snekkja),
              (prop_instance_set_scale, ":ship_ramp_left", 55, 100, 155),
            (else_try),
              (prop_instance_set_scale, ":ship_ramp_left", 100, 100, 100),
            (end_try),
            (try_begin),# exceptions cages
              (le, ":ship2_type", ship_type_skei),
              (prop_instance_animate_to_position, ":ship_cage_left_2", pos3, ":animation_duration"),
              (prop_instance_animate_to_position, ":ship_cage_left_3", pos3, ":animation_duration"),
            (else_try),
              (le, ":ship2_type", ship_type_knorr),
              (prop_instance_animate_to_position, ":ship_cage_left_2", pos3, ":animation_duration"),
              (prop_instance_animate_to_position, ":ship_cage_left_3", pos1, ":animation_duration"),
            (else_try),
              (prop_instance_animate_to_position, ":ship_cage_left_2", pos1, ":animation_duration"),
              (prop_instance_animate_to_position, ":ship_cage_left_3", pos1, ":animation_duration"),
            (end_try),
          (else_try),
            # 3.Regular driving
            (prop_instance_animate_to_position, ":ship_ramp_left", pos2, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_left", pos1, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_left_2", pos1, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_left_3", pos1, ":animation_duration"),
            (prop_instance_set_scale, ":ship_ramp_left", 1, 1, 1),
          (end_try),
          
          #RIGHT
          (try_begin),
            # 0.Ship is landed
            (eq, ":boarding_progress", -1),
            (prop_instance_animate_to_position, ":ship_ramp_right", pos2, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_right", pos3, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_right_2", pos3, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_right_3", pos3, ":animation_duration"),
            (prop_instance_set_scale, ":ship_ramp_right", 1, 1, 1),
          (else_try),
            # 1.Ship is currently decoupling
            (lt, ":timer", 20),
            (eq, ":boarding_progress", 0),
            (neq, ":right_boardet_ship", 0),
            (prop_instance_animate_to_position, ":ship_ramp_right", pos2, ":animation_duration_2"),
            (prop_instance_animate_to_position, ":ship_cage_right", pos1, ":animation_duration_2"),
            (prop_instance_animate_to_position, ":ship_cage_right_2", pos1, ":animation_duration_2"),
            (prop_instance_animate_to_position, ":ship_cage_right_3", pos1, ":animation_duration_2"),
          (else_try),
            # 2.Currently boarding other ship
            (eq, ":boarding_progress", 0),
            (neq, ":right_boardet_ship", 0),
            (prop_instance_animate_to_position, ":ship_ramp_right", pos1, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_right", pos3, ":animation_duration"),
            (scene_prop_get_slot, ":ship2_type", ":right_boardet_ship", scene_prop_ship_type),
            (try_begin),# exceptions ramps
              (scene_prop_get_slot, ":ship_type", ":ship_instance", scene_prop_ship_type),
              (this_or_next | eq, ":ship_type", ship_type_skei),
              (eq, ":ship_type", ship_type_snekkja),
              (neq, ":ship2_type", ship_type_skei),
              (neq, ":ship2_type", ship_type_snekkja),
              (prop_instance_set_scale, ":ship_ramp_right", 55, 100, 155),
            (else_try),
              (prop_instance_set_scale, ":ship_ramp_right", 100, 100, 100),
            (end_try),
            (try_begin),# exceptions cages
              (le, ":ship2_type", ship_type_skei),
              (prop_instance_animate_to_position, ":ship_cage_right_2", pos3, ":animation_duration"),
              (prop_instance_animate_to_position, ":ship_cage_right_3", pos3, ":animation_duration"),
            (else_try),
              (le, ":ship2_type", ship_type_knorr),
              (prop_instance_animate_to_position, ":ship_cage_right_2", pos3, ":animation_duration"),
              (prop_instance_animate_to_position, ":ship_cage_right_3", pos1, ":animation_duration"),
            (else_try),
              (prop_instance_animate_to_position, ":ship_cage_right_2", pos1, ":animation_duration"),
              (prop_instance_animate_to_position, ":ship_cage_right_3", pos1, ":animation_duration"),
            (end_try),
          (else_try),
            # 3.Regular driving
            (prop_instance_animate_to_position, ":ship_ramp_right", pos2, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_right", pos1, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_right_2", pos1, ":animation_duration"),
            (prop_instance_animate_to_position, ":ship_cage_right_3", pos1, ":animation_duration"),
            (prop_instance_set_scale, ":ship_ramp_right", 1, 1, 1),
          (end_try),
          
        (try_end),			### END OF BIG LOOP
      (try_end),]),
  
  # script_cf_is_port_party
  # Component: Ships and sea (map)
  ("cf_is_port_party",
    [(store_script_param, ":tested_party", 1),
      (assign, reg7, 0),
      
      (assign, ":is_port", 0),
      (try_for_range, ":cur_town", towns_begin, towns_end),
        (party_slot_eq, ":cur_town",slot_town_port, 1),
        (party_get_slot, ":cur_port", ":cur_town", slot_party_port_party),
        (eq, ":cur_port", ":tested_party"),
        (assign, reg7, ":cur_town"),
        (assign, ":is_port", 1),
      (try_end),
      (try_for_range, ":cur_jetty", "p_jetty_1", "p_wales_spawn_point"),
        (party_get_slot, ":cur_port", ":cur_jetty", slot_party_port_party),
        (eq, ":cur_port", ":tested_party"),
        (assign, reg7, ":cur_jetty"),
        (assign, ":is_port", 1),
      (try_end),
      (eq, ":is_port", 1),
      
      (try_begin),
        (eq, ":tested_party", 0),
        (str_store_party_name, s1, reg7),
        (display_message, "@{!}ERROR in script_cf_is_port_party: {s1}"),
      (end_try),]),
  
  # script_remove_sea_parties_on_land
  # Fixing VC-3467
  ("remove_sea_parties_on_land", [
      (try_for_parties, ":current_party"),
        (party_is_active, ":current_party"),
        (party_slot_eq, ":current_party", slot_party_on_water, 0),
        (party_get_template_id, reg1, ":current_party"),
        (is_between, reg1, "pt_fisher_ship", "pt_viking_raider"),
        (neg|party_is_in_any_town, ":current_party"),
        (remove_party, ":current_party"),
      (try_end),
  ]),
]




vc_text_scripts = [
  
  # script_texto_historia_character
  # character creation personaje creacion chief
  ("texto_historia_character",
    [(str_clear,s1),
      
      (try_begin),
        (eq, "$age_type", cb4_elder),
        (str_store_string,s1,"@You are an elder. You can count many years toward your life experiences. You may not be as strong and agile as before, but certainly you are wiser."),
      (else_try),
        (eq, "$age_type", cb4_adult),
        (str_store_string,s1,"@You are an adult. You are at an age where a reasonable amount of experience and health meet."),
      (else_try),
        (eq, "$age_type", cb4_young),
        (str_store_string,s1,"@You are a youth. You are full of energy and eager to change the world around you."),
      (try_end),
      
      ## Virtue
      (str_store_string,s2,"@According to Galen's ancient temperaments, your personality is:"),
      (try_begin),
        (eq, "$virtue_type", cb5_calm),
        (str_store_string,s3,"@Phlegmatic. Its element is water and its Hippocrates humor is phlegm. It is associated with people who are calm, compassionate, self-content, reliable, shy, observant and consistent. These people are also resistant to change, distant, relaxed and reserved."),
      (else_try),
        (eq, "$virtue_type", cb5_responsible),
        (str_store_string,s3,"@Melancholic. Its element is earth and its Hippocrates humor is black bile. It is associated with people who are introverted, sensitive, intuitive, creative, perfectionistic, patient, caring and tolerant. These people are also clumsy, obsessed, depressed and dissatisfied."),
      (else_try),
        (eq, "$virtue_type", cb5_extroverted),
        (str_store_string,s3,"@Choleric. Its element is fire and its Hippocrates humor is yellow bile. It is associated with people who are persuasive, egocentric, extroverted, independent, charismatic and ambitious. These people are also insistent, impetuous, bad-tempered, restless and touchy."),
      (else_try),
        (eq, "$virtue_type", cb5_bold),
        (str_store_string,s3,"@Sanguine. Its element is air and its Hippocrates humor is blood. It is associated with people who are optimistic, imaginative, artistic, cheerful, sociable, rational and pleasure-seeking. These people are also forgetful, impulsive and unpredictable."),
      (try_end),
      (str_store_string,s1,"@{s1}^^{s2} {s3}"),
      
      ## defects
      (str_store_string,s2,"@According the four cardinal virtues recognized in the writings of classical antiquity, you most often exhibit:"),
      (try_begin),
        (eq, "$defect_type", cb6_prudence),
        (str_store_string,s3,"@Wisdom. Also called Prudence, it governs actions according to the dictate of reason in order to choose the right response to any situation. Wisdom implies the ability to judge in advance the probable consequences of one's actions."),
      (else_try),
        (eq, "$defect_type", cb6_fortitude),
        (str_store_string,s3,"@Fortitude. Also called Courage, it ensures firmness during difficulties and constancy in the pursuit of goals. It is the strength to make a decision based on moral convictions, whatever the consequences."),
      (else_try),
        (eq, "$defect_type", cb6_temperance),
        (str_store_string,s3,"@Temperance. Also called Self-Control, it resists the attraction of pleasures in order to render the body governable. Temperance represents restraint and control over passions and desires."),
      (else_try),
        (eq, "$defect_type", cb6_justice),
        (str_store_string,s3,"@Justice. It symbolizes respect for law and promotes rewarding good and punishing evil."),
      (try_end),
      (str_store_string,s1,"@{s1}^^{s2} {s3}"),
      
      ## nationalty
      (try_begin),
        (eq, "$nacionalidad_type", cb7_foreigner),
        (str_store_string,s2,"@You are a foreigner."),
        (str_store_string,s3,"@You come from far away, seeking to get to know the north of Europe, a world dotted with dozens of small kingdoms all harassed by the Vikings."),
      (else_try),
        (eq, "$nacionalidad_type", cb7_scotopict),
        (str_store_string,s2,"@You are Scoto-Pict,"),
        (str_store_string,s3,"@a descendant of an ancient people of uncertain origin who inhabited parts of northern Britain, or of Goidelic invaders now mixed in."),
      (else_try),
        (eq, "$nacionalidad_type", cb7_briton),
        (str_store_string,s2,"@You are Briton,"),
        (str_store_string,s3,"@a member of the Celtic people who inhabited Britain since before the Roman invasion."),
      (else_try),
        (eq, "$nacionalidad_type", cb7_irish),
        (str_store_string,s2,"@You are Goidel,"),
        (str_store_string,s3,"@a member of Celtic people inhabiting the island of Eriu, which the Romans called Hibernia."),
      (else_try),
        (eq, "$nacionalidad_type", cb7_frisian),
        (str_store_string,s2,"@You are Frisian."),
        (str_store_string,s3,"@You come from the dike country of Friese on the north edge of continental Europe."),
      (else_try),
        (eq, "$nacionalidad_type", cb7_norseman),
        (str_store_string,s2,"@You are a Norseman,"),
        (str_store_string,s3,"@a member of the pagan peoples native to Scandia who raid the coast of Europe."),
      (else_try),
        (eq, "$nacionalidad_type", cb7_anglesaxon),
        (str_store_string,s2,"@You are Anglo-Saxon,"),
        (str_store_string,s3,"@a descendant of a Germanic people who, in ancient times, dwelt in Sleswick, near the mouth of the Elbe. Your people have been taking over Britain for four hundred years."),
      (try_end),
      (str_store_string,s1,"@{s1}^^{s2} {s3}"),
      
      ## father
      (try_begin),
        (eq, "$background_type", cb_noble),
        (str_store_string,s2,"@an impoverished nobleman"),
        (str_store_string,s3,"@You came into this world as {reg59?daughter:son} of a declining family of influence. " +
          "By then, the estates were gone and only the house in which you lived remained as a possession. However, despite your hardships, your parents gave you a good education, " +
        "preparing you from childhood for the chance to become a companion to royalty."),
      (else_try),
        (eq, "$background_type", cb_merchant),
        (str_store_string,s2,"@a travelling merchant"),
        (str_store_string,s3,"@You were always moving from one place to another seeking to make a profit. " +
          "Although your parents were wealthier than most and gave you the best education they could, " +
        "you found few chances to make friends on the road. When you did succeed to make a friend, you were always glad for the chance to sell them something."),
      (else_try),
        (eq, "$background_type", cb_guard),
        (str_store_string,s2,"@a craftsman"),
        (str_store_string,s3,"@Your father's ability to make beautiful and useful things were always highly valued by the community. " +
        "You grew accustomed to live with some comfort and luxuries -- and you can tell a hammer from a mallet."),
      (else_try),
        (eq, "$background_type", cb_forester),
        (str_store_string,s2,"@a priest"),
        (str_store_string,s3,"@You were placed in the care of a monastery when but an infant. The brothers were not true parents, but provided care and camaraderie. Your mother visited you sometimes, but you have never heard from your father. " +
        "Your life has been easier than most, as you always enjoyed a full dish at table after a long day of hard work."),
      (else_try),
        (eq, "$background_type", cb_nomad),
        (str_store_string,s2,"@a peasant"),
        (str_store_string,s3,"@You were born into a small community directed by a nearby lord. " +
          "Your childhood was happy enough, but there was little time to play. " +
        "Practically from the time you could walk, you were put to work harvesting cabbages with your frozen fingers."),
      (else_try),
        (eq, "$background_type", cb_thief),
        (str_store_string,s2,"@an outlaw"),
        (str_store_string,s3,"@Your education was rather informal. " +
          "Living in abandoned huts or even along the road, you were put to work begging until you became more useful for cutting purses. " +
        "Some may say your childhood was a waste, but nobody knows the shadowy back streets and secrets of the towns like you do."),
      (try_end),
      (str_store_string,s1,"@{s1}^^You were born years ago to {s2}. {s3}"),
      
      
      ##      Religion
      (try_begin),
        (eq, "$religion_type", cb3_christian),
        (str_store_string,s2,"@Your family baptised you into Roman Christianity,"),
        (str_store_string,s3,"@and you were fascinated by the stories that the priest told when he visited your village. " +
          "Since those days, you remain devoted to that religion. " +
        "Christianity is based on the Scriptures, the testimony of the Jesus' life and the teachings of His Apostles."),
      (else_try),
        (eq, "$religion_type", cb3_pagan),
        (str_store_string,s2,"@Your family believed in the Scandinavian gods."),
        (str_store_string,s3,"@You soaked up all of the stories, told far into the night with everyone gathered around the fireplace. " +
          "You are a staunch believer of the old ways. Your worship gives all the deities their due: Odin, the chief god; Thor, the god of the sky and thunder; " +
        "Freya, the goddess of love, and all the rest required to oversee all the disparate processes of the world."),
      (try_end),
      (str_store_string,s1,"@{s1}^^{s2} {s3}"),
      
      ## child life
      (try_begin),
        (eq, "$child_type", cb2_page),
        (str_store_string,s2,"@a page at a nobleman's hall"),
        (str_store_string,s3,"@You were chosen to be a companion to one of the wealthiest nobleman of the kingdom, so were given a place at the hall. " +
          "Serving the more senior members of the household, your first lessons were in humility. " +
          "In compensation, there were all sorts of entertainments: chess games, gossip, bards visiting to recite the poems of great deeds and loves. You were drawn most by the world of conflict " +
        "and competition. You played rough games with other children, batting each other with sticks as swords. Such games provided you invaluable lessons for your current life."),
      (else_try),
        (eq, "$child_type", cb2_apprentice),
        (str_store_string,s2,"@a craftsman's apprentice"),
        (str_store_string,s3,"@Your parents sent you to learn a trade. After years of hard work and study under your " +
          "new master, he promoted you first to journeyman and finally to fully paid craftsman for as long as you wished to stay. " +
        "You became adept with tools and crafting."),
      (else_try),
        (eq, "$child_type", cb2_merchants_helper),
        (str_store_string,s2,"@a shop assistant"),
        (str_store_string,s3,"@In the employ of a shopkeeper, " +
          "you learned to attract people's interest and convince them with words. " +
          "Traveling from one place to another in caravans to trade, " +
        "you learned the customs of very different peoples and proper behavior in each culture."),
      (else_try),
        (eq, "$child_type", cb2_urchin),
        (str_store_string,s2,"@a street urchin"),
        (str_store_string,s3,"@You ended up in the streets, doing whatever you must to survive. " +
          "Begging and thieving to earn your bread, daily involved in this violent world, " +
        "always one step ahead of the law and others who wished to catch you."),
      (else_try),
        (eq, "$child_type", cb2_steppe_child),
        (str_store_string,s2,"@a farmer child"),
        (str_store_string,s3,"@You put your strong back to help with farm work. You learned the land and the livestock that grazed on it. " +
          "Some years you enjoyed good harvests but others you survived on an empty stomach. " +
        "You became unflappable and adept at adapting to every circumstance."),
      (try_end),
      (str_store_string,s1,"@{s1}^^You spent your youth as {s2}. {s3}"),
      
      ## blife life
      (try_begin),
        (eq, "$blife_type", cb3_slave),
        (str_store_string,s2,"@Formerly, being a slave,"),
        (str_store_string,s3,"@your adult life was full of misery. Forced to do all the hard menial tasks for your owners, you grew to be thick-skinned, fast, and strong. " +
          "Sometimes your masters gave you no food, and so you had to learn to forage in the wild in order to satisfy your hunger. Your world was small and monotonous. The days seemed to be always the same. " +
          "Rain and sun punished your body during these endless working days. From sunrise to sunset, with your back aching all the time, your only reward was to see another day, " +
          "although some of your fellow sufferers were not so lucky. When you got some free time -- something that rarely happened -- you practiced hurling stones with a simple sling. " +
        "You can hit a dried gourd some paces distant and cause it to explode with a satisfying CRACK."),
      (else_try),
        (eq, "$blife_type", cb3_student), #priest
        (str_store_string,s2,"@Formerly, you were a priest."),
        (str_store_string,s3,"@When you were not working or studying at the monastery, you traveled around spreading the word of God. " +
          "Anyone who listened to you was greatly impressed with the passion of your sermons. Though you had kept the vow " +
          "of poverty through all those itinerant years, you never lacked for food, drink or shelter, as the " +
          "hospitality of the peasants was particularly generous toward a rising {reg59?woman:man} of God. Now and then, the villagers came to the monastery to purchase the unique products that you made. " +
          "^^Healing is another of your abilities. Your skill mended many a broken bone and kept many a soul in its body instead of venturing into God's hands. " +
        "Full of faith, you feel safe outside the thick monastery walls. You are armed with nothing more than a rod or a staff, symbols of the divine ones that comfort you."),
      (else_try),
        (eq, "$blife_type", cb3_traveler),
        (str_store_string,s2,"@Formerly, you were a traveler."),
        (str_store_string,s3,"@Responding to the call of the road, you traveled from one village to another, trading whatever you could find. " +
          "This is not a wealthy life, but you have become a master at getting a good price from even the most miserly of spinsters. " +
        "Soon, you will be able to create your own trading empire."),
      (else_try),
        (eq, "$blife_type", cb3_roamer),
        (str_store_string,s2,"@Formerly, you were a poacher."),
        (str_store_string,s3,"@Unsatisfied with common men's desperate desire of wealth, you took to your local lord's own forests " +
          "and helped yourself to their bounty, laws be damned. You hunted stags, boars and geese and sold " +
          "their meat under the table. You cut down trees right under the watchmen's noses and turned them into " +
          "firewood to warm many a freezing home during harsh winters. " +
        "You found that honor is easy enough to barter away for a few hard-to-find silvers. When the hard times came, you foraged for food and became an expert at finding edible plants in the wild."),
      (else_try),
        (eq, "$blife_type", cb3_landowner),
        (str_store_string,s2,"@Formerly, you were a free {reg59?woman:man}, and moreover {reg59?mistress:master} over a bit of land."),
        (str_store_string,s3,"@{reg59?Being wife to a free man meant that you at least had it easier than did the wives of the servants. " +
          "You owned some servants who helped you with the hard work, but since your husband didn't own a lot of land, you still had to do a variety of tasks that peasant wives have to do. " +
          "Your husband had to pay rent to his overlord and owed him labour services for the land. " +
          "When the lord was involved in a war, your husband had to answer his call for foot soldiers. In those times, he had to rely on his shield and spear in the thick of battle. " +
          "He often taught you how to fight properly with those arms in case he wouldn't be available to defend your home. " +
          ":This meant that you at least had it easier than the servants. " +
          "You owned some servants who helped you with the hard work, but since you didn't own a lot of land, you still had to do a variety of tasks that peasants have to do. " +
          "You had to pay rent to your overlord and owed him labour services for the land. " +
        "When the lord was involved in a war, you had to answer his call for foot soldiers. In those times, you had to rely on your shield and spear in the thick of battle.}"),
      (try_end),
      (str_store_string,s1,"@{s1}^^{s2} {s3}"),
      
      (try_begin),
        (eq, "$campaign_type", camp_lordc),
        (try_begin),
          (eq, reg59, 0),
          (str_store_string,s1,"@{s1}^^Through persistence and a little luck, you learned how to curry favor with one of the local lords. " +
            "Eventually, he recommended you to the king, who awarded you with a small fief from his own holdings. " +
          "Now you must prove yourself worthy of the trust you have been given."),
        (else_try),
          (str_store_string,s1,"@{s1}^^Then you happened to catch the interest of a young lord. " +
            "Ignoring tradition and the strident pleas of his advisors, he married you, but shortly thereafter suffered an accidental fatal wound during a tournament. " +
            "The lord lacked a living uncle, brother, or son. You therefore take over his small, neglected estate, even as the other lords of the kingdom consider it as having become available. " +
          "Now you must establish yourself on your lands."),
        (try_end),
      (else_try),
        (eq, "$campaign_type", camp_kingc),
        (try_begin),
          (eq, reg59, 0),
          (str_store_string,s1,"@{s1}^^Through persistence and a little luck, you learned how to curry favor with one of the local lords. " +
            "A series of events having left the kingdom without a ruler, he proposed you to share the kingship as a compromise between contending factions. " +
            "You were deliberately chosen to be the minor partner, but a sudden fatal illness of the other share-king has left you sole ruler. " +
            "Not everyone is happy about this turn of events, and neighboring kingdoms have taken note of the apparent weakness of your realm. " +
          "Now you must establish your dominion."),
        (else_try),
          (str_store_string,s1,"@{s1}^^Then you happened to catch the interest of the young king. " +
            "Ignoring tradition and the strident pleas of his advisors, he married you, but shortly thereafter suffered an accidental fatal wound during a tournament. " +
            "The king lacked a living uncle, brother, or son. As the lords debate succession, the neighboring kingdoms have taken note of the apparent weakness of your realm. " +
          "Now you must establish your dominion."),
        (try_end),
      (try_end),]),
  
  #campaign selection type and difficulty chief textos
  ("texto_initial_inform",
    [(str_clear,s1),
      
      (try_begin),
        (eq, "$campaign_type", camp_storyline),
        (str_store_string,s2,"@> Viking Conquest Main Campaign",0xFF910012),
        (str_store_string,s3,"@A storyline campaign with a main quest and other special features."),
      (else_try),
        (eq, "$campaign_type", camp_sandbox),
        (str_store_string,s2,"@> Sandbox Campaign",0xFF910012),
        (str_store_string,s3,"@No main story or special features. You are free to play as you wish."),
      (else_try),
        (eq, "$campaign_type", camp_lordc),
        (str_store_string,s2,"@> Lordly Sandbox Campaign",0xFF910012),
        (str_store_string,s3,"@You start as a lord or lady of a faction."),
      (else_try),
        (eq, "$campaign_type", camp_kingc),
        (str_store_string,s2,"@> Royal Sandbox Campaign",0xFF910012),
        (str_store_string,s3,"@You start as leader of a faction. Long live the king!"),
      (try_end),
      (str_store_string,s1,"@{s2}^^{s3}"),
      
      ##      ##
      ##      (try_begin),
      ##        (eq, "$difficulty_type", camp_d1),
      ##        (str_store_string,s2,"@> Beginner",0xFFf1e73f),
      ##        (str_store_string,s3,"@An easy campaign without advanced or realistic features. Great for learning the basics of the game."),
      ##      (else_try),
      ##        (eq, "$difficulty_type", camp_d2),
      ##        (str_store_string,s2,"@> Normal",0xFFf1e73f),
      ##        (str_store_string,s3,"@Some advanced features:^^Realistic Casualties ON^Weapon Break ON"),
      ##      (else_try),
      ##        (eq, "$difficulty_type", camp_d3),
      ##        (str_store_string,s2,"@> Challenging",0xFFf1e73f),
      ##        (str_store_string,s3,"@More advanced features:^^Realistic Casualties ON^Weapon Break ON^Stamina System ON^Formations ON"),
      ##      (else_try),
      ##        (eq, "$difficulty_type", camp_d4),
      ##        (str_store_string,s2,"@> Full Realism",0xFFf1e73f),
      ##        (str_store_string,s3,"@All advanced Features:^^Realistic Casualties ON^Weapon Break ON^Stamina System ON^Formations ON^Fall when Walking Backwards ON^Critical Hits ON^More... ON"),
      ##      (else_try),
      ##        (eq, "$difficulty_type", camp_d5),
      ##        (str_store_string,s2,"@> Impossible",0xFFf1e73f),
      ##        (str_store_string,s3,"@For crazy players:^^All Features ON^Kings and Lords at Double Strength^Enemy Hits at Double Damage"),
      ##      (try_end),
      ##      (str_store_string,s1,"@{s1}^^^^{s2}. {s3}"),
      
  ]),
  ("texto_initial_difficult",
    [(str_clear,s2),
      (str_clear,s3),
      (str_clear,s4),
      ##
      (try_begin),
        (eq, "$temp", 1),
        (str_store_string,s2,"@STAMINA > Realism Feature.",0xFFf1e73f),
        (str_store_string,s3,"@This feature affects the player and all game characters.^" +
        "Stamina is calculated from Health and Athletic skills and affects physical endurance during battle. When a character's stamina is exhausted, he or she won't be able to run and his or her weapons' damage will be halved."),
      (else_try),
        (eq, "$temp", 2),
        (str_store_string,s2,"@ARMOR PENALTY > Realism Feature."),
        (str_store_string,s3,"@This feature affects the player and all game characters.^" +
        "Your skill points will deteriorate when you equip heavy armor. This skill brings better balance to the game, as light troops become more important."),
        ##      (else_try),
        ##        (eq, ":object", "$g_presentation_obj_19"),
        ##        (str_store_string,s2,"@> Realism Feature.  Highly
        ##        recommended.",0xFFf1e73f),
        ##        (str_store_string,s3,"@This feature affects the player and all game
        ##        characters.^\
        ##         Enjoy real battles with greatly improved AI and formations as
        ##         Shieldwall and Wedge."),
        ##      (else_try),
        ##        (eq, "$temp", 3),
        ##        (str_store_string,s2,"@AUTOSAVE > Game Feature.  Highly
        ##        Recommended.",0xFFf1e73f),
        ##        (str_store_string,s3,"@Your game is saved in the last savegame slot
        ##        every 48 hours of gameplay."),
      (else_try),
        (eq, "$temp", 3),
        (str_store_string,s2,"@EASY RECRUITMENT > Beginner's Feature.",0xFFf1e73f),
        (str_store_string,s3,"@When you ask permission in the villages to recruit, the answer is always positive and without cost. Not recommended for players who like role-play and immersion."),
      (else_try),
        (eq, "$temp", 4),
        (str_store_string,s2,"@RESTING MORALE > Realism Feature.",0xFFf1e73f),
        (str_store_string,s3,"@Your army needs regular rest in a settlement, your camp site or your refuge from time to time. Lack of rest will lower your troop morale, while resting will improve it."),
      (else_try),
        (eq, "$temp", 5),
        (str_store_string,s2,"@INSANE DIFFICULTY > Advanced Player's Feature.",0xFFf1e73f),
        (str_store_string,s3,"@Your troops need three times the normal experience to upgrade. Your enemies will cause double damage while you cause only half. Only recommended for hardcore players."),
      (else_try),
        (eq, "$temp", 6),
        (str_store_string,s2,"@GORE > +18 Feature.",0xFFf1e73f),
        (str_store_string,s3,"@Add decapitation to battles."),
      (else_try),
        (eq, "$temp", 7),
        (str_store_string,s2,"@DISABLE SCENIC MENU > Performance Feature.",0xFFf1e73f),
        (str_store_string,s3,"@Disable the 3D view in menus to improve the loading time for slower machines."),
      (else_try),
        (eq, "$temp", 8),
        (str_store_string,s2,"@SHIELD BASH > Realism Feature.",0xFFf1e73f),
        (str_store_string,s3,"@Player and AI troops will be able to bash enemies with their shields."),
      (try_end),
      (str_store_string,s4,"@{s2}^^{s3}"),]),
  
  #journal info chief
  ("texto_journal",
    [(str_clear,s1),
      (str_store_string,s2,"@...Anno Domini 868..."),
      (str_store_string,s3,"@My name is Beda the Old, and if you delve into this world, I recommend you listen carefully...^^" +
        "Britain. The Britons call her Ynys Prydein, the Goidels call her Alba while the Angles and Saxons gave the name of Englaland to the lands they conquered. These islands are large, rich and beautiful... and are coveted. " +
        "So the land is currently at war and respects only the law of the sword and spear. Realms are born and killed off, and kings die on the battlefield. Women are widowed, and horses carry arms instead of hay. The shouts of warriors fill the air, and the earth lies barren from the trampling of armies. As in past eras of strife, mortal enemies rise, equipped in armour. " +
        "Ravens are calling, wolves are howling, spears clash and shields answer.^^" +
        "In the north, the kingdoms of the Angles crumble before the advance of the Danes. The sons of the notorious Viking lord Ragnar, the new lords of these lands, now rule Bernaccia, Deira and Elmet, the land known as Northhymbre. " +
        "^^In the south, the Saxons of West Seaxe, led by the Brytenwalda Aethelred and his brother Aelfred, prepare for war. They know that on the other side of the sea, more Danes prepare to cross the ocean in search of fame and wealth. " +
        "^^Pushed into the remote fringes of their native land by centuries of landward invasion, the native Britons now are threatened by Vikings on their coasts. They fight desperately not to lose what little they have left, dreaming of a king to unify them all and lead them to final victory -- a new King Arthur. The reality is that their equitable inheritance laws keep the holdings of each lord small and unity harder to achieve. Nevertheless, one strongman, Rhodri the Great of Gwynedd, has begun to impose his will over the rest of Wales. " +
        "^^In the lands of the far north, Norwegian invaders thirty years ago wiped out the old competing Gaelic and Pict aristocracies in one fatal blow at a chance three-way battle. After this disaster, Gaelic strongman Cinead McAlpin started to forge a new kingdom out of the two peoples, but they have been driven to the highland center of their former territories. " +
        "^^Across the sea, in Ireland, Aed 'Fair Warrior' mac Niall of Aileach has emerged as high king in Temair after overcoming his enemies, but it is nothing more than a prestigious title. His claim is never accepted by the other half of the Ui Neill, and the fighting for land, women and cattle, in the sort of fratricidal wars endemic to that land, continues unabated. Meanwhile, both Danish and Norwegian Vikings have established dozens of coastal strongholds. They use the rivers to plunder the Goidelic hinterland, but they are all too prone to join the native conflict -- on all sides. " +
      "^^^The world is chaos, and chaos is war."),
      (str_store_string,s1,"@BEDA CHRONICLE^^{s2}^^ {s3}"),]),
  
  # script_store_wound_report_to_s0
  # journal acaba chief
  # Output: s3, s4, s5, s6
  ("store_wound_report",
    [# INJURIES (s3)
      (str_clear, s1),
      (try_for_range, ":curr_slot", slot_quest_int_penalty_left_days, slot_quest_end_penalty_left_days),
        (quest_get_slot, reg1, "qst_vc_wounds", ":curr_slot"),
        (gt, reg1, 0),
        (store_add, ":fluid_points_slot", ":curr_slot", 10),
        (quest_get_slot, reg2, "qst_vc_wounds", ":fluid_points_slot"),
        (str_store_string, s2, "str_it_will_be_permanent_in_reg1_days"),
        (try_begin),
          (eq, ":curr_slot", slot_quest_int_penalty_left_days),
          (str_store_string, s1, "@{s1}On head. (-{reg2} intelligence)^{s2}^^"),
        (else_try),
          (eq, ":curr_slot", slot_quest_cha_penalty_left_days),
          (str_store_string, s1, "@{s1}On face. (-{reg2} charisma)^{s2}^^"),
        (else_try),
          (eq, ":curr_slot", slot_quest_str_penalty_left_days),
          (str_store_string, s1, "@{s1}On body. (-{reg2} strength)^{s2}^^"),
        (else_try),
          (str_store_string, s1, "@{s1}On limbs. (-{reg2} agility)^{s2}^^"),
        (end_try),
      (end_try),
      (try_begin),
        (str_is_empty, s1),
        (str_store_string, s3, "@Accumulated Injuries:^^none"),
      (else_try),
        (str_store_string, s3, "@Accumulated Injuries:^^{s1}A physician in a larger town can treat these injuries."),
      (try_end),
      
      # HEALING WOUNDS (s4)
      (str_clear, s1),
      (try_for_range, ":curr_slot", slot_quest_int_penalty_left_days, slot_quest_end_penalty_left_days),
        (quest_get_slot, reg1, "qst_vc_wounds", ":curr_slot"),
        (val_mul, reg1, -1),	# to get healing wounds now
        (gt, reg1, 0),
        (store_add, ":fluid_points_slot", ":curr_slot", 10),
        (quest_get_slot, reg2, "qst_vc_wounds", ":fluid_points_slot"),
        (str_store_string, s2, "str_it_will_be_cured_in_reg1_days"),
        (try_begin),
          (eq, ":curr_slot", slot_quest_int_penalty_left_days),
          (str_store_string, s1, "@{s1}On head. (-{reg2} intelligence)^{s2}^^"),
        (else_try),
          (eq, ":curr_slot", slot_quest_cha_penalty_left_days),
          (str_store_string, s1, "@{s1}On face. (-{reg2} charisma)^{s2}^^"),
        (else_try),
          (eq, ":curr_slot", slot_quest_str_penalty_left_days),
          (str_store_string, s1, "@{s1}On body. (-{reg2} strength)^{s2}^^"),
        (else_try),
          (str_store_string, s1, "@{s1}On limbs. (-{reg2} agility)^{s2}^^"),
        (end_try),
      (end_try),
      (try_begin),
        (str_is_empty, s1),
        (str_store_string, s4, "@Healing wounds:^^none"),
      (else_try),
        (str_store_string, s4, "@Healing wounds:^^{s1}These wounds have been treated. They will heal on their own."),
      (try_end),
      
      # SCARS (s5)
      (str_clear, s1),
      (try_for_range, ":curr_slot", slot_quest_int_penalty_perma_points, slot_quest_end_penalty_perma_points),
        (quest_get_slot, reg1, "qst_vc_wounds", ":curr_slot"),
        (neq, reg1, 0),
        (try_begin),
          (eq, ":curr_slot", slot_quest_int_penalty_perma_points),
          (str_store_string, s1, "@{s1}On head. (-{reg1} intelligence)^^"),
        (else_try),
          (eq, ":curr_slot", slot_quest_cha_penalty_perma_points),
          (str_store_string, s1, "@{s1}On face. (-{reg1} charisma)^^"),
        (else_try),
          (eq, ":curr_slot", slot_quest_str_penalty_perma_points),
          (str_store_string, s1, "@{s1}On body. (-{reg1} strength)^^"),
        (else_try),
          (str_store_string, s1, "@{s1}On limbs. (-{reg1} agility)^^"),
        (end_try),
      (end_try),
      (try_begin),
        (str_is_empty, s1),
        (str_store_string, s5, "@Scars:^^none"),
      (else_try),
        (str_store_string, s5, "@Scars:^^{s1}These injuries have become permanent. They now can't be cured."),
      (try_end),
      
      # TOTAL WOUNDS EVER (s6)
      (assign, reg1, "$total_wounds_ever"),
      (str_store_string, s6, "@Lifetime wounds: {reg1} so far."),]),
  ####
  
  #####scripts player character sheet
  ("texto_partysize_morale",
    [(str_clear,s1),
      (str_clear,s2),
      (str_clear,s3),
      (str_clear,s4),
      (str_clear,s5),
      (str_clear,s6),
      (str_clear,s7),
      (str_clear,s8),
      (str_clear,s13),
      (str_clear,s14),
      (str_clear,s18),
      (str_clear,s19),
      (str_clear,s20),
      
      
      (call_script, "script_game_get_party_companion_limit"),
      
      (try_begin),
        (party_slot_eq, "p_main_party", slot_party_on_water, 1),
        (assign, reg0, "$temp"), #(VC-3389)
      (end_try),
      
      (store_div, reg36, reg0, 2),
      (party_get_num_companions, reg37, "p_main_party"),
      (try_begin),    #base
        (gt, reg31, 0),
        (party_slot_eq, "p_main_party", slot_party_1_ship_type, 0),	# with own ship
        (str_store_string, s5, "@{!} +"),
      (else_try),
        (str_store_string, s5, "str_space"),
      (try_end),
      (try_begin),    #leadership
        (gt, reg32, 0),
        (party_slot_eq, "p_main_party", slot_party_1_ship_type, 0),	# with own ship
        (str_store_string, s2, "@{!} +"),
      (else_try),
        (str_store_string, s2, "str_space"),
      (try_end),
      (try_begin),    #charisma
        (gt, reg33, 0),
        (party_slot_eq, "p_main_party", slot_party_1_ship_type, 0),	# with own ship
        (str_store_string, s3, "@{!} +"),
      (else_try),
        (str_store_string, s3, "str_space"),
      (try_end),
      (try_begin),    #renown
        (gt, reg34, 0),
        (party_slot_eq, "p_main_party", slot_party_1_ship_type, 0),	# with own ship
        (str_store_string, s4, "@{!} +"),
      (else_try),
        (str_store_string, s4, "str_space"),
      (try_end),
      (try_begin),    #fiefs
        (gt, reg35, 0),
        (party_slot_eq, "p_main_party", slot_party_1_ship_type, 0),	# with own ship
        (str_store_string, s6, "@{!} +"),
      (else_try),
        (str_store_string, s6, "str_space"),
      (try_end),
      (try_begin),    #ships
        (gt, reg2, 0),
        (str_store_string, s7, "@{!} +"),
      (else_try),
        (str_store_string, s7, "str_space"),
      (try_end),
      # (str_store_string, s18, "@Current party size limit:
      # {reg0}.^^Modifiers are...^Base size: {s5}{reg31}^Combined
      # leadership: {s2}{reg32}^Charisma: {s3}{reg33}^Renown:
      # {s4}{reg34}^Fiefs: {s6}{reg35}^Ships (only in sea):
      # {s7}{reg2}^^{playername} can completely manage {reg36}
      # troops.^{reg59?She:He} currently has {reg37}."),
      (str_store_string, s18, "@Current party size limit: {reg0}.^^Renown: {reg34}^Combined leadership: {s2}{reg32}^Charisma: {s3}{reg33}^Fiefs: {s6}{reg35}^Ships (only in sea): {s7}{reg2}^^{playername} can completely manage {reg36} troops.^{reg59?She:He} currently has {reg37}."),
      ##
      ###morale
      (call_script, "script_get_player_party_morale_values"),
      (assign, reg8, 50),
      (assign, ":sum_modifiers", reg8),
      
      (assign, reg1, "$g_player_party_morale_modifier_party_size"),
      (try_begin),
        (gt, reg1, 0),
        (str_store_string, s2, "@{!} -"),
      (else_try),
        (str_store_string, s2, "str_space"),
      (try_end),
      (val_sub, ":sum_modifiers", reg1),
      
      (try_begin),
        (gt, "$g_player_party_morale_modifier_religion", 0),
        (assign, reg7, "$g_player_party_morale_modifier_religion"),
        (str_store_string, s14, "@^Religious Differences:  -{reg7}"),
        (val_sub, ":sum_modifiers", reg7),
      (else_try),
        (str_store_string, s14, "str_space"),
      (try_end),
      
      (assign, reg2, "$g_player_party_morale_modifier_weariness"),
      (try_begin),
        (gt, reg2, 0),
        (str_store_string, s3, "@{!} -"),
      (else_try),
        (str_store_string, s3, "str_space"),
      (try_end),
      (val_sub, ":sum_modifiers", reg2),
      
      (try_begin),
        (gt, "$g_player_party_morale_modifier_no_food", 0),
        (assign, reg7, "$g_player_party_morale_modifier_no_food"),
        (str_store_string, s5, "@^No food:  -{reg7}"),
        (val_sub, ":sum_modifiers", reg7),
      (else_try),
        (str_store_string, s5, "str_space"),
      (try_end),
      
      (assign, reg3, "$g_player_party_morale_modifier_food"),
      (try_begin),
        (gt, reg3, 0),
        (str_store_string, s4, "@{!} +"),
      (else_try),
        (str_store_string, s4, "str_space"),
      (try_end),
      (val_add, ":sum_modifiers", reg3),
      
      (try_begin),
        (gt, "$g_player_party_morale_modifier_debt", 0),
        (assign, reg7, "$g_player_party_morale_modifier_debt"),
        (str_store_string, s6, "@^Wage debt:  -{reg7}"),
        (val_sub, ":sum_modifiers", reg7),
      (else_try),
        (str_store_string, s6, "str_space"),
      (try_end),
      
      (party_get_morale, reg5, "p_main_party"),
      (store_sub, reg4, reg5, ":sum_modifiers"),
      (try_begin),
        (gt, reg4, 0),
        (str_store_string, s7, "@Currently falling from: +"),
      (else_try),
        (str_store_string, s7, "@Currently rising from: "),
      (try_end),
      
      (try_for_range, ":kingdom_no", npc_kingdoms_begin, npc_kingdoms_end),
        (faction_set_slot, ":kingdom_no", slot_faction_troop_in_player_party, 0),
      (try_end),
      
      #religion opuesta tropas chief
      (party_get_num_companion_stacks, ":num_stacks","p_main_party"),
      (str_clear,s11),
      (str_clear,s10),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop","p_main_party",":i_stack"),
        
        (store_troop_faction, ":kingdom_no", ":stack_troop"),
        (faction_set_slot, ":kingdom_no", slot_faction_troop_in_player_party, 1),
        
        (try_begin),
          (call_script, "script_cf_troop_religion_matches_players", ":stack_troop"),
          
        (else_try),
          (str_store_troop_name,s12,":stack_troop"),
          
          (try_begin),
            (eq, "$g_player_faith", 1), #player es cristiano
            (str_store_string, s11, "@{s11}{s10}{s12}"),
            (str_store_string, s10, "@{!}, "),
            (str_store_string, s13, "@^^These pagan troops cause trouble: {s11}"),
          (else_try),
            (eq, "$g_player_faith", 2), #player es pagano
            (str_store_string, s11, "@{s11}{s10}{s12}"),
            (str_store_string, s10, "@{!}, "),
            (str_store_string, s13, "@^^These Christian troops cause trouble: {s11}"),
          (try_end),
        (try_end),
      (try_end),
      #religion chief acaba
      
      ##tropas conflictivas
      (try_for_range, ":kingdom_no", npc_kingdoms_begin, npc_kingdoms_end),
        (faction_slot_eq, ":kingdom_no", slot_faction_troop_in_player_party, 1),
        (faction_get_slot, ":faction_morale", ":kingdom_no",  slot_faction_morale_of_player_troops),
        (val_div, ":faction_morale", 100),
        (neq, ":faction_morale", 0),
        (assign, reg6, ":faction_morale"),
        (str_store_faction_name, s9, ":kingdom_no"),
        (str_store_string, s19, "str_s1extra_morale_for_s9_troops__reg6_"),
      (try_end),
      
      (try_begin),
        (gt, reg1, 0),
        (str_store_string, s8, "@^^Player currently has more troops than can be completely managed."),
      (try_end),
      
      # (str_store_string, s20, "@Current party morale: {reg5}.^^Modifiers
      # are...^Base morale: +{reg8}^Party size penalty: {s2}{reg1}^Lack of rest:
      # {s3}{reg2}^Food variety: {s4}{reg3}{s5}{s6}^{s7}{reg4}\
      (str_store_string, s20, "@Current party morale: {reg5}.^^Base morale:  +{reg8}^Party size penalty: {s2}{reg1}{s14}^Lack of rest: {s3}{reg2}^Food variety: {s4}{reg3}{s5}{s6}^{s7}{reg4} " +
      "{s8}{s13}^^{s19}"),
      
      ####
      
      (str_store_string,s1,"@{s18}^^----------------^^{s20}"),]),
  ####
  ("texto_kigdom_info",
    [(str_clear,s1),
      (str_clear,s2),
      (str_clear,s6),
      (str_clear,s8),
      (str_clear,s12),
      (str_clear,s15),
      (str_clear,s16),
      (str_clear,s17),
      (str_clear,s18),
      (str_clear,s19),
      (str_clear,s22),
      (str_clear,s25),
      
      
      (assign, ":num_friends", 0),
      (assign, ":num_enemies", 0),
      (str_store_string, s16, "str_none"),
      (str_store_string, s18, "str_none"),
      (try_for_range, ":troop_no", active_npcs_begin, active_npcs_end),
        (this_or_next | troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
        (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_inactive_pretender),
        (call_script, "script_troop_get_player_relation", ":troop_no"),
        (assign, ":player_relation", reg0),
        #(troop_get_slot, ":player_relation", ":troop_no",
        #slot_troop_player_relation),
        (try_begin),
          (gt, ":player_relation", 20),
          (try_begin),
            (eq, ":num_friends", 0),
            (str_store_troop_name, s18, ":troop_no"),
          (else_try),
            (eq, ":num_friends", 1),
            (str_store_troop_name, s17, ":troop_no"),
            (str_store_string, s8, "@{s17} ^ {s18}"),
          (else_try),
            (str_store_troop_name, s17, ":troop_no"),
            (str_store_string, s8, "@{!}{s17}, {s18}"),
          (try_end),
          (val_add, ":num_friends", 1),
        (else_try),
          (lt, ":player_relation", -20),
          (try_begin),
            (eq, ":num_enemies", 0),
            (str_store_troop_name, s16, ":troop_no"),
          (else_try),
            (eq, ":num_enemies", 1),
            (str_store_troop_name, s15, ":troop_no"),
            (str_store_string, s6, "@{s15} ^ {s16}"),
          (else_try),
            (str_store_troop_name, s15, ":troop_no"),
            (str_store_string, s6, "@{!}{s15}, {s16}"),
          (try_end),
          (val_add, ":num_enemies", 1),
        (try_end),
      (try_end),
      
      #lord recruitment changes begin
      (str_clear, s22),
      (try_begin),
        (gt, "$player_right_to_rule", 0),
        (assign, reg12, "$player_right_to_rule"),
        (str_store_string, s22, "str__right_to_rule_reg12"),
      (try_end),
      
      (str_clear, s25),
      # (try_begin),
        # (this_or_next | gt, "$claim_arguments_made", 0),
        # (this_or_next | gt, "$ruler_arguments_made", 0),
        # (this_or_next | gt, "$victory_arguments_made", 0),
        # (this_or_next | gt, "$lords_arguments_made", 0),
        # (eq, 1, 0),
        
        # (assign, reg3, "$claim_arguments_made"),
        # (assign, reg4, "$ruler_arguments_made"),
        # (assign, reg5, "$victory_arguments_made"),
        # (assign, reg6, "$lords_arguments_made"),
        # (assign, reg7, "$benefit_arguments_made"),
        
        # (str_store_string, s25, "str_political_arguments_made_legality_reg3_rights_of_lords_reg4_unificationpeace_reg5_rights_of_commons_reg6_fief_pledges_reg7"),
      # (try_end),
      
      #lord recruitment changes begin
      
      (str_store_string, s19, "@{s22}^^Friends: ^{s8} ^^Enemies: ^{s6}^{s19}"),
      
      (call_script, "script_get_number_of_hero_centers", "trp_player"),
      (assign, ":no_centers", reg0),
      (try_begin),
        (gt, ":no_centers", 0),
        (try_for_range, ":i_center", 0, ":no_centers"),
          (call_script, "script_troop_get_leaded_center_with_index", "trp_player", ":i_center"),
          (assign, ":cur_center", reg0),
          (try_begin),
            (eq, ":i_center", 0),
            (str_store_party_name, s18, ":cur_center"),
          (else_try),
            (eq, ":i_center", 1),
            (str_store_party_name, s17, ":cur_center"),
            (str_store_string, s18, "@{s17} ^ {s18}"),
          (else_try),
            (str_store_party_name, s17, ":cur_center"),
            (str_store_string, s18, "@{!}{s17}, {s18}"),
          (try_end),
        (try_end),
        (str_store_string, s19, "@^^Your estates are: ^{s18}^{s19}"),
      (try_end),
      (try_begin),
        (gt, "$players_kingdom", 0),
        
        (str_store_faction_name, s18, "$players_kingdom"),
        (try_begin),
          (this_or_next | is_between, "$players_kingdom", npc_kingdoms_begin, npc_kingdoms_end),
          (neg | faction_slot_eq, "fac_player_supporters_faction", slot_faction_leader, "trp_player"),
          (store_current_day, ":cur_day"),
          (try_begin),
            (neq, ":cur_day", 0),
            (le, ":cur_day", "$mercenary_service_next_renew_day"),
            (str_store_string, s19, "@Mercenary employed by the {s18}.^{s19}"),
          (else_try),
            (str_store_string, s19, "str_you_are_a_lord_lady_of_s18_s19"),
          (try_end),
        (else_try),
          (str_store_string, s19, "str_you_are_king_queen_of_s18_s19"),
        (try_end),
        
      (try_end),
      ###
      (str_store_string,s1,"@Report: ^^ {s19}"),]),
  
  #
  ##companions mission and info
  ("texto_companions_report",
    [(str_clear, s1),
      (str_clear, s6),
      (str_clear, s7),
      (str_clear, s10),
      (str_clear, s11),
      (str_clear, s12),
      (str_clear, s13),
      (str_clear, s14),
      (str_clear, s15),
      (str_clear, s16),
      (str_clear, s17),
      (str_clear, s18),
      (str_clear, s19),
      (str_clear, s21),
      (str_store_string, s7, "str_no_companions_in_service"),
      
      (try_begin),
        (troop_get_slot, ":spouse_or_betrothed", "trp_player", slot_troop_spouse),
        (try_begin),
          (troop_get_type, ":is_female", "trp_player"),
          (val_mod, ":is_female", 2),
          (eq, ":is_female", 1),
          (str_store_string, s8, "str_husband"),
        (else_try),
          (str_store_string, s8, "str_wife"),
        (try_end),
        
        (try_begin),
          (le, ":spouse_or_betrothed", 0),
          (troop_get_slot, ":spouse_or_betrothed", "trp_player", slot_troop_betrothed),
          (str_store_string, s8, "str_betrothed"),
        (try_end),
        (gt, ":spouse_or_betrothed", 0),
        
        (str_store_troop_name, s4, ":spouse_or_betrothed"),
        (troop_get_slot, ":cur_center", ":spouse_or_betrothed", slot_troop_cur_center),
        (try_begin),
          (is_between, ":cur_center", centers_begin, centers_end),
          (str_store_party_name, s5, ":cur_center"),
        (else_try),
          (troop_slot_eq, ":spouse_or_betrothed", slot_troop_occupation, slto_kingdom_hero),
          (str_store_string, s5, "str_leading_party"),
        (else_try),
          (str_store_string, s5, "str_whereabouts_unknown"),
        (try_end),
        (str_store_string, s3, "str_s4_s8_s5"),
        (str_store_string, s2, s1),
        (str_store_string, s1, "str_s2_s3"),
        
      (try_end),
      
      
      (try_begin),
        (ge, "$cheat_mode", 1),
        (ge, "$npc_to_rejoin_party", 0),
        (str_store_troop_name, s5, "$npc_to_rejoin_party"),
        (str_store_string, s1, "@{!}DEBUG -- {s1}^NPC in rejoin queue: {s5}^"),
      (try_end),
      
      
      (try_for_range, ":companion", companions_begin, companions_end),
        (str_clear, s2),
        (str_clear, s3),
        
        (try_begin),
          (troop_get_slot, ":days_left", ":companion", slot_troop_days_on_mission),
          
          (troop_slot_eq, ":companion", slot_troop_occupation, slto_player_companion),
          
          
          (str_store_troop_name, s4, ":companion"),
          
          (try_begin),
            (troop_slot_eq, ":companion", slot_troop_current_mission, npc_mission_kingsupport),
            (str_store_string, s8, "str_gathering_support"),
            (try_begin),
              (eq, ":days_left", 1),
              (str_store_string, s5, "str_expected_back_imminently"),
            (else_try),
              (assign, reg3, ":days_left"),
              (str_store_string, s5, "str_expected_back_in_approximately_reg3_days"),
            (try_end),
          (else_try),
            (troop_slot_eq, ":companion", slot_troop_current_mission, npc_mission_gather_intel),
            (troop_get_slot, ":town_with_contacts", ":companion", slot_troop_town_with_contacts),
            (str_store_party_name, s11, ":town_with_contacts"),
            
            (str_store_string, s8, "str_gathering_intelligence"),
            (try_begin),
              (eq, ":days_left", 1),
              (str_store_string, s5, "str_expected_back_imminently"),
            (else_try),
              (assign, reg3, ":days_left"),
              (str_store_string, s5, "str_expected_back_in_approximately_reg3_days"),
            (try_end),
          (else_try),
            (troop_slot_eq, ":companion", slot_troop_current_mission, npc_mission_improve_relations),
            (troop_get_slot, ":mission_object", ":companion", slot_troop_mission_object),
            (str_store_party_name, s44, ":mission_object"),
            (try_begin),
              (neg | troop_slot_ge, ":companion", slot_troop_prisoner_of_party, 0),
              (str_store_string, s8, "@Trying to improve your relations with {s44}"),
              (try_begin),
                (eq, ":days_left", 1),
                (str_store_string, s5, "str_expected_back_imminently"),
              (else_try),
                (assign, reg3, ":days_left"),
                (str_store_string, s5, "str_expected_back_in_approximately_reg3_days"),
              (try_end),
            (else_try),
              (str_store_string, s8, "@Is imprisoned in the dungeon of {s44}"),
            (try_end),
          (else_try),	#This covers most diplomatic missions
            
            (troop_slot_ge, ":companion", slot_troop_current_mission, npc_mission_peace_request),
            (neg | troop_slot_ge, ":companion", slot_troop_current_mission, 8),
            
            ##				(try_begin), #VC-2698 provisional fix begin
            (str_clear, s9),
            (troop_get_slot, ":faction", ":companion", slot_troop_mission_object),
            (ge, ":faction", 0),
            (str_store_faction_name, s9, ":faction"),
            ##				(end_try), #VC-2698 provisional fix end
            (str_store_string, s8, "str_diplomatic_embassy_to_s9"),
            (try_begin),
              (eq, ":days_left", 1),
              (str_store_string, s5, "str_expected_back_imminently"),
            (else_try),
              (assign, reg3, ":days_left"),
              (str_store_string, s5, "str_expected_back_in_approximately_reg3_days"),
            (try_end),
          (else_try),
            (eq, ":companion", "$g_player_minister"),
            (str_store_string, s8, "str_serving_as_minister"),
            (try_begin),
              (is_between, "$g_player_court", centers_begin, centers_end),
              (str_store_party_name, s9, "$g_player_court"),
              (str_store_string, s5, "str_in_your_court_at_s9"),
            (else_try),
              (str_store_string, s5, "str_whereabouts_unknown"),
            (try_end),
          (else_try),
            (main_party_has_troop, ":companion"),
            (str_store_string, s8, "str_under_arms"),
            (str_store_string, s5, "str_in_your_party"),
          (else_try),
            (troop_slot_eq, ":companion", slot_troop_current_mission, npc_mission_rejoin_when_possible),
            (str_store_string, s8, "str_attempting_to_rejoin_party"),
            (str_store_string, s5, "str_whereabouts_unknown"),
          (else_try),	#Companions who are in a center
            (troop_slot_ge, ":companion", slot_troop_cur_center, 1),
            
            (str_store_string, s8, "str_separated_from_party"),
            (str_store_string, s5, "str_whereabouts_unknown"),
          (else_try), #Excludes companions who have occupation = retirement
            (try_begin),
              (check_quest_active, "qst_lend_companion"),
              (quest_slot_eq, "qst_lend_companion", slot_quest_target_troop, ":companion"),
              (str_store_string, s8, "@On loan,"),
            (else_try),
              (check_quest_active, "qst_lend_surgeon"),
              (quest_slot_eq, "qst_lend_surgeon", slot_quest_target_troop, ":companion"),
              (str_store_string, s8, "@On loan,"),
            (else_try),
              (troop_set_slot, ":companion", slot_troop_current_mission, npc_mission_rejoin_when_possible),
              (str_store_string, s8, "str_attempting_to_rejoin_party"),
            (try_end),
            
            (str_store_string, s5, "str_whereabouts_unknown"),
            (try_begin),
              (ge, "$cheat_mode", 1),
              (troop_get_slot, reg2, ":companion", slot_troop_current_mission),
              (troop_get_slot, reg3, ":companion", slot_troop_days_on_mission),
              (troop_get_slot, reg4, ":companion", slot_troop_prisoner_of_party),
              (troop_get_slot, reg4, ":companion", slot_troop_playerparty_history),
              
              (display_message, "@{!}DEBUG: {s4} current mission: {reg2}, days on mission: {reg3}, prisoner: {reg4}, pphistory: {reg5}"),
            (try_end),
          (try_end),
          
          (str_store_string, s3, "str_s4_s8_s5"),
          
          (str_store_string, s2, s1),
          (str_store_string, s1, "str_s2_s3"),
          
          (str_clear, s7), #"no companions in service"
        (else_try),
          (neg | troop_slot_eq, ":companion", slot_troop_occupation, slto_kingdom_hero),
          (troop_slot_ge, ":companion", slot_troop_prisoner_of_party, centers_begin),
          
          (str_store_troop_name, s4, ":companion"),
          (str_store_string, s8, "str_missing_after_battle"),
          (str_store_string, s5, "str_whereabouts_unknown"),
          
          (str_store_string, s3, "str_s4_s8_s5"),
          (str_store_string, s2, s1),
          (str_store_string, s1, "str_s2_s3"),
          (str_clear, s7), #"no companions in service"
          
        (try_end),
        
      (try_end),
      
      ### #companions info
      (try_for_range, ":npc", companions_begin, companions_end),
        (main_party_has_troop, ":npc"),
        (str_store_troop_name, s10, ":npc"),
        
        (troop_get_slot, ":morality_grievances", ":npc", slot_troop_morality_penalties),
        (troop_get_slot, ":personality_grievances", ":npc", slot_troop_personalityclash_penalties),
        (party_get_morale, ":party_morale", "p_main_party"),
        
        (store_sub, ":troop_morale", ":party_morale", ":morality_grievances"),
        (val_sub, ":troop_morale", ":personality_grievances"),
        (val_add, ":troop_morale", 50),
        
        (assign, reg8, ":troop_morale"),
        
        (val_mul, ":troop_morale", 3),
        (val_div, ":troop_morale", 4),
        (val_clamp, ":troop_morale", 0, 100),
        
        (assign, reg5, ":party_morale"),
        (assign, reg6, ":morality_grievances"),
        (assign, reg7, ":personality_grievances"),
        (assign, reg9, ":troop_morale"),
        
        #        (str_store_troop_name, s11, ":npc"),
        #        (display_message, "@{!}{s11}'s morale = PM{reg5} + 50 - MG{reg6} -
        #        PG{reg7} = {reg8} x 0.75 = {reg9}"),
        
        (try_begin),
          (lt, ":morality_grievances", 3),
          (str_store_string, 17, "str_happy"),
        (else_try),
          (lt, ":morality_grievances", 15),
          (str_store_string, 17, "str_content"),
        (else_try),
          (lt, ":morality_grievances", 30),
          (str_store_string, 17, "str_concerned"),
        (else_try),
          (lt, ":morality_grievances", 45),
          (str_store_string, 17, "str_not_happy"),
        (else_try),
          (str_store_string, 17, "str_miserable"),
        (try_end),
        
        
        (try_begin),
          (lt, ":personality_grievances", 3),
          (str_store_string, 16, "str_happy"),
        (else_try),
          (lt, ":personality_grievances", 15),
          (str_store_string, 16, "str_content"),
        (else_try),
          (lt, ":personality_grievances", 30),
          (str_store_string, 16, "str_concerned"),
        (else_try),
          (lt, ":personality_grievances", 45),
          (str_store_string, 16, "str_not_happy"),
        (else_try),
          (str_store_string, 16, "str_miserable"),
        (try_end),
        
        
        (try_begin),
          (gt, ":troop_morale", 80),
          (str_store_string, 18, "str_happy"),
          (str_store_string, 63, "str_bar_enthusiastic"),
        (else_try),
          (gt, ":troop_morale", 60),
          (str_store_string, 18, "str_content"),
          (str_store_string, 63, "str_bar_content"),
        (else_try),
          (gt, ":troop_morale", 40),
          (str_store_string, 18, "str_concerned"),
          (str_store_string, 63, "str_bar_weary"),
        (else_try),
          (gt, ":troop_morale", 20),
          (str_store_string, 18, "str_not_happy"),
          (str_store_string, 63, "str_bar_disgruntled"),
        (else_try),
          (str_store_string, 18, "str_miserable"),
          (str_store_string, 63, "str_bar_miserable"),
        (try_end),
        
        (assign, reg0, ":troop_morale"),
        (str_store_string, s21, "@is {s16} your choice of companions, {s17} your style of leadership, and {s18} the general state of affairs. Morale is {reg0}"),
        
        (str_store_string,s19,"@{!}{s19}^^{s10} {s21}"),
      (try_end),
      
      (str_store_string,s1,"@Companions Info:^^ {s19} ^^-----------^^ Currently:^^ {s7}{s1}"),]),
  
  ####
  ("texto_kigdomsr_info",
    [(str_clear, s2),
      (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"),
        (store_relation, ":cur_relation", "fac_player_faction", ":cur_kingdom"),
        (try_begin),
          (ge, ":cur_relation", 90),
          (str_store_string, s3, "@Loyal"),
        (else_try),
          (ge, ":cur_relation", 80),
          (str_store_string, s3, "@Devoted"),
        (else_try),
          (ge, ":cur_relation", 70),
          (str_store_string, s3, "@Fond"),
        (else_try),
          (ge, ":cur_relation", 60),
          (str_store_string, s3, "@Gracious"),
        (else_try),
          (ge, ":cur_relation", 50),
          (str_store_string, s3, "@Friendly"),
        (else_try),
          (ge, ":cur_relation", 40),
          (str_store_string, s3, "@Supportive"),
        (else_try),
          (ge, ":cur_relation", 30),
          (str_store_string, s3, "@Favorable"),
        (else_try),
          (ge, ":cur_relation", 20),
          (str_store_string, s3, "@Cooperative"),
        (else_try),
          (ge, ":cur_relation", 10),
          (str_store_string, s3, "@Accepting"),
        (else_try),
          (ge, ":cur_relation", 0),
          (str_store_string, s3, "@Indifferent"),
        (else_try),
          (ge, ":cur_relation", -10),
          (str_store_string, s3, "@Suspicious"),
        (else_try),
          (ge, ":cur_relation", -20),
          (str_store_string, s3, "@Unhappy"),
        (else_try),
          (ge, ":cur_relation", -30),
          (str_store_string, s3, "@Resentful"),
        (else_try),
          (ge, ":cur_relation", -40),
          (str_store_string, s3, "@Angry"),
        (else_try),
          (ge, ":cur_relation", -50),
          (str_store_string, s3, "@Hostile"),
        (else_try),
          (ge, ":cur_relation", -60),
          (str_store_string, s3, "@Hateful"),
        (else_try),
          (ge, ":cur_relation", -70),
          (str_store_string, s3, "@Vengeful"),
        (else_try),
          (str_store_string, s3, "@Sworn Enmity"),
        (try_end),
        (str_store_faction_name, s4, ":cur_kingdom"),
        (assign, reg1, ":cur_relation"),
        (str_store_string, s2, "@{!}{s2}^{s4}: {reg1} ({s3})"),
      (try_end),
      (str_store_string, s1, "@Your relations with the kingdoms are:^^{s2}"),]),
  
  #########
  ##########camp screen
  ("texto_refuge_construir",
    [(str_clear,s1),
      (str_store_string,s2,"@Build your personal refuge."),
      
      (try_begin),
        (eq, "$lair_on", 1),
        (str_store_string,s3,"@You already have a refuge built. Destroy your current refuge if you want to move it here."),
      (else_try),
        (store_troop_gold, ":gold_amount", "trp_player"),
        (lt, ":gold_amount", 1000),
        (str_store_string,s3,"@You don't have enough money! The workers who would build a refuge for you demand 1000 peningas."),
      (else_try),
        (party_get_num_companion_stacks, ":num_stacks","p_main_party"),
        (assign, ":num_men", 0),
        (try_for_range, ":i_stack", 0, ":num_stacks"),
          (party_stack_get_size, ":stack_size","p_main_party",":i_stack"),
          (val_add, ":num_men", ":stack_size"),
        (try_end),
        (lt, ":num_men", 30), # se muestra si el player tiene por lo menos 200 hombres
        (str_store_string,s3,"@You need to have more than thirty men to start this job."),
      (else_try),
        (str_store_string,s3,"@Your men await your orders to start building the refuge. There is enough material collected so far, and what may be missing can be purchased in nearby villages.^^" +
          "Total Cost: 1000 peningas.^" +
        "Time need: 6 hours."),
        (assign,"$temp",3), #active options - orders
      (try_end),
      
      (str_store_string,s1,"@{s2}^^ {s3}"),]),
  
  # ("texto_wait_here",
    # [(str_clear,s1),
      # (str_store_string,s2,"@Wait here for some time."),
      # (str_store_string,s3,"@Camping allows your men to rest for a while and recover from the hard life of travels. It also allows you to forage, increasing your food supplies. This is particularly helpful when campaigning in enemy territory, where it is difficult to buy supplies."),
      # (str_store_string,s1,"@{s2}^^ {s3}"),]),
  # ("texto_wait_here_sea",
    # [(str_clear,s1),
      # (str_store_string,s2,"@Wait here for some time."),
      # (str_store_string,s3,"@Traveling by sea exhausts men completely. Allowing your warriors some rest time will help make the trip more bearable."),
      # (str_store_string,s1,"@{s2}^^ {s3}"),]),
  
  ("texto_faction_background", [(store_script_param, ":center_faction", 1),
      (str_clear,s16),
      (try_begin),
        (eq, ":center_faction", "fac_kingdom_1"),
        (str_store_string,s16,"@You are in the kingdom of DANMARK.^^" +
          "Danmark is an important Norse state in the Viking Age. The Danes not only maintain their independence against the powerful Frankish empire but also attack and plunder towns, churches and monasteries throughout Western Europe. " +
          "Danish Vikings are excellent merchants who trade with most of their known world. A strong central authority has been established in Jutland, and the Danes now look beyond their own territory for land, trade and plunder. " +
        "While the men are out at sea, the women manage the farms."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_2"),
        (str_store_string,s16,"@You are in the kingdom of NORTHVEGR.^^" +
          "In Northvegr, mountainous terrain and fjords form strong natural boundaries. Communities remain independent of each other, and thirty small kingdoms exist in this land. " +
          "The most powerful leaders of the petty kingdoms have entered a major power struggle. The sea is the easiest way to communicate between the Norwegian kingdoms and the outside world, and the people have become traders and explorers as well as plunderers. " +
          "The lack of suitable farming land forces Norwegians to colonize the sparsely populated islands to the west. " +
        "Norwegians carried out the early Viking raids on Britain and Ireland, but later the Danes joined in."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_3"),
        (str_store_string,s16,"@You are in the kingdom of LAITHLIND.^^" +
          "Laithlind is the Norse-dominated part of Scotland and the Northern Islands, conquered by the Vikings three decades ago. " +
          "This kingdom has become an operating base for Norwegian expansion in the British Isles. " +
        "On the other hand, internal strife between the various strongmen is common."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_4"),
        (str_store_string,s16,"@You are in the kingdom of FRIESE.^^" +
          "Friese is region along the southeastern coast of the North Sea, the homeland of the Frisians, a people closely related to the Anglo-Saxons by language. " +
          "The Frisian realm is an important trading region that extends from the north of Frankia to the border of Denmark. " +
        "The Frisians lost their independence first to the Franks, but now their country is under the rule of the Danish."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_5"),
        (str_store_string,s16,"@You are in the kingdom of WEST SEAXE.^^" +
          "West Seaxe is the westernmost kingdom of Saxons in Britain. Some decades ago, King Egbert conquered Mierce and secured his overlordship over the king of Northhymbre. " +
          "He thereby became the Brytenwalda, or high king of Britain. This position of dominance was short-lived, but the expansion of West Seaxe across south England proved permanent. " +
          "Egbert's later years saw the beginning of the Viking raids on West Seaxe. The Danes were decisively crushed by Egbert's son and successor, King Aethelwulf, in the Battle of Aclea. " +
          "This victory postponed Danish conquests in England for fifteen years, but raids on West Seaxe continued. " +
        "Aethelwulf was succeeded by each of his four surviving sons, ruling one after another: Aethelbald, Aethelbert, Aethelred, the current king, and Aelfred, currently sub-king."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_6"),
        (str_store_string,s16,"@You are in the kingdom of EAST ENGLE.^^" +
          "The Kingdom of East Engle is a small independent kingdom in the eastern extremity of Britain, settled by the Angles earlier than many other British regions. " +
          "It was ruled by the Wuffingas dynasty but was conquered by Mierce. The Vikings ravaged East Engle, but in later years, the Saxons defeated them in two great battles. " +
        "There then followed a relatively peaceful period in which the Vikings attacked only once. However, the Danes eventually stopped raiding and showed up in force."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_7"),
        (str_store_string,s16,"@You are in the kingdom of MIERCE.^^" +
          "Mierce is one of the most powerful kingdoms of the Anglo-Saxons, centered on the river Trent and its tributaries. " +
          "It was originally composed the border areas, called Midlands, that lay between the districts of Anglo-Saxon settlement and the Celtic tribes they had driven to the west. " +
          "From the kingdom's capital of Tomtun, the Mercians dominated the Anglo-Saxon realms for three centuries, having annexed or gained submissions from five of the other six Anglo-Saxon kingdoms. That period is known as the Mercian Supremacy. " +
        "However, the conquest of Northhymbre by the Great Heathen Army has put Mercian dominance into question."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_8"),
        (str_store_string,s16,"@You are in the kingdom of NORTHHYMBRE.^^" +
          "Northhymbre is the northernmost Anglo-Saxon kingdom. It extended from the south of Alban to the north of the river Humber. " +
          "It was originally composed of the union of the kingdoms of Bernaccia and Deira, but gradually extended north and west, further encroaching on Celtic realms. " +
        "The brothers Halfdan Ragnarsson and Ivar the Boneless have conquered Northhymbre and installed an Anglo-Saxon nobleman, Ecgberht, as a puppet king."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_9"),
        (str_store_string,s16,"@You are in the kingdom of GWYNEDD.^^" +
          "The Kingdom of Gwynedd is one of the successor states that emerged during the fall of the Roman Empire and the arrival of the Saxons. Based in Northwest Wales, the rulers of Gwynedd were acclaimed as 'Kings of the Britons' before losing their power in civil wars and invasions. " +
          "The Britons of Gwynedd remain conscious of their Romano-British heritage, and their kings continue to link their rule to the old imperial Roman order with the continued adoption of Latin and Christianity. " +
        "Roman knowledge, however, is mainly lost, and the Britons of Gwynedd shift towards a militaristic tribal society. The kingdom has been expanding due to the efforts of Rhodri the Great."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_10"),
        (str_store_string,s16,"@You are in the kingdom of BRYCHEINIOG.^^" +
          "Brycheiniog is a small independent kingdom in South Wales. It often acts as a buffer between England to the east and the powerful Briton realms to the west. " +
          "The kingdom was probably founded by Irish raiders, very likely the Ui Liathain, whose power had grown great in Wales until they were reduced by the sons of Cunedda. " +
        "Currently the kingdom is locked in a feud with its neighbor, Glywyssing."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_11"),
        (str_store_string,s16,"@You are in the kingdom of ALT CLUT.^^" +
          "Alt Clut is a Briton kingdom located northwest of the Anglo-Saxon realms and south of Alban, along the banks of the river Clyde. " +
          "It emerged after the fall of Roman Empire with the town of Alt Clut as its capital. With its high, fortified rock (Alt), Alt Clut resisted one attack by the Dal Riata and three by the Picts. " +
        "An alliance of Picts and Northumbrians finally captured Alt Clut after a siege of several years. However, 18 years ago the men of Alt Clut were able to burn the Pictish city of Dunblane."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_12"),
        (str_store_string,s16,"@You are in the kingdom of CORNUBIA.^^" +
          "The Kingdom of Cornubia occupies the southeastern peninsula of Great Britain, bordered on the north and west by the Celtic Sea and on the south by the English Channel. " +
          "Cornubia was the home of the Celtic tribe known as the Cornovii, whose economy was based on tin since ancient times. Separated from the Britons of Wales after the Battle of Deorham, they have been all but eclipsed by the expanding Saxon kingdom of West Seaxe. " +
        "These people span the Channel, and they take refuge on the continent."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_13"),
        (str_store_string,s16,"@You are in the kingdom of GLYWYSSING.^^" +
          "The small Celtic kingdom of Glywyssing was founded out of a western chunk of the former Silures by Eugenius, the son of High King Magnus Maximus. " +
          "This was the most Romanised part of Wales, although they were descended from the Iron Age tribe of the Silures. Glywyssing is said to be named after Glywys, a legendary early monarch. " +
        "Lately, it has been locked in a bitter feud with Brycheiniog."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_14"),
        (str_store_string,s16,"@You are in the kingdom of ULADH.^^" +
          "Uladh is the kingdom located in the north of the island, the homeland of the Gaelic Ulaid, after whom the kingdom is named, and the Cruithne tribes, an early population linked with the Picts of northern Britannia. " +
        "The Ui Neill dynasty, descendants of Niall of the Nine Hostages, are absorbing the kingdom, dominating it from their base in Tir Eoghain."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_15"),
        (str_store_string,s16,"@You are in the kingdom of LAIGIN.^^" +
          "Situated in the southeast of Ireland, the tribes of Laigin were united by Ugaine Mor, its first historical king, who built the hill-fort of Dun Ailinne. When the Roman legions left Britain, the people from Laigin settled North Wales. " +
          "For a long time, the rule of Laigin was disputed by two dynasties, the Ui Dunlainge and the Ui Cheinnselaig, but finally the first one got the control of the crown. Nineteen years ago, during a general Norse attack on the British Isles, a Norse army was defeated at Sciath Nechtain by Lorcan mac Cellaig of Laigin and Olchobar mac Cinaeda of Mumain. " +
        "Nevertheless, the Vikings have created several colonies on the coast."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_16"),
        (str_store_string,s16,"@You are in the kingdom of MUMAIN.^^" +
          "Situated in the south of Ireland, the kingdom of Mumain is ruled by the Eoganachta dynasty. Nineteen years ago, after multiple defeats by invading Norsemen, Olchobar joined forces with his eastern neighbour, Lorcan mac Cellaig, King of Laigin, to stop them at Sciath Nechtain. " +
        "Among the dead was one Tomrair, jarl of the king of Laithlind."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_17"),
        (str_store_string,s16,"@You are in the kingdom of CONNACHTA.^^" +
          "Connachta is an Irish kingdom situated in the northwest of the island. Conn of the Hundred Battles, a legendary king of Ireland, was progenitor of the later ruling Connachta tribes and the namesake for the kingdom of Connacht. " +
          "Some centuries later, the ancient line of Connacht kings was displaced by midland rulers, whose centre was at Tara. " +
        "Two members of this Tara dynasty, Brion and Fiachra, founded clans, the Ui Briuin and the Ui Fiachrach, to which all the current rulers of Connaught belong. Conchobar mac Taidg Mor is from the Ui Briuin branch. Currently, the Norse seem intent on settling an island in the middle of the river Shannon."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_18"),
        (str_store_string,s16,"@You are in the territory of the Ui Neill of AILEACH.^^" +
          "Aileach is an Irish kingdom in far northwestern Ireland. The kings of Ailech belong to the northern Ui Neill, said to be descended from Niall of the Nine Hostages. The kingdom takes its name from the hill fort Grianan of Ailech. " +
        "This kingdom broke away from the Kingdom of Uladh, inhabited and under the sovereignty of the Cenel nEogain tribe. Now in a feud with Ui Neill to the south, the rulers engage in a 'tradition' of alternating the crown of the High King of Ireland between them."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_19"),
        (str_store_string,s16,"@You are in the kingdom of MIDE.^^" +
          "Mide means 'middle,' due this kingdom's position in the middle of Ireland. It was created by Tuathal Teachtmhar who, having subdued the rulers of the four kingdoms of Ireland, created his own realm from lands around the Hill of Tara that he annexed from them. " +
          "Niall of the Nine Hostages later established himself as king of Tara. For six centuries afterward, almost without interruption, his descendants were considered the high kings of Ireland. " +
          "His son Loegaire succeeded him and welcomed St. Patrick to his court. Niall's sons and grandsons have continued to annex parts of northern Laigin to carve out the sub-kingdoms of Mide and Brega for their descendants. " +
        "Currently, the Ui Neill of Meath have an arrangement to alternate the crown of High King with their cousins from Ailech, even as they continue to feud."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_20"),
        (str_store_string,s16,"@You are in the kingdom of ALBAN.^^" +
          "In the North of Britain, Picts and Scots, with their separate languages, had been traditional enemies. However, both dynasties were nearly destroyed in a chance three-way battle with Vikings. Then Kenneth MacAlpin of the Dal Riata unified the land by somehow finishing off the Pict royal house. " +
        "However, continued Viking pressure forced him from the coasts and lowlands. He placed the capital of his new kingdom at Forteviot, in old Pictish territory, and moved his religious center to Dunkeld. There he transferred the remains of St. Columba from Iona."),
      (else_try),
        (eq, ":center_faction", "fac_kingdom_21"),
        (str_store_string,s16,"@You are in territory of the clan of OSRAIGE.^^" +
          "Osraige is one of the largest clans in Ireland. It was the easternmost region within the Kingdom of Mumain until Osraige warlord, Cerball mac Dunlainge, adeptly obtained independence. " +
        "Cerball at different times fought and allied with Waterford, Dublin, and Aed Findliath, but generally remains at odds with the neighboring, larger kingdoms of Mumain and Laigin."),
      (try_end),]),
  
  ("texto_global_camp_info",
    [(str_clear,s1),
      (str_clear,s2),
      (str_clear,s3),
      (str_clear,s4),
      (str_clear,s5),
      (str_clear,s6),
      (str_clear,s7),
      (str_clear,s11),
      (party_get_current_terrain, ":terrain_type", "p_main_party"),
      (try_begin),
        (eq, ":terrain_type", rt_steppe),
        (str_store_string,s2,"@Plain."),
      (else_try),
        (eq, ":terrain_type", rt_plain),
        (str_store_string,s2,"@Plain."),
      (else_try),
        (eq, ":terrain_type", rt_snow),
        (str_store_string,s2,"@Snow."),
      (else_try),
        (eq, ":terrain_type", rt_desert),
        (str_store_string,s2,"@Plain."),
      (else_try),
        (eq, ":terrain_type", rt_steppe_forest),
        (str_store_string,s2,"@Forest."),
      (else_try),
        (eq, ":terrain_type", rt_forest),
        (str_store_string,s2,"@Forest."),
      (else_try),
        (eq, ":terrain_type", rt_snow_forest),
        (str_store_string,s2,"@Snow Forest."),
      (else_try),
        (eq, ":terrain_type", rt_water),
        (str_store_string,s2,"@Ocean."),
      (else_try),
        (eq, ":terrain_type", rt_bridge),
        (str_store_string,s2,"@Sea."),
      (else_try),
        (eq, ":terrain_type", rt_river),
        (str_store_string,s2,"@Sea."),
      (else_try),
        #(eq, ":terrain_type", rt_desert_forest),
        (str_store_string,s2,"@Plain."),
      (try_end),
      
      (try_begin),
        (eq, "$send_scouts", 1), #scouts ok
        (str_store_string,s5,"str_yes_wo_dot"),
      (else_try),
        (str_store_string,s5,"str_no_wo_dot"),
      (try_end),
      (try_begin),
        (eq, "$foragers_a", 1), #foragers ok
        (str_store_string,s3,"str_yes_wo_dot"),
      (else_try),
        (str_store_string,s3,"str_no_wo_dot"),
      (try_end),
      (try_begin),
        (eq, "$fortified_camp", 1), #foragers ok
        (str_store_string,s4,"str_yes_wo_dot"),
      (else_try),
        (str_store_string,s4,"str_no_wo_dot"),
      (try_end),
      
      (assign, ":party", "p_main_party"),
      (try_begin),
        (assign, ":total_valor", 0),
        (call_script, "script_count_troops_inparty", ":party", "trp_norse_standard_bearer"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_briton_standard_bearer"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_saxon_standard_bearer"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_scotch_standard_bearer"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_angle_standard_bearer"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_irish_standard_bearer"),
        (val_add, ":total_valor", reg0),
        (assign, reg1, ":total_valor"), #total priest
        (try_begin),
          (gt, ":total_valor", 29),
          (assign, ":modifier_value", 3),
        (else_try),
          (gt, ":total_valor", 19),
          (assign, ":modifier_value", 2),
        (else_try),
          (gt, ":total_valor", 9),
          (assign, ":modifier_value", 1),
        (else_try),
          (assign, ":modifier_value", 0),
        (try_end),
        (assign, reg2, ":modifier_value"), #total bonus
        
        (str_store_string, s6, "@Standard Bearer: {reg1}^Leadership Bonus: {reg2} (max 3)"), #idenfiticamos tropa y numero
      (try_end),
      
      (try_begin),
        (assign, ":total_valor", 0),
        (call_script, "script_count_troops_inparty", ":party", "trp_scotch_musician"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_todos_cuerno"),
        (val_add, ":total_valor", reg0),
        (assign, reg1, ":total_valor"), #total priest
        (try_begin),
          (gt, ":total_valor", 29),
          (assign, ":modifier_value", 3),
        (else_try),
          (gt, ":total_valor", 9),
          (assign, ":modifier_value", 2),
        (else_try),
          (gt, ":total_valor", 0),
          (assign, ":modifier_value", 1),
        (else_try),
          (assign, ":modifier_value", 0),
        (try_end),
        (assign, reg2, ":modifier_value"), #total bonus
        
        (str_store_string, s7, "@Musician: {reg1}^Tactics Bonus: {reg2} (max 3)"), #idenfiticamos tropa y numero
      (try_end),
      
      (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
        (store_faction_of_party, ":center_faction", ":center_no"),
        (store_relation, ":cur_relation", "fac_player_faction", ":center_faction"),
        (lt, ":cur_relation", 0),
        (store_distance_to_party_from_party, ":dist", "p_main_party", ":center_no"),
        (lt, ":dist", 15),
        #  (display_message, "@You're close to the settlement of
        #  an enemy, forcing to you to exercise caution.  This
        #  makes you progress more slowly!", 0xff3333),
        (str_store_string, s11, "@Note: You're close to the enemy settlement, forcing you to exercise caution."), #idenfiticamos tropa y numero
      (try_end),
      
      (try_for_range, ":center_no", centers_begin, centers_end),
        (store_faction_of_party, ":center_faction", ":center_no"),
        (store_distance_to_party_from_party, ":dist", "p_main_party", ":center_no"),
        (lt, ":dist", 10), #10 km
        
        (call_script, "script_texto_faction_background", ":center_faction"),
      (try_end),
      
      
      (str_store_string,s1,"@{s16}^^-------------------^^Camp Info:^^^{s6}^^{s7}^^Terrain around: {s2}^^Scouts: {s5}^^Foragers: {s3}^^Fortified camp {s4}^^{s11}"),]),
  
  ("texto_global_camp_info2",
    [(str_clear,s1),
      (str_clear,s2),
      (str_clear,s3),
      (str_clear,s4),
      (party_get_current_terrain, ":terrain_type", "p_main_party"),
      (try_begin),
        (eq, ":terrain_type", rt_water),
        (str_store_string,s2,"@Ocean."),
      (else_try),
        (eq, ":terrain_type", rt_bridge),
        (str_store_string,s2,"@Sea."),
      (else_try),
        (eq, ":terrain_type", rt_river),
        (str_store_string,s2,"@Sea."),
      (else_try),
        #(eq, ":terrain_type", rt_desert_forest),
        (str_store_string,s2,"@Plain."),
      (try_end),
      
	  (try_begin),
        (eq, "$cheat_mode", 1),
		(set_fixed_point_multiplier, 100),
		(party_get_position, pos1, "p_main_party"),
		(position_get_x, reg1, pos1),
		(position_get_y, reg2, pos1),
        (str_store_string,s2,"@{!}{s1} (x:{reg1}, y:{reg2})."),
		(set_fixed_point_multiplier, 1000),
      (try_end),
      
      (str_store_string,s1,"@Camp Info:^^^^Position: {s2}^^You're sailing on your boat.^^^^Ship Management:^^If you are next to a coast, you can search for the closest landing point with a right click on your party."),
      # phaiak begin
      (try_begin),
        (eq, "$ship_management_explained", 1),
        (str_store_string, s1, "@Camp Info:^^^^Position: {s2}^^You're sailing on your boat.^^^^Ship Management:^^In a sea battle, you can give a 'stand ground' or 'charge' command to your ships by giving it to class number nine, like you give commands to a troop class in a field battle. The first troops in your army list will be together with you on the flagship. The other troops will be in the following ships. You can change your flagship in a port and while camping on the sea. If you are next to a coast you can also split your fleet while camping on the sea or search for the closest landing point with a right click on your party."),
      (try_end),]),
  
  ##text followers info
  ("texto_followers_info",
    [(str_clear,s1),
      (str_clear,s2),
      (str_clear,s3),
      (str_clear,s4),
      (str_clear,s5),
      (str_clear,s6),
      (str_clear,s7),
      (str_clear,s8),
      
      (assign, ":party", "p_followers_camp"),
      
      ###priest
      (try_begin),
        (assign, ":total_valor", 0),
        (call_script, "script_count_troops_inparty", ":party", "trp_norse_priest"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_briton_priest"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_saxon_priest"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_scotch_priest"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_angle_priest"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_irish_priest"),
        (val_add, ":total_valor", reg0),
        (assign, reg1, ":total_valor"), #total priest
        (try_begin),
          (gt, ":total_valor", 29),
          (assign, ":modifier_value", 3),
        (else_try),
          (gt, ":total_valor", 9),
          (assign, ":modifier_value", 2),
        (else_try),
          (gt, ":total_valor", 0),
          (assign, ":modifier_value", 1),
        (else_try),
          (assign, ":modifier_value", 0),
        (try_end),
        (assign, reg2, ":modifier_value"), #total bonus
        
        (str_store_string, s2, "@Priests in followers' party: {reg1}^Surgery Bonus: {reg2} (max 3)"), #idenfiticamos tropa y numero
      (try_end),
      
      
      ###women
      (try_begin),
        (assign, ":total_valor", 0),
        (call_script, "script_count_troops_inparty", ":party", "trp_follower_woman"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_hunter_woman"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_fighter_woman"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_sword_sister"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_refugee"),
        (val_add, ":total_valor", reg0),
        (call_script, "script_count_troops_inparty", ":party", "trp_peasant_woman"),
        (val_add, ":total_valor", reg0),
        (assign, reg1, ":total_valor"), #total priest
        (try_begin),
          (gt, ":total_valor", 29),
          (assign, ":modifier_value", 3),
        (else_try),
          (gt, ":total_valor", 9),
          (assign, ":modifier_value", 2),
        (else_try),
          (gt, ":total_valor", 0),
          (assign, ":modifier_value", 1),
        (else_try),
          (assign, ":modifier_value", 0),
        (try_end),
        (assign, reg2, ":modifier_value"), #total bonus
        
        (str_store_string, s3, "@Women in followers' party: {reg1}^Wound Treatment Bonus: {reg2} (max 3)^^Note: Every 10 women (as refugees, camp women, soldiers' wives and peasants) will give +1 morale each 72 hours. Maximum +10 morale points."), #idenfiticamos tropa y numero
      (try_end),
      
      (str_store_string, s1, "@^^^^Followers:^^{s2}^^--------------^^{s3}"),
      
      ##         (eq, "$g_infinite_camping", 0),
      ##
      ##         (tutorial_box, s1, "str_weekly_report"),
      #   (try_end),
      #   (try_end),
  ]),
  
  # Kingdom Reports: Diplomacy (character presentation)
  # script texto factions diplomacy
  # INPUT: faction_no
  # OUTPUT: s1 (report text)
  ("texto_factions_diplomacy_player_andglobal",
    [(store_script_param, ":faction_no", 1),
      (str_clear, s1),(str_clear, s5),
      (str_store_faction_name, s5, ":faction_no"),
      
      (try_begin),
        (is_between, ":faction_no", kingdoms_begin, kingdoms_end),
        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        
        (assign, ":show_link", 0),
        (call_script, "script_game_faction_note_diplomatic_relations", ":faction_no", ":show_link"),
        
        (str_store_string, s1, s21),
        
      (else_try),
        (str_store_string, s1, "@{s5} has been defeated!"),
      (try_end),]),
  
  #############
  #lords
  ("texto_lords_relation",
    [(str_clear, s1),
      (str_clear, s3),
      (str_clear, s4),
      (str_clear, s9),
      (str_clear, s10),
      (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
        (troop_set_slot, ":active_npc", slot_troop_temp_slot, 0),
      (try_end),
      
      (try_for_range, ":unused", active_npcs_begin, active_npcs_end),
        (assign, ":score_to_beat", -100),
        (assign, ":best_relation_remaining_npc", -1),
        (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
          (troop_slot_eq, ":active_npc", slot_troop_temp_slot, 0),
          (troop_slot_eq, ":active_npc", slot_troop_occupation, slto_kingdom_hero),
          (troop_slot_ge, ":active_npc", slot_troop_met, 1),
          
          (call_script, "script_troop_get_player_relation", ":active_npc"),
          (assign, ":relation_with_player", reg0),
          (ge, ":relation_with_player", ":score_to_beat"),
          
          (assign, ":score_to_beat", ":relation_with_player"),
          (assign, ":best_relation_remaining_npc", ":active_npc"),
          #                       (store_troop_faction, ":lord_faction",
          #                       ":best_relation_remaining_npc"),
        (try_end),
        (gt, ":best_relation_remaining_npc", -1),
        
        # for current count lords and centers
        (assign, ":num_centers", 0),
        (try_for_parties, ":cur_party"),
          (party_slot_eq, ":cur_party", slot_town_lord, ":best_relation_remaining_npc"),
          (try_begin),
            (this_or_next | party_slot_eq, ":cur_party", slot_party_type, spt_town),
            (this_or_next | party_slot_eq, ":cur_party", slot_party_type, spt_castle),
            (party_slot_eq, ":cur_party", slot_party_type, spt_village),
            (val_add, ":num_centers", 1),
          (try_end),
        (try_end), # end of parties loop
        
        (str_clear, s5),
        (store_troop_faction, ":lord_faction", ":best_relation_remaining_npc"),
        (faction_get_slot, ":faction_leader", ":lord_faction", slot_faction_leader),
        
        (try_begin),
          (eq, ":lord_faction", "fac_adventurers"),  #VC-3949 avoid 0 return w trp_player
          (assign, ":faction_leader", -1),
        (try_end),
        
        (try_begin),
          (troop_slot_ge, ":best_relation_remaining_npc", slot_troop_prisoner_of_party, 0),
          (troop_get_slot, ":party", ":best_relation_remaining_npc", slot_troop_prisoner_of_party),
          (store_faction_of_party, ":prison_faction", ":party"),
          
          (try_begin),
            (is_between, ":prison_faction", kingdoms_begin, kingdoms_end),
            (faction_get_slot, ":prison_faction_leader", ":prison_faction", slot_faction_leader),
            
            (this_or_next|eq, ":faction_leader", "trp_player"),
            (eq, ":prison_faction_leader", "trp_player"),
            
            (str_store_party_name, s10, ":party"),
            (str_store_string, s5, "@This lord is a prisoner of {s10}.^"),
            
          (else_try),
            (str_store_string, s5, "str_whereabouts_unknown"),
            (str_store_string, s5, "@{!}{s5}^"),
          (try_end),
        (try_end),
        
        (try_begin),
          
          ## only good relation
          (assign, ":continue", 0),
          (try_begin),
            (ge, ":relation_with_player", 30),## only good relation or very bad.  this mean long time with troop
            (assign, ":continue", 1),
          (else_try),
            (le, ":relation_with_player", -30),## only good relation or very bad.  this mean long time with troop
            (assign, ":continue", 1),
          (else_try),
            (assign, ":continue", 0),
          (try_end),
          
          (str_clear, s47),
          (str_clear, s40),
          (str_clear, s45),
          (assign, ":logged_a_rivalry", 0),
          (try_for_range, ":kingdom_hero", active_npcs_begin, active_npcs_end),
            (eq, ":continue", 1),
            
            (call_script, "script_troop_get_relation_with_troop", ":best_relation_remaining_npc", ":kingdom_hero"),
            (lt, reg0, -10),
            
            (str_store_troop_name, s39, ":kingdom_hero"),
            (try_begin),
              (eq, ":logged_a_rivalry", 0),
              (str_store_string, s40, "str_s39_rival"),
              (assign, ":logged_a_rivalry", 1),
            (else_try),
              (str_store_string, s41, "str_s40"),
              (str_store_string, s40, "str_s41_s39_rival"),
            (try_end),
          (try_end),
          
          (str_clear, s46),
          # (try_begin),  too much text for field
            # (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_martial),
            # (str_store_string, s46, "str_reputation_only_martial_"),
          # (else_try),
            # (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_debauched),
            # (str_store_string, s46, "str_reputation_only_debauched_"),
          # (else_try),
            # (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_selfrighteous),
            # (str_store_string, s46, "str_reputation_only_pitiless_"),
          # (else_try),
            # (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_cunning),
            # (str_store_string, s46, "str_reputation_only_calculating_"),
          # (else_try),
            # (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_quarrelsome),
            # (str_store_string, s46, "str_reputation_only_quarrelsome_"),
          # (else_try),
            # (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_goodnatured),
            # (str_store_string, s46, "str_reputation_only_goodnatured_"),
          # (else_try),
            # (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_upstanding),
            # (str_store_string, s46, "str_reputation_only_upstanding_"),
          # (else_try),
            # (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_conventional),
            # (str_store_string, s46, "str_reputation_only_conventional_"),
          # (else_try),
            # (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_adventurous),
            # (str_store_string, s46, "str_reputation_only_adventurous_"),
          # (else_try),
            # (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_otherworldly),
            # (str_store_string, s46, "str_reputation_only_romantic_"),
          # (else_try),
            # (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_moralist),
            # (str_store_string, s46, "str_reputation_only_moralist_"),
          # (else_try),
            # (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_ambitious),
            # (str_store_string, s46, "str_reputation_only_ambitious_"),
          # (else_try),
            # (troop_get_slot, reg11, ":best_relation_remaining_npc", slot_lord_reputation_type),
            # (str_store_string, s46, "str_reputation_only_reg11_"),
          # (try_end),
          
          (str_clear, s49),
          
          (try_begin),
            (troop_slot_eq, ":best_relation_remaining_npc", slot_troop_religion, 1),
            (str_store_string, s49, "@Christian Lord"),
          (else_try),
            (str_store_string, s49, "@Pagan Lord"),
          (try_end),
          
          (assign, ":love_count", 0),
          (str_clear, s38),
          (str_clear, s37),
          (try_for_range, ":love_interest_slot", slot_troop_love_interest_1, slot_troop_love_interests_end),
            (troop_get_slot, ":love_interest", ":best_relation_remaining_npc", ":love_interest_slot"),
            (is_between, ":love_interest", "trp_kingdom_1_lady_1", kingdom_ladies_end),
            (str_store_troop_name, s39, ":love_interest"),
            (str_store_string, s37, "@{!}{s37}{s38}{s39}"),
            (str_store_string, s38, "@{!}, "),
            (val_add, ":love_count", 1),
            (try_begin),
              (troop_slot_eq, ":best_relation_remaining_npc", slot_troop_betrothed, ":love_interest"),
              (str_store_string, s37, "@{s37} (his betrothed)"),
            (try_end),
            #(str_store_string, s40, "str_s40_s39_s2_reg0"),
          (try_end),
          
          (try_begin),
            (gt, ":love_count", 1),
            (try_begin),
              (eq, ":logged_a_rivalry", 1),
              (str_store_string, s40, "@{s40}. His love interests are reported to be {s37}."),
            (else_try),
              (str_store_string, s40, "@His love interests are reported to be {s37}."),
            (try_end),
          (else_try),
            (eq, ":love_count", 1),
            (try_begin),
              (eq, ":logged_a_rivalry", 1),
              (str_store_string, s40, "@{s40}. His love interest is reported to be {s37}."),
            (else_try),
              (str_store_string, s40, "@His love interest is reported to be {s37}."),
            (try_end),
          (try_end),
          
          (try_begin),
            (this_or_next | gt, ":love_count", 0),
            (eq, ":logged_a_rivalry", 1),
            # (str_store_string, s45, "str_other_relations_s40_"),  string too big to display
          (try_end),
          
          (str_clear, s44),
          (try_begin),
            (neq, ":best_relation_remaining_npc", ":faction_leader"),
            (call_script, "script_troop_get_relation_with_troop", ":best_relation_remaining_npc", ":faction_leader"),
            (str_store_string, s44, "str_relation_with_liege_reg0_"),
          (try_end),
          
          (str_clear, s48),
          
          (try_begin),
            (eq, "$cheat_mode", 1),
            (store_current_hours, ":hours"),
            (gt, ":hours", 0),
            (call_script, "script_calculate_troop_political_factors_for_liege", ":best_relation_remaining_npc", ":faction_leader"),
            (str_store_string, s48, "str_sense_of_security_military_reg1_court_position_reg3_"),
          (try_end),
          (str_store_string, s47, "str_s46s45s44s48"),
          
          (add_troop_note_from_sreg, ":best_relation_remaining_npc", 3, "str_political_details_s47_", 1),
          
        (try_end),
        
        #####
        (str_clear, s3),
        (str_store_troop_name, s4, ":best_relation_remaining_npc"),
        (str_store_faction_name, s3, ":lord_faction"),
        (assign, reg5, ":num_centers"),
        
        (assign, reg4, ":score_to_beat"),
        (str_store_string, s1, "@{s1}^{s4}^Kingdom: {s3}^Relation: {reg4}^Centers: {reg5}^{s5}{s47}"),
        (troop_set_slot, ":best_relation_remaining_npc", slot_troop_temp_slot, 1),
      (try_end),]),
  ####
  ("texto_kingdomlords_info",
    [(str_clear, s1),
      (str_clear, s3),
      (str_clear, s4),
      (str_clear, s9),
      (str_clear, s10),
      (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
        (troop_set_slot, ":active_npc", slot_troop_temp_slot, 0),
      (try_end),
      
      (try_for_range, ":unused", active_npcs_begin, active_npcs_end),
        (assign, ":score_to_beat", -100),
        (assign, ":best_relation_remaining_npc", -1),
        (try_for_range, ":active_npc", active_npcs_begin, active_npcs_end),
          (store_troop_faction, ":lord_faction", ":active_npc"),
          (eq, ":lord_faction", "fac_player_supporters_faction"),
          
          (troop_slot_eq, ":active_npc", slot_troop_temp_slot, 0),
          (troop_slot_eq, ":active_npc", slot_troop_occupation, slto_kingdom_hero),
          (troop_slot_ge, ":active_npc", slot_troop_met, 1),
          
          (call_script, "script_troop_get_player_relation", ":active_npc"),
          (assign, ":relation_with_player", reg0),
          (ge, ":relation_with_player", ":score_to_beat"),
          
          (assign, ":score_to_beat", ":relation_with_player"),
          (assign, ":best_relation_remaining_npc", ":active_npc"),
          #                      (store_troop_faction, ":lord_faction",
          #                      ":best_relation_remaining_npc"),
        (try_end),
        (gt, ":best_relation_remaining_npc", -1),
        # for current count lords and centers
        (assign, ":num_centers", 0),
        (try_for_parties, ":cur_party"),
          (party_slot_eq, ":cur_party", slot_town_lord, ":best_relation_remaining_npc"),
          (try_begin),
            (this_or_next | party_slot_eq, ":cur_party", slot_party_type, spt_town),
            (this_or_next | party_slot_eq, ":cur_party", slot_party_type, spt_castle),
            (party_slot_eq, ":cur_party", slot_party_type, spt_village),
            (val_add, ":num_centers", 1),
          (try_end),
        (try_end), # end of parties loop
        
        (str_clear, s5),
        (try_begin),
          (troop_slot_ge, ":best_relation_remaining_npc", slot_troop_prisoner_of_party, 0),
          (troop_get_slot, ":party", ":best_relation_remaining_npc", slot_troop_prisoner_of_party),
          ##                  (is_between, ":party", walled_centers_begin,
          ##                  walled_centers_end),
          ##                  (store_faction_of_party, ":party_faction", ":party"),
          ##                  (eq, ":party_faction", "$g_faction_selected"),
          (str_store_party_name, s10, ":party"),
          (str_store_string, s5, "@This lord is a prisoner of {s10}"),
        (else_try),
          #Commanders troops size
          (try_for_parties, ":party_no"),
            (party_stack_get_troop_id, ":leader", ":party_no", 0),
            (eq, ":leader", ":best_relation_remaining_npc"),
            (store_party_size_wo_prisoners,":army_size",":party_no"),
            (assign, reg26, ":army_size"),
            (str_store_string, s5, "@Army size: {reg26}"),
          (try_end),
          #			(else_try),
          #                     (str_clear, s5),
        (try_end),
        
        ## no necesary good relation, they are in kingdom
        (store_troop_faction, ":lord_faction", ":best_relation_remaining_npc"),
        (neq, ":lord_faction", "fac_adventurers"),
        (faction_get_slot, ":faction_leader", ":lord_faction", slot_faction_leader),
        
        (str_clear, s47),
        (str_clear, s40),
        (assign, ":logged_a_rivalry", 0),
        (try_for_range, ":kingdom_hero", active_npcs_begin, active_npcs_end),
          (call_script, "script_troop_get_relation_with_troop", ":best_relation_remaining_npc", ":kingdom_hero"),
          (lt, reg0, -10),
          
          (str_store_troop_name, s39, ":kingdom_hero"),
          (try_begin),
            (eq, ":logged_a_rivalry", 0),
            (str_store_string, s40, "str_s39_rival"),
            (assign, ":logged_a_rivalry", 1),
          (else_try),
            (str_store_string, s41, "str_s40"),
            (str_store_string, s40, "str_s41_s39_rival"),
          (try_end),
        (try_end),
        
        (str_clear, s46),
        (try_begin),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_martial),
          (str_store_string, s46, "str_reputation_only_martial_"),
        (else_try),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_debauched),
          (str_store_string, s46, "str_reputation_only_debauched_"),
        (else_try),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_selfrighteous),
          (str_store_string, s46, "str_reputation_only_pitiless_"),
        (else_try),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_cunning),
          (str_store_string, s46, "str_reputation_only_calculating_"),
        (else_try),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_quarrelsome),
          (str_store_string, s46, "str_reputation_only_quarrelsome_"),
        (else_try),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_goodnatured),
          (str_store_string, s46, "str_reputation_only_goodnatured_"),
        (else_try),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_upstanding),
          (str_store_string, s46, "str_reputation_only_upstanding_"),
        (else_try),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_conventional),
          (str_store_string, s46, "str_reputation_only_conventional_"),
        (else_try),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_adventurous),
          (str_store_string, s46, "str_reputation_only_adventurous_"),
        (else_try),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_otherworldly),
          (str_store_string, s46, "str_reputation_only_romantic_"),
        (else_try),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_moralist),
          (str_store_string, s46, "str_reputation_only_moralist_"),
        (else_try),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_ambitious),
          (str_store_string, s46, "str_reputation_only_ambitious_"),
        (else_try),  #VC-3927 extend from script_lord_comment_to_s43
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_roguish),
          (str_store_string, s46, "str_reputation_only_goodnatured_"),
        (else_try),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_custodian),
          (str_store_string, s46, "str_reputation_only_calculating_"),
        (else_try),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_lord_reputation_type, lrep_benefactor),
          (str_store_string, s46, "str_reputation_only_goodnatured_"),
        # (else_try),   VC-3953 cheat string wasn't localized and is not needed
          # (troop_get_slot, reg11, ":best_relation_remaining_npc", slot_lord_reputation_type),
          # (str_store_string, s46, "str_reputation_only_reg11_"),
        (try_end),
        
        (str_clear, s49),
        (try_begin),
          (troop_slot_eq, ":best_relation_remaining_npc", slot_troop_religion, 1),
          (str_store_string, s49, "@Christian Lord"),
        (else_try),
          (str_store_string, s49, "@Pagan Lord"),
        (try_end),
        
        (troop_get_slot, ":love_interest", ":best_relation_remaining_npc", slot_troop_betrothed),  #VC-3927 replaced botched lovers list with the one thing player would know
        (try_begin),
          (gt, ":love_interest", 0),
          (str_store_troop_name, s39, ":love_interest"),
          (str_store_string, s2, "str_betrothed"),
          (try_begin),
            (eq, ":logged_a_rivalry", 0),
            (str_store_string, s40, "@{!}{s39} ({s2})"),
          (else_try),
            (str_store_string, s40, "@{!}{s40}, {s39} ({s2})"),
          (try_end),
        (try_end),
        
        (str_store_string, s45, "str_other_relations_s40_"),
        
        (str_clear, s44),
        # (try_begin), VC-3927 duplicates reg4 below
          # (neq, ":best_relation_remaining_npc", ":faction_leader"),
          # (call_script, "script_troop_get_relation_with_troop", ":best_relation_remaining_npc", ":faction_leader"),
          # (str_store_string, s44, "str_relation_with_liege_reg0_"),
        # (try_end),
        
        (str_clear, s48),
        
        (try_begin),
          (eq, "$cheat_mode", 1),
          (store_current_hours, ":hours"),
          (gt, ":hours", 0),
          (call_script, "script_calculate_troop_political_factors_for_liege", ":best_relation_remaining_npc", ":faction_leader"),
          (str_store_string, s48, "str_sense_of_security_military_reg1_court_position_reg3_"),
        (try_end),
        
        (str_store_string, s47, "str_s46s45s44s48"),
        (add_troop_note_from_sreg, ":best_relation_remaining_npc", 3, "str_political_details_s47_", 1),
        
        #####
        (str_clear, s3),
        (str_store_troop_name, s4, ":best_relation_remaining_npc"),
        (str_store_faction_name, s3, ":lord_faction"),
        (assign, reg5, ":num_centers"),
        
        (assign, reg4, ":score_to_beat"),
        # (str_store_string, s1, "@{s1}^^{s4}^Kingdom: {s3}^Relation: {reg4}^Centers: {reg5}^{s5}^{s47}"),  #VC-3927 this is a report on the kingdom, so no need to name it
        (str_store_string, s1, "@{s1}^^{s4}^Relation: {reg4}^Centers: {reg5}^{s5}^{s47}"),
        (troop_set_slot, ":best_relation_remaining_npc", slot_troop_temp_slot, 1),
      (try_end),]),
  ####
  
  #courtship
  ("texto_courtship_relation",
    [(str_clear, s1),
      (str_clear, s2),
      (str_clear, s3),
      
      (str_store_string, s1, "str_courtships_in_progress_"),
      (try_for_range, ":lady", "trp_kingdom_1_lady_1", kingdom_ladies_end),
        (troop_slot_ge, ":lady", slot_troop_met, 1), # met or better
        (troop_slot_eq, ":lady", slot_troop_spouse, -1), # unmarried
        #(troop_slot_eq, ":lady", slot_troop_met, 2),
        (store_troop_faction, ":lady_faction", ":lady"),
        
        (call_script, "script_troop_get_relation_with_troop", "trp_player", ":lady"),
        (gt, reg0, 0),
        (assign, reg3, reg0),
        
        (str_store_troop_name, s2, ":lady"),
        (str_store_faction_name, s3, ":lady_faction"),
        
        (store_current_hours, ":hours_since_last_visit"),
        (troop_get_slot, ":last_visit_hour", ":lady", slot_troop_last_talk_time),
        (val_sub, ":hours_since_last_visit", ":last_visit_hour"),
        (store_div, ":days_since_last_visit", ":hours_since_last_visit", 24),
        (assign, reg4, ":days_since_last_visit"),
        
        #(str_store_string, s1,
        #"str_s1_s2_relation_reg3_last_visit_reg4_days_ago"),
        (str_store_string, s1, "@{s1}^^{s2} of the {s3}^Relation {reg3}^Last visit {reg4} days ago"),
      (try_end),
      
      (str_store_string, s1, "str_s1__poems_known"),
      (try_begin),
        (gt, "$allegoric_poem_recitations", 0),
        (str_store_string, s1, "str_s1_storming_the_castle_of_love_allegoric"),
      (try_end),
      (try_begin),
        (gt, "$tragic_poem_recitations", 0),
        (str_store_string, s1, "str_s1_kais_and_layali_tragic"),
      (try_end),
      (try_begin),
        (gt, "$comic_poem_recitations", 0),
        (str_store_string, s1, "str_s1_a_conversation_in_the_garden_comic"),
      (try_end),
      (try_begin),
        (gt, "$heroic_poem_recitations", 0),
        (str_store_string, s1, "str_s1_helgered_and_kara_epic"),
      (try_end),
      (try_begin),
        (gt, "$mystic_poem_recitations", 0),
        (str_store_string, s1, "str_s1_a_hearts_desire_mystic"),
      (try_end),]),
  
  ####texto kingdom strengh
  ("texto_kingdom_armies",
    [(str_clear, s1),
      (str_clear, s2),
      (str_clear, s3),
      
      (assign, ":town_garrison_size", 0),
      (assign, ":castle_garrison_size", 0),
      (assign, ":king_field_troops", 0),
      (assign, ":lords_field_troops", 0),
      
      (try_for_parties, ":party_no"),
        (try_begin),
          (party_slot_eq, ":party_no", slot_party_type, spt_town),
          (store_faction_of_party, ":party_faction", ":party_no"),
          (eq, ":party_faction", "fac_player_supporters_faction"),
          
          (party_get_num_companion_stacks, ":num_stacks", ":party_no"),
          (try_for_range, ":i_stack", 0, ":num_stacks"),
            (party_stack_get_size, ":stack_size", ":party_no", ":i_stack"),
            (val_add, ":town_garrison_size", ":stack_size"),
          (try_end),
          
        (else_try),
          (party_slot_eq, ":party_no", slot_party_type, spt_castle),
          (store_faction_of_party, ":party_faction", ":party_no"),
          (eq, ":party_faction", "fac_player_supporters_faction"),
          
          (party_get_num_companion_stacks, ":num_stacks", ":party_no"),
          (try_for_range, ":i_stack", 0, ":num_stacks"),
            (party_stack_get_size, ":stack_size", ":party_no", ":i_stack"),
            (val_add, ":castle_garrison_size", ":stack_size"),
          (try_end),
          
        (else_try),
          (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_hero_party),
          (store_faction_of_party, ":party_faction", ":party_no"),
          (eq, ":party_faction", "fac_player_supporters_faction"),
          (party_get_num_companion_stacks, ":num_stacks", ":party_no"),
          (try_for_range, ":i_stack", 0, ":num_stacks"),
            (party_stack_get_size, ":stack_size", ":party_no", ":i_stack"),
            (val_add, ":lords_field_troops", ":stack_size"),
          (try_end),
        (else_try),
          (eq, ":party_no", "p_main_party"),
          (party_get_num_companion_stacks, ":num_stacks", ":party_no"),
          (try_for_range, ":i_stack", 0, ":num_stacks"),
            (party_stack_get_size, ":stack_size", ":party_no", ":i_stack"),
            (val_add, ":king_field_troops", ":stack_size"),
          (try_end),
        (try_end),
        
      (try_end),
      (try_begin),
        (gt, ":king_field_troops", 0),
        (assign, reg2, ":king_field_troops"),
        (str_store_string, s2, "@{reg2} warriors accompany the king."),
      (try_end),
      (try_begin),
        (gt, ":lords_field_troops", 0),
        (assign, reg2, ":lords_field_troops"),
        (str_store_string, s2, "@{s2}^^{reg2} warriors serve various lords of the kingdom."),
      (try_end),
      (assign, reg2, ":town_garrison_size"),
      (str_store_string, s2, "@{s2}^^{reg2} warriors garrison our towns."),
      (assign, reg2, ":castle_garrison_size"),
      (str_store_string, s2, "@{s2}^^{reg2} warriors garrison our forts."),
      (str_store_string, s1, "@Kingdom Armies Report:^Land levy not included^^^{s2}"),]),
  
  ####texto ships
  ("texto_ships",
    [(str_clear, s10),
      
      (try_begin),
        (party_slot_ge, "p_main_party", slot_party_1_ship_type, 1),
        (call_script, "script_print_party_ship_info_to_s0", "p_main_party"),
        (str_store_string, s10, "@{s10}Current fleet:{s0}^^"),
      (end_try),
      
      (try_begin),
        #(party_is_active, "p_yourlair"),
        (party_slot_ge, "p_yourlair", slot_party_1_ship_type, 1),
        (call_script, "script_print_party_ship_info_to_s0", "p_yourlair"),
        (str_store_string, s10, "@{s10}Refuge:{s0}^^"),
      (end_try),
      
      (try_for_range, ":town", towns_begin, towns_end),
        (party_slot_eq, ":town", slot_town_port, 1),
        (party_slot_ge, ":town", slot_party_1_ship_type, 1),
        (str_store_party_name, s9, ":town"),
        # (str_store_party_name_link, s9, ":town"),
        (call_script, "script_print_party_ship_info_to_s0", ":town"),
        (str_store_string, s10, "@{s10}Port of {s9}:{s0}^^"),
      (end_try),
      
      (try_for_parties, ":cur_party"),
        (party_get_template_id, ":template_id", ":cur_party"),
        (eq, ":template_id", "pt_landet_ships"),
        (party_is_active, ":cur_party"),
        (call_script, "script_get_closest_center", ":cur_party"),
        (str_store_party_name, s9, reg0),
        # (str_store_party_name_link, s9, reg0),
        (call_script, "script_print_party_ship_info_to_s0", ":cur_party"),
        (str_store_string, s10, "@{s10}Landed ships next to {s9}:{s0}^^"),
      (try_end),
      
      (try_begin),
        (str_is_empty, s10),
        (str_store_string, s1, "@You don't own any ships."),
      (else_try),
        (str_store_string, s1, "@Your ships with ship type, name, current condition and type of wood: ^^{s10}"),
      (try_end),]),
  
  #######economy kingdom
  ("texto_kingdom_economy",
    [(str_clear, s1),
      (str_clear, s2),
      (str_clear, s3),
      
      #kingdom income
      
      (assign, ":total_income_lords", 0),
      (assign, ":total_income_player", 0),
      (assign, ":total_income", 0),
      (assign, ":total_expenses_playerparty", 0),
      (assign, ":total_expenses_garrison", 0),
      (assign, ":total_expenses", 0),
      
      (try_for_range, ":center_no", centers_begin, centers_end), #lords vassals income
        (party_slot_eq, ":center_no", slot_village_infested_by_bandits, 0),
        (party_slot_ge, ":center_no", slot_town_lord, 1), #excludes player since the player is troop 0
        (party_get_slot, ":lord", ":center_no", slot_town_lord),
        (store_troop_faction, ":faction", ":lord"),
        (eq, ":faction", "fac_player_supporters_faction"),
        (party_get_slot, ":prosperity", ":center_no", slot_town_prosperity), #prosperty changes between 0..100
        (try_begin),
          (party_slot_eq, ":center_no", slot_party_type, spt_village),
          (party_slot_eq, ":center_no", slot_village_state, svs_normal),
          (store_mul, ":prosperity_add", 6, ":prosperity"), #multiplier changes between 20..120
          (store_add, ":lords_payment", 600, ":prosperity_add"), #multiplier changes between 20..120
        (else_try),
          (party_slot_eq, ":center_no", slot_party_type, spt_castle),
          (store_mul, ":prosperity_add", 8, ":prosperity"), #multiplier changes between 20..120
          (store_add, ":lords_payment", 1000, ":prosperity_add"), #multiplier changes between 20..120
        (else_try),
          (party_slot_eq, ":center_no", slot_party_type, spt_town),
          (store_mul, ":prosperity_add", 10, ":prosperity"), #multiplier changes between 20..120
          (store_add, ":lords_payment", 2000, ":prosperity_add"), #multiplier changes between 20..120
        (try_end),
        
        (try_begin),
          (eq, "$control_tax", 1),  #tax control active
          (val_div, ":lords_payment", 2),
        (else_try),
          (val_div, ":lords_payment", 4),
        (try_end),
        
        (val_clamp, ":lords_payment", 0, 3000),
        (assign, ":lords_payment_final", ":lords_payment"),
        (val_add, ":total_income_lords", ":lords_payment_final"),
      (try_end),
      
      
      (try_for_range, ":center", centers_begin, centers_end),
        (party_slot_eq, ":center", slot_village_infested_by_bandits, 0),
        (party_slot_eq, ":center", slot_town_lord, "trp_player"),
        
        (party_get_slot, ":accumulated_rents", ":center", slot_center_accumulated_rents),
        (val_add, ":total_income_player", ":accumulated_rents"),
        
        (try_begin),
          (party_slot_eq, ":center", slot_party_type, spt_town),
          (party_get_slot, ":accumulated_tariffs", ":center", slot_center_accumulated_tariffs),
          (assign, reg0, ":accumulated_tariffs"),
          (val_add, ":total_income_player", ":accumulated_tariffs"),
        (try_end),
        
        #expenses
        (try_begin),#garrisons
          (this_or_next | party_slot_eq, ":center", slot_party_type, spt_town),
          (party_slot_eq, ":center", slot_party_type, spt_castle),
          (party_get_num_companion_stacks, ":num_stacks", ":center"),
          
          (assign, ":troop_size", 0),
          (party_get_num_companion_stacks, ":num_stacks", ":center"),
          (try_for_range, ":i_stack", 0, ":num_stacks"),
            (party_stack_get_troop_id, ":stack_troop", ":center", ":i_stack"),
            (party_stack_get_size, ":stack_size", ":center", ":i_stack"),
            (val_add, ":troop_size", ":stack_size"),
            (call_script, "script_game_get_troop_wage", ":stack_troop", ":center"),
            (assign, ":cur_wage", reg0),
            (val_mul, ":cur_wage", ":stack_size"),
            (val_add, ":total_expenses_garrison", ":cur_wage"),
          (try_end),
          
        (try_end),
      (try_end),
      
      # (try_begin),#player army
      (assign, ":troop_size", 0),
      (party_get_num_companion_stacks, ":num_stacks", "p_main_party"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (party_stack_get_troop_id, ":stack_troop", "p_main_party", ":i_stack"),
        (party_stack_get_size, ":stack_size", "p_main_party", ":i_stack"),
        (val_add, ":troop_size", ":stack_size"),
        (call_script, "script_game_get_troop_wage", ":stack_troop", "p_main_party"),
        (assign, ":cur_wage", reg0),
        (val_mul, ":cur_wage", ":stack_size"),
        (val_add, ":total_expenses_playerparty", ":cur_wage"),
      (try_end),
      
      (val_div, ":total_expenses_garrison", 4), #Half payment for garrisons- changed to 4
      
      (val_add, ":total_expenses", ":total_expenses_playerparty"), #total expenses with player party
      (val_add, ":total_expenses", ":total_expenses_garrison"), #total expenses with garrisons
      
      (val_add, ":total_income", ":total_income_lords"),
      (val_add, ":total_income", ":total_income_player"),
      (assign, reg0, ":total_income_player"),
      (str_store_string, s2, "@Rents and taxes from the {reg59?queen:king}'s holdings: {reg0} peningas."),
      (assign, reg0, ":total_income_lords"),
      (str_store_string, s2, "@{s2}^Taxes from kingdom lords: {reg0} peningas."),
      (assign, reg0, ":total_income"),
      (str_store_string, s2, "@{s2}^^Kingdom Total Income: {reg0} peningas."),
      
      (assign, reg1, ":total_expenses_playerparty"),
      (str_store_string, s3, "@Payroll for the {reg59?queen:king}'s army: {reg1} peningas."),
      (assign, reg1, ":total_expenses_garrison"),
      (str_store_string, s3, "@{s3}^Payroll for garrisons: {reg1} peningas."),
      (assign, reg1, ":total_expenses"),
      (str_store_string, s3, "@{s3}^^Kingdom Total Expenses: {reg1} peningas."),
      
      (assign, ":rest", ":total_income"),
      (val_sub, ":rest", ":total_expenses"),
      (assign, reg0, ":rest"),
      (str_store_string, s1, "@Kingdom Economy Report:^^INCOME:^Tax losses are variable^^{s2}^^^^EXPENSES:^These values could vary because of events or extraordinary expenses.^^{s3}^^^^" +
  "The result of the accounts of the realm is: {reg0} peningas"),]),
  
  ##centers info kingdom
  # ("texto_kingdom_centerinfo",
  # [
  # (str_clear, s1),
  # (str_clear, s2),
  # (str_clear, s3),
  # (str_clear, s4),
  
  # (try_for_range, ":center_no", towns_begin, towns_end), #lords vassals
  # income
  ##(party_get_slot, ":lord", ":center_no", slot_town_lord),
  # (store_faction_of_party, ":faction", ":center_no"),
  # (eq, ":faction", "fac_player_supporters_faction"),
  # (str_store_party_name, s2, ":center_no"),
  # (try_begin),
  # (party_get_slot, ":relation", ":center_no",
  # slot_center_player_relation),
  # (call_script, "script_describe_center_relation_to_s3",
  # ":relation"),
  # (party_get_slot, ":accumulated_rents", ":center_no",
  # slot_center_accumulated_rents),
  # (assign, reg0, ":accumulated_rents"),
  # (try_begin),
  # (party_slot_eq, ":center_no", slot_party_type, spt_town),
  # (party_get_slot, ":accumulated_tariffs", ":center_no",
  # slot_center_accumulated_tariffs),
  # (assign, reg1, ":accumulated_tariffs"),
  # (try_end),
  # (try_end),
  # (str_store_string, s4, "@{s4}^^{s2}^Relation: {s3}^Prosperity:
  # {s50}^Rents: {reg0}^Tariffs: {reg1}"),
  # (try_end),
  
  # (try_for_range, ":center_no", castles_begin, castles_end), #lords
  # vassals income
  ##     (party_get_slot, ":lord", ":center_no", slot_town_lord),
  # (store_faction_of_party, ":faction", ":center_no"),
  # (eq, ":faction", "fac_player_supporters_faction"),
  # (str_store_party_name, s2, ":center_no"),
  # (try_begin),
  # (party_get_slot, ":relation", ":center_no",
  # slot_center_player_relation),
  # (call_script, "script_describe_center_relation_to_s3",
  # ":relation"),
  # (party_get_slot, ":accumulated_rents", ":center_no",
  # slot_center_accumulated_rents),
  # (assign, reg0, ":accumulated_rents"),
  # (try_begin),
  # (this_or_next|is_between, ":center_no", villages_begin, villages_end),
  # (is_between, ":center_no", towns_begin, towns_end),
  # (call_script, "script_get_prosperity_text_to_s50", ":center_no"),
  # (try_end),
  # (try_end),
  # (str_store_string, s5, "@{s5}^^{s2}^Relation: {s3}^Prosperity:
  # {s50}^Rents: {reg0}^Tariffs: {reg1}"),
  # (try_end),
  
  # (try_for_range, ":center_no", villages_begin, villages_end), #lords
  # vassals income
  # (store_faction_of_party, ":faction", ":center_no"),
  # (eq, ":faction", "fac_player_supporters_faction"),
  # (str_store_party_name, s2, ":center_no"),
  # (try_begin),
  # (party_slot_ge, ":center_no",
  # slot_village_infested_by_bandits, 1),
  # (str_store_string, s4, "@{s2}^No information.  This
  # village is occupied by bandits."),
  # (else_try),
  # (party_get_slot, ":relation", ":center_no",
  # slot_center_player_relation),
  # (call_script, "script_describe_center_relation_to_s3",
  # ":relation"),
  # (party_get_slot, ":accumulated_rents", ":center_no",
  # slot_center_accumulated_rents),
  # (assign, reg0, ":accumulated_rents"),
  # (try_begin),
  # (this_or_next|is_between, ":center_no", villages_begin, villages_end),
  # (is_between, ":center_no", towns_begin, towns_end),
  # (call_script, "script_get_prosperity_text_to_s50", ":center_no"),
  # (try_end),
  # (try_end),
  # (str_store_string, s6, "@{s6}^^{s2}^Relation: {s3}^Prosperity:
  # {s50}^Rents: {reg0}^Tariffs: {reg1}"),
  # (try_end),
  
  # (str_store_string, s1, "@Kingdom Centers Report:^^^--Towns--
  # {s4}^^^^--Forts-- {s5}^^^^--Villages--{s6}^^"),
  # ]),
  ###centers info kingdom
  ("texto_kingdom_centerinfo", [(str_clear, s1),
      (str_clear, s2),
      (str_clear, s3),
      (str_clear, s4),
      (str_clear, s5),
      (str_clear, s6),
      (str_clear, s50),
      
      (try_for_range, ":center_no", towns_begin, towns_end),
        (store_faction_of_party, ":faction", ":center_no"),
        (eq, ":faction", "fac_player_supporters_faction"),
        (str_store_party_name, s2, ":center_no"),
        (party_get_slot, ":relation", ":center_no", slot_center_player_relation),
        (call_script, "script_describe_center_relation_to_s3", ":relation"),
        (party_get_slot, ":accumulated_rents", ":center_no", slot_center_accumulated_rents),
        (assign, reg0, ":accumulated_rents"),
        (party_get_slot, ":accumulated_tariffs", ":center_no", slot_center_accumulated_tariffs),
        (assign, reg1, ":accumulated_tariffs"),
        (call_script, "script_get_prosperity_text_to_s50", ":center_no"),
        
        (str_store_string, s4, "@{s4}^^{s2}^Relation: {s3}^Prosperity: {s50}^Rents: {reg0}^Tariffs: {reg1}"),
      (try_end),
      
      (try_for_range, ":center_no", castles_begin, castles_end),
        (store_faction_of_party, ":faction", ":center_no"),
        (eq, ":faction", "fac_player_supporters_faction"),
        (str_store_party_name, s2, ":center_no"),
        (party_get_slot, ":relation", ":center_no", slot_center_player_relation),
        (call_script, "script_describe_center_relation_to_s3", ":relation"),
        (party_get_slot, ":accumulated_rents", ":center_no", slot_center_accumulated_rents),
        (assign, reg0, ":accumulated_rents"),
        (call_script, "script_get_prosperity_text_to_s50", ":center_no"),
        
        (str_store_string, s5, "@{s5}^^{s2}^Relation: {s3}^Prosperity: {s50}^Rents: {reg0}"),
      (try_end),
      
      (try_for_range, ":center_no", villages_begin, villages_end),
        (store_faction_of_party, ":faction", ":center_no"),
        (eq, ":faction", "fac_player_supporters_faction"),
        (str_store_party_name, s2, ":center_no"),
        (try_begin),
          (party_slot_ge, ":center_no", slot_village_infested_by_bandits, 1),
          (str_store_string, s2, "@{s2}^No information. This village is occupied by bandits."),
          (str_store_string, s6, "@{s6}^^{s2}"),
        (else_try),
          (party_get_slot, ":relation", ":center_no", slot_center_player_relation),
          (call_script, "script_describe_center_relation_to_s3", ":relation"),
          (party_get_slot, ":accumulated_rents", ":center_no", slot_center_accumulated_rents),
          (assign, reg0, ":accumulated_rents"),
          (call_script, "script_get_prosperity_text_to_s50", ":center_no"),
          (str_store_string, s6, "@{s6}^^{s2}^Relation: {s3}^Prosperity: {s50}^Rents: {reg0}"),
        (try_end),
      (try_end),
      
      (str_store_string, s1, "@Kingdom Centers Report:^^^--Towns-- {s4}^^^^--Forts-- {s5}^^^^--Villages--{s6}^^"),]),
  
  ####faction screen chief
  ("texto_faction_orders",
    [(str_clear, s1),
      (str_clear, s9),
      (store_current_hours, ":cur_hours"),
      (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
        (faction_slot_eq, ":faction_no", slot_faction_state, sfs_active),
        (eq, ":faction_no", "fac_player_supporters_faction"),
        
        (faction_get_slot, ":old_faction_ai_state", ":faction_no", slot_faction_ai_state),
        
        (try_begin),
          (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
          (gt, ":faction_marshal", -1),
          (assign, ":faction_ai_decider", ":faction_marshal"),
        (else_try),
          (faction_get_slot, ":faction_ai_decider", ":faction_no", slot_faction_leader),
        (try_end),
        
        ##
        ##              #(*1) these two lines moved to here from (*2)
        (call_script, "script_npc_decision_checklist_faction_ai_alt", ":faction_ai_decider"),
        (assign, ":new_strategy", reg0),
        ##	               (str_store_string, s26, s14),
        
        #(3*) these three lines moved to here from (*4)
        (faction_get_slot, ":faction_ai_state", ":faction_no", slot_faction_ai_state),
        (faction_get_slot, ":faction_ai_object", ":faction_no", slot_faction_ai_object),
        (faction_get_slot, ":faction_marshal", ":faction_no", slot_faction_marshal),
        
        (faction_get_slot, ":faction_ai_offensive_max_followers", ":faction_no", slot_faction_ai_offensive_max_followers),
        (str_store_faction_name, s10, ":faction_no"),
        
        (try_begin),
          (faction_get_slot, ":faction_issue", ":faction_no", slot_faction_political_issue),
          
          (try_begin),
            (eq, ":faction_issue", 1),
            (str_store_string, s11, "@Appoint next marshal"),
          (else_try),
            (is_between, ":faction_issue", centers_begin, centers_end),
            (str_store_party_name, s12, ":faction_issue"),
            (str_store_string, s11, "@Award {s12} as fief"),
          (else_try),
            (eq, ":faction_issue", 0),
            (str_store_string, s11, "str_none"),
          (else_try),
            (eq, ":faction_issue", -1),
            (str_store_string, s11, "str_no"),
          (else_try),
            (assign, reg3, ":faction_issue"),
            (str_store_string, s11, "@{!}Error ({reg3})"),
          (try_end),
          
          (store_current_hours, reg4),
          (faction_get_slot, ":faction_issue_put_on_agenda", ":faction_no", slot_faction_political_issue_time),
          (val_sub, reg4, ":faction_issue_put_on_agenda"),
          
          (str_store_string, s10, "@Political issue: {s11}"),
          (try_begin),
            (faction_slot_ge, ":faction_no", slot_faction_political_issue, 1),
            (str_store_string, s10, "@{s10} (on agenda {reg4} hours)"),
          (try_end),
        (try_end),
        
        
        (assign, reg2, ":faction_ai_offensive_max_followers"),
        (try_begin),
          (eq, ":faction_ai_state", sfai_default),
          (str_store_string, s11, "@Defending"),
        (else_try),
          (eq, ":faction_ai_state", sfai_gathering_army),
          (str_store_string, s11, "@Gathering army"),
        (else_try),
          (eq, ":faction_ai_state", sfai_attacking_center),
          (str_store_party_name, s11, ":faction_ai_object"),
          (str_store_string, s11, "@Besieging {s11}"),
        (else_try),
          (eq, ":faction_ai_state", sfai_raiding_village),
          (str_store_party_name, s11, ":faction_ai_object"),
          (str_store_string, s11, "@Raiding {s11}"),
        (else_try),
          (eq, ":faction_ai_state", sfai_attacking_enemy_army),
          (str_store_party_name, s11, ":faction_ai_object"),
          (str_store_string, s11, "str_attacking_enemy_army_near_s11"),
        (else_try),
          (eq, ":faction_ai_state", sfai_feast),
          (str_store_party_name, s11, ":faction_ai_object"),
          (str_store_string, s11, "str_holding_feast_at_s11"),
        (else_try),
          (eq, ":faction_ai_state", sfai_attacking_enemies_around_center),
          (str_store_party_name, s11, ":faction_ai_object"),
          (str_store_string, s11, "@Attacking enemies around {s11}"),
        (else_try),
          (assign, reg4, ":faction_ai_state"),
          (str_store_string, s11, "str_sfai_reg4"),
        (try_end),
        
        (try_begin),
          (lt, ":faction_marshal", 0),
          (str_store_string, s12, "str_noone"),
        (else_try),
          (str_store_troop_name, s12, ":faction_marshal"),
          (troop_get_slot, reg21, ":faction_marshal", slot_troop_controversy),
          (str_store_string, s12, "@{s12} (controversy: {reg21})"),
        (try_end),
        
        (try_for_parties, ":screen_party"),
          #MOTO chief use substate to represent screening properly
          # (party_slot_eq, ":screen_party", slot_party_ai_state,
          # spai_screening_army),
          (party_slot_eq, ":screen_party", slot_party_ai_state, spai_accompanying_army),
          (party_slot_eq, ":screen_party", slot_party_ai_substate, 1),
          #MOTO end use substate to represent screening properly
          (store_faction_of_party, ":screen_party_faction", ":screen_party"),
          (eq, ":screen_party_faction", ":faction_no"),
          
          (str_store_party_name, s38, ":screen_party"),
          (str_store_string, s12, "@{s12}^Screening party: {s38}"),
        (try_end),
        
        (try_begin),
          #new condition to rest, (a faction's new strategy should be feast or
          #default) and (":hours_at_current_state" > 20)
          (this_or_next | eq, ":new_strategy", sfai_default),
          (eq, ":new_strategy", sfai_feast),
          
          (store_current_hours, ":hours_at_current_state"),
          (faction_get_slot, ":current_state_started", ":faction_no", slot_faction_ai_current_state_started),
          (val_sub, ":hours_at_current_state", ":current_state_started"),
          (ge, ":hours_at_current_state", 18),
          
          (store_current_hours, ":hours"),
          (faction_set_slot, ":faction_no", slot_faction_ai_last_rest_time, ":hours"),
        (try_end),
        
        #Change of strategy
        (try_begin),
          (neq, ":new_strategy", ":old_faction_ai_state"),
          
          (store_current_hours, ":hours"),
          (faction_set_slot, ":faction_no", slot_faction_ai_current_state_started, ":hours"),
        (try_end),
        
        (call_script, "script_evaluate_realm_stability", ":faction_no"),
        (assign, ":disgruntled_lords", reg0),
        (assign, ":restless_lords", reg1),
        
        (faction_get_slot, ":last_feast_ended", ":faction_no", slot_faction_last_feast_start_time),
        (store_sub, ":hours_since_last_feast", ":cur_hours", ":last_feast_ended"),
        # (val_sub, ":last_feast_ended", 72),
        (val_sub, ":hours_since_last_feast", 72),
        
        (faction_get_slot, ":current_state_started", ":faction_no", slot_faction_ai_current_state_started),
        (store_sub, ":hours_at_current_state", ":cur_hours", ":current_state_started"),
        
        (faction_get_slot, ":faction_ai_last_offensive_time", ":faction_no", slot_faction_last_offensive_concluded),
        (store_sub, ":hours_since_last_offensive", ":cur_hours", ":faction_ai_last_offensive_time"),
        
        (faction_get_slot, ":faction_ai_last_rest", ":faction_no", slot_faction_ai_last_rest_time),
        (store_sub, ":hours_since_last_rest", ":cur_hours", ":faction_ai_last_rest"),
        
        (faction_get_slot, ":faction_ai_last_decisive_event", ":faction_no", slot_faction_ai_last_decisive_event),
        (store_sub, ":hours_since_last_decisive_event", ":cur_hours", ":faction_ai_last_decisive_event"),
        
        (assign, reg3, ":hours_at_current_state"),
        (assign, reg4, ":hours_since_last_offensive"),
        (assign, reg5, ":hours_since_last_feast"),
        
        (assign, reg7, ":disgruntled_lords"),
        (assign, reg8, ":restless_lords"),
        (assign, reg9, ":hours_since_last_rest"),
        (assign, reg10, ":hours_since_last_decisive_event"),
        #   (str_store_string, s14, s26),
        
        (str_store_string, s9, "str_s9s10_current_state_s11_hours_at_current_state_reg3_current_strategic_thinking_s14_marshal_s12_since_the_last_offensive_ended_reg4_hours_since_the_decisive_event_reg10_hours_since_the_last_rest_reg9_hours_since_the_last_feast_ended_reg5_hours_percent_disgruntled_lords_reg7_percent_restless_lords_reg8__"),
      (try_end),
      
      (str_store_faction_name, s10, "fac_player_supporters_faction"),
      (str_store_string, s1, "@Kingdom Global Info:^^{s10}^^{s9}"),
      # (str_store_string, s1, "@Your relations with the kingdoms are:^^{s2}"),
  ]),
  
  ("texto_factions_orders2",
    [(str_clear,s1),
      
      (try_begin),
        (eq, "$factionorders_type", factionorders_no),
        (str_store_string,s2,"@From this area, you can give the following kingdom orders:",0xFF910012),
        (str_store_string,s3,"@> Order the army to gather.^^" +
          "> Order all resources to be focused on the defense of the kingdom.^^" +
        "> Order the landed levy to be called out. This is done only in situations of common peril or high campaign, as the levy requires all free and most capable men of fighting age to join."),
      (else_try),
        (eq, "$factionorders_type", factionorders_commonarmy),
        (str_store_string,s2,"@> Kingdom Strategy",0xFF910012),
        (str_store_string,s3,"@You have given the order that all the household armies of your kingdom lords come to you."),
      (else_try),
        (eq, "$factionorders_type", factionorders_deffense),
        (str_store_string,s2,"@> Kingdom Strategy",0xFF910012),
        (str_store_string,s3,"@You have given the order to make every effort to defend the kingdom."),
      (else_try),
        (eq, "$factionorders_type", factionorders_fyrd),
        (str_store_string,s2,"@> Kingdom Strategy",0xFF910012),
        (str_store_string,s3,"@The landed levy of the entire kingdom will be called out. Practically every man capable of fighting will join you."),
      (try_end),
      (str_store_string,s1,"@^^^^^^^^{s2}^^{s3}"),]),
  
  # script_texto_story_beda1
  # Beda story chief
  ("texto_story_beda1",
    
    [(str_clear,s10),
      
      (try_begin),
        (str_store_string,s2,"@My name is Beda the Old, and this is my chronicle of what happened in the times in which I lived..."),
        (str_store_string,s3,"@They weren't good times. In Anno Domini 861, my master, Bishop Swithunus, died. Before he died, he told me that the end of time was coming.^^" +
          "In 865, a pagan army attacked Cent. Men from Cent managed to placate them, but under the pretence of peace and the lure of money, the invaders pillaged the country nevertheless.^^" +
          "Later that year, in East Anglia, a large pagan army made winter quarters. The following summer, King Eadmund, eager to see them off, quickly horsed them. This was no raiding party; it became known as the Great Heathen Army. Thousands of men, led by the sons of Ragnar, had come to stay.^^" +
          "In 867, the end of time started, as Swithunus had predicted. The Great Heathen Army of Danes went over the mouth of the river Humbre to Eoferwic. They captured the town, and there was an immense slaughter of the Northumbrians. Both kings, Osbert and Aella, were slain. " +
          "So now the Danish rule the north, imposing their laws and pagan ways... Where is God when you need him?^^" +
        "In the south, the Friese coast was being devastated by Vikings. Merchant ships fled north, seeking the protection of West Seaxe.^^In Ireland and Wales, the pagans were becoming more powerful each day. Many said that the Dal Riata and land of the Picts would soon be no more..."),
      (try_end),
      (str_store_string,s10,"@BEDA CHRONICLE^^{s2}^^{s3}"),]),
  
  ("texto_story_beda2",
    [(str_clear,s10),
      
      (try_begin),
        (str_store_string,s2,"@The news coming from the north was disturbing. Ragnar's sons were calling men to advance on Mercia. It was still a great army; nonetheless, reinforcements from Danmark were expected. God protect us."),
        (str_store_string,s3,"@Some merchants report that Sigurd 'Snake in the Eye,' Ragnar's son in Danmark, is bringing warriors on hundreds of boats. I overheard some of the king's advisers in Witan Ceaster saying that the Danish want to destroy all the kingdoms of Englaland.^^" +
          "The only good news I heard was when a messenger informed us that some Danish sent by Jarl Sigurd to attack Friese were defeated in Doccinga.^^" +
          "That was the first time I heard the name of {playername}, who had led the defense of Doccinga.^^" +
        "Little did I know then that {reg59?she:he} was going to be an important figure in my life."),
      (try_end),
      (str_store_string,s10,"@BEDA CHRONICLE^^{s2}^^{s3}"),]),
  
  ("texto_story_beda3", #escribir story
    [(str_clear,s10),
      
      (try_begin),
        (str_store_string,s2,"@There was a man called Egbert, or I should say a king, Egbert Cyning, for he was king of Northhymbre. But he was a puppet king, chosen by the Vikings because he was a weak man whom they could handle. Was this the fate that awaited us also?"),
        (str_store_string,s3,"@In Anno Domini 868, the Vikings took Snotingaham. There was fear, much fear, but West Seaxe and Mierce joined forces. Aelfred of West Seaxe, the king's brother, was married to Ealswith, daughter of Aethelred Mucel, ealdorman of the Gaini, and of Eadburh, from the royal line of the king of the Mercians.^^" +
          "The two realms called the fyrd to war. West Seaxe advanced on Snotingaham from the south, and Mierce joined them on the way. Maybe they could finally put an end to the advance of the sons of Ragnar?^^" +
        "My life was about to change, but I did not know that."),
      (try_end),
      (str_store_string,s10,"@BEDA CHRONICLE^^{s2}^^{s3}"),]),
  
  ("texto_story_beda4", #escribir story
    [(str_clear,s10),
      
      (try_begin),
        (str_store_string,s2,"@I still dream of Douar-an-Enez when I close my eyes. No man of God should allow such dreams -- it was as if the devil had escaped from prison and taken over the world."),
        (str_store_string,s3,"@I remember sailing into battle and vomiting several times. My hands were shaking and a painful chill overlaid my spine. I was no warrior, but my fate lay with {playername} and {reg59?her:his} men. " +
          "If we were defeated, we would die with the rest. Fortunately, by some miracle of God, I survived. Too many did not, including Bodo, with whom, I hear, {playername} had spent much time since they first met on a ship called the Woden Ric. " +
          "I figured Bodo would be a great loss for {reg59?her:him} after everything that had happened, but {reg59?her:his} face was imperturbable, and no one dared to ask {reg59?her:him} how {reg59?she:he} felt.^^" +
          "After the two battles of Douar-an-Enez, we spent a few weeks in the remains of the village, healing the wounded, burying the dead, and recovering from all that had passed. " +
          "I really think that times like that are when my work is most needed. Many came to me to find comfort and to thank me for all my words over the dead.^^" +
        "Then we sailed back to Englaland, where, we didn't yet know, the world had changed..."),
      (try_end),
      (str_store_string,s10,"@BEDA CHRONICLE^^{s2}^^{s3}"),]),
  
  ("texto_story_bedafinal", #storyplayer final after sven death
    [(str_clear,s2),
      (str_clear,s3),
      (str_clear,s10),
      (str_clear,s16),
      (try_begin),
        (eq, "$thora_hijo", 1), #player romance and hijo
        (str_store_string,s16,"@Maybe part of him is left in the baby that he had with Thora, daughter of Ulf from Danmark. The boy, named after his father, grows strong and healthy, and promises to be a powerful man someday."),
      (try_end),
      
      (try_begin),
        (str_store_string,s2,"@My name is Beda, and this book started as a chronicle of a dark time, but the time is not so dreadful, as it was overshadowed by a hero."),
        (str_store_string,s3,"@I was there when the armies of the Saxons and Danes fought at Aescendun. I was there when {playername} changed the course of history. But even this and what happened next pales in importance when the world lost its hero. " +
          "I remember it like it just happened, when we entered the hall and watched as Sven Bull Neck finished off {playername}'s mother. I remember {reg59?her:his} face. It became ice cold. Neither anger nor fear nor crying; only coldness. " +
          "I assure you that Sven Bull Neck turned pale because he knew that his death was inevitable. We fought in the hall and Sven died, but that did not change the hardness of the face of {playername}. " +
          "Neither then nor later, at the funeral of {reg59?her:his} mother, revered like a queen, and buried with rich grave goods in a warship.^^" +
          "After that day, we lost our hero. {reg59?She:He} said farewell to us, who we had followed {reg59?her:him} from battle to battle, and said {reg59?she:he} was leaving without revealing {reg59?her:his} destination.^^" +
        "{reg59?She:He} left us, I fear, to never return. I hope I am wrong, because this dark world still stands in need of {reg59?her:his} exploits."),
      (try_end),
      (str_store_string,s10,"@BEDA CHRONICLE^^{s2}^^{s3}^^{s16}"),]),
  
  ("texto_story_bedafinal2", #storyplayer final after sven death
    [(str_clear,s2),
      (str_clear,s3),
      (str_clear,s10),
      (str_clear,s11),
      (str_clear,s12),
      (str_clear,s13),
      (str_clear,s14),
      (str_clear,s15),
      (str_clear,s16),
      (str_clear,s17),
      (str_clear,s18),
      (str_clear,s19),
      (str_clear,s20),
      (str_clear,s21),
      (str_clear,s22),
      (str_clear,s23),
      
      (try_for_range,":current_npc",companions_begin,companions_end),
        (main_party_has_troop,":current_npc"),
        (try_begin),
          (eq, ":current_npc", "trp_npc1"), #caio
          (str_store_string,s11,"@Little news has reached me about the young Caio. I do know for sure that he never returned to Hadrian's Wall, not even to visit his family. Last I heard is that he embarked on a Danish expedition headed to Konstantinoupolis."),
        (else_try),
          (eq, ":current_npc", "trp_npc2"), #egil
          (neg|troop_slot_eq, "trp_npc2", slot_troop_occupation, slto_dead),
          (str_store_string,s12,"@^^Egil returned to Ribe, his home, made rich by the spoils of his adventure. He spent all his money on women and ale, and ended up dead in a duel shortly thereafter."),
        (else_try),
          (eq, ":current_npc", "trp_npc3"), #Brunhild
          (str_store_string,s13,"@^^Brunhild married a Dane, despite having always hated them, and settled near Jorvik. A merchant told me that currently she is a happy mother with three children."),
        (else_try),
          (eq, ":current_npc", "trp_npc4"), #Donnachdh
          (str_store_string,s14,"@^^The fate of Donnachdh was dark. He returned to Alban, where he recruited a crew of Scots to pillage the Norwegian merchants in the Sea of Hibernia. The Norwegians of Dubh Linn put a price on his head, gave chase and killed him in an epic battle that the bards still sing about at the court of Alban."),
        (else_try),
          (eq, ":current_npc", "trp_npc5"), #Morgant
          (str_store_string,s15,"@^^Morgant returned to Cornubia and sought to unite the clans to try to recover the territory of the old Kingdom of Dunmonia, but was unsuccessful. Many were simply abandoning their homeland for the other side of the channel, which amazingly enough failed to reduce the many rivalries. In the end, he gave up and retired to the monastery of Petrockstow, where I know he took vows."),
          # (else_try),
          # (eq, ":current_npc", "trp_npc6"), #Bodo = death in campaign
        (else_try),
          (eq, ":current_npc", "trp_npc7"), #Dwywei
          (try_begin),
            (eq,"$romance_target","trp_npc7"),
            (str_store_string,s16,"@^^Dwywei was one of the few who did not give up after the departure of {playername}, and I know she left in pursuit of him. She loved him despite all that happened, and was willing to travel the world to find him. I don't know if she was successful or if she is even still alive."),
          (else_try),
            (str_store_string,s16,"@^^Dwywei, despite all the past, never resigned herself to the departure of {playername}. I heard she left in pursuit of him."),
          (try_end),
        (else_try),
          (eq, ":current_npc", "trp_npc8"), #Reginhard
          (neg|troop_slot_eq, "trp_npc8", slot_troop_occupation, slto_dead),
          (str_store_string,s17,"@^^Reginhard returned to Dorestad where he married Adda, the shopkeeper, and they had a son named Thiaderd. " +
          "Their marriage was a happy one, but it didn't last. The latest news is that, tormented by his double betrayal, first of Thiaderd and then of {playername}, he returned to his vice."),
        (else_try),
          (eq, ":current_npc", "trp_npc9"), #Clovis
          (str_store_string,s18,"@^^About Clovis, he returned to Frankia where he took land and wife. But he seems to want to forget the past and does not receive my messengers."),
        (else_try),
          (eq, ":current_npc", "trp_npc11"), #Solveig
          (str_store_string,s19,"@^^The beautiful and indomitable Solveig decided to continue {playername}'s revenge on the family of Sven Bull Neck on her own. She returned to Danmark, where she hunted and killed all the brothers of Sven, along with their families. The Thing sentenced her to death, so now she's hiding somewhere among other bandits and exiles."),
        (else_try),
          (eq, ":current_npc", "trp_npc12"), #Asbjorn
          (str_store_string,s20,"@^^The loeknir, the physician, Asbjorn returned to Odinsve, the home of his parents, weary of war. He puts his knowledge to use helping cure people, all for just a few peningas -- even free to anyone who asks. " +
          "His fame runs throughout the north, and people worship him like a saint."),
        (else_try),
          (eq, ":current_npc", "trp_npc13"), #Helgi
          (try_begin),
            (eq,"$romance_target","trp_npc13"),
            (str_store_string,s21,"@^^Helgi was very fond of {playername}, despite all the past, and when she left, he plunged into a terrible depression. The last thing I know is that he went back east and became a Rus, hoping more blood would soothe his grief."),
          (else_try),
            (str_store_string,s21,"@^^Helgi became depressed after the departure of {playername}. Without clear direction, he marched east and became a Rus, looking for his fate in unending war."),
          (try_end),
        (else_try),
          (eq, ":current_npc", "trp_npc15"), #Agathinos
          (str_store_string,s22,"@^^The Greek, Agathinos, stayed in the north, near Lundenwic. There he got a job rebuilding the old Roman infrastructure. Sometimes I get letters from him. I do not know why we were never friends, but he likes to keep up the relations he made in his past, and I must be the only remnant of another time he has left."),
        (else_try),
          (str_store_string,s23,"@^^And that is all what I know about people who served under {playername}."),
        (try_end),
      (try_end),
      
      
      #variables para exploits + companions try para companiosn list.
      (try_begin),
        (str_store_string,s2,"@After the departure of our leader, we, {reg59?her:his} followers, dispersed to the four winds. I hid in a monastery in a remote part of West Seaxe, far from it all. Here I will wait for the end of my days. " +
        "I pay people to bring me news from outside, especially if they find {playername}, but nobody has seen {reg59?her:him}. However, I have a little news from others who were with {reg59?her:him} until the end."),
        (str_store_string,s3,"@{s11}{s12}{s13}{s14}{s15}{s16}{s17}{s18}{s19}{s20}{s21}{s22}{s23}."),
      (try_end),
      (str_store_string,s10,"@{s2}{s3}^^" +
        "I hope my writings serve to keep {playername} in history. The {reg59?lady:man} who started from nothing, on a ship called the Woden Ric, and rose to be a warlord who was able to put in check the powers of {reg59?her:his} time. " +
        "{reg59?She:He} it was who killed the dreaded Sven Bull Neck and could be measured face to face with the most powerful men, like the sons of Ragnar and the kings of West Seaxe.^^" +
  "May the world remember {playername} when the rest of us have all perished."),]),
  ###
  #script_duel_init_system
  #input: scene to use.
  #uses first troop (party leader) for opponent, plus a couple quest troops
  #party troops as spectators
  ("duel_init_system", [
      (store_script_param_1, ":scene"),
      (store_script_param_2, "$g_duel_troop"),
      
      (modify_visitors_at_site, ":scene"),
      (reset_visitors),
      (set_visitor, 0, "trp_player"),
      (set_visitor, 1, "$g_duel_troop"),
      (assign,":enemy_count",0),
      (assign,":friend_count",0),
      (assign,":cur_pos",2),
      
      (try_begin),
        (neq,"$g_duel_troop","trp_orm_robagranjas"),(neq,"$g_duel_troop","trp_npc2"),(neq,"$g_duel_troop","$choose_duel_troop"), #fix problem with storyline
        (party_get_num_companion_stacks, ":num_stacks","$g_encountered_party"),
        (try_for_range, ":i_stack", 0, ":num_stacks"),
          (lt,":enemy_count",10),
          (party_stack_get_troop_id, ":stack_troop","$g_encountered_party",":i_stack"),
          (neq,":stack_troop","$g_duel_troop"),
          
          (party_stack_get_size,":stack_size","$g_encountered_party",":i_stack"),
          (try_for_range,":unused",1,":stack_size"),
            (lt, ":cur_pos", 12),
            (set_visitor, ":cur_pos", ":stack_troop"),
            (val_add,":cur_pos", 1),
            (val_add,":enemy_count",1),
          (try_end),
        (try_end),
      (try_end),
      (assign,":cur_pos",12),
      (party_get_num_companion_stacks, ":num_stacks","p_main_party"),
      (try_for_range, ":i_stack", 0, ":num_stacks"),
        (lt,":friend_count",10),
        (party_stack_get_size,":stack_size","p_main_party",":i_stack"),
        (party_stack_get_troop_id, ":stack_troop","p_main_party",":i_stack"),
        (neq,":stack_troop","trp_player"),
        (try_for_range,":unused",1,":stack_size"),
          (lt, ":cur_pos", 22),
          (set_visitor, ":cur_pos", ":stack_troop"),
          (val_add,":cur_pos", 1),
          (val_add,":friend_count",1),
        (try_end),
      (try_end),
      
      (jump_to_scene, ":scene"),
    ]),
  
  ###add-on final text
  ("texto_add_final1",
    [(str_clear,s10),
      
      (try_begin),
        (str_store_string,s2,"@They say that Eriu is a wilderness, inhabited by people too untrustworthy for truce or trade, and too violent to make it worth conquering. " +
        "I have heard many stories about Eriu, but none is worth retelling. What I am sure of is that the time {playername} spent there left him scarred for life."),
        
        (try_begin),
          (check_quest_active,"qst_blank_quest_26"),(quest_slot_eq,"qst_blank_quest_26",slot_quest_current_state,11),
          (str_store_string,s3,"@Honestly, Mael Bresail's death meant nothing to {playername}. {reg59?She:He} continues {reg59?her:his} life largely as if Mael Bresail never existed. Perhaps {reg59?she:he} has a few more enemies among the lords of Eriu and a few extra peningas in {reg59?her:his} pockets, but nothing more.^^" +
            "But Morrigan was another thing altogether. That woman had something magical. It was not just her singing, which itself was able to freeze time. It was something deeper. When she left, the whole army felt a void. " +
          "Nobody has ever seen her since, although I know that my {reg59?lady:lord} always hopes for news about her.^^They say many things about Eriu, but nobody tells the legend of Morrigan."),
        (else_try),
          (check_quest_active,"qst_blank_quest_26"),(quest_slot_eq,"qst_blank_quest_26",slot_quest_current_state,10), #pacto
          (str_store_string,s3,"@The agreement with Mael Bresail made {playername} a rich person, but his fate was above wealth, and money was only a way to achieve {reg59?her:his} goals. " +
            "Little news reached us of Mael Bresail after the pact. He always tried to stay out of the way of my {reg59?lady:lord}, even when the affairs of {reg59?my lady:my lord} and his king crossed. " +
          "I don't know if Mael Bresail just honored his oath or felt sheer terror toward {playername}.^^They say many things about Eriu, but few people remember the legend of how Flaithbertach's children were killed."),
        (else_try), #mata y no morrigan
          (str_store_string,s3,"@Honestly, Mael Bresail's death meant nothing to {playername}. {reg59?She:He} continues {reg59?her:his} life largely as if Mael Bresail never existed. Perhaps {reg59?she:he} has a few more enemies among the lords of Eriu and a few extra peningas in {reg59?her:his} pockets, but nothing more.^^" +
            "But Morrigan was another thing altogether. That woman had something magical. It was not just her singing, which itself was able to freeze time. It was something deeper. When {playername} killed her, I think that {reg59?she:he} felt {reg59?she:he} had destroyed something that does not belong to this time or world.^^" +
          "They say many things about Eriu, but few people remember the legend of how Flaithbertach's children were killed."),
        (try_end),
        
      (try_end),
      (str_store_string,s10,"@BEDA CHRONICLE^^{s2}^^{s3}"),]),
  
  # script_start_text_sandbox
  # start text for sandbox
  ("start_text_sandbox",
    [(str_clear,s10),
      
      (try_begin),
        (str_store_string,s2,"@Sandbox mode gives you total freedom to choose your gaming experience. You may choose to be a peaceful trader, a fearsome Viking, a king loved by his people or any other role you may want."),
        (str_store_string,s3,"@Starting is always difficult, so it is recommended to explore the surroundings before heading into your first fight. Visit nearby settlements, obtain quests from local leaders or lords or work to earn a salary at farmsteads, quarries, salt mines or lumber camps. " +
          "Recruit your first men at farmsteads or monasteries or try to bribe a village leader. " +
          "You can also buy merchandise at farmsteads and villages and try to sell it in the town markets for a profit.^^" +
          "It is best to play slowly at first, to explore the possibilities and workings of the game. If you have questions, you can always read the notes.^^" +
        "Viking Conquest is immense. It will take you into the Dark Ages like never before."),
      (try_end),
      (str_store_string,s10,"@^Welcome to Viking Conquest Reforged Edition^^^{s2}^^{s3}"),]),
  
  # script_start_text_kingmode
  ("start_text_kingmode",
    [(str_clear,s10),
      
      (try_begin),
        (str_store_string,s2,"@In this mode, you start out either as a nobleman or a king of a faction. As a nobleman, you'll take control of your fief and lead armies. As a king, you decide kingdom's policies, declare wars, lead armies and develop your kingdom."),
        (str_store_string,s3,"@Your first priority should be to develop your possessions, increase income and fill your coffers with peningas. Stabilize your kingdom before starting new conquests.^^" +
        "Leading people is not easy. War devours resources, and this world is full of dangers. Your actions will determine the future of your kingdom..."),
      (try_end),
      (str_store_string,s10,"@^^Welcome to Viking Conquest Reforged Edition^^^^{s2}^^{s3}"),]),
  
  # script_start_text_storyline
  ("start_text_storyline",
    [(str_clear,s10),
      
      (try_begin),
        (str_store_string,s2,"@In the storyline, you start as a hero with an uncertain fate.^Please read the following carefully, because the gameplay varies slightly from the sandbox campaign."),
        (str_store_string,s3,"@The story follows its own development path and the decisions you make may affect the world around you in addition to standard game mechanics. This means you won't be able to freely join any kingdom or serve any king, except when allowed to do so.^" +
          "The behavior of the kingdoms, especially those important to story and in history, will follow the chronology of historical events as much as possible. As a result, these kingdoms may not be destroyed if they were not defeated historically.^" +
          "Moreover, you won't be able to get rid of your companions, grant them fiefs, or make them vassals, as long as you're playing the storyline. As many of your companions are important to many missions, it is critical that they remain a part of your war band.^^" +
        "It took many months to build and research Viking Conquest storyline, using historical sources, sagas, and deep love for Viking world. We do hope that you enjoy it as much as we did."),
      (try_end),
      (str_store_string,s10,"@^Welcome to Viking Conquest Reforged Edition^^^{s2}^^{s3}"),]),
  
]

scripts = edited_native_scripts + vc_scripts + vc_sea_scripts + vc_text_scripts + multi_scripts


