# Formations for Warband by Motomataru
# rel. 06/08/11

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 ID_animations import *

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

scripts = [
# #Formations Scripts	  
  # script_cf_formation v2 by motomataru
  # Input: team, troop class, spacing, formation type
  # Output: reg0 number of troops in formation, pos61 formation position
  # Formation(s) form near team leader (player)
  ("cf_formation", [
	(store_script_param, ":fteam", 1),
	(store_script_param, ":fclass", 2),
	(store_script_param, ":formation_extra_spacing", 3),
	(store_script_param, ":fformation", 4),
	(team_get_leader, ":fleader", ":fteam"),
	(gt, ":fleader", -1),	#any team members left?
	(agent_get_position, pos1, ":fleader"),
	(try_begin),
		(eq, "$autorotate_at_player", 1),
		(call_script, "script_team_get_position_of_enemies", pos60, ":fteam", grc_everyone),
		(neq, reg0, 0),	#more than 0 enemies still alive?
		(call_script, "script_point_y_toward_position", pos1, pos60),
	(try_end),
	(call_script, "script_battlegroup_get_size", ":fteam", ":fclass"),
	(assign, ":num_troops", reg0),
	(try_begin),
		(eq, ":fclass", grc_cavalry),
		(position_move_x, pos1, 500),		#cavalry set up 5m RIGHT of leader
		(copy_position, pos61, pos1),
		(call_script, "script_form_cavalry", ":fteam", ":fleader", ":formation_extra_spacing"),
	(else_try),
		(eq, ":fclass", grc_infantry),
		(position_move_x, pos1, -100),		#infantry set up 1m LEFT of leader
		(copy_position, pos61, pos1),
		(call_script, "script_form_infantry", ":fteam", ":fleader", ":formation_extra_spacing", ":fformation"),
		(call_script, "script_get_centering_amount", ":fformation", ":num_troops", ":formation_extra_spacing"),
		(val_mul, reg0, -1),
		(position_move_x, pos61, reg0, 0),
	(else_try),
		(position_move_y, pos1, 1000),		#archers set up 10m FRONT of leader
		(copy_position, pos61, pos1),
		(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", ":fleader", ":formation_extra_spacing", "$archer_formation_type"),
	(try_end),
	(try_begin),
		(eq, ":fclass", grc_cavalry),
		(try_begin),
			(ge, ":num_troops", formation_min_cavalry_troops),
			(assign, ":formed_up", 1),
		(else_try),
			(assign, ":formed_up", 0),
		(try_end),
	(else_try),
		(this_or_next|eq, ":fclass", grc_infantry),
		(eq, ":fclass", grc_archers),
		(try_begin),
			(ge, ":num_troops", formation_min_foot_troops),
			(assign, ":formed_up", 1),
		(else_try),
			(assign, ":formed_up", 0),
		(try_end),
	(else_try),	#unsupported division
		(assign, ":formed_up", 0),
	(try_end),
#	(team_set_order_position, ":fteam", ":fclass", pos61),
	(call_script, "script_set_formation_position", ":fteam", ":fclass", pos61),
	(assign, reg0, ":num_troops"),
	(eq, ":formed_up", 1),
  ]),
   
  # script_form_cavalry v2 by motomataru
  # Input: team, agent number of team leader, spacing
  # Output: reg0 troop count
  # Form in wedge, (now not) excluding horse archers
  # Creates formation starting at pos1
  ("form_cavalry", [
	(store_script_param, ":fteam", 1),
	(store_script_param, ":fleader", 2),
	(store_script_param, ":formation_extra_spacing", 3),
	(store_mul, ":extra_space", ":formation_extra_spacing", 50),
	(store_add, ":x_distance", 150, ":extra_space"),
	(store_add, ":y_distance", 250, ":extra_space"),		#larger y minimum distance to accommodate mounts
	(assign, ":max_level", 0),
	(try_for_agents, ":agent"),
		(call_script, "script_cf_valid_formation_member", ":fteam", grc_cavalry, ":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, ":troop_count", 0),
	(val_add, ":max_level", 1),
	(assign, ":form_left", 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", grc_cavalry, ":fleader", ":agent"),
			(val_add, ":troop_count", 1),
			(agent_set_scripted_destination, ":agent", pos1, 1),
			(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),
	(assign, reg0, ":troop_count")
  ]),
	   
  # script_form_archers v2 by motomataru
  # Input: team, agent number of team leader, spacing
  # Output: reg0 troop count
  # Form in staggered line both directions
  # Creates formation starting at pos1
  ("form_archers", [
	(store_script_param, ":fteam", 1),
	(store_script_param, ":fleader", 2),
	(store_script_param, ":formation_extra_spacing", 3),
	(store_script_param, ":archers_formation", 4),
	(store_mul, ":extra_space", ":formation_extra_spacing", 50),
	(store_add, ":distance", formation_minimum_spacing, ":extra_space"),		#minimum distance between troops
	(assign, ":troop_count", 0),
	(assign, ":total_move_y", 0),	#staggering variable	
	(try_for_agents, ":agent"),
		(call_script, "script_cf_valid_formation_member", ":fteam", grc_archers, ":fleader", ":agent"),
		(agent_set_scripted_destination, ":agent", pos1, 1),
		(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),
		(val_add, ":troop_count", 1),
	(try_end),
	(assign, reg0, ":troop_count")
  ]),
	   
  # script_form_infantry v2 by motomataru
  # Input: (pos1), team, agent number of team leader, spacing, formation
  # Output: reg0 troop count
  # 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, ":fleader", 2),
	(store_script_param, ":formation_extra_spacing", 3),
	(store_script_param, ":infantry_formation", 4),
	(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),
	(call_script, "script_battlegroup_get_size", ":fteam", grc_infantry),
	(assign, ":num_troops", reg0),	
	(assign, ":troop_count", reg0),
	(try_begin),
		(eq, ":infantry_formation", formation_default),
		(call_script, "script_get_default_formation", ":fteam"),
		(assign, ":infantry_formation", reg0),
	(try_end),
	(assign, ":form_left", 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),
		(assign, ":column", 1),
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", grc_infantry, ":fleader", ":agent"),
			(agent_set_scripted_destination, ":agent", pos1, 1),
			(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),		
		(end_try),
		
	(else_try),
		(eq, ":infantry_formation", formation_wedge),
		(assign, ":max_level", 0),
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", grc_infantry, ":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_div, ":wedge_adj", ":distance", 2),
		(store_div, ":neg_wedge_adj", ":neg_distance", 2),
		(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", grc_infantry, ":fleader", ":agent"),
				(agent_set_scripted_destination, ":agent", pos1, 1),
				(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),
		(store_div, ":rank_dimension", ":num_troops", 3),		#basic three ranks
		(val_add, ":rank_dimension", 1),		
		(assign, ":max_level", 0),
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", grc_infantry, ":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),
		(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", grc_infantry, ":fleader", ":agent"),
				(agent_set_scripted_destination, ":agent", pos1, 1),
				(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_distance", 0),
				(else_try),
					(assign, ":form_left", 0),
					(position_move_x, pos1, ":distance", 0),
				(try_end),			
				(assign, ":column", 1),
			(end_try),
		(end_try),
		
	(else_try),
		(eq, ":infantry_formation", formation_shield),
		(store_div, ":rank_dimension", ":num_troops", 3),		#basic three ranks
		(val_add, ":rank_dimension", 1),
		(assign, ":column", 1),
	#shields
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", grc_infantry, ":fleader", ":agent"),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 1),
			(gt, ":agent_weapon", -1),
			(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(eq, ":agent_weapon_type", itp_type_shield),
			(agent_set_scripted_destination, ":agent", pos1, 1),
			(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_distance", 0),
			(else_try),
				(assign, ":form_left", 0),
				(position_move_x, pos1, ":distance", 0),
			(try_end),			
			(assign, ":column", 1),
		(try_end),
	#short weapons
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", grc_infantry, ":fleader", ":agent"),
			(assign, ":agent_weapon_type", 0),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 0),
			(try_begin),
				(gt, ":agent_weapon", -1),
				(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(try_end),
			(neq, ":agent_weapon_type", itp_type_polearm),
			(assign, ":agent_weapon_type", 0),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 1),
			(try_begin),
				(gt, ":agent_weapon", -1),
				(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(try_end),
			(neq, ":agent_weapon_type", itp_type_shield),
			(agent_set_scripted_destination, ":agent", pos1, 1),
			(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_distance", 0),
			(else_try),
				(assign, ":form_left", 0),
				(position_move_x, pos1, ":distance", 0),
			(try_end),			
			(assign, ":column", 1),
		(try_end),
	#pole weapons
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", grc_infantry, ":fleader", ":agent"),
			(assign, ":agent_weapon_type", 0),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 1),
			(try_begin),
				(gt, ":agent_weapon", -1),
				(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(try_end),
			(neq, ":agent_weapon_type", itp_type_shield),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 0),
			(gt, ":agent_weapon", -1),
			(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(eq, ":agent_weapon_type", itp_type_polearm),
			(agent_set_scripted_destination, ":agent", pos1, 1),
			(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_distance", 0),
			(else_try),
				(assign, ":form_left", 0),
				(position_move_x, pos1, ":distance", 0),
			(try_end),			
			(assign, ":column", 1),
		(try_end),
	(try_end),
	(assign, reg0, ":troop_count")
  ]),
	   
  # script_get_default_formation by motomataru
  # Input: team id
  # Output: reg0 default formation
  ("get_default_formation", [
	(store_script_param, ":fteam", 1),
	(try_begin),
		(eq, ":fteam", 0),
		(assign, ":ffaction", "$team0_faction"),
	(else_try),
		(eq, ":fteam", 1),
		(assign, ":ffaction", "$team1_faction"),
	(else_try),
		(eq, ":fteam", 2),
		(assign, ":ffaction", "$team2_faction"),
	(else_try),
		(eq, ":fteam", 3),
		(assign, ":ffaction", "$team3_faction"),
	(try_end),
	
	(try_begin),
		(eq, ":ffaction", fac_player_supporters_faction),
		(assign, ":ffaction", fac_player_faction),
	(try_end),
	(try_begin),
		(eq, ":ffaction", fac_player_faction),
		(is_between, "$players_kingdom", kingdoms_begin, kingdoms_end),
		(neq, "$players_kingdom", fac_player_supporters_faction),
		(assign, ":ffaction", "$players_kingdom"),
	(try_end),

	#assign default formation
	(try_begin),
		(eq, ":ffaction", fac_kingdom_1),	#Swadians
		(assign, reg0, formation_shield),
	(else_try),
		(eq, ":ffaction", fac_kingdom_2),	#Vaegirs
		(assign, reg0, formation_ranks),
	(else_try),
		(eq, ":ffaction", fac_kingdom_3),	#Khergit
		(assign, reg0, formation_none),	#Khergit have underdeveloped infantry
	(else_try),
		(eq, ":ffaction", fac_kingdom_4),	#Nords
		(assign, reg0, formation_shield),
	(else_try),
		(eq, ":ffaction", fac_kingdom_5),	#Rhodoks
		(assign, reg0, formation_shield),
	(else_try),
		(eq, ":ffaction", fac_kingdom_6),	#Sarranid
		(assign, reg0, formation_ranks),
	(else_try),
		(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_formation_current_position by motomataru
  # Input: destination position, team, troop class, formation type, extra spacing
  # Output: in destination position
  ("formation_current_position", [
	(store_script_param, ":fposition", 1),
	(store_script_param, ":fteam", 2),
	(store_script_param, ":fclass", 3),
	(store_script_param, ":fformation", 4),
	(store_script_param, ":formation_extra_spacing", 5),
	(call_script, "script_get_first_formation_member", ":fteam", ":fclass", ":fformation"),
	(assign, ":first_agent_in_formation", reg0),
	(call_script, "script_get_formation_position", pos0, ":fteam", ":fclass"),
	(try_begin),
		(eq, ":first_agent_in_formation", -1),
		(copy_position, ":fposition", pos0),
	(else_try),
		(agent_get_position, ":fposition", ":first_agent_in_formation"),
		(position_copy_rotation, ":fposition", pos0),
		(call_script, "script_battlegroup_get_size", ":fteam", ":fclass"),
		(assign, ":num_troops", reg0),
		(try_begin),
			(eq, ":fclass", grc_archers),
			(call_script, "script_get_centering_amount", formation_default, ":num_troops", ":formation_extra_spacing"),
		(else_try),
			(call_script, "script_get_centering_amount", ":fformation", ":num_troops", ":formation_extra_spacing"),
			(val_mul, reg0, -1),
		(try_end),
		(position_move_x, ":fposition", reg0, 0),
	(try_end),
  ]),
  
  
  # script_get_centering_amount by motomataru
  # Input: formation type, number of infantry, 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"),
		(val_mul, reg0, ":troop_space"),
		(convert_from_fixed_point, reg0),
		(val_sub, reg0, ":troop_space"),
	(else_try),
		(this_or_next|eq, ":troop_formation", formation_ranks),
		(eq, ":troop_formation", formation_shield),
		(store_mul, reg0, ":num_troops", ":troop_space"),
		(val_div, reg0, 3),
		(val_sub, 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, troop class
  # Output: none
  ("formation_end", [
	(store_script_param, ":fteam", 1),
	(store_script_param, ":fclass", 2),
	(try_for_agents, ":agent"),
		(agent_is_alive, ":agent"),
		(agent_is_human, ":agent"),
		(agent_get_team, ":team", ":agent"),
		(eq, ":team", ":fteam"),
		(call_script, "script_classify_agent", ":agent"),
		(this_or_next|eq, reg0, ":fclass"), 
		(eq, ":fclass", grc_everyone), 
		(agent_clear_scripted_mode, ":agent"),
	(try_end)
  ]),


  # script_formation_move_position v2 by motomataru
  # Input: team, troop class, formation current position, formation type, extra spacing, (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, ":fclass", 2),
	(store_script_param, ":fcurrentpos", 3),
	(store_script_param, ":fformation", 4),
	(store_script_param, ":formation_extra_spacing", 5),
	(store_script_param, ":direction", 6),
	(copy_position, pos1, ":fcurrentpos"),
	(call_script, "script_team_get_position_of_enemies", pos60, ":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, pos60),	#record angle from center to enemy
		(assign, ":distance_to_enemy", reg0),
#		(team_get_order_position, pos61, ":fteam", ":fclass"),
		(call_script, "script_get_formation_position", pos61, ":fteam", ":fclass"),
		(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),
		(try_begin),
			(lt, ":distance_to_enemy", 1000),	#less than a move away?
			(position_copy_rotation, pos1, pos61),	#avoid rotating formation
		(try_end),
#		(team_set_order_position, ":fteam", ":fclass", pos1),
		(call_script, "script_set_formation_position", ":fteam", ":fclass", pos1),
		(call_script, "script_battlegroup_get_size", ":fteam", ":fclass"),
		(assign, ":num_troops", reg0),
		(try_begin),
			(neq, ":fclass", grc_archers),
			(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),
	(try_end),
  ]),


  # script_set_formation_position by motomataru
  # Input: team, troop class, position
  # Kluge around buggy *_order_position functions for teams 0-3
  ("set_formation_position", [
	(store_script_param, ":fteam", 1),
	(store_script_param, ":fclass", 2),
	(store_script_param, ":fposition", 3),
	(try_begin),
		(eq, ":fteam", 0),
		(try_begin),
			(eq, ":fclass", 0),
			(position_get_x, "$formation_00_x", ":fposition"),
			(position_get_y, "$formation_00_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_00_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 1),
			(position_get_x, "$formation_01_x", ":fposition"),
			(position_get_y, "$formation_01_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_01_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 2),
			(position_get_x, "$formation_02_x", ":fposition"),
			(position_get_y, "$formation_02_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_02_rot", ":fposition"),
		(end_try),
	(else_try),
		(eq, ":fteam", 1),
		(try_begin),
			(eq, ":fclass", 0),
			(position_get_x, "$formation_10_x", ":fposition"),
			(position_get_y, "$formation_10_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_10_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 1),
			(position_get_x, "$formation_11_x", ":fposition"),
			(position_get_y, "$formation_11_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_11_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 2),
			(position_get_x, "$formation_12_x", ":fposition"),
			(position_get_y, "$formation_12_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_12_rot", ":fposition"),
		(end_try),
	(else_try),
		(eq, ":fteam", 2),
		(try_begin),
			(eq, ":fclass", 0),
			(position_get_x, "$formation_20_x", ":fposition"),
			(position_get_y, "$formation_20_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_20_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 1),
			(position_get_x, "$formation_21_x", ":fposition"),
			(position_get_y, "$formation_21_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_21_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 2),
			(position_get_x, "$formation_22_x", ":fposition"),
			(position_get_y, "$formation_22_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_22_rot", ":fposition"),
		(end_try),
	(else_try),
		(eq, ":fteam", 3),
		(try_begin),
			(eq, ":fclass", 0),
			(position_get_x, "$formation_30_x", ":fposition"),
			(position_get_y, "$formation_30_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_30_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 1),
			(position_get_x, "$formation_31_x", ":fposition"),
			(position_get_y, "$formation_31_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_31_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 2),
			(position_get_x, "$formation_32_x", ":fposition"),
			(position_get_y, "$formation_32_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_32_rot", ":fposition"),
		(end_try),	
	(end_try),
	(team_set_order_position, ":fteam", ":fclass", ":fposition"),
  ]),	


  # script_get_formation_position by motomataru
  # Input: position, team, troop class
  # Output: input position (pos0 used)
  # Kluge around buggy *_order_position functions for teams 0-3
  ("get_formation_position", [
	(store_script_param, ":fposition", 1),
	(store_script_param, ":fteam", 2),
	(store_script_param, ":fclass", 3),
	(init_position, ":fposition"),
	(try_begin),
		(eq, ":fteam", 0),
		(try_begin),
			(eq, ":fclass", 0),
			(position_set_x, ":fposition", "$formation_00_x"),
			(position_set_y, ":fposition", "$formation_00_y"),
			(position_rotate_z, ":fposition", "$formation_00_rot"),
		(else_try),
			(eq, ":fclass", 1),
			(position_set_x, ":fposition", "$formation_01_x"),
			(position_set_y, ":fposition", "$formation_01_y"),
			(position_rotate_z, ":fposition", "$formation_01_rot"),
		(else_try),
			(eq, ":fclass", 2),
			(position_set_x, ":fposition", "$formation_02_x"),
			(position_set_y, ":fposition", "$formation_02_y"),
			(position_rotate_z, ":fposition", "$formation_02_rot"),
		(try_end),
	(else_try),
		(eq, ":fteam", 1),
		(try_begin),
			(eq, ":fclass", 0),
			(position_set_x, ":fposition", "$formation_10_x"),
			(position_set_y, ":fposition", "$formation_10_y"),
			(position_rotate_z, ":fposition", "$formation_10_rot"),
		(else_try),
			(eq, ":fclass", 1),
			(position_set_x, ":fposition", "$formation_11_x"),
			(position_set_y, ":fposition", "$formation_11_y"),
			(position_rotate_z, ":fposition", "$formation_11_rot"),
		(else_try),
			(eq, ":fclass", 2),
			(position_set_x, ":fposition", "$formation_12_x"),
			(position_set_y, ":fposition", "$formation_12_y"),
			(position_rotate_z, ":fposition", "$formation_12_rot"),
		(try_end),
	(else_try),
		(eq, ":fteam", 2),
		(try_begin),
			(eq, ":fclass", 0),
			(position_set_x, ":fposition", "$formation_20_x"),
			(position_set_y, ":fposition", "$formation_20_y"),
			(position_rotate_z, ":fposition", "$formation_20_rot"),
		(else_try),
			(eq, ":fclass", 1),
			(position_set_x, ":fposition", "$formation_21_x"),
			(position_set_y, ":fposition", "$formation_21_y"),
			(position_rotate_z, ":fposition", "$formation_21_rot"),
		(else_try),
			(eq, ":fclass", 2),
			(position_set_x, ":fposition", "$formation_22_x"),
			(position_set_y, ":fposition", "$formation_22_y"),
			(position_rotate_z, ":fposition", "$formation_22_rot"),
		(try_end),
	(else_try),
		(eq, ":fteam", 3),
		(try_begin),
			(eq, ":fclass", 0),
			(position_set_x, ":fposition", "$formation_30_x"),
			(position_set_y, ":fposition", "$formation_30_y"),
			(position_rotate_z, ":fposition", "$formation_30_rot"),
		(else_try),
			(eq, ":fclass", 1),
			(position_set_x, ":fposition", "$formation_31_x"),
			(position_set_y, ":fposition", "$formation_31_y"),
			(position_rotate_z, ":fposition", "$formation_31_rot"),
		(else_try),
			(eq, ":fclass", 2),
			(position_set_x, ":fposition", "$formation_32_x"),
			(position_set_y, ":fposition", "$formation_32_y"),
			(position_rotate_z, ":fposition", "$formation_32_rot"),
		(try_end),	
	(else_try),
		(call_script, "script_get_first_formation_member", ":fteam", ":fclass", formation_square),
		(try_begin),	  # "launder" team_get_order_position shutting down position_move_x
			(gt, reg0, -1),
			(team_get_order_position, ":fposition", ":fteam", ":fclass"),
			(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_get_first_formation_member by motomataru
  # Input: team, troop class, formation
  # Output: reg0 leader agent number or -1
  # Ought to follow script_form_infantry
  ("get_first_formation_member", [
	(store_script_param, ":fteam", 1),
	(store_script_param, ":fclass", 2),
	(store_script_param, ":fformation", 3),
	(team_get_leader, ":fleader", ":fteam"),
	(assign, ":first_agent_in_formation", -1),
	(try_begin),
		(eq, ":fformation", formation_square),
		(try_for_agents, ":agent"),
			(eq, ":first_agent_in_formation", -1),
			(call_script, "script_cf_valid_formation_member", ":fteam", ":fclass", ":fleader", ":agent"),
			(assign, ":first_agent_in_formation", ":agent"),
		(try_end),
	(else_try),
		(eq, ":fformation", formation_shield),
		(try_for_agents, ":agent"),
			(eq, ":first_agent_in_formation", -1),
			(call_script, "script_cf_valid_formation_member", ":fteam", ":fclass", ":fleader", ":agent"),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 1),
			(gt, ":agent_weapon", -1),
			(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(eq, ":agent_weapon_type", itp_type_shield),
			(assign, ":first_agent_in_formation", ":agent"),
		(try_end),
		(try_for_agents, ":agent"),
			(eq, ":first_agent_in_formation", -1),
			(call_script, "script_cf_valid_formation_member", ":fteam", ":fclass", ":fleader", ":agent"),
			(assign, ":agent_weapon_type", 0),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 0),
			(try_begin),
				(gt, ":agent_weapon", -1),
				(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(try_end),
			(neq, ":agent_weapon_type", itp_type_polearm),
			(assign, ":first_agent_in_formation", ":agent"),
		(try_end),
		(try_for_agents, ":agent"),
			(eq, ":first_agent_in_formation", -1),
			(call_script, "script_cf_valid_formation_member", ":fteam", ":fclass", ":fleader", ":agent"),
			(assign, ":first_agent_in_formation", ":agent"),	#must have polearm w/o shields...
		(try_end),
	(else_try),	#must be a level sort
		(assign, ":rank_level", 0),
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", ":fclass", ":fleader", ":agent"),
			(agent_get_troop_id, ":troop_id", ":agent"),
			(store_character_level, ":troop_level", ":troop_id"),
			(gt, ":troop_level", ":rank_level"),
			(assign, ":rank_level", ":troop_level"),
			(assign, ":first_agent_in_formation", ":agent"),
		(try_end),
	(try_end),
	(assign, reg0, ":first_agent_in_formation")
  ]),
  
	
  # script_cf_valid_formation_member by motomataru
  # Input: team, troop class, 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, ":fclass", 2),
	# (store_script_param, ":fleader", 3),
	(store_script_param, ":agent", 4),
	(call_script, "script_classify_agent", ":agent"),
	(eq, reg0, ":fclass"),
	(agent_get_team, ":team", ":agent"),
	(eq, ":team", ":fteam"),
	(agent_is_alive, ":agent"),
	(agent_is_human, ":agent"),
	(agent_slot_eq, ":agent", slot_agent_is_running_away, 0),
  ]),

	
  # script_classify_agent by motomataru
  # Input: agent
  # Output: reg0 (-1 if leader)
  # Assigns a "division" to the agent
  ("classify_agent", [
	(store_script_param, ":agent", 1),
	(agent_get_team, ":ateam", ":agent"),
	(team_get_leader, ":aleader", ":ateam"),
	(try_begin),
		(eq, ":agent", ":aleader"),
		(assign, reg0, -1),
	(else_try),
		(eq, ":ateam", "$fplayer_team_no"),
		(agent_get_division, reg0, ":agent"),
	(else_try),
		(agent_get_class, reg0, ":agent"),
	(try_end)
  ]),

	
# #Player team formations functions

  # script_player_attempt_formation
  # Inputs:	arg1: troop class (grc_*)
  #			arg2: formation identifier (formation_*)
  # Output: none
  ("player_attempt_formation", [
	(store_script_param, ":fclass", 1),
	(store_script_param, ":fformation", 2),
	(set_fixed_point_multiplier, 100),
	(try_begin),
		(class_is_listening_order, "$fplayer_team_no", ":fclass"),
		(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),
		(assign, ":new_formation", 0),

		(try_begin),
			(eq, ":fclass", grc_infantry),
			(try_begin),
				(neq, "$infantry_formation_type", ":fformation"),
				(assign, ":new_formation", 1),
				(assign, "$infantry_formation_type", ":fformation"),
				(lt, "$infantry_space", formation_start_spread_out),
				(assign, "$infantry_space", formation_start_spread_out),	#spread out for ease of forming up
			(try_end),
			(try_begin),
				(call_script, "script_cf_formation", "$fplayer_team_no", grc_infantry, "$infantry_space", "$infantry_formation_type"),
				(try_begin),
					(eq, ":new_formation", 1),
					(display_message, "@Infantry forming {s1}."),
				(try_end),
			(else_try),
				(assign, "$infantry_formation_type", formation_none),
				(gt, reg0, 0),
				(display_message, "@Not enough infantry to form {s1}, but holding."),
				(call_script, "script_formation_end", "$fplayer_team_no", grc_infantry),
			(try_end),
			(assign, "$infantry_formation_move_order", mordr_hold),
			(set_show_messages, 0),
			(team_give_order, "$fplayer_team_no", grc_infantry, mordr_hold),
			(team_set_order_position, "$fplayer_team_no", grc_infantry, pos61),
			(set_show_messages, 1),
		(else_try),
			(eq, ":fclass", grc_cavalry),
			(try_begin),
				(neq, "$cavalry_formation_type", ":fformation"),
				(assign, ":new_formation", 1),
				(assign, "$cavalry_formation_type", ":fformation"),
				(lt, "$cavalry_space", formation_start_spread_out),
				(assign, "$cavalry_space", formation_start_spread_out),	#spread out for ease of forming up
			(try_end),
			(try_begin),
				(call_script, "script_cf_formation", "$fplayer_team_no", grc_cavalry, "$cavalry_space", "$cavalry_formation_type"),
				(try_begin),
					(eq, ":new_formation", 1),
					(display_message, "@Cavalry forming {s1}."),
				(try_end),
			(else_try),
				(assign, "$cavalry_formation_type", formation_none),
				(gt, reg0, 0),
				(display_message, "@Not enough cavalry to form {s1}, but holding."),
				(call_script, "script_formation_end", "$fplayer_team_no", grc_cavalry),
			(try_end),
			(assign, "$cavalry_formation_move_order", mordr_hold),
			(set_show_messages, 0),
			(team_give_order, "$fplayer_team_no", grc_cavalry, mordr_hold),
			(team_set_order_position, "$fplayer_team_no", grc_cavalry, pos61),
			(set_show_messages, 1),
		(else_try),	#must be archers
			(eq, ":fclass", grc_archers),
			(try_begin),
				(neq, "$archer_formation_type", ":fformation"),
				(assign, ":new_formation", 1),
				(assign, "$archer_formation_type", ":fformation"),
				(lt, "$archer_space", formation_start_spread_out),
				(assign, "$archer_space", formation_start_spread_out),	#spread out for ease of forming up
			(try_end),
			(try_begin),
				(call_script, "script_cf_formation", "$fplayer_team_no", grc_archers, "$archer_space", "$archer_formation_type"),
				(try_begin),
					(eq, ":new_formation", 1),
					(display_message, "@Archers forming {s1}."),
				(try_end),
			(else_try),
				(assign, "$archer_formation_type", formation_none),
				(gt, reg0, 0),
				(display_message, "@Not enough archers to form {s1}, but holding."),
				(call_script, "script_formation_end", "$fplayer_team_no", grc_archers),
			(try_end),
			(assign, "$archer_formation_move_order", mordr_hold),
			(set_show_messages, 0),
			(team_give_order, "$fplayer_team_no", grc_archers, mordr_hold),
			(team_set_order_position, "$fplayer_team_no", grc_archers, pos61),
			(set_show_messages, 1),
		(try_end),
	(try_end)
  ]),
  
  
  # script_player_order_formations by motomataru
  # 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),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(call_script, "script_player_attempt_formation", grc_infantry, "$infantry_formation_type"),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(call_script, "script_player_attempt_formation", grc_cavalry, "$cavalry_formation_type"),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(call_script, "script_player_attempt_formation", grc_archers, "$archer_formation_type"),
		(try_end),
		
	(else_try),	#Follow is hold	repeated frequently
		(eq, ":forder", mordr_follow),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(assign, "$infantry_formation_move_order", mordr_follow),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(assign, "$cavalry_formation_move_order", mordr_follow),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(assign, "$archer_formation_move_order", mordr_follow),
		(try_end),
		
	(else_try),	#charge or retreat ends formation
		(this_or_next|eq, ":forder", mordr_charge),
		(eq, ":forder", mordr_retreat),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(call_script, "script_formation_end", "$fplayer_team_no", grc_infantry),
			(display_message, "@Infantry formation disassembled."),
			(assign, "$infantry_formation_type", formation_none),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_formation_end", "$fplayer_team_no", grc_cavalry),
			(display_message, "@Cavalry formation disassembled."),
			(assign, "$cavalry_formation_type", formation_none),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(call_script, "script_formation_end", "$fplayer_team_no", grc_archers),
			(display_message, "@Archer formation disassembled."),
			(assign, "$archer_formation_type", formation_none),
		(try_end),
		
	(else_try),	#dismount ends formation
		(eq, ":forder", mordr_dismount),
		(neq, "$cavalry_formation_type", formation_none),
		(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
		(call_script, "script_formation_end", "$fplayer_team_no", grc_cavalry),
		(display_message, "@Cavalry formation disassembled."),
		(assign, "$cavalry_formation_type", formation_none),

	(else_try),
		(eq, ":forder", mordr_advance),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_infantry, "$infantry_formation_type", "$infantry_space"),
			(try_begin),	#on change of orders cancel order position
				(neq, "$infantry_formation_move_order", mordr_advance),
				# (team_set_order_position, "$fplayer_team_no", grc_infantry, pos63),
				(call_script, "script_set_formation_position", "$fplayer_team_no", grc_infantry, pos63),
			(try_end),
			(call_script, "script_formation_move_position", "$fplayer_team_no", grc_infantry, pos63, "$infantry_formation_type", "$infantry_space", 1),			
			(call_script, "script_form_infantry", "$fplayer_team_no", "$fplayer_agent_no", "$infantry_space", "$infantry_formation_type"),
			(assign, "$infantry_formation_move_order", mordr_advance),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_cavalry, "$cavalry_formation_type", "$cavalry_space"),
			(try_begin),	#on change of orders cancel order position
				(neq, "$cavalry_formation_move_order", mordr_advance),
				# (team_set_order_position, "$fplayer_team_no", grc_cavalry, pos63),
				(call_script, "script_set_formation_position", "$fplayer_team_no", grc_cavalry, pos63),
			(try_end),
			(call_script, "script_formation_move_position", "$fplayer_team_no", grc_cavalry, pos63, "$cavalry_formation_type", "$cavalry_space", 1),
			(call_script, "script_form_cavalry", "$fplayer_team_no", "$fplayer_agent_no", "$cavalry_space"),
			(assign, "$cavalry_formation_move_order", mordr_advance),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_archers, "$archer_formation_type", "$archer_space"),
			(try_begin),	#on change of orders cancel order position
				(neq, "$archer_formation_move_order", mordr_advance),
				# (team_set_order_position, "$fplayer_team_no", grc_archers, pos63),
				(call_script, "script_set_formation_position", "$fplayer_team_no", grc_archers, pos63),
			(try_end),
			(call_script, "script_formation_move_position", "$fplayer_team_no", grc_archers, pos63, "$archer_formation_type", "$archer_space", 1),
			(call_script, "script_form_archers", "$fplayer_team_no", "$fplayer_agent_no", "$archer_space", "$archer_formation_type"),
			(assign, "$archer_formation_move_order", mordr_advance),
		(try_end),

	(else_try),
		(eq, ":forder", mordr_fall_back),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_infantry, "$infantry_formation_type", "$infantry_space"),
			(try_begin),	#on change of orders cancel order position
				(neq, "$infantry_formation_move_order", mordr_fall_back),
				# (team_set_order_position, "$fplayer_team_no", grc_infantry, pos63),
				(call_script, "script_set_formation_position", "$fplayer_team_no", grc_infantry, pos63),
			(try_end),
			(call_script, "script_formation_move_position", "$fplayer_team_no", grc_infantry, pos63, "$infantry_formation_type", "$infantry_space", -1),
			(call_script, "script_form_infantry", "$fplayer_team_no", "$fplayer_agent_no", "$infantry_space", "$infantry_formation_type"),
			(assign, "$infantry_formation_move_order", mordr_fall_back),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_cavalry, "$cavalry_formation_type", "$cavalry_space"),
			(try_begin),	#on change of orders cancel order position
				(neq, "$cavalry_formation_move_order", mordr_fall_back),
				# (team_set_order_position, "$fplayer_team_no", grc_cavalry, pos63),
				(call_script, "script_set_formation_position", "$fplayer_team_no", grc_cavalry, pos63),
			(try_end),
			(call_script, "script_formation_move_position", "$fplayer_team_no", grc_cavalry, pos63, "$cavalry_formation_type", "$cavalry_space", -1),
			(call_script, "script_form_cavalry", "$fplayer_team_no", "$fplayer_agent_no", "$cavalry_space"),
			(assign, "$cavalry_formation_move_order", mordr_fall_back),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_archers, "$archer_formation_type", "$archer_space"),
			(try_begin),	#on change of orders cancel order position
				(neq, "$archer_formation_move_order", mordr_fall_back),
				# (team_set_order_position, "$fplayer_team_no", grc_archers, pos63),
				(call_script, "script_set_formation_position", "$fplayer_team_no", grc_archers, pos63),
			(try_end),
			(call_script, "script_formation_move_position", "$fplayer_team_no", grc_archers, pos63, "$archer_formation_type", "$archer_space", -1),
			(call_script, "script_form_archers", "$fplayer_team_no", "$fplayer_agent_no", "$archer_space", "$archer_formation_type"),
			(assign, "$archer_formation_move_order", mordr_fall_back),
		(try_end),

	(else_try),
		(eq, ":forder", mordr_stand_closer),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(gt, "$infantry_space", 0),
			(val_sub, "$infantry_space", 1),
#			(team_get_order_position, pos1, "$fplayer_team_no", grc_infantry),
			(call_script, "script_get_formation_position", pos1, "$fplayer_team_no", grc_infantry),			
			(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_infantry),
			(assign, ":troop_count", reg0),			
			(call_script, "script_get_centering_amount", "$infantry_formation_type", ":troop_count", "$infantry_space"),
			(position_move_x, pos1, reg0),
			(call_script, "script_form_infantry", "$fplayer_team_no", "$fplayer_agent_no", "$infantry_space", "$infantry_formation_type"),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(gt, "$cavalry_space", 0),
			(val_sub, "$cavalry_space", 1),
#			(team_get_order_position, pos1, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_get_formation_position", pos1, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_form_cavalry", "$fplayer_team_no", "$fplayer_agent_no", "$cavalry_space"),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(gt, "$archer_space", 0),
			(val_sub, "$archer_space", 1),
#			(team_get_order_position, pos1, "$fplayer_team_no", grc_archers),
			(call_script, "script_get_formation_position", pos1, "$fplayer_team_no", grc_archers),
			(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_archers),
			(assign, ":troop_count", reg0),
			(call_script, "script_get_centering_amount", formation_default, ":troop_count", "$archer_space"),
			(val_mul, reg0, -1),
			(position_move_x, pos1, reg0),
			(call_script, "script_form_archers", "$fplayer_team_no", "$fplayer_agent_no", "$archer_space", "$archer_formation_type"),
		(try_end),

	(else_try),
		(eq, ":forder", mordr_spread_out),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(val_add, "$infantry_space", 1),
#			(team_get_order_position, pos1, "$fplayer_team_no", grc_infantry),
			(call_script, "script_get_formation_position", pos1, "$fplayer_team_no", grc_infantry),			
			(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_infantry),
			(assign, ":troop_count", reg0),			
			(call_script, "script_get_centering_amount", "$infantry_formation_type", ":troop_count", "$infantry_space"),
			(position_move_x, pos1, reg0),
			(call_script, "script_form_infantry", "$fplayer_team_no", "$fplayer_agent_no", "$infantry_space", "$infantry_formation_type"),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(val_add, "$cavalry_space", 1),
#			(team_get_order_position, pos1, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_get_formation_position", pos1, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_form_cavalry", "$fplayer_team_no", "$fplayer_agent_no", "$cavalry_space"),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(val_add, "$archer_space", 1),
#			(team_get_order_position, pos1, "$fplayer_team_no", grc_archers),
			(call_script, "script_get_formation_position", pos1, "$fplayer_team_no", grc_archers),
			(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_archers),
			(assign, ":troop_count", reg0),
			(call_script, "script_get_centering_amount", formation_default, ":troop_count", "$archer_space"),
			(val_mul, reg0, -1),
			(position_move_x, pos1, reg0),
			(call_script, "script_form_archers", "$fplayer_team_no", "$fplayer_agent_no", "$archer_space", "$archer_formation_type"),
		(try_end),

	(else_try),
		(eq, ":forder", mordr_stand_ground),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_infantry, "$infantry_formation_type", "$infantry_space"),
			(copy_position, pos1, pos63),
			(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_infantry),
			(assign, ":troop_count", reg0),			
			(call_script, "script_get_centering_amount", "$infantry_formation_type", ":troop_count", "$infantry_space"),
			(position_move_x, pos1, reg0),
			(call_script, "script_form_infantry", "$fplayer_team_no", "$fplayer_agent_no", "$infantry_space", "$infantry_formation_type"),
			(assign, "$infantry_formation_move_order", mordr_stand_ground),
			(call_script, "script_set_formation_position", "$fplayer_team_no", grc_infantry, pos63),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_cavalry, "$cavalry_formation_type", "$cavalry_space"),
			(copy_position, pos1, pos63),
			(call_script, "script_form_cavalry", "$fplayer_team_no", "$fplayer_agent_no", "$cavalry_space"),
			(assign, "$cavalry_formation_move_order", mordr_stand_ground),
			(call_script, "script_set_formation_position", "$fplayer_team_no", grc_cavalry, pos63),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_archers, "$archer_formation_type", "$archer_space"),
			(copy_position, pos1, pos63),
			(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_archers),
			(assign, ":troop_count", reg0),
			(call_script, "script_get_centering_amount", formation_default, ":troop_count", "$archer_space"),
			(val_mul, reg0, -1),
			(position_move_x, pos1, reg0),
			(call_script, "script_form_archers", "$fplayer_team_no", "$fplayer_agent_no", "$archer_space", "$archer_formation_type"),
			(assign, "$archer_formation_move_order", mordr_stand_ground),
			(call_script, "script_set_formation_position", "$fplayer_team_no", grc_archers, pos63),
		(try_end),			
	(try_end)
  ]),

  
# #Utilities used by formations
  # script_point_y_toward_position by motomataru
  # Input: from position, to position
  # Output: reg0 fixed point distance
  ("point_y_toward_position", [
	(store_script_param, ":from_position", 1),
	(store_script_param, ":to_position", 2),
	(position_get_x, ":dist_x_to_cosine", ":to_position"),
	(position_get_x, ":from_coord", ":from_position"),
	(val_sub, ":dist_x_to_cosine", ":from_coord"),
	(store_mul, ":sum_square", ":dist_x_to_cosine", ":dist_x_to_cosine"),
	(position_get_y, ":dist_y_to_sine", ":to_position"),
	(position_get_y, ":from_coord", ":from_position"),
	(val_sub, ":dist_y_to_sine", ":from_coord"),
	(store_mul, reg0, ":dist_y_to_sine", ":dist_y_to_sine"),
	(val_add, ":sum_square", reg0),
	(convert_from_fixed_point, ":sum_square"),
	(store_sqrt, ":distance_between", ":sum_square"),
	(convert_to_fixed_point, ":dist_x_to_cosine"),
	(val_div, ":dist_x_to_cosine", ":distance_between"),
	(convert_to_fixed_point, ":dist_y_to_sine"),
	(val_div, ":dist_y_to_sine", ":distance_between"),
	(try_begin),
		(lt, ":dist_x_to_cosine", 0),
		(assign, ":bound_a", 90),
		(assign, ":bound_b", 270),
		(assign, ":theta", 180),
	(else_try),
		(assign, ":bound_a", 90),
		(assign, ":bound_b", -90),
		(assign, ":theta", 0),
	(try_end),
	(assign, ":sine_theta", 0),	#avoid error on compile
	(convert_to_fixed_point, ":theta"),
	(convert_to_fixed_point, ":bound_a"),
	(convert_to_fixed_point, ":bound_b"),
	(try_for_range, reg0, 0, 6),	#precision 90/2exp6 (around 2 degrees)
		(store_sin, ":sine_theta", ":theta"),
		(try_begin),
			(gt, ":sine_theta", ":dist_y_to_sine"),
			(assign, ":bound_a", ":theta"),
		(else_try),
			(lt, ":sine_theta", ":dist_y_to_sine"),
			(assign, ":bound_b", ":theta"),
		(try_end),
		(store_add, ":angle_sum", ":bound_b", ":bound_a"),
		(store_div, ":theta", ":angle_sum", 2),
	(try_end),
	(convert_from_fixed_point, ":theta"),
	(position_get_rotation_around_z, reg0, ":from_position"),
	(val_sub, ":theta", reg0),
	(val_sub, ":theta", 90),	#point y-axis at destination
	(position_rotate_z, ":from_position", ":theta"),
	(assign, reg0, ":distance_between"),
  ]),


  # script_store_battlegroup_data by motomataru
  # Input: none
  # Output: sets positions and globals to track data on ALL groups in a battle
  # Globals used: pos1, reg0, reg1, positions 24-45
  ("store_battlegroup_data", [
	(assign, ":team0_leader", 0),
	(assign, ":team0_x_leader", 0),
	(assign, ":team0_y_leader", 0),
	(assign, ":team0_level_leader", 0),
	(assign, "$team0_num_infantry", 0),
	(assign, "$team0_num_archers", 0),
	(assign, "$team0_num_cavalry", 0),
	(assign, "$team0_archers_have_ammo", 1),
	(assign, "$team0_percent_ranged_throw", 0),
	(assign, "$team0_percent_cavalry_are_archers", 0),
	(assign, "$team0_level_infantry", 0),
	(assign, "$team0_level_archers", 0),
	(assign, "$team0_level_cavalry", 0),
	(assign, ":team0_x_infantry", 0),
	(assign, ":team0_x_archers", 0),
	(assign, ":team0_x_cavalry", 0),
	(assign, ":team0_y_infantry", 0),
	(assign, ":team0_y_archers", 0),
	(assign, ":team0_y_cavalry", 0),
	(assign, ":team1_leader", 0),
	(assign, ":team1_x_leader", 0),
	(assign, ":team1_y_leader", 0),
	(assign, ":team1_level_leader", 0),
	(assign, "$team1_num_infantry", 0),
	(assign, "$team1_num_archers", 0),
	(assign, "$team1_num_cavalry", 0),
	(assign, "$team1_archers_have_ammo", 1),
	(assign, "$team1_percent_ranged_throw", 0),
	(assign, "$team1_percent_cavalry_are_archers", 0),
	(assign, "$team1_level_infantry", 0),
	(assign, "$team1_level_archers", 0),
	(assign, "$team1_level_cavalry", 0),
	(assign, ":team1_x_infantry", 0),
	(assign, ":team1_x_archers", 0),
	(assign, ":team1_x_cavalry", 0),
	(assign, ":team1_y_infantry", 0),
	(assign, ":team1_y_archers", 0),
	(assign, ":team1_y_cavalry", 0),
	(assign, ":team2_leader", 0),
	(assign, ":team2_x_leader", 0),
	(assign, ":team2_y_leader", 0),
	(assign, ":team2_level_leader", 0),
	(assign, "$team2_num_infantry", 0),
	(assign, "$team2_num_archers", 0),
	(assign, "$team2_num_cavalry", 0),
	(assign, "$team2_archers_have_ammo", 1),
	(assign, "$team2_percent_ranged_throw", 0),
	(assign, "$team2_percent_cavalry_are_archers", 0),
	(assign, "$team2_level_infantry", 0),
	(assign, "$team2_level_archers", 0),
	(assign, "$team2_level_cavalry", 0),
	(assign, ":team2_x_infantry", 0),
	(assign, ":team2_x_archers", 0),
	(assign, ":team2_x_cavalry", 0),
	(assign, ":team2_y_infantry", 0),
	(assign, ":team2_y_archers", 0),
	(assign, ":team2_y_cavalry", 0),
	(assign, ":team3_leader", 0),
	(assign, ":team3_x_leader", 0),
	(assign, ":team3_y_leader", 0),
	(assign, ":team3_level_leader", 0),
	(assign, "$team3_num_infantry", 0),
	(assign, "$team3_num_archers", 0),
	(assign, "$team3_num_cavalry", 0),
	(assign, "$team3_archers_have_ammo", 1),
	(assign, "$team3_percent_ranged_throw", 0),
	(assign, "$team3_percent_cavalry_are_archers", 0),
	(assign, "$team3_level_infantry", 0),
	(assign, "$team3_level_archers", 0),
	(assign, "$team3_level_cavalry", 0),
	(assign, ":team3_x_infantry", 0),
	(assign, ":team3_x_archers", 0),
	(assign, ":team3_x_cavalry", 0),
	(assign, ":team3_y_infantry", 0),
	(assign, ":team3_y_archers", 0),
	(assign, ":team3_y_cavalry", 0),
	(assign, "$teamp_num_group3", 0),
	(assign, "$teamp_level_group3", 0),
	(assign, ":teamp_x_group3", 0),
	(assign, ":teamp_y_group3", 0),
	(assign, "$teamp_num_group4", 0),
	(assign, "$teamp_level_group4", 0),
	(assign, ":teamp_x_group4", 0),
	(assign, ":teamp_y_group4", 0),
	(assign, "$teamp_num_group5", 0),
	(assign, "$teamp_level_group5", 0),
	(assign, ":teamp_x_group5", 0),
	(assign, ":teamp_y_group5", 0),
	(assign, "$teamp_num_group6", 0),
	(assign, "$teamp_level_group6", 0),
	(assign, ":teamp_x_group6", 0),
	(assign, ":teamp_y_group6", 0),
	(assign, "$teamp_num_group7", 0),
	(assign, "$teamp_level_group7", 0),
	(assign, ":teamp_x_group7", 0),
	(assign, ":teamp_y_group7", 0),
	(assign, "$teamp_num_group8", 0),
	(assign, "$teamp_level_group8", 0),
	(assign, ":teamp_x_group8", 0),
	(assign, ":teamp_y_group8", 0),

	(try_for_agents, ":cur_agent"),
		(agent_is_alive, ":cur_agent"),      
		(agent_is_human, ":cur_agent"), 
		(agent_slot_eq, ":cur_agent", slot_agent_is_running_away, 0),
		(agent_get_team, ":bgteam", ":cur_agent"),
		(call_script, "script_classify_agent", ":cur_agent"),
		(assign, ":bgroup", reg0),
		(agent_get_troop_id, ":cur_troop", ":cur_agent"),
		(store_character_level, ":cur_level", ":cur_troop"),
		(agent_get_ammo, ":cur_ammo", ":cur_agent", 0),
		(agent_get_wielded_item, reg0, ":cur_agent", 0),
		(try_begin),
			(lt, reg0, 0),
			(assign, ":cur_weapon_type", 0),
			(assign, ":cur_weapon_length", 0),
		(else_try),
			(item_get_type, ":cur_weapon_type", reg0),
			(try_begin),
				(eq, ":cur_weapon_type", itp_type_polearm),
				(store_mul, ":cur_weapon_length", Weapon_Length_Proxy, 2),
			(else_try),
				(this_or_next|eq, ":cur_weapon_type", itp_type_one_handed_wpn),
				(eq, ":cur_weapon_type", itp_type_two_handed_wpn),
				(assign, ":cur_weapon_length", Weapon_Length_Proxy),
			(else_try),
				(assign, ":cur_weapon_length", 0),
			(try_end),
		(try_end),
		(agent_get_position, pos1, ":cur_agent"),
		(position_get_x, ":x_value", pos1),
		(position_get_y, ":y_value", pos1),
		(try_begin),
			(eq, ":bgroup", grc_infantry),	#AKA battle group 0
			(try_begin),
				(eq, ":bgteam", 0),
				(val_add, "$team0_num_infantry", 1),
				(val_add, ":team0_x_infantry", ":x_value"),
				(val_add, ":team0_y_infantry", ":y_value"),
				(val_add, "$team0_level_infantry", ":cur_level"),
				(val_add, "$team0_weapon_length_infantry", ":cur_weapon_length"),
			(else_try),
				(eq, ":bgteam", 1),
				(val_add, "$team1_num_infantry", 1),
				(val_add, ":team1_x_infantry", ":x_value"),
				(val_add, ":team1_y_infantry", ":y_value"),
				(val_add, "$team1_level_infantry", ":cur_level"),
				(val_add, "$team1_weapon_length_infantry", ":cur_weapon_length"),
			(else_try),
				(eq, ":bgteam", 2),
				(val_add, "$team2_num_infantry", 1),
				(val_add, ":team2_x_infantry", ":x_value"),
				(val_add, ":team2_y_infantry", ":y_value"),
				(val_add, "$team2_level_infantry", ":cur_level"),
				(val_add, "$team2_weapon_length_infantry", ":cur_weapon_length"),
			(else_try),
				(eq, ":bgteam", 3),
				(val_add, "$team3_num_infantry", 1),
				(val_add, ":team3_x_infantry", ":x_value"),
				(val_add, ":team3_y_infantry", ":y_value"),
				(val_add, "$team3_level_infantry", ":cur_level"),
				(val_add, "$team3_weapon_length_infantry", ":cur_weapon_length"),
			(try_end),
		(else_try),
			(eq, ":bgroup", grc_archers),	#AKA battle group 1
			(try_begin),
				(eq, ":bgteam", 0),
				(val_add, "$team0_num_archers", 1),
				(val_add, ":team0_x_archers", ":x_value"),
				(val_add, ":team0_y_archers", ":y_value"),
				(val_add, "$team0_level_archers", ":cur_level"),
				(val_add, "$team0_weapon_length_archers", ":cur_weapon_length"),
				(try_begin),
					(le, ":cur_ammo", 0),
					(assign, "$team0_archers_have_ammo", 0),
				(try_end),
				(try_begin),
					(eq, ":cur_weapon_type", itp_type_thrown),
					(val_add, "$team0_percent_ranged_throw", 1),
				(try_end),
			(else_try),
				(eq, ":bgteam", 1),
				(val_add, "$team1_num_archers", 1),
				(val_add, ":team1_x_archers", ":x_value"),
				(val_add, ":team1_y_archers", ":y_value"),
				(val_add, "$team1_level_archers", ":cur_level"),
				(val_add, "$team1_weapon_length_archers", ":cur_weapon_length"),
				(try_begin),
					(le, ":cur_ammo", 0),
					(assign, "$team1_archers_have_ammo", 0),
				(try_end),
				(try_begin),
					(eq, ":cur_weapon_type", itp_type_thrown),
					(val_add, "$team1_percent_ranged_throw", 1),
				(try_end),
			(else_try),
				(eq, ":bgteam", 2),
				(val_add, "$team2_num_archers", 1),
				(val_add, ":team2_x_archers", ":x_value"),
				(val_add, ":team2_y_archers", ":y_value"),
				(val_add, "$team2_level_archers", ":cur_level"),
				(val_add, "$team2_weapon_length_archers", ":cur_weapon_length"),
				(try_begin),
					(le, ":cur_ammo", 0),
					(assign, "$team2_archers_have_ammo", 0),
				(try_end),
				(try_begin),
					(eq, ":cur_weapon_type", itp_type_thrown),
					(val_add, "$team2_percent_ranged_throw", 1),
				(try_end),
			(else_try),
				(eq, ":bgteam", 3),
				(val_add, "$team3_num_archers", 1),
				(val_add, ":team3_x_archers", ":x_value"),
				(val_add, ":team3_y_archers", ":y_value"),
				(val_add, "$team3_level_archers", ":cur_level"),
				(val_add, "$team3_weapon_length_archers", ":cur_weapon_length"),
				(try_begin),
					(le, ":cur_ammo", 0),
					(assign, "$team3_archers_have_ammo", 0),
				(try_end),
				(try_begin),
					(eq, ":cur_weapon_type", itp_type_thrown),
					(val_add, "$team3_percent_ranged_throw", 1),
				(try_end),
			(try_end),
		(else_try),
			(eq, ":bgroup", grc_cavalry),	#AKA battle group 2
			(try_begin),
				(eq, ":bgteam", 0),
				(val_add, "$team0_num_cavalry", 1),
				(val_add, ":team0_x_cavalry", ":x_value"),
				(val_add, ":team0_y_cavalry", ":y_value"),
				(val_add, "$team0_level_cavalry", ":cur_level"),
				(val_add, "$team0_weapon_length_cavalry", ":cur_weapon_length"),
				(try_begin),
					(gt, ":cur_ammo", 0),
					(val_add, "$team0_percent_cavalry_are_archers", 1),
				(try_end),
			(else_try),
				(eq, ":bgteam", 1),
				(val_add, "$team1_num_cavalry", 1),
				(val_add, ":team1_x_cavalry", ":x_value"),
				(val_add, ":team1_y_cavalry", ":y_value"),
				(val_add, "$team1_level_cavalry", ":cur_level"),
				(val_add, "$team1_weapon_length_cavalry", ":cur_weapon_length"),
				(try_begin),
					(gt, ":cur_ammo", 0),
					(val_add, "$team1_percent_cavalry_are_archers", 1),
				(try_end),
			(else_try),
				(eq, ":bgteam", 2),
				(val_add, "$team2_num_cavalry", 1),
				(val_add, ":team2_x_cavalry", ":x_value"),
				(val_add, ":team2_y_cavalry", ":y_value"),
				(val_add, "$team2_level_cavalry", ":cur_level"),
				(val_add, "$team2_weapon_length_cavalry", ":cur_weapon_length"),
				(try_begin),
					(gt, ":cur_ammo", 0),
					(val_add, "$team2_percent_cavalry_are_archers", 1),
				(try_end),
			(else_try),
				(eq, ":bgteam", 3),
				(val_add, "$team3_num_cavalry", 1),
				(val_add, ":team3_x_cavalry", ":x_value"),
				(val_add, ":team3_y_cavalry", ":y_value"),
				(val_add, "$team3_level_cavalry", ":cur_level"),
				(val_add, "$team3_weapon_length_cavalry", ":cur_weapon_length"),
				(try_begin),
					(gt, ":cur_ammo", 0),
					(val_add, "$team3_percent_cavalry_are_archers", 1),
				(try_end),
			(try_end),
		(else_try),
			(eq, ":bgroup", 3),
			(val_add, "$teamp_num_group3", 1),
			(val_add, ":teamp_x_group3", ":x_value"),
			(val_add, ":teamp_y_group3", ":y_value"),
			(val_add, "$teamp_level_group3", ":cur_level"),
			(val_add, "$teamp_weapon_length_group3", ":cur_weapon_length"),
		(else_try),
			(eq, ":bgroup", 4),
			(val_add, "$teamp_num_group4", 1),
			(val_add, ":teamp_x_group4", ":x_value"),
			(val_add, ":teamp_y_group4", ":y_value"),
			(val_add, "$teamp_level_group4", ":cur_level"),
			(val_add, "$teamp_weapon_length_group4", ":cur_weapon_length"),
		(else_try),
			(eq, ":bgroup", 5),
			(val_add, "$teamp_num_group5", 1),
			(val_add, ":teamp_x_group5", ":x_value"),
			(val_add, ":teamp_y_group5", ":y_value"),
			(val_add, "$teamp_level_group5", ":cur_level"),
			(val_add, "$teamp_weapon_length_group5", ":cur_weapon_length"),
		(else_try),
			(eq, ":bgroup", 6),
			(val_add, "$teamp_num_group6", 1),
			(val_add, ":teamp_x_group6", ":x_value"),
			(val_add, ":teamp_y_group6", ":y_value"),
			(val_add, "$teamp_level_group6", ":cur_level"),
			(val_add, "$teamp_weapon_length_group6", ":cur_weapon_length"),
		(else_try),
			(eq, ":bgroup", 7),
			(val_add, "$teamp_num_group7", 1),
			(val_add, ":teamp_x_group7", ":x_value"),
			(val_add, ":teamp_y_group7", ":y_value"),
			(val_add, "$teamp_level_group7", ":cur_level"),
			(val_add, "$teamp_weapon_length_group7", ":cur_weapon_length"),
		(else_try),
			(eq, ":bgroup", 8),
			(val_add, "$teamp_num_group8", 1),
			(val_add, ":teamp_x_group8", ":x_value"),
			(val_add, ":teamp_y_group8", ":y_value"),
			(val_add, "$teamp_level_group8", ":cur_level"),
			(val_add, "$teamp_weapon_length_group8", ":cur_weapon_length"),
		(else_try),
			(eq, ":bgroup", -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_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_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_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_level_leader", ":cur_level"),
			(try_end),
		(try_end),
	(try_end),

	#calculate team sizes, sum positions
	(store_add, "$team0_size", "$team0_num_infantry", "$team0_num_archers"),
	(val_add, "$team0_size", "$team0_num_cavalry"),
	(val_add, "$team0_size", ":team0_leader"),
	(store_add, ":team0_x", ":team0_x_infantry", ":team0_x_archers"),
	(val_add, ":team0_x", ":team0_x_cavalry"),
	(val_add, ":team0_x", ":team0_x_leader"),
	(store_add, ":team0_y", ":team0_y_infantry", ":team0_y_archers"),
	(val_add, ":team0_y", ":team0_y_cavalry"),
	(val_add, ":team0_y", ":team0_y_leader"),
	(store_add, "$team0_level", "$team0_level_infantry", "$team0_level_archers"),
	(val_add, "$team0_level", "$team0_level_cavalry"),
	(val_add, "$team0_level", ":team0_level_leader"),

	(store_add, "$team1_size", "$team1_num_infantry", "$team1_num_archers"),
	(val_add, "$team1_size", "$team1_num_cavalry"),
	(val_add, "$team1_size", ":team1_leader"),
	(store_add, ":team1_x", ":team1_x_infantry", ":team1_x_archers"),
	(val_add, ":team1_x", ":team1_x_cavalry"),
	(val_add, ":team1_x", ":team1_x_leader"),
	(store_add, ":team1_y", ":team1_y_infantry", ":team1_y_archers"),
	(val_add, ":team1_y", ":team1_y_cavalry"),
	(val_add, ":team1_y", ":team1_y_leader"),
	(store_add, "$team1_level", "$team1_level_infantry", "$team1_level_archers"),
	(val_add, "$team1_level", "$team1_level_cavalry"),
	(val_add, "$team1_level", ":team1_level_leader"),

	(store_add, "$team2_size", "$team2_num_infantry", "$team2_num_archers"),
	(val_add, "$team2_size", "$team2_num_cavalry"),
	(val_add, "$team2_size", ":team2_leader"),
	(store_add, ":team2_x", ":team2_x_infantry", ":team2_x_archers"),
	(val_add, ":team2_x", ":team2_x_cavalry"),
	(val_add, ":team2_x", ":team2_x_leader"),
	(store_add, ":team2_y", ":team2_y_infantry", ":team2_y_archers"),
	(val_add, ":team2_y", ":team2_y_cavalry"),
	(val_add, ":team2_y", ":team2_y_leader"),
	(store_add, "$team2_level", "$team2_level_infantry", "$team2_level_archers"),
	(val_add, "$team2_level", "$team2_level_cavalry"),
	(val_add, "$team2_level", ":team2_level_leader"),

	(store_add, "$team3_size", "$team3_num_infantry", "$team3_num_archers"),
	(val_add, "$team3_size", "$team3_num_cavalry"),
	(val_add, "$team3_size", ":team3_leader"),
	(store_add, ":team3_x", ":team3_x_infantry", ":team3_x_archers"),
	(val_add, ":team3_x", ":team3_x_cavalry"),
	(val_add, ":team3_x", ":team3_x_leader"),
	(store_add, ":team3_y", ":team3_y_infantry", ":team3_y_archers"),
	(val_add, ":team3_y", ":team3_y_cavalry"),
	(val_add, ":team3_y", ":team3_y_leader"),
	(store_add, "$team3_level", "$team3_level_infantry", "$team3_level_archers"),
	(val_add, "$team3_level", "$team3_level_cavalry"),
	(val_add, "$team3_level", ":team3_level_leader"),

	(store_add, ":additional_troops", "$teamp_num_group3", "$teamp_num_group4"),
	(val_add, ":additional_troops", "$teamp_num_group5"),
	(val_add, ":additional_troops", "$teamp_num_group6"),
	(val_add, ":additional_troops", "$teamp_num_group7"),
	(val_add, ":additional_troops", "$teamp_num_group8"),
	(store_add, ":additional_level", "$teamp_level_group3", "$teamp_level_group4"),
	(val_add, ":additional_level", "$teamp_level_group5"),
	(val_add, ":additional_level", "$teamp_level_group6"),
	(val_add, ":additional_level", "$teamp_level_group7"),
	(val_add, ":additional_level", "$teamp_level_group8"),
	(try_begin),
		(eq, "$fplayer_team_no", 0),
		(val_add, "$team0_size", ":additional_troops"),
		(val_add, "$team0_level", ":additional_level"),
	(else_try),
		(eq, "$fplayer_team_no", 1),
		(val_add, "$team1_size", ":additional_troops"),
		(val_add, "$team1_level", ":additional_level"),
	(else_try),
		(eq, "$fplayer_team_no", 2),
		(val_add, "$team2_size", ":additional_troops"),
		(val_add, "$team2_level", ":additional_level"),
	(else_try),
		(eq, "$fplayer_team_no", 3),
		(val_add, "$team3_size", ":additional_troops"),
		(val_add, "$team3_level", ":additional_level"),
	(try_end),
	
	#calculate team averages
	(init_position, Team0_Average_Pos),
	(try_begin),
		(gt, "$team0_size", 0),
		(val_div, ":team0_x", "$team0_size"),
		(position_set_x, Team0_Average_Pos, ":team0_x"),
		(val_div, ":team0_y", "$team0_size"),
		(position_set_y, Team0_Average_Pos, ":team0_y"),
		(position_set_z_to_ground_level, Team0_Average_Pos),
		(val_div, "$team0_level", "$team0_size"),
	(try_end),
	(init_position, Team1_Average_Pos),
	(try_begin),
		(gt, "$team1_size", 0),
		(val_div, ":team1_x", "$team1_size"),
		(position_set_x, Team1_Average_Pos, ":team1_x"),
		(val_div, ":team1_y", "$team1_size"),
		(position_set_y, Team1_Average_Pos, ":team1_y"),
		(position_set_z_to_ground_level, Team1_Average_Pos),
		(val_div, "$team1_level", "$team1_size"),
	(try_end),
	(init_position, Team2_Average_Pos),
	(try_begin),
		(gt, "$team2_size", 0),
		(val_div, ":team2_x", "$team2_size"),
		(position_set_x, Team2_Average_Pos, ":team2_x"),
		(val_div, ":team2_y", "$team2_size"),
		(position_set_y, Team2_Average_Pos, ":team2_y"),
		(position_set_z_to_ground_level, Team2_Average_Pos),
		(val_div, "$team2_level", "$team2_size"),
	(try_end),
	(init_position, Team3_Average_Pos),
	(try_begin),
		(gt, "$team3_size", 0),
		(val_div, ":team3_x", "$team3_size"),
		(position_set_x, Team3_Average_Pos, ":team3_x"),
		(val_div, ":team3_y", "$team3_size"),
		(position_set_y, Team3_Average_Pos, ":team3_y"),
		(position_set_z_to_ground_level, Team3_Average_Pos),
		(val_div, "$team3_level", "$team3_size"),
	(try_end),


	#calculate battle group averages
	(init_position, Team0_Infantry_Pos),
	(try_begin),
		(gt, "$team0_num_infantry", 0),
		(val_div, ":team0_x_infantry", "$team0_num_infantry"),
		(position_set_x, Team0_Infantry_Pos, ":team0_x_infantry"),
		(val_div, ":team0_y_infantry", "$team0_num_infantry"),
		(position_set_y, Team0_Infantry_Pos, ":team0_y_infantry"),
		(position_set_z_to_ground_level, Team0_Infantry_Pos),
		(val_div, "$team0_level_infantry", "$team0_num_infantry"),
		(val_div, "$team0_weapon_length_infantry", "$team0_num_infantry"),
	(try_end),
	(init_position, Team1_Infantry_Pos),
	(try_begin),
		(gt, "$team1_num_infantry", 0),
		(val_div, ":team1_x_infantry", "$team1_num_infantry"),
		(position_set_x, Team1_Infantry_Pos, ":team1_x_infantry"),
		(val_div, ":team1_y_infantry", "$team1_num_infantry"),
		(position_set_y, Team1_Infantry_Pos, ":team1_y_infantry"),
		(position_set_z_to_ground_level, Team1_Infantry_Pos),
		(val_div, "$team1_level_infantry", "$team1_num_infantry"),
		(val_div, "$team1_weapon_length_infantry", "$team1_num_infantry"),
	(try_end),
	(init_position, Team2_Infantry_Pos),
	(try_begin),
		(gt, "$team2_num_infantry", 0),
		(val_div, ":team2_x_infantry", "$team2_num_infantry"),
		(position_set_x, Team2_Infantry_Pos, ":team2_x_infantry"),
		(val_div, ":team2_y_infantry", "$team2_num_infantry"),
		(position_set_y, Team2_Infantry_Pos, ":team2_y_infantry"),
		(position_set_z_to_ground_level, Team2_Infantry_Pos),
		(val_div, "$team2_level_infantry", "$team2_num_infantry"),
		(val_div, "$team2_weapon_length_infantry", "$team2_num_infantry"),
	(try_end),
	(init_position, Team3_Infantry_Pos),
	(try_begin),
		(gt, "$team3_num_infantry", 0),
		(val_div, ":team3_x_infantry", "$team3_num_infantry"),
		(position_set_x, Team3_Infantry_Pos, ":team3_x_infantry"),
		(val_div, ":team3_y_infantry", "$team3_num_infantry"),
		(position_set_y, Team3_Infantry_Pos, ":team3_y_infantry"),
		(position_set_z_to_ground_level, Team3_Infantry_Pos),
		(val_div, "$team3_level_infantry", "$team3_num_infantry"),
		(val_div, "$team3_weapon_length_infantry", "$team3_num_infantry"),
	(try_end),
	(init_position, Team0_Archers_Pos),
	(try_begin),
		(gt, "$team0_num_archers", 0),
		(val_div, ":team0_x_archers", "$team0_num_archers"),
		(position_set_x, Team0_Archers_Pos, ":team0_x_archers"),
		(val_div, ":team0_y_archers", "$team0_num_archers"),
		(position_set_y, Team0_Archers_Pos, ":team0_y_archers"),
		(position_set_z_to_ground_level, Team0_Archers_Pos),
		(val_div, "$team0_level_archers", "$team0_num_archers"),
		(val_div, "$team0_weapon_length_archers", "$team0_num_archers"),
		(val_mul, "$team0_percent_ranged_throw", 100),
		(val_div, "$team0_percent_ranged_throw", "$team0_num_archers"),
	(try_end),
	(init_position, Team1_Archers_Pos),
	(try_begin),
		(gt, "$team1_num_archers", 0),
		(val_div, ":team1_x_archers", "$team1_num_archers"),
		(position_set_x, Team1_Archers_Pos, ":team1_x_archers"),
		(val_div, ":team1_y_archers", "$team1_num_archers"),
		(position_set_y, Team1_Archers_Pos, ":team1_y_archers"),
		(position_set_z_to_ground_level, Team1_Archers_Pos),
		(val_div, "$team1_level_archers", "$team1_num_archers"),
		(val_div, "$team1_weapon_length_archers", "$team1_num_archers"),
		(val_mul, "$team1_percent_ranged_throw", 100),
		(val_div, "$team1_percent_ranged_throw", "$team1_num_archers"),
	(try_end),
	(init_position, Team2_Archers_Pos),
	(try_begin),
		(gt, "$team2_num_archers", 0),
		(val_div, ":team2_x_archers", "$team2_num_archers"),
		(position_set_x, Team2_Archers_Pos, ":team2_x_archers"),
		(val_div, ":team2_y_archers", "$team2_num_archers"),
		(position_set_y, Team2_Archers_Pos, ":team2_y_archers"),
		(position_set_z_to_ground_level, Team2_Archers_Pos),
		(val_div, "$team2_level_archers", "$team2_num_archers"),
		(val_div, "$team2_weapon_length_archers", "$team2_num_archers"),
		(val_mul, "$team2_percent_ranged_throw", 100),
		(val_div, "$team2_percent_ranged_throw", "$team2_num_archers"),
	(try_end),
	(init_position, Team3_Archers_Pos),
	(try_begin),
		(gt, "$team3_num_archers", 0),
		(val_div, ":team3_x_archers", "$team3_num_archers"),
		(position_set_x, Team3_Archers_Pos, ":team3_x_archers"),
		(val_div, ":team3_y_archers", "$team3_num_archers"),
		(position_set_y, Team3_Archers_Pos, ":team3_y_archers"),
		(position_set_z_to_ground_level, Team3_Archers_Pos),
		(val_div, "$team3_level_archers", "$team3_num_archers"),
		(val_div, "$team3_weapon_length_archers", "$team3_num_archers"),
		(val_mul, "$team3_percent_ranged_throw", 100),
		(val_div, "$team3_percent_ranged_throw", "$team3_num_archers"),
	(try_end),
	(init_position, Team0_Cavalry_Pos),
	(try_begin),
		(gt, "$team0_num_cavalry", 0),
		(val_div, ":team0_x_cavalry", "$team0_num_cavalry"),
		(position_set_x, Team0_Cavalry_Pos, ":team0_x_cavalry"),
		(val_div, ":team0_y_cavalry", "$team0_num_cavalry"),
		(position_set_y, Team0_Cavalry_Pos, ":team0_y_cavalry"),
		(position_set_z_to_ground_level, Team0_Cavalry_Pos),
		(val_div, "$team0_level_cavalry", "$team0_num_cavalry"),
		(val_div, "$team0_weapon_length_cavalry", "$team0_num_cavalry"),
		(val_mul, "$team0_percent_cavalry_are_archers", 100),
		(val_div, "$team0_percent_cavalry_are_archers", "$team0_num_cavalry"),
	(try_end),
	(init_position, Team1_Cavalry_Pos),
	(try_begin),
		(gt, "$team1_num_cavalry", 0),
		(val_div, ":team1_x_cavalry", "$team1_num_cavalry"),
		(position_set_x, Team1_Cavalry_Pos, ":team1_x_cavalry"),
		(val_div, ":team1_y_cavalry", "$team1_num_cavalry"),
		(position_set_y, Team1_Cavalry_Pos, ":team1_y_cavalry"),
		(position_set_z_to_ground_level, Team1_Cavalry_Pos),
		(val_div, "$team1_level_cavalry", "$team1_num_cavalry"),
		(val_div, "$team1_weapon_length_cavalry", "$team1_num_cavalry"),
		(val_mul, "$team1_percent_cavalry_are_archers", 100),
		(val_div, "$team1_percent_cavalry_are_archers", "$team1_num_cavalry"),
	(try_end),
	(init_position, Team2_Cavalry_Pos),
	(try_begin),
		(gt, "$team2_num_cavalry", 0),
		(val_div, ":team2_x_cavalry", "$team2_num_cavalry"),
		(position_set_x, Team2_Cavalry_Pos, ":team2_x_cavalry"),
		(val_div, ":team2_y_cavalry", "$team2_num_cavalry"),
		(position_set_y, Team2_Cavalry_Pos, ":team2_y_cavalry"),
		(position_set_z_to_ground_level, Team2_Cavalry_Pos),
		(val_div, "$team2_level_cavalry", "$team2_num_cavalry"),
		(val_div, "$team2_weapon_length_cavalry", "$team2_num_cavalry"),
		(val_mul, "$team2_percent_cavalry_are_archers", 100),
		(val_div, "$team2_percent_cavalry_are_archers", "$team2_num_cavalry"),
	(try_end),
	(init_position, Team3_Cavalry_Pos),
	(try_begin),
		(gt, "$team3_num_cavalry", 0),
		(val_div, ":team3_x_cavalry", "$team3_num_cavalry"),
		(position_set_x, Team3_Cavalry_Pos, ":team3_x_cavalry"),
		(val_div, ":team3_y_cavalry", "$team3_num_cavalry"),
		(position_set_y, Team3_Cavalry_Pos, ":team3_y_cavalry"),
		(position_set_z_to_ground_level, Team3_Cavalry_Pos),
		(val_div, "$team3_level_cavalry", "$team3_num_cavalry"),
		(val_div, "$team3_weapon_length_cavalry", "$team3_num_cavalry"),
		(val_mul, "$team3_percent_cavalry_are_archers", 100),
		(val_div, "$team3_percent_cavalry_are_archers", "$team3_num_cavalry"),
	(try_end),

	(init_position, Player_Battle_Group3_Pos),
	(try_begin),
		(gt, "$teamp_num_group3", 0),
		(val_div, ":teamp_x_group3", "$teamp_num_group3"),
		(position_set_x, Player_Battle_Group3_Pos, ":teamp_x_group3"),
		(val_div, ":teamp_y_group3", "$teamp_num_group3"),
		(position_set_y, Player_Battle_Group3_Pos, ":teamp_y_group3"),
		(position_set_z_to_ground_level, Player_Battle_Group3_Pos),
		(val_div, "$teamp_level_group3", "$teamp_num_group3"),
		(val_div, "$teamp_weapon_length_group3", "$teamp_num_group3"),
	(try_end),
	(init_position, Player_Battle_Group4_Pos),
	(try_begin),
		(gt, "$teamp_num_group4", 0),
		(val_div, ":teamp_x_group4", "$teamp_num_group4"),
		(position_set_x, Player_Battle_Group4_Pos, ":teamp_x_group4"),
		(val_div, ":teamp_y_group4", "$teamp_num_group4"),
		(position_set_y, Player_Battle_Group4_Pos, ":teamp_y_group4"),
		(position_set_z_to_ground_level, Player_Battle_Group4_Pos),
		(val_div, "$teamp_level_group4", "$teamp_num_group4"),
		(val_div, "$teamp_weapon_length_group4", "$teamp_num_group4"),
	(try_end),
	(init_position, Player_Battle_Group5_Pos),
	(try_begin),
		(gt, "$teamp_num_group5", 0),
		(val_div, ":teamp_x_group5", "$teamp_num_group5"),
		(position_set_x, Player_Battle_Group5_Pos, ":teamp_x_group5"),
		(val_div, ":teamp_y_group5", "$teamp_num_group5"),
		(position_set_y, Player_Battle_Group5_Pos, ":teamp_y_group5"),
		(position_set_z_to_ground_level, Player_Battle_Group5_Pos),
		(val_div, "$teamp_level_group5", "$teamp_num_group5"),
		(val_div, "$teamp_weapon_length_group5", "$teamp_num_group5"),
	(try_end),
	(init_position, Player_Battle_Group6_Pos),
	(try_begin),
		(gt, "$teamp_num_group6", 0),
		(val_div, ":teamp_x_group6", "$teamp_num_group6"),
		(position_set_x, Player_Battle_Group6_Pos, ":teamp_x_group6"),
		(val_div, ":teamp_y_group6", "$teamp_num_group6"),
		(position_set_y, Player_Battle_Group6_Pos, ":teamp_y_group6"),
		(position_set_z_to_ground_level, Player_Battle_Group6_Pos),
		(val_div, "$teamp_level_group6", "$teamp_num_group6"),
		(val_div, "$teamp_weapon_length_group6", "$teamp_num_group6"),
	(try_end),
	(init_position, Player_Battle_Group7_Pos),
	(try_begin),
		(gt, "$teamp_num_group7", 0),
		(val_div, ":teamp_x_group7", "$teamp_num_group7"),
		(position_set_x, Player_Battle_Group7_Pos, ":teamp_x_group7"),
		(val_div, ":teamp_y_group7", "$teamp_num_group7"),
		(position_set_y, Player_Battle_Group7_Pos, ":teamp_y_group7"),
		(position_set_z_to_ground_level, Player_Battle_Group7_Pos),
		(val_div, "$teamp_level_group7", "$teamp_num_group7"),
		(val_div, "$teamp_weapon_length_group7", "$teamp_num_group7"),
	(try_end),
	(init_position, Player_Battle_Group8_Pos),
	(try_begin),
		(gt, "$teamp_num_group8", 0),
		(val_div, ":teamp_x_group8", "$teamp_num_group8"),
		(position_set_x, Player_Battle_Group8_Pos, ":teamp_x_group8"),
		(val_div, ":teamp_y_group8", "$teamp_num_group8"),
		(position_set_y, Player_Battle_Group8_Pos, ":teamp_y_group8"),
		(position_set_z_to_ground_level, Player_Battle_Group8_Pos),
		(val_div, "$teamp_level_group8", "$teamp_num_group8"),
		(val_div, "$teamp_weapon_length_group8", "$teamp_num_group8"),
	(try_end),
	]),


  # script_battlegroup_get_size by motomataru
  # Input: team, battle group (troop class)
  # Output:	reg0 contains size
  #			size of whole team if "troop class" input NOT set to 0-8
  # NB: Assumes that battle groups beyond 2 are PLAYER team
  ("battlegroup_get_size", [
	(store_script_param, ":bgteam", 1),
	(store_script_param, ":bgroup", 2),
	(try_begin),
		(eq, ":bgroup", grc_infantry),	#AKA battle group 0
		(try_begin),
			(eq, ":bgteam", 0),
			(assign, reg0, "$team0_num_infantry"),
		(else_try),
			(eq, ":bgteam", 1),
			(assign, reg0, "$team1_num_infantry"),
		(else_try),
			(eq, ":bgteam", 2),
			(assign, reg0, "$team2_num_infantry"),
		(else_try),
			(eq, ":bgteam", 3),
			(assign, reg0, "$team3_num_infantry"),
		(try_end),
	(else_try),
		(eq, ":bgroup", grc_archers),	#AKA battle group 1
		(try_begin),
			(eq, ":bgteam", 0),
			(assign, reg0, "$team0_num_archers"),
		(else_try),
			(eq, ":bgteam", 1),
			(assign, reg0, "$team1_num_archers"),
		(else_try),
			(eq, ":bgteam", 2),
			(assign, reg0, "$team2_num_archers"),
		(else_try),
			(eq, ":bgteam", 3),
			(assign, reg0, "$team3_num_archers"),
		(try_end),
	(else_try),
		(eq, ":bgroup", grc_cavalry),	#AKA battle group 2
		(try_begin),
			(eq, ":bgteam", 0),
			(assign, reg0, "$team0_num_cavalry"),
		(else_try),
			(eq, ":bgteam", 1),
			(assign, reg0, "$team1_num_cavalry"),
		(else_try),
			(eq, ":bgteam", 2),
			(assign, reg0, "$team2_num_cavalry"),
		(else_try),
			(eq, ":bgteam", 3),
			(assign, reg0, "$team3_num_cavalry"),
		(try_end),
	(else_try),
		(eq, ":bgroup", 3),
		(assign, reg0, "$teamp_num_group3"),
	(else_try),
		(eq, ":bgroup", 4),
		(assign, reg0, "$teamp_num_group4"),
	(else_try),
		(eq, ":bgroup", 5),
		(assign, reg0, "$teamp_num_group5"),
	(else_try),
		(eq, ":bgroup", 6),
		(assign, reg0, "$teamp_num_group6"),
	(else_try),
		(eq, ":bgroup", 7),
		(assign, reg0, "$teamp_num_group7"),
	(else_try),
		(eq, ":bgroup", 8),
		(assign, reg0, "$teamp_num_group8"),
	(else_try),	#undefined battle group from here on
		(eq, ":bgteam", 0),
		(assign, reg0, "$team0_size"),
	(else_try),
		(eq, ":bgteam", 1),
		(assign, reg0, "$team1_size"),
	(else_try),
		(eq, ":bgteam", 2),
		(assign, reg0, "$team2_size"),
	(else_try),
		(eq, ":bgteam", 3),
		(assign, reg0, "$team3_size"),
	(try_end),
  ]),	

 
  # 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_for_range, ":other_team", 0, 4),
		(teams_are_enemies, ":other_team", ":team_no"),
		(try_begin),
			(eq, ":troop_type", grc_everyone),
			(call_script, "script_battlegroup_get_size", ":other_team", grc_everyone),
			(assign, ":team_size", reg0),
			(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),
			(assign, ":team_size", 0),
			(try_begin),
				(eq, ":other_team", "$fplayer_team_no"),
				(assign, ":num_groups", 9),
			(else_try),
				(assign, ":num_groups", 3),
			(try_end),
			(try_for_range, ":enemy_battle_group", 0, ":num_groups"),
				(eq, ":enemy_battle_group", ":troop_type"),
				(call_script, "script_battlegroup_get_size", ":other_team", ":troop_type"),
				(gt, reg0, 0),
				(val_add, ":team_size", reg0),
				(call_script, "script_battlegroup_get_position", ":enemy_position", ":other_team", ":troop_type"),
				(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),
		(try_end),
		(val_add, ":total_size", ":team_size"),
	(try_end),
	
	(try_begin),
		(eq, ":total_size", 0),
		(init_position, ":enemy_position"),
	(else_try),
		(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"),
	(try_end),

	(assign, reg0, ":total_size"),
  ]),


# # M&B Standard AI with changes for formations
  # script_formation_battle_tactic_init_aux
  # Input: team_no, battle_tactic
  # Output: none
  ("formation_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),
# formations additions
	  (try_begin),
		(call_script, "script_cf_formation", ":team_no", grc_infantry, 0, formation_default),
	  (try_end),
	  (try_begin),
		(call_script, "script_cf_formation", ":team_no", grc_cavalry, 0, formation_wedge),
	  (try_end),
	  (try_begin),
		(call_script, "script_cf_formation", ":team_no", grc_archers, 2, formation_default),
	  (try_end),
	  (team_give_order, ":team_no", grc_archers, mordr_spread_out),
	  (team_give_order, ":team_no", grc_archers, mordr_spread_out),
# end formations additions
  ]),
  
  # script_formation_battle_tactic_apply_aux
  # Input: team_no, battle_tactic
  # Output: battle_tactic
  ("formation_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),
		  (call_script, "script_formation_end", ":team_no", grc_everyone),	#formations
          (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),
          (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),
          (ge, ":ai_leader", 0),
          (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),
#formations code
		  (call_script, "script_point_y_toward_position", pos63, pos60),
		  (agent_get_position, pos49, ":ai_leader"),
		  (agent_set_position, ":ai_leader", pos63),	#fake out script_cf_formation
		  (try_begin),
			(call_script, "script_cf_formation", ":team_no", grc_infantry, 0, formation_default),
		  (try_end),
		  (try_begin),
			(call_script, "script_cf_formation", ":team_no", grc_cavalry, 0, formation_wedge),
		  (try_end),
		  (try_begin),
			(call_script, "script_cf_formation", ":team_no", grc_archers, 2, formation_default),
		  (try_end),
		  (agent_set_position, ":ai_leader", pos49),
#end formations code
          (agent_get_position, pos1, ":ai_leader"),
          (try_begin),
            (lt, ":distance_to_enemy", 50),
            (ge, ":mission_time", 30),
            (assign, ":battle_tactic", 0),
			(call_script, "script_formation_end", ":team_no", grc_everyone),	#formations code
            (team_give_order, ":team_no", grc_everyone, mordr_charge),
            (agent_set_speed_limit, ":ai_leader", 60),
          (try_end),
        (else_try),
          (assign, ":battle_tactic", 0),
		  (call_script, "script_formation_end", ":team_no", grc_everyone),	#formations code
          (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),
		(call_script, "script_formation_end", ":team_no", grc_everyone),	#formations code
        (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"),
  ]),
  
  # Replacement script for battle_tactic_init_aux to switch between using
  # M&B Standard AI with changes for formations and original based on
  # NOTE: original script "battle_tactic_init_aux" should be renamed to "orig_battle_tactic_init_aux"
  # constant formation_native_ai_use_formation ( 0: original, 1: use formation )
  # 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),
	  (try_begin),
		(eq, formation_native_ai_use_formation, 1),
		(call_script, "script_formation_battle_tactic_init_aux", ":team_no", ":battle_tactic"),
	  (else_try),
		(call_script, "script_orig_battle_tactic_init_aux", ":team_no", ":battle_tactic"),
	  (try_end),
    ]),

  # Replacement script for battle_tactic_init_aux to switch between using
  # M&B Standard AI with changes for formations and original based on
  # NOTE: original script "battle_tactic_apply_aux" should be renamed to "orig_battle_tactic_apply_aux"
  # constant formation_native_ai_use_formation ( 0: original, 1: use formation )
  # 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),
	  (try_begin),
		(eq, formation_native_ai_use_formation, 1),
		(call_script, "script_formation_battle_tactic_apply_aux", ":team_no", ":battle_tactic"),
	  (else_try),
		(call_script, "script_orig_battle_tactic_apply_aux", ":team_no", ":battle_tactic"),
	  (try_end),
  ]),

] # scripts


from util_wrappers import *
from util_scripts import *

scripts_directives = [
	#rename scripts to "insert" switch scripts (see end of scripts[])
	[SD_RENAME, "battle_tactic_init_aux" , "orig_battle_tactic_init_aux"],
	[SD_RENAME, "battle_tactic_apply_aux" , "orig_battle_tactic_apply_aux"],
	#insert formations before last call in team_give_order_from_order_panel
	[SD_OP_BLOCK_INSERT, "team_give_order_from_order_panel", D_SEARCH_FROM_BOTTOM | D_SEARCH_LINENUMBER | D_INSERT_BEFORE, 0, 0, [
		(call_script, "script_player_order_formations", ":order"),	#for formations
	]],
] # scripts_rename

def modmerge_formations_scripts(orig_scripts):
	# process script directives first
	process_script_directives(orig_scripts, scripts_directives)
	# add remaining scripts
	add_scripts(orig_scripts, scripts, True)
# Formations for Warband by Motomataru
# rel. 06/08/11

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 ID_animations import *

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

scripts = [
# #Formations Scripts	  
  # script_cf_formation v2 by motomataru
  # Input: team, troop class, spacing, formation type
  # Output: reg0 number of troops in formation, pos61 formation position
  # Formation(s) form near team leader (player)
  ("cf_formation", [
	(store_script_param, ":fteam", 1),
	(store_script_param, ":fclass", 2),
	(store_script_param, ":formation_extra_spacing", 3),
	(store_script_param, ":fformation", 4),
	(team_get_leader, ":fleader", ":fteam"),
	(gt, ":fleader", -1),	#any team members left?
	(agent_get_position, pos1, ":fleader"),
	(try_begin),
		(eq, "$autorotate_at_player", 1),
		(call_script, "script_team_get_position_of_enemies", pos60, ":fteam", grc_everyone),
		(neq, reg0, 0),	#more than 0 enemies still alive?
		(call_script, "script_point_y_toward_position", pos1, pos60),
	(try_end),
	(call_script, "script_battlegroup_get_size", ":fteam", ":fclass"),
	(assign, ":num_troops", reg0),
	(try_begin),
		(eq, ":fclass", grc_cavalry),
		(position_move_x, pos1, 500),		#cavalry set up 5m RIGHT of leader
		(copy_position, pos61, pos1),
		(call_script, "script_form_cavalry", ":fteam", ":fleader", ":formation_extra_spacing"),
	(else_try),
		(eq, ":fclass", grc_infantry),
		(position_move_x, pos1, -100),		#infantry set up 1m LEFT of leader
		(copy_position, pos61, pos1),
		(call_script, "script_form_infantry", ":fteam", ":fleader", ":formation_extra_spacing", ":fformation"),
		(call_script, "script_get_centering_amount", ":fformation", ":num_troops", ":formation_extra_spacing"),
		(val_mul, reg0, -1),
		(position_move_x, pos61, reg0, 0),
	(else_try),
		(position_move_y, pos1, 1000),		#archers set up 10m FRONT of leader
		(copy_position, pos61, pos1),
		(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", ":fleader", ":formation_extra_spacing", "$archer_formation_type"),
	(try_end),
	(try_begin),
		(eq, ":fclass", grc_cavalry),
		(try_begin),
			(ge, ":num_troops", formation_min_cavalry_troops),
			(assign, ":formed_up", 1),
		(else_try),
			(assign, ":formed_up", 0),
		(try_end),
	(else_try),
		(this_or_next|eq, ":fclass", grc_infantry),
		(eq, ":fclass", grc_archers),
		(try_begin),
			(ge, ":num_troops", formation_min_foot_troops),
			(assign, ":formed_up", 1),
		(else_try),
			(assign, ":formed_up", 0),
		(try_end),
	(else_try),	#unsupported division
		(assign, ":formed_up", 0),
	(try_end),
#	(team_set_order_position, ":fteam", ":fclass", pos61),
	(call_script, "script_set_formation_position", ":fteam", ":fclass", pos61),
	(assign, reg0, ":num_troops"),
	(eq, ":formed_up", 1),
  ]),
   
  # script_form_cavalry v2 by motomataru
  # Input: team, agent number of team leader, spacing
  # Output: reg0 troop count
  # Form in wedge, (now not) excluding horse archers
  # Creates formation starting at pos1
  ("form_cavalry", [
	(store_script_param, ":fteam", 1),
	(store_script_param, ":fleader", 2),
	(store_script_param, ":formation_extra_spacing", 3),
	(store_mul, ":extra_space", ":formation_extra_spacing", 50),
	(store_add, ":x_distance", 150, ":extra_space"),
	(store_add, ":y_distance", 250, ":extra_space"),		#larger y minimum distance to accommodate mounts
	(assign, ":max_level", 0),
	(try_for_agents, ":agent"),
		(call_script, "script_cf_valid_formation_member", ":fteam", grc_cavalry, ":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, ":troop_count", 0),
	(val_add, ":max_level", 1),
	(assign, ":form_left", 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", grc_cavalry, ":fleader", ":agent"),
			(val_add, ":troop_count", 1),
			(agent_set_scripted_destination, ":agent", pos1, 1),
			(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),
	(assign, reg0, ":troop_count")
  ]),
	   
  # script_form_archers v2 by motomataru
  # Input: team, agent number of team leader, spacing
  # Output: reg0 troop count
  # Form in staggered line both directions
  # Creates formation starting at pos1
  ("form_archers", [
	(store_script_param, ":fteam", 1),
	(store_script_param, ":fleader", 2),
	(store_script_param, ":formation_extra_spacing", 3),
	(store_script_param, ":archers_formation", 4),
	(store_mul, ":extra_space", ":formation_extra_spacing", 50),
	(store_add, ":distance", formation_minimum_spacing, ":extra_space"),		#minimum distance between troops
	(assign, ":troop_count", 0),
	(assign, ":total_move_y", 0),	#staggering variable	
	(try_for_agents, ":agent"),
		(call_script, "script_cf_valid_formation_member", ":fteam", grc_archers, ":fleader", ":agent"),
		(agent_set_scripted_destination, ":agent", pos1, 1),
		(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),
		(val_add, ":troop_count", 1),
	(try_end),
	(assign, reg0, ":troop_count")
  ]),
	   
  # script_form_infantry v2 by motomataru
  # Input: (pos1), team, agent number of team leader, spacing, formation
  # Output: reg0 troop count
  # 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, ":fleader", 2),
	(store_script_param, ":formation_extra_spacing", 3),
	(store_script_param, ":infantry_formation", 4),
	(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),
	(call_script, "script_battlegroup_get_size", ":fteam", grc_infantry),
	(assign, ":num_troops", reg0),	
	(assign, ":troop_count", reg0),
	(try_begin),
		(eq, ":infantry_formation", formation_default),
		(call_script, "script_get_default_formation", ":fteam"),
		(assign, ":infantry_formation", reg0),
	(try_end),
	(assign, ":form_left", 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),
		(assign, ":column", 1),
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", grc_infantry, ":fleader", ":agent"),
			(agent_set_scripted_destination, ":agent", pos1, 1),
			(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),		
		(end_try),
		
	(else_try),
		(eq, ":infantry_formation", formation_wedge),
		(assign, ":max_level", 0),
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", grc_infantry, ":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_div, ":wedge_adj", ":distance", 2),
		(store_div, ":neg_wedge_adj", ":neg_distance", 2),
		(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", grc_infantry, ":fleader", ":agent"),
				(agent_set_scripted_destination, ":agent", pos1, 1),
				(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),
		(store_div, ":rank_dimension", ":num_troops", 3),		#basic three ranks
		(val_add, ":rank_dimension", 1),		
		(assign, ":max_level", 0),
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", grc_infantry, ":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),
		(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", grc_infantry, ":fleader", ":agent"),
				(agent_set_scripted_destination, ":agent", pos1, 1),
				(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_distance", 0),
				(else_try),
					(assign, ":form_left", 0),
					(position_move_x, pos1, ":distance", 0),
				(try_end),			
				(assign, ":column", 1),
			(end_try),
		(end_try),
		
	(else_try),
		(eq, ":infantry_formation", formation_shield),
		(store_div, ":rank_dimension", ":num_troops", 3),		#basic three ranks
		(val_add, ":rank_dimension", 1),
		(assign, ":column", 1),
	#shields
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", grc_infantry, ":fleader", ":agent"),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 1),
			(gt, ":agent_weapon", -1),
			(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(eq, ":agent_weapon_type", itp_type_shield),
			(agent_set_scripted_destination, ":agent", pos1, 1),
			(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_distance", 0),
			(else_try),
				(assign, ":form_left", 0),
				(position_move_x, pos1, ":distance", 0),
			(try_end),			
			(assign, ":column", 1),
		(try_end),
	#short weapons
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", grc_infantry, ":fleader", ":agent"),
			(assign, ":agent_weapon_type", 0),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 0),
			(try_begin),
				(gt, ":agent_weapon", -1),
				(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(try_end),
			(neq, ":agent_weapon_type", itp_type_polearm),
			(assign, ":agent_weapon_type", 0),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 1),
			(try_begin),
				(gt, ":agent_weapon", -1),
				(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(try_end),
			(neq, ":agent_weapon_type", itp_type_shield),
			(agent_set_scripted_destination, ":agent", pos1, 1),
			(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_distance", 0),
			(else_try),
				(assign, ":form_left", 0),
				(position_move_x, pos1, ":distance", 0),
			(try_end),			
			(assign, ":column", 1),
		(try_end),
	#pole weapons
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", grc_infantry, ":fleader", ":agent"),
			(assign, ":agent_weapon_type", 0),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 1),
			(try_begin),
				(gt, ":agent_weapon", -1),
				(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(try_end),
			(neq, ":agent_weapon_type", itp_type_shield),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 0),
			(gt, ":agent_weapon", -1),
			(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(eq, ":agent_weapon_type", itp_type_polearm),
			(agent_set_scripted_destination, ":agent", pos1, 1),
			(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_distance", 0),
			(else_try),
				(assign, ":form_left", 0),
				(position_move_x, pos1, ":distance", 0),
			(try_end),			
			(assign, ":column", 1),
		(try_end),
	(try_end),
	(assign, reg0, ":troop_count")
  ]),
	   
  # script_get_default_formation by motomataru
  # Input: team id
  # Output: reg0 default formation
  ("get_default_formation", [
	(store_script_param, ":fteam", 1),
	(try_begin),
		(eq, ":fteam", 0),
		(assign, ":ffaction", "$team0_faction"),
	(else_try),
		(eq, ":fteam", 1),
		(assign, ":ffaction", "$team1_faction"),
	(else_try),
		(eq, ":fteam", 2),
		(assign, ":ffaction", "$team2_faction"),
	(else_try),
		(eq, ":fteam", 3),
		(assign, ":ffaction", "$team3_faction"),
	(try_end),
	
	(try_begin),
		(eq, ":ffaction", fac_player_supporters_faction),
		(assign, ":ffaction", fac_player_faction),
	(try_end),
	(try_begin),
		(eq, ":ffaction", fac_player_faction),
		(is_between, "$players_kingdom", kingdoms_begin, kingdoms_end),
		(neq, "$players_kingdom", fac_player_supporters_faction),
		(assign, ":ffaction", "$players_kingdom"),
	(try_end),

	#assign default formation
	(try_begin),
		(eq, ":ffaction", fac_kingdom_1),	#Swadians
		(assign, reg0, formation_shield),
	(else_try),
		(eq, ":ffaction", fac_kingdom_2),	#Vaegirs
		(assign, reg0, formation_ranks),
	(else_try),
		(eq, ":ffaction", fac_kingdom_3),	#Khergit
		(assign, reg0, formation_none),	#Khergit have underdeveloped infantry
	(else_try),
		(eq, ":ffaction", fac_kingdom_4),	#Nords
		(assign, reg0, formation_shield),
	(else_try),
		(eq, ":ffaction", fac_kingdom_5),	#Rhodoks
		(assign, reg0, formation_shield),
	(else_try),
		(eq, ":ffaction", fac_kingdom_6),	#Sarranid
		(assign, reg0, formation_ranks),
	(else_try),
		(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_formation_current_position by motomataru
  # Input: destination position, team, troop class, formation type, extra spacing
  # Output: in destination position
  ("formation_current_position", [
	(store_script_param, ":fposition", 1),
	(store_script_param, ":fteam", 2),
	(store_script_param, ":fclass", 3),
	(store_script_param, ":fformation", 4),
	(store_script_param, ":formation_extra_spacing", 5),
	(call_script, "script_get_first_formation_member", ":fteam", ":fclass", ":fformation"),
	(assign, ":first_agent_in_formation", reg0),
	(call_script, "script_get_formation_position", pos0, ":fteam", ":fclass"),
	(try_begin),
		(eq, ":first_agent_in_formation", -1),
		(copy_position, ":fposition", pos0),
	(else_try),
		(agent_get_position, ":fposition", ":first_agent_in_formation"),
		(position_copy_rotation, ":fposition", pos0),
		(call_script, "script_battlegroup_get_size", ":fteam", ":fclass"),
		(assign, ":num_troops", reg0),
		(try_begin),
			(eq, ":fclass", grc_archers),
			(call_script, "script_get_centering_amount", formation_default, ":num_troops", ":formation_extra_spacing"),
		(else_try),
			(call_script, "script_get_centering_amount", ":fformation", ":num_troops", ":formation_extra_spacing"),
			(val_mul, reg0, -1),
		(try_end),
		(position_move_x, ":fposition", reg0, 0),
	(try_end),
  ]),
  
  
  # script_get_centering_amount by motomataru
  # Input: formation type, number of infantry, 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"),
		(val_mul, reg0, ":troop_space"),
		(convert_from_fixed_point, reg0),
		(val_sub, reg0, ":troop_space"),
	(else_try),
		(this_or_next|eq, ":troop_formation", formation_ranks),
		(eq, ":troop_formation", formation_shield),
		(store_mul, reg0, ":num_troops", ":troop_space"),
		(val_div, reg0, 3),
		(val_sub, 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, troop class
  # Output: none
  ("formation_end", [
	(store_script_param, ":fteam", 1),
	(store_script_param, ":fclass", 2),
	(try_for_agents, ":agent"),
		(agent_is_alive, ":agent"),
		(agent_is_human, ":agent"),
		(agent_get_team, ":team", ":agent"),
		(eq, ":team", ":fteam"),
		(call_script, "script_classify_agent", ":agent"),
		(this_or_next|eq, reg0, ":fclass"), 
		(eq, ":fclass", grc_everyone), 
		(agent_clear_scripted_mode, ":agent"),
	(try_end)
  ]),


  # script_formation_move_position v2 by motomataru
  # Input: team, troop class, formation current position, formation type, extra spacing, (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, ":fclass", 2),
	(store_script_param, ":fcurrentpos", 3),
	(store_script_param, ":fformation", 4),
	(store_script_param, ":formation_extra_spacing", 5),
	(store_script_param, ":direction", 6),
	(copy_position, pos1, ":fcurrentpos"),
	(call_script, "script_team_get_position_of_enemies", pos60, ":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, pos60),	#record angle from center to enemy
		(assign, ":distance_to_enemy", reg0),
#		(team_get_order_position, pos61, ":fteam", ":fclass"),
		(call_script, "script_get_formation_position", pos61, ":fteam", ":fclass"),
		(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),
		(try_begin),
			(lt, ":distance_to_enemy", 1000),	#less than a move away?
			(position_copy_rotation, pos1, pos61),	#avoid rotating formation
		(try_end),
#		(team_set_order_position, ":fteam", ":fclass", pos1),
		(call_script, "script_set_formation_position", ":fteam", ":fclass", pos1),
		(call_script, "script_battlegroup_get_size", ":fteam", ":fclass"),
		(assign, ":num_troops", reg0),
		(try_begin),
			(neq, ":fclass", grc_archers),
			(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),
	(try_end),
  ]),


  # script_set_formation_position by motomataru
  # Input: team, troop class, position
  # Kluge around buggy *_order_position functions for teams 0-3
  ("set_formation_position", [
	(store_script_param, ":fteam", 1),
	(store_script_param, ":fclass", 2),
	(store_script_param, ":fposition", 3),
	(try_begin),
		(eq, ":fteam", 0),
		(try_begin),
			(eq, ":fclass", 0),
			(position_get_x, "$formation_00_x", ":fposition"),
			(position_get_y, "$formation_00_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_00_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 1),
			(position_get_x, "$formation_01_x", ":fposition"),
			(position_get_y, "$formation_01_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_01_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 2),
			(position_get_x, "$formation_02_x", ":fposition"),
			(position_get_y, "$formation_02_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_02_rot", ":fposition"),
		(end_try),
	(else_try),
		(eq, ":fteam", 1),
		(try_begin),
			(eq, ":fclass", 0),
			(position_get_x, "$formation_10_x", ":fposition"),
			(position_get_y, "$formation_10_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_10_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 1),
			(position_get_x, "$formation_11_x", ":fposition"),
			(position_get_y, "$formation_11_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_11_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 2),
			(position_get_x, "$formation_12_x", ":fposition"),
			(position_get_y, "$formation_12_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_12_rot", ":fposition"),
		(end_try),
	(else_try),
		(eq, ":fteam", 2),
		(try_begin),
			(eq, ":fclass", 0),
			(position_get_x, "$formation_20_x", ":fposition"),
			(position_get_y, "$formation_20_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_20_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 1),
			(position_get_x, "$formation_21_x", ":fposition"),
			(position_get_y, "$formation_21_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_21_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 2),
			(position_get_x, "$formation_22_x", ":fposition"),
			(position_get_y, "$formation_22_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_22_rot", ":fposition"),
		(end_try),
	(else_try),
		(eq, ":fteam", 3),
		(try_begin),
			(eq, ":fclass", 0),
			(position_get_x, "$formation_30_x", ":fposition"),
			(position_get_y, "$formation_30_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_30_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 1),
			(position_get_x, "$formation_31_x", ":fposition"),
			(position_get_y, "$formation_31_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_31_rot", ":fposition"),
		(else_try),
			(eq, ":fclass", 2),
			(position_get_x, "$formation_32_x", ":fposition"),
			(position_get_y, "$formation_32_y", ":fposition"),
			(position_get_rotation_around_z, "$formation_32_rot", ":fposition"),
		(end_try),	
	(end_try),
	(team_set_order_position, ":fteam", ":fclass", ":fposition"),
  ]),	


  # script_get_formation_position by motomataru
  # Input: position, team, troop class
  # Output: input position (pos0 used)
  # Kluge around buggy *_order_position functions for teams 0-3
  ("get_formation_position", [
	(store_script_param, ":fposition", 1),
	(store_script_param, ":fteam", 2),
	(store_script_param, ":fclass", 3),
	(init_position, ":fposition"),
	(try_begin),
		(eq, ":fteam", 0),
		(try_begin),
			(eq, ":fclass", 0),
			(position_set_x, ":fposition", "$formation_00_x"),
			(position_set_y, ":fposition", "$formation_00_y"),
			(position_rotate_z, ":fposition", "$formation_00_rot"),
		(else_try),
			(eq, ":fclass", 1),
			(position_set_x, ":fposition", "$formation_01_x"),
			(position_set_y, ":fposition", "$formation_01_y"),
			(position_rotate_z, ":fposition", "$formation_01_rot"),
		(else_try),
			(eq, ":fclass", 2),
			(position_set_x, ":fposition", "$formation_02_x"),
			(position_set_y, ":fposition", "$formation_02_y"),
			(position_rotate_z, ":fposition", "$formation_02_rot"),
		(try_end),
	(else_try),
		(eq, ":fteam", 1),
		(try_begin),
			(eq, ":fclass", 0),
			(position_set_x, ":fposition", "$formation_10_x"),
			(position_set_y, ":fposition", "$formation_10_y"),
			(position_rotate_z, ":fposition", "$formation_10_rot"),
		(else_try),
			(eq, ":fclass", 1),
			(position_set_x, ":fposition", "$formation_11_x"),
			(position_set_y, ":fposition", "$formation_11_y"),
			(position_rotate_z, ":fposition", "$formation_11_rot"),
		(else_try),
			(eq, ":fclass", 2),
			(position_set_x, ":fposition", "$formation_12_x"),
			(position_set_y, ":fposition", "$formation_12_y"),
			(position_rotate_z, ":fposition", "$formation_12_rot"),
		(try_end),
	(else_try),
		(eq, ":fteam", 2),
		(try_begin),
			(eq, ":fclass", 0),
			(position_set_x, ":fposition", "$formation_20_x"),
			(position_set_y, ":fposition", "$formation_20_y"),
			(position_rotate_z, ":fposition", "$formation_20_rot"),
		(else_try),
			(eq, ":fclass", 1),
			(position_set_x, ":fposition", "$formation_21_x"),
			(position_set_y, ":fposition", "$formation_21_y"),
			(position_rotate_z, ":fposition", "$formation_21_rot"),
		(else_try),
			(eq, ":fclass", 2),
			(position_set_x, ":fposition", "$formation_22_x"),
			(position_set_y, ":fposition", "$formation_22_y"),
			(position_rotate_z, ":fposition", "$formation_22_rot"),
		(try_end),
	(else_try),
		(eq, ":fteam", 3),
		(try_begin),
			(eq, ":fclass", 0),
			(position_set_x, ":fposition", "$formation_30_x"),
			(position_set_y, ":fposition", "$formation_30_y"),
			(position_rotate_z, ":fposition", "$formation_30_rot"),
		(else_try),
			(eq, ":fclass", 1),
			(position_set_x, ":fposition", "$formation_31_x"),
			(position_set_y, ":fposition", "$formation_31_y"),
			(position_rotate_z, ":fposition", "$formation_31_rot"),
		(else_try),
			(eq, ":fclass", 2),
			(position_set_x, ":fposition", "$formation_32_x"),
			(position_set_y, ":fposition", "$formation_32_y"),
			(position_rotate_z, ":fposition", "$formation_32_rot"),
		(try_end),	
	(else_try),
		(call_script, "script_get_first_formation_member", ":fteam", ":fclass", formation_square),
		(try_begin),	  # "launder" team_get_order_position shutting down position_move_x
			(gt, reg0, -1),
			(team_get_order_position, ":fposition", ":fteam", ":fclass"),
			(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_get_first_formation_member by motomataru
  # Input: team, troop class, formation
  # Output: reg0 leader agent number or -1
  # Ought to follow script_form_infantry
  ("get_first_formation_member", [
	(store_script_param, ":fteam", 1),
	(store_script_param, ":fclass", 2),
	(store_script_param, ":fformation", 3),
	(team_get_leader, ":fleader", ":fteam"),
	(assign, ":first_agent_in_formation", -1),
	(try_begin),
		(eq, ":fformation", formation_square),
		(try_for_agents, ":agent"),
			(eq, ":first_agent_in_formation", -1),
			(call_script, "script_cf_valid_formation_member", ":fteam", ":fclass", ":fleader", ":agent"),
			(assign, ":first_agent_in_formation", ":agent"),
		(try_end),
	(else_try),
		(eq, ":fformation", formation_shield),
		(try_for_agents, ":agent"),
			(eq, ":first_agent_in_formation", -1),
			(call_script, "script_cf_valid_formation_member", ":fteam", ":fclass", ":fleader", ":agent"),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 1),
			(gt, ":agent_weapon", -1),
			(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(eq, ":agent_weapon_type", itp_type_shield),
			(assign, ":first_agent_in_formation", ":agent"),
		(try_end),
		(try_for_agents, ":agent"),
			(eq, ":first_agent_in_formation", -1),
			(call_script, "script_cf_valid_formation_member", ":fteam", ":fclass", ":fleader", ":agent"),
			(assign, ":agent_weapon_type", 0),
			(agent_get_wielded_item, ":agent_weapon", ":agent", 0),
			(try_begin),
				(gt, ":agent_weapon", -1),
				(item_get_type, ":agent_weapon_type", ":agent_weapon"),
			(try_end),
			(neq, ":agent_weapon_type", itp_type_polearm),
			(assign, ":first_agent_in_formation", ":agent"),
		(try_end),
		(try_for_agents, ":agent"),
			(eq, ":first_agent_in_formation", -1),
			(call_script, "script_cf_valid_formation_member", ":fteam", ":fclass", ":fleader", ":agent"),
			(assign, ":first_agent_in_formation", ":agent"),	#must have polearm w/o shields...
		(try_end),
	(else_try),	#must be a level sort
		(assign, ":rank_level", 0),
		(try_for_agents, ":agent"),
			(call_script, "script_cf_valid_formation_member", ":fteam", ":fclass", ":fleader", ":agent"),
			(agent_get_troop_id, ":troop_id", ":agent"),
			(store_character_level, ":troop_level", ":troop_id"),
			(gt, ":troop_level", ":rank_level"),
			(assign, ":rank_level", ":troop_level"),
			(assign, ":first_agent_in_formation", ":agent"),
		(try_end),
	(try_end),
	(assign, reg0, ":first_agent_in_formation")
  ]),
  
	
  # script_cf_valid_formation_member by motomataru
  # Input: team, troop class, 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, ":fclass", 2),
	# (store_script_param, ":fleader", 3),
	(store_script_param, ":agent", 4),
	(call_script, "script_classify_agent", ":agent"),
	(eq, reg0, ":fclass"),
	(agent_get_team, ":team", ":agent"),
	(eq, ":team", ":fteam"),
	(agent_is_alive, ":agent"),
	(agent_is_human, ":agent"),
	(agent_slot_eq, ":agent", slot_agent_is_running_away, 0),
  ]),

	
  # script_classify_agent by motomataru
  # Input: agent
  # Output: reg0 (-1 if leader)
  # Assigns a "division" to the agent
  ("classify_agent", [
	(store_script_param, ":agent", 1),
	(agent_get_team, ":ateam", ":agent"),
	(team_get_leader, ":aleader", ":ateam"),
	(try_begin),
		(eq, ":agent", ":aleader"),
		(assign, reg0, -1),
	(else_try),
		(eq, ":ateam", "$fplayer_team_no"),
		(agent_get_division, reg0, ":agent"),
	(else_try),
		(agent_get_class, reg0, ":agent"),
	(try_end)
  ]),

	
# #Player team formations functions

  # script_player_attempt_formation
  # Inputs:	arg1: troop class (grc_*)
  #			arg2: formation identifier (formation_*)
  # Output: none
  ("player_attempt_formation", [
	(store_script_param, ":fclass", 1),
	(store_script_param, ":fformation", 2),
	(set_fixed_point_multiplier, 100),
	(try_begin),
		(class_is_listening_order, "$fplayer_team_no", ":fclass"),
		(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),
		(assign, ":new_formation", 0),

		(try_begin),
			(eq, ":fclass", grc_infantry),
			(try_begin),
				(neq, "$infantry_formation_type", ":fformation"),
				(assign, ":new_formation", 1),
				(assign, "$infantry_formation_type", ":fformation"),
				(lt, "$infantry_space", formation_start_spread_out),
				(assign, "$infantry_space", formation_start_spread_out),	#spread out for ease of forming up
			(try_end),
			(try_begin),
				(call_script, "script_cf_formation", "$fplayer_team_no", grc_infantry, "$infantry_space", "$infantry_formation_type"),
				(try_begin),
					(eq, ":new_formation", 1),
					(display_message, "@Infantry forming {s1}."),
				(try_end),
			(else_try),
				(assign, "$infantry_formation_type", formation_none),
				(gt, reg0, 0),
				(display_message, "@Not enough infantry to form {s1}, but holding."),
				(call_script, "script_formation_end", "$fplayer_team_no", grc_infantry),
			(try_end),
			(assign, "$infantry_formation_move_order", mordr_hold),
			(set_show_messages, 0),
			(team_give_order, "$fplayer_team_no", grc_infantry, mordr_hold),
			(team_set_order_position, "$fplayer_team_no", grc_infantry, pos61),
			(set_show_messages, 1),
		(else_try),
			(eq, ":fclass", grc_cavalry),
			(try_begin),
				(neq, "$cavalry_formation_type", ":fformation"),
				(assign, ":new_formation", 1),
				(assign, "$cavalry_formation_type", ":fformation"),
				(lt, "$cavalry_space", formation_start_spread_out),
				(assign, "$cavalry_space", formation_start_spread_out),	#spread out for ease of forming up
			(try_end),
			(try_begin),
				(call_script, "script_cf_formation", "$fplayer_team_no", grc_cavalry, "$cavalry_space", "$cavalry_formation_type"),
				(try_begin),
					(eq, ":new_formation", 1),
					(display_message, "@Cavalry forming {s1}."),
				(try_end),
			(else_try),
				(assign, "$cavalry_formation_type", formation_none),
				(gt, reg0, 0),
				(display_message, "@Not enough cavalry to form {s1}, but holding."),
				(call_script, "script_formation_end", "$fplayer_team_no", grc_cavalry),
			(try_end),
			(assign, "$cavalry_formation_move_order", mordr_hold),
			(set_show_messages, 0),
			(team_give_order, "$fplayer_team_no", grc_cavalry, mordr_hold),
			(team_set_order_position, "$fplayer_team_no", grc_cavalry, pos61),
			(set_show_messages, 1),
		(else_try),	#must be archers
			(eq, ":fclass", grc_archers),
			(try_begin),
				(neq, "$archer_formation_type", ":fformation"),
				(assign, ":new_formation", 1),
				(assign, "$archer_formation_type", ":fformation"),
				(lt, "$archer_space", formation_start_spread_out),
				(assign, "$archer_space", formation_start_spread_out),	#spread out for ease of forming up
			(try_end),
			(try_begin),
				(call_script, "script_cf_formation", "$fplayer_team_no", grc_archers, "$archer_space", "$archer_formation_type"),
				(try_begin),
					(eq, ":new_formation", 1),
					(display_message, "@Archers forming {s1}."),
				(try_end),
			(else_try),
				(assign, "$archer_formation_type", formation_none),
				(gt, reg0, 0),
				(display_message, "@Not enough archers to form {s1}, but holding."),
				(call_script, "script_formation_end", "$fplayer_team_no", grc_archers),
			(try_end),
			(assign, "$archer_formation_move_order", mordr_hold),
			(set_show_messages, 0),
			(team_give_order, "$fplayer_team_no", grc_archers, mordr_hold),
			(team_set_order_position, "$fplayer_team_no", grc_archers, pos61),
			(set_show_messages, 1),
		(try_end),
	(try_end)
  ]),
  
  
  # script_player_order_formations by motomataru
  # 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),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(call_script, "script_player_attempt_formation", grc_infantry, "$infantry_formation_type"),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(call_script, "script_player_attempt_formation", grc_cavalry, "$cavalry_formation_type"),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(call_script, "script_player_attempt_formation", grc_archers, "$archer_formation_type"),
		(try_end),
		
	(else_try),	#Follow is hold	repeated frequently
		(eq, ":forder", mordr_follow),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(assign, "$infantry_formation_move_order", mordr_follow),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(assign, "$cavalry_formation_move_order", mordr_follow),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(assign, "$archer_formation_move_order", mordr_follow),
		(try_end),
		
	(else_try),	#charge or retreat ends formation
		(this_or_next|eq, ":forder", mordr_charge),
		(eq, ":forder", mordr_retreat),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(call_script, "script_formation_end", "$fplayer_team_no", grc_infantry),
			(display_message, "@Infantry formation disassembled."),
			(assign, "$infantry_formation_type", formation_none),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_formation_end", "$fplayer_team_no", grc_cavalry),
			(display_message, "@Cavalry formation disassembled."),
			(assign, "$cavalry_formation_type", formation_none),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(call_script, "script_formation_end", "$fplayer_team_no", grc_archers),
			(display_message, "@Archer formation disassembled."),
			(assign, "$archer_formation_type", formation_none),
		(try_end),
		
	(else_try),	#dismount ends formation
		(eq, ":forder", mordr_dismount),
		(neq, "$cavalry_formation_type", formation_none),
		(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
		(call_script, "script_formation_end", "$fplayer_team_no", grc_cavalry),
		(display_message, "@Cavalry formation disassembled."),
		(assign, "$cavalry_formation_type", formation_none),

	(else_try),
		(eq, ":forder", mordr_advance),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_infantry, "$infantry_formation_type", "$infantry_space"),
			(try_begin),	#on change of orders cancel order position
				(neq, "$infantry_formation_move_order", mordr_advance),
				# (team_set_order_position, "$fplayer_team_no", grc_infantry, pos63),
				(call_script, "script_set_formation_position", "$fplayer_team_no", grc_infantry, pos63),
			(try_end),
			(call_script, "script_formation_move_position", "$fplayer_team_no", grc_infantry, pos63, "$infantry_formation_type", "$infantry_space", 1),			
			(call_script, "script_form_infantry", "$fplayer_team_no", "$fplayer_agent_no", "$infantry_space", "$infantry_formation_type"),
			(assign, "$infantry_formation_move_order", mordr_advance),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_cavalry, "$cavalry_formation_type", "$cavalry_space"),
			(try_begin),	#on change of orders cancel order position
				(neq, "$cavalry_formation_move_order", mordr_advance),
				# (team_set_order_position, "$fplayer_team_no", grc_cavalry, pos63),
				(call_script, "script_set_formation_position", "$fplayer_team_no", grc_cavalry, pos63),
			(try_end),
			(call_script, "script_formation_move_position", "$fplayer_team_no", grc_cavalry, pos63, "$cavalry_formation_type", "$cavalry_space", 1),
			(call_script, "script_form_cavalry", "$fplayer_team_no", "$fplayer_agent_no", "$cavalry_space"),
			(assign, "$cavalry_formation_move_order", mordr_advance),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_archers, "$archer_formation_type", "$archer_space"),
			(try_begin),	#on change of orders cancel order position
				(neq, "$archer_formation_move_order", mordr_advance),
				# (team_set_order_position, "$fplayer_team_no", grc_archers, pos63),
				(call_script, "script_set_formation_position", "$fplayer_team_no", grc_archers, pos63),
			(try_end),
			(call_script, "script_formation_move_position", "$fplayer_team_no", grc_archers, pos63, "$archer_formation_type", "$archer_space", 1),
			(call_script, "script_form_archers", "$fplayer_team_no", "$fplayer_agent_no", "$archer_space", "$archer_formation_type"),
			(assign, "$archer_formation_move_order", mordr_advance),
		(try_end),

	(else_try),
		(eq, ":forder", mordr_fall_back),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_infantry, "$infantry_formation_type", "$infantry_space"),
			(try_begin),	#on change of orders cancel order position
				(neq, "$infantry_formation_move_order", mordr_fall_back),
				# (team_set_order_position, "$fplayer_team_no", grc_infantry, pos63),
				(call_script, "script_set_formation_position", "$fplayer_team_no", grc_infantry, pos63),
			(try_end),
			(call_script, "script_formation_move_position", "$fplayer_team_no", grc_infantry, pos63, "$infantry_formation_type", "$infantry_space", -1),
			(call_script, "script_form_infantry", "$fplayer_team_no", "$fplayer_agent_no", "$infantry_space", "$infantry_formation_type"),
			(assign, "$infantry_formation_move_order", mordr_fall_back),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_cavalry, "$cavalry_formation_type", "$cavalry_space"),
			(try_begin),	#on change of orders cancel order position
				(neq, "$cavalry_formation_move_order", mordr_fall_back),
				# (team_set_order_position, "$fplayer_team_no", grc_cavalry, pos63),
				(call_script, "script_set_formation_position", "$fplayer_team_no", grc_cavalry, pos63),
			(try_end),
			(call_script, "script_formation_move_position", "$fplayer_team_no", grc_cavalry, pos63, "$cavalry_formation_type", "$cavalry_space", -1),
			(call_script, "script_form_cavalry", "$fplayer_team_no", "$fplayer_agent_no", "$cavalry_space"),
			(assign, "$cavalry_formation_move_order", mordr_fall_back),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_archers, "$archer_formation_type", "$archer_space"),
			(try_begin),	#on change of orders cancel order position
				(neq, "$archer_formation_move_order", mordr_fall_back),
				# (team_set_order_position, "$fplayer_team_no", grc_archers, pos63),
				(call_script, "script_set_formation_position", "$fplayer_team_no", grc_archers, pos63),
			(try_end),
			(call_script, "script_formation_move_position", "$fplayer_team_no", grc_archers, pos63, "$archer_formation_type", "$archer_space", -1),
			(call_script, "script_form_archers", "$fplayer_team_no", "$fplayer_agent_no", "$archer_space", "$archer_formation_type"),
			(assign, "$archer_formation_move_order", mordr_fall_back),
		(try_end),

	(else_try),
		(eq, ":forder", mordr_stand_closer),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(gt, "$infantry_space", 0),
			(val_sub, "$infantry_space", 1),
#			(team_get_order_position, pos1, "$fplayer_team_no", grc_infantry),
			(call_script, "script_get_formation_position", pos1, "$fplayer_team_no", grc_infantry),			
			(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_infantry),
			(assign, ":troop_count", reg0),			
			(call_script, "script_get_centering_amount", "$infantry_formation_type", ":troop_count", "$infantry_space"),
			(position_move_x, pos1, reg0),
			(call_script, "script_form_infantry", "$fplayer_team_no", "$fplayer_agent_no", "$infantry_space", "$infantry_formation_type"),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(gt, "$cavalry_space", 0),
			(val_sub, "$cavalry_space", 1),
#			(team_get_order_position, pos1, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_get_formation_position", pos1, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_form_cavalry", "$fplayer_team_no", "$fplayer_agent_no", "$cavalry_space"),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(gt, "$archer_space", 0),
			(val_sub, "$archer_space", 1),
#			(team_get_order_position, pos1, "$fplayer_team_no", grc_archers),
			(call_script, "script_get_formation_position", pos1, "$fplayer_team_no", grc_archers),
			(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_archers),
			(assign, ":troop_count", reg0),
			(call_script, "script_get_centering_amount", formation_default, ":troop_count", "$archer_space"),
			(val_mul, reg0, -1),
			(position_move_x, pos1, reg0),
			(call_script, "script_form_archers", "$fplayer_team_no", "$fplayer_agent_no", "$archer_space", "$archer_formation_type"),
		(try_end),

	(else_try),
		(eq, ":forder", mordr_spread_out),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(val_add, "$infantry_space", 1),
#			(team_get_order_position, pos1, "$fplayer_team_no", grc_infantry),
			(call_script, "script_get_formation_position", pos1, "$fplayer_team_no", grc_infantry),			
			(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_infantry),
			(assign, ":troop_count", reg0),			
			(call_script, "script_get_centering_amount", "$infantry_formation_type", ":troop_count", "$infantry_space"),
			(position_move_x, pos1, reg0),
			(call_script, "script_form_infantry", "$fplayer_team_no", "$fplayer_agent_no", "$infantry_space", "$infantry_formation_type"),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(val_add, "$cavalry_space", 1),
#			(team_get_order_position, pos1, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_get_formation_position", pos1, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_form_cavalry", "$fplayer_team_no", "$fplayer_agent_no", "$cavalry_space"),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(val_add, "$archer_space", 1),
#			(team_get_order_position, pos1, "$fplayer_team_no", grc_archers),
			(call_script, "script_get_formation_position", pos1, "$fplayer_team_no", grc_archers),
			(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_archers),
			(assign, ":troop_count", reg0),
			(call_script, "script_get_centering_amount", formation_default, ":troop_count", "$archer_space"),
			(val_mul, reg0, -1),
			(position_move_x, pos1, reg0),
			(call_script, "script_form_archers", "$fplayer_team_no", "$fplayer_agent_no", "$archer_space", "$archer_formation_type"),
		(try_end),

	(else_try),
		(eq, ":forder", mordr_stand_ground),
		(try_begin),
			(neq, "$infantry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_infantry),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_infantry, "$infantry_formation_type", "$infantry_space"),
			(copy_position, pos1, pos63),
			(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_infantry),
			(assign, ":troop_count", reg0),			
			(call_script, "script_get_centering_amount", "$infantry_formation_type", ":troop_count", "$infantry_space"),
			(position_move_x, pos1, reg0),
			(call_script, "script_form_infantry", "$fplayer_team_no", "$fplayer_agent_no", "$infantry_space", "$infantry_formation_type"),
			(assign, "$infantry_formation_move_order", mordr_stand_ground),
			(call_script, "script_set_formation_position", "$fplayer_team_no", grc_infantry, pos63),
		(try_end),
		(try_begin),
			(neq, "$cavalry_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_cavalry),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_cavalry, "$cavalry_formation_type", "$cavalry_space"),
			(copy_position, pos1, pos63),
			(call_script, "script_form_cavalry", "$fplayer_team_no", "$fplayer_agent_no", "$cavalry_space"),
			(assign, "$cavalry_formation_move_order", mordr_stand_ground),
			(call_script, "script_set_formation_position", "$fplayer_team_no", grc_cavalry, pos63),
		(try_end),
		(try_begin),
			(neq, "$archer_formation_type", formation_none),
			(class_is_listening_order, "$fplayer_team_no", grc_archers),
			(call_script, "script_formation_current_position", pos63, "$fplayer_team_no", grc_archers, "$archer_formation_type", "$archer_space"),
			(copy_position, pos1, pos63),
			(call_script, "script_battlegroup_get_size", "$fplayer_team_no", grc_archers),
			(assign, ":troop_count", reg0),
			(call_script, "script_get_centering_amount", formation_default, ":troop_count", "$archer_space"),
			(val_mul, reg0, -1),
			(position_move_x, pos1, reg0),
			(call_script, "script_form_archers", "$fplayer_team_no", "$fplayer_agent_no", "$archer_space", "$archer_formation_type"),
			(assign, "$archer_formation_move_order", mordr_stand_ground),
			(call_script, "script_set_formation_position", "$fplayer_team_no", grc_archers, pos63),
		(try_end),			
	(try_end)
  ]),

  
# #Utilities used by formations
  # script_point_y_toward_position by motomataru
  # Input: from position, to position
  # Output: reg0 fixed point distance
  ("point_y_toward_position", [
	(store_script_param, ":from_position", 1),
	(store_script_param, ":to_position", 2),
	(position_get_x, ":dist_x_to_cosine", ":to_position"),
	(position_get_x, ":from_coord", ":from_position"),
	(val_sub, ":dist_x_to_cosine", ":from_coord"),
	(store_mul, ":sum_square", ":dist_x_to_cosine", ":dist_x_to_cosine"),
	(position_get_y, ":dist_y_to_sine", ":to_position"),
	(position_get_y, ":from_coord", ":from_position"),
	(val_sub, ":dist_y_to_sine", ":from_coord"),
	(store_mul, reg0, ":dist_y_to_sine", ":dist_y_to_sine"),
	(val_add, ":sum_square", reg0),
	(convert_from_fixed_point, ":sum_square"),
	(store_sqrt, ":distance_between", ":sum_square"),
	(convert_to_fixed_point, ":dist_x_to_cosine"),
	(val_div, ":dist_x_to_cosine", ":distance_between"),
	(convert_to_fixed_point, ":dist_y_to_sine"),
	(val_div, ":dist_y_to_sine", ":distance_between"),
	(try_begin),
		(lt, ":dist_x_to_cosine", 0),
		(assign, ":bound_a", 90),
		(assign, ":bound_b", 270),
		(assign, ":theta", 180),
	(else_try),
		(assign, ":bound_a", 90),
		(assign, ":bound_b", -90),
		(assign, ":theta", 0),
	(try_end),
	(assign, ":sine_theta", 0),	#avoid error on compile
	(convert_to_fixed_point, ":theta"),
	(convert_to_fixed_point, ":bound_a"),
	(convert_to_fixed_point, ":bound_b"),
	(try_for_range, reg0, 0, 6),	#precision 90/2exp6 (around 2 degrees)
		(store_sin, ":sine_theta", ":theta"),
		(try_begin),
			(gt, ":sine_theta", ":dist_y_to_sine"),
			(assign, ":bound_a", ":theta"),
		(else_try),
			(lt, ":sine_theta", ":dist_y_to_sine"),
			(assign, ":bound_b", ":theta"),
		(try_end),
		(store_add, ":angle_sum", ":bound_b", ":bound_a"),
		(store_div, ":theta", ":angle_sum", 2),
	(try_end),
	(convert_from_fixed_point, ":theta"),
	(position_get_rotation_around_z, reg0, ":from_position"),
	(val_sub, ":theta", reg0),
	(val_sub, ":theta", 90),	#point y-axis at destination
	(position_rotate_z, ":from_position", ":theta"),
	(assign, reg0, ":distance_between"),
  ]),


  # script_store_battlegroup_data by motomataru
  # Input: none
  # Output: sets positions and globals to track data on ALL groups in a battle
  # Globals used: pos1, reg0, reg1, positions 24-45
  ("store_battlegroup_data", [
	(assign, ":team0_leader", 0),
	(assign, ":team0_x_leader", 0),
	(assign, ":team0_y_leader", 0),
	(assign, ":team0_level_leader", 0),
	(assign, "$team0_num_infantry", 0),
	(assign, "$team0_num_archers", 0),
	(assign, "$team0_num_cavalry", 0),
	(assign, "$team0_archers_have_ammo", 1),
	(assign, "$team0_percent_ranged_throw", 0),
	(assign, "$team0_percent_cavalry_are_archers", 0),
	(assign, "$team0_level_infantry", 0),
	(assign, "$team0_level_archers", 0),
	(assign, "$team0_level_cavalry", 0),
	(assign, ":team0_x_infantry", 0),
	(assign, ":team0_x_archers", 0),
	(assign, ":team0_x_cavalry", 0),
	(assign, ":team0_y_infantry", 0),
	(assign, ":team0_y_archers", 0),
	(assign, ":team0_y_cavalry", 0),
	(assign, ":team1_leader", 0),
	(assign, ":team1_x_leader", 0),
	(assign, ":team1_y_leader", 0),
	(assign, ":team1_level_leader", 0),
	(assign, "$team1_num_infantry", 0),
	(assign, "$team1_num_archers", 0),
	(assign, "$team1_num_cavalry", 0),
	(assign, "$team1_archers_have_ammo", 1),
	(assign, "$team1_percent_ranged_throw", 0),
	(assign, "$team1_percent_cavalry_are_archers", 0),
	(assign, "$team1_level_infantry", 0),
	(assign, "$team1_level_archers", 0),
	(assign, "$team1_level_cavalry", 0),
	(assign, ":team1_x_infantry", 0),
	(assign, ":team1_x_archers", 0),
	(assign, ":team1_x_cavalry", 0),
	(assign, ":team1_y_infantry", 0),
	(assign, ":team1_y_archers", 0),
	(assign, ":team1_y_cavalry", 0),
	(assign, ":team2_leader", 0),
	(assign, ":team2_x_leader", 0),
	(assign, ":team2_y_leader", 0),
	(assign, ":team2_level_leader", 0),
	(assign, "$team2_num_infantry", 0),
	(assign, "$team2_num_archers", 0),
	(assign, "$team2_num_cavalry", 0),
	(assign, "$team2_archers_have_ammo", 1),
	(assign, "$team2_percent_ranged_throw", 0),
	(assign, "$team2_percent_cavalry_are_archers", 0),
	(assign, "$team2_level_infantry", 0),
	(assign, "$team2_level_archers", 0),
	(assign, "$team2_level_cavalry", 0),
	(assign, ":team2_x_infantry", 0),
	(assign, ":team2_x_archers", 0),
	(assign, ":team2_x_cavalry", 0),
	(assign, ":team2_y_infantry", 0),
	(assign, ":team2_y_archers", 0),
	(assign, ":team2_y_cavalry", 0),
	(assign, ":team3_leader", 0),
	(assign, ":team3_x_leader", 0),
	(assign, ":team3_y_leader", 0),
	(assign, ":team3_level_leader", 0),
	(assign, "$team3_num_infantry", 0),
	(assign, "$team3_num_archers", 0),
	(assign, "$team3_num_cavalry", 0),
	(assign, "$team3_archers_have_ammo", 1),
	(assign, "$team3_percent_ranged_throw", 0),
	(assign, "$team3_percent_cavalry_are_archers", 0),
	(assign, "$team3_level_infantry", 0),
	(assign, "$team3_level_archers", 0),
	(assign, "$team3_level_cavalry", 0),
	(assign, ":team3_x_infantry", 0),
	(assign, ":team3_x_archers", 0),
	(assign, ":team3_x_cavalry", 0),
	(assign, ":team3_y_infantry", 0),
	(assign, ":team3_y_archers", 0),
	(assign, ":team3_y_cavalry", 0),
	(assign, "$teamp_num_group3", 0),
	(assign, "$teamp_level_group3", 0),
	(assign, ":teamp_x_group3", 0),
	(assign, ":teamp_y_group3", 0),
	(assign, "$teamp_num_group4", 0),
	(assign, "$teamp_level_group4", 0),
	(assign, ":teamp_x_group4", 0),
	(assign, ":teamp_y_group4", 0),
	(assign, "$teamp_num_group5", 0),
	(assign, "$teamp_level_group5", 0),
	(assign, ":teamp_x_group5", 0),
	(assign, ":teamp_y_group5", 0),
	(assign, "$teamp_num_group6", 0),
	(assign, "$teamp_level_group6", 0),
	(assign, ":teamp_x_group6", 0),
	(assign, ":teamp_y_group6", 0),
	(assign, "$teamp_num_group7", 0),
	(assign, "$teamp_level_group7", 0),
	(assign, ":teamp_x_group7", 0),
	(assign, ":teamp_y_group7", 0),
	(assign, "$teamp_num_group8", 0),
	(assign, "$teamp_level_group8", 0),
	(assign, ":teamp_x_group8", 0),
	(assign, ":teamp_y_group8", 0),

	(try_for_agents, ":cur_agent"),
		(agent_is_alive, ":cur_agent"),      
		(agent_is_human, ":cur_agent"), 
		(agent_slot_eq, ":cur_agent", slot_agent_is_running_away, 0),
		(agent_get_team, ":bgteam", ":cur_agent"),
		(call_script, "script_classify_agent", ":cur_agent"),
		(assign, ":bgroup", reg0),
		(agent_get_troop_id, ":cur_troop", ":cur_agent"),
		(store_character_level, ":cur_level", ":cur_troop"),
		(agent_get_ammo, ":cur_ammo", ":cur_agent", 0),
		(agent_get_wielded_item, reg0, ":cur_agent", 0),
		(try_begin),
			(lt, reg0, 0),
			(assign, ":cur_weapon_type", 0),
			(assign, ":cur_weapon_length", 0),
		(else_try),
			(item_get_type, ":cur_weapon_type", reg0),
			(try_begin),
				(eq, ":cur_weapon_type", itp_type_polearm),
				(store_mul, ":cur_weapon_length", Weapon_Length_Proxy, 2),
			(else_try),
				(this_or_next|eq, ":cur_weapon_type", itp_type_one_handed_wpn),
				(eq, ":cur_weapon_type", itp_type_two_handed_wpn),
				(assign, ":cur_weapon_length", Weapon_Length_Proxy),
			(else_try),
				(assign, ":cur_weapon_length", 0),
			(try_end),
		(try_end),
		(agent_get_position, pos1, ":cur_agent"),
		(position_get_x, ":x_value", pos1),
		(position_get_y, ":y_value", pos1),
		(try_begin),
			(eq, ":bgroup", grc_infantry),	#AKA battle group 0
			(try_begin),
				(eq, ":bgteam", 0),
				(val_add, "$team0_num_infantry", 1),
				(val_add, ":team0_x_infantry", ":x_value"),
				(val_add, ":team0_y_infantry", ":y_value"),
				(val_add, "$team0_level_infantry", ":cur_level"),
				(val_add, "$team0_weapon_length_infantry", ":cur_weapon_length"),
			(else_try),
				(eq, ":bgteam", 1),
				(val_add, "$team1_num_infantry", 1),
				(val_add, ":team1_x_infantry", ":x_value"),
				(val_add, ":team1_y_infantry", ":y_value"),
				(val_add, "$team1_level_infantry", ":cur_level"),
				(val_add, "$team1_weapon_length_infantry", ":cur_weapon_length"),
			(else_try),
				(eq, ":bgteam", 2),
				(val_add, "$team2_num_infantry", 1),
				(val_add, ":team2_x_infantry", ":x_value"),
				(val_add, ":team2_y_infantry", ":y_value"),
				(val_add, "$team2_level_infantry", ":cur_level"),
				(val_add, "$team2_weapon_length_infantry", ":cur_weapon_length"),
			(else_try),
				(eq, ":bgteam", 3),
				(val_add, "$team3_num_infantry", 1),
				(val_add, ":team3_x_infantry", ":x_value"),
				(val_add, ":team3_y_infantry", ":y_value"),
				(val_add, "$team3_level_infantry", ":cur_level"),
				(val_add, "$team3_weapon_length_infantry", ":cur_weapon_length"),
			(try_end),
		(else_try),
			(eq, ":bgroup", grc_archers),	#AKA battle group 1
			(try_begin),
				(eq, ":bgteam", 0),
				(val_add, "$team0_num_archers", 1),
				(val_add, ":team0_x_archers", ":x_value"),
				(val_add, ":team0_y_archers", ":y_value"),
				(val_add, "$team0_level_archers", ":cur_level"),
				(val_add, "$team0_weapon_length_archers", ":cur_weapon_length"),
				(try_begin),
					(le, ":cur_ammo", 0),
					(assign, "$team0_archers_have_ammo", 0),
				(try_end),
				(try_begin),
					(eq, ":cur_weapon_type", itp_type_thrown),
					(val_add, "$team0_percent_ranged_throw", 1),
				(try_end),
			(else_try),
				(eq, ":bgteam", 1),
				(val_add, "$team1_num_archers", 1),
				(val_add, ":team1_x_archers", ":x_value"),
				(val_add, ":team1_y_archers", ":y_value"),
				(val_add, "$team1_level_archers", ":cur_level"),
				(val_add, "$team1_weapon_length_archers", ":cur_weapon_length"),
				(try_begin),
					(le, ":cur_ammo", 0),
					(assign, "$team1_archers_have_ammo", 0),
				(try_end),
				(try_begin),
					(eq, ":cur_weapon_type", itp_type_thrown),
					(val_add, "$team1_percent_ranged_throw", 1),
				(try_end),
			(else_try),
				(eq, ":bgteam", 2),
				(val_add, "$team2_num_archers", 1),
				(val_add, ":team2_x_archers", ":x_value"),
				(val_add, ":team2_y_archers", ":y_value"),
				(val_add, "$team2_level_archers", ":cur_level"),
				(val_add, "$team2_weapon_length_archers", ":cur_weapon_length"),
				(try_begin),
					(le, ":cur_ammo", 0),
					(assign, "$team2_archers_have_ammo", 0),
				(try_end),
				(try_begin),
					(eq, ":cur_weapon_type", itp_type_thrown),
					(val_add, "$team2_percent_ranged_throw", 1),
				(try_end),
			(else_try),
				(eq, ":bgteam", 3),
				(val_add, "$team3_num_archers", 1),
				(val_add, ":team3_x_archers", ":x_value"),
				(val_add, ":team3_y_archers", ":y_value"),
				(val_add, "$team3_level_archers", ":cur_level"),
				(val_add, "$team3_weapon_length_archers", ":cur_weapon_length"),
				(try_begin),
					(le, ":cur_ammo", 0),
					(assign, "$team3_archers_have_ammo", 0),
				(try_end),
				(try_begin),
					(eq, ":cur_weapon_type", itp_type_thrown),
					(val_add, "$team3_percent_ranged_throw", 1),
				(try_end),
			(try_end),
		(else_try),
			(eq, ":bgroup", grc_cavalry),	#AKA battle group 2
			(try_begin),
				(eq, ":bgteam", 0),
				(val_add, "$team0_num_cavalry", 1),
				(val_add, ":team0_x_cavalry", ":x_value"),
				(val_add, ":team0_y_cavalry", ":y_value"),
				(val_add, "$team0_level_cavalry", ":cur_level"),
				(val_add, "$team0_weapon_length_cavalry", ":cur_weapon_length"),
				(try_begin),
					(gt, ":cur_ammo", 0),
					(val_add, "$team0_percent_cavalry_are_archers", 1),
				(try_end),
			(else_try),
				(eq, ":bgteam", 1),
				(val_add, "$team1_num_cavalry", 1),
				(val_add, ":team1_x_cavalry", ":x_value"),
				(val_add, ":team1_y_cavalry", ":y_value"),
				(val_add, "$team1_level_cavalry", ":cur_level"),
				(val_add, "$team1_weapon_length_cavalry", ":cur_weapon_length"),
				(try_begin),
					(gt, ":cur_ammo", 0),
					(val_add, "$team1_percent_cavalry_are_archers", 1),
				(try_end),
			(else_try),
				(eq, ":bgteam", 2),
				(val_add, "$team2_num_cavalry", 1),
				(val_add, ":team2_x_cavalry", ":x_value"),
				(val_add, ":team2_y_cavalry", ":y_value"),
				(val_add, "$team2_level_cavalry", ":cur_level"),
				(val_add, "$team2_weapon_length_cavalry", ":cur_weapon_length"),
				(try_begin),
					(gt, ":cur_ammo", 0),
					(val_add, "$team2_percent_cavalry_are_archers", 1),
				(try_end),
			(else_try),
				(eq, ":bgteam", 3),
				(val_add, "$team3_num_cavalry", 1),
				(val_add, ":team3_x_cavalry", ":x_value"),
				(val_add, ":team3_y_cavalry", ":y_value"),
				(val_add, "$team3_level_cavalry", ":cur_level"),
				(val_add, "$team3_weapon_length_cavalry", ":cur_weapon_length"),
				(try_begin),
					(gt, ":cur_ammo", 0),
					(val_add, "$team3_percent_cavalry_are_archers", 1),
				(try_end),
			(try_end),
		(else_try),
			(eq, ":bgroup", 3),
			(val_add, "$teamp_num_group3", 1),
			(val_add, ":teamp_x_group3", ":x_value"),
			(val_add, ":teamp_y_group3", ":y_value"),
			(val_add, "$teamp_level_group3", ":cur_level"),
			(val_add, "$teamp_weapon_length_group3", ":cur_weapon_length"),
		(else_try),
			(eq, ":bgroup", 4),
			(val_add, "$teamp_num_group4", 1),
			(val_add, ":teamp_x_group4", ":x_value"),
			(val_add, ":teamp_y_group4", ":y_value"),
			(val_add, "$teamp_level_group4", ":cur_level"),
			(val_add, "$teamp_weapon_length_group4", ":cur_weapon_length"),
		(else_try),
			(eq, ":bgroup", 5),
			(val_add, "$teamp_num_group5", 1),
			(val_add, ":teamp_x_group5", ":x_value"),
			(val_add, ":teamp_y_group5", ":y_value"),
			(val_add, "$teamp_level_group5", ":cur_level"),
			(val_add, "$teamp_weapon_length_group5", ":cur_weapon_length"),
		(else_try),
			(eq, ":bgroup", 6),
			(val_add, "$teamp_num_group6", 1),
			(val_add, ":teamp_x_group6", ":x_value"),
			(val_add, ":teamp_y_group6", ":y_value"),
			(val_add, "$teamp_level_group6", ":cur_level"),
			(val_add, "$teamp_weapon_length_group6", ":cur_weapon_length"),
		(else_try),
			(eq, ":bgroup", 7),
			(val_add, "$teamp_num_group7", 1),
			(val_add, ":teamp_x_group7", ":x_value"),
			(val_add, ":teamp_y_group7", ":y_value"),
			(val_add, "$teamp_level_group7", ":cur_level"),
			(val_add, "$teamp_weapon_length_group7", ":cur_weapon_length"),
		(else_try),
			(eq, ":bgroup", 8),
			(val_add, "$teamp_num_group8", 1),
			(val_add, ":teamp_x_group8", ":x_value"),
			(val_add, ":teamp_y_group8", ":y_value"),
			(val_add, "$teamp_level_group8", ":cur_level"),
			(val_add, "$teamp_weapon_length_group8", ":cur_weapon_length"),
		(else_try),
			(eq, ":bgroup", -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_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_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_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_level_leader", ":cur_level"),
			(try_end),
		(try_end),
	(try_end),

	#calculate team sizes, sum positions
	(store_add, "$team0_size", "$team0_num_infantry", "$team0_num_archers"),
	(val_add, "$team0_size", "$team0_num_cavalry"),
	(val_add, "$team0_size", ":team0_leader"),
	(store_add, ":team0_x", ":team0_x_infantry", ":team0_x_archers"),
	(val_add, ":team0_x", ":team0_x_cavalry"),
	(val_add, ":team0_x", ":team0_x_leader"),
	(store_add, ":team0_y", ":team0_y_infantry", ":team0_y_archers"),
	(val_add, ":team0_y", ":team0_y_cavalry"),
	(val_add, ":team0_y", ":team0_y_leader"),
	(store_add, "$team0_level", "$team0_level_infantry", "$team0_level_archers"),
	(val_add, "$team0_level", "$team0_level_cavalry"),
	(val_add, "$team0_level", ":team0_level_leader"),

	(store_add, "$team1_size", "$team1_num_infantry", "$team1_num_archers"),
	(val_add, "$team1_size", "$team1_num_cavalry"),
	(val_add, "$team1_size", ":team1_leader"),
	(store_add, ":team1_x", ":team1_x_infantry", ":team1_x_archers"),
	(val_add, ":team1_x", ":team1_x_cavalry"),
	(val_add, ":team1_x", ":team1_x_leader"),
	(store_add, ":team1_y", ":team1_y_infantry", ":team1_y_archers"),
	(val_add, ":team1_y", ":team1_y_cavalry"),
	(val_add, ":team1_y", ":team1_y_leader"),
	(store_add, "$team1_level", "$team1_level_infantry", "$team1_level_archers"),
	(val_add, "$team1_level", "$team1_level_cavalry"),
	(val_add, "$team1_level", ":team1_level_leader"),

	(store_add, "$team2_size", "$team2_num_infantry", "$team2_num_archers"),
	(val_add, "$team2_size", "$team2_num_cavalry"),
	(val_add, "$team2_size", ":team2_leader"),
	(store_add, ":team2_x", ":team2_x_infantry", ":team2_x_archers"),
	(val_add, ":team2_x", ":team2_x_cavalry"),
	(val_add, ":team2_x", ":team2_x_leader"),
	(store_add, ":team2_y", ":team2_y_infantry", ":team2_y_archers"),
	(val_add, ":team2_y", ":team2_y_cavalry"),
	(val_add, ":team2_y", ":team2_y_leader"),
	(store_add, "$team2_level", "$team2_level_infantry", "$team2_level_archers"),
	(val_add, "$team2_level", "$team2_level_cavalry"),
	(val_add, "$team2_level", ":team2_level_leader"),

	(store_add, "$team3_size", "$team3_num_infantry", "$team3_num_archers"),
	(val_add, "$team3_size", "$team3_num_cavalry"),
	(val_add, "$team3_size", ":team3_leader"),
	(store_add, ":team3_x", ":team3_x_infantry", ":team3_x_archers"),
	(val_add, ":team3_x", ":team3_x_cavalry"),
	(val_add, ":team3_x", ":team3_x_leader"),
	(store_add, ":team3_y", ":team3_y_infantry", ":team3_y_archers"),
	(val_add, ":team3_y", ":team3_y_cavalry"),
	(val_add, ":team3_y", ":team3_y_leader"),
	(store_add, "$team3_level", "$team3_level_infantry", "$team3_level_archers"),
	(val_add, "$team3_level", "$team3_level_cavalry"),
	(val_add, "$team3_level", ":team3_level_leader"),

	(store_add, ":additional_troops", "$teamp_num_group3", "$teamp_num_group4"),
	(val_add, ":additional_troops", "$teamp_num_group5"),
	(val_add, ":additional_troops", "$teamp_num_group6"),
	(val_add, ":additional_troops", "$teamp_num_group7"),
	(val_add, ":additional_troops", "$teamp_num_group8"),
	(store_add, ":additional_level", "$teamp_level_group3", "$teamp_level_group4"),
	(val_add, ":additional_level", "$teamp_level_group5"),
	(val_add, ":additional_level", "$teamp_level_group6"),
	(val_add, ":additional_level", "$teamp_level_group7"),
	(val_add, ":additional_level", "$teamp_level_group8"),
	(try_begin),
		(eq, "$fplayer_team_no", 0),
		(val_add, "$team0_size", ":additional_troops"),
		(val_add, "$team0_level", ":additional_level"),
	(else_try),
		(eq, "$fplayer_team_no", 1),
		(val_add, "$team1_size", ":additional_troops"),
		(val_add, "$team1_level", ":additional_level"),
	(else_try),
		(eq, "$fplayer_team_no", 2),
		(val_add, "$team2_size", ":additional_troops"),
		(val_add, "$team2_level", ":additional_level"),
	(else_try),
		(eq, "$fplayer_team_no", 3),
		(val_add, "$team3_size", ":additional_troops"),
		(val_add, "$team3_level", ":additional_level"),
	(try_end),
	
	#calculate team averages
	(init_position, Team0_Average_Pos),
	(try_begin),
		(gt, "$team0_size", 0),
		(val_div, ":team0_x", "$team0_size"),
		(position_set_x, Team0_Average_Pos, ":team0_x"),
		(val_div, ":team0_y", "$team0_size"),
		(position_set_y, Team0_Average_Pos, ":team0_y"),
		(position_set_z_to_ground_level, Team0_Average_Pos),
		(val_div, "$team0_level", "$team0_size"),
	(try_end),
	(init_position, Team1_Average_Pos),
	(try_begin),
		(gt, "$team1_size", 0),
		(val_div, ":team1_x", "$team1_size"),
		(position_set_x, Team1_Average_Pos, ":team1_x"),
		(val_div, ":team1_y", "$team1_size"),
		(position_set_y, Team1_Average_Pos, ":team1_y"),
		(position_set_z_to_ground_level, Team1_Average_Pos),
		(val_div, "$team1_level", "$team1_size"),
	(try_end),
	(init_position, Team2_Average_Pos),
	(try_begin),
		(gt, "$team2_size", 0),
		(val_div, ":team2_x", "$team2_size"),
		(position_set_x, Team2_Average_Pos, ":team2_x"),
		(val_div, ":team2_y", "$team2_size"),
		(position_set_y, Team2_Average_Pos, ":team2_y"),
		(position_set_z_to_ground_level, Team2_Average_Pos),
		(val_div, "$team2_level", "$team2_size"),
	(try_end),
	(init_position, Team3_Average_Pos),
	(try_begin),
		(gt, "$team3_size", 0),
		(val_div, ":team3_x", "$team3_size"),
		(position_set_x, Team3_Average_Pos, ":team3_x"),
		(val_div, ":team3_y", "$team3_size"),
		(position_set_y, Team3_Average_Pos, ":team3_y"),
		(position_set_z_to_ground_level, Team3_Average_Pos),
		(val_div, "$team3_level", "$team3_size"),
	(try_end),


	#calculate battle group averages
	(init_position, Team0_Infantry_Pos),
	(try_begin),
		(gt, "$team0_num_infantry", 0),
		(val_div, ":team0_x_infantry", "$team0_num_infantry"),
		(position_set_x, Team0_Infantry_Pos, ":team0_x_infantry"),
		(val_div, ":team0_y_infantry", "$team0_num_infantry"),
		(position_set_y, Team0_Infantry_Pos, ":team0_y_infantry"),
		(position_set_z_to_ground_level, Team0_Infantry_Pos),
		(val_div, "$team0_level_infantry", "$team0_num_infantry"),
		(val_div, "$team0_weapon_length_infantry", "$team0_num_infantry"),
	(try_end),
	(init_position, Team1_Infantry_Pos),
	(try_begin),
		(gt, "$team1_num_infantry", 0),
		(val_div, ":team1_x_infantry", "$team1_num_infantry"),
		(position_set_x, Team1_Infantry_Pos, ":team1_x_infantry"),
		(val_div, ":team1_y_infantry", "$team1_num_infantry"),
		(position_set_y, Team1_Infantry_Pos, ":team1_y_infantry"),
		(position_set_z_to_ground_level, Team1_Infantry_Pos),
		(val_div, "$team1_level_infantry", "$team1_num_infantry"),
		(val_div, "$team1_weapon_length_infantry", "$team1_num_infantry"),
	(try_end),
	(init_position, Team2_Infantry_Pos),
	(try_begin),
		(gt, "$team2_num_infantry", 0),
		(val_div, ":team2_x_infantry", "$team2_num_infantry"),
		(position_set_x, Team2_Infantry_Pos, ":team2_x_infantry"),
		(val_div, ":team2_y_infantry", "$team2_num_infantry"),
		(position_set_y, Team2_Infantry_Pos, ":team2_y_infantry"),
		(position_set_z_to_ground_level, Team2_Infantry_Pos),
		(val_div, "$team2_level_infantry", "$team2_num_infantry"),
		(val_div, "$team2_weapon_length_infantry", "$team2_num_infantry"),
	(try_end),
	(init_position, Team3_Infantry_Pos),
	(try_begin),
		(gt, "$team3_num_infantry", 0),
		(val_div, ":team3_x_infantry", "$team3_num_infantry"),
		(position_set_x, Team3_Infantry_Pos, ":team3_x_infantry"),
		(val_div, ":team3_y_infantry", "$team3_num_infantry"),
		(position_set_y, Team3_Infantry_Pos, ":team3_y_infantry"),
		(position_set_z_to_ground_level, Team3_Infantry_Pos),
		(val_div, "$team3_level_infantry", "$team3_num_infantry"),
		(val_div, "$team3_weapon_length_infantry", "$team3_num_infantry"),
	(try_end),
	(init_position, Team0_Archers_Pos),
	(try_begin),
		(gt, "$team0_num_archers", 0),
		(val_div, ":team0_x_archers", "$team0_num_archers"),
		(position_set_x, Team0_Archers_Pos, ":team0_x_archers"),
		(val_div, ":team0_y_archers", "$team0_num_archers"),
		(position_set_y, Team0_Archers_Pos, ":team0_y_archers"),
		(position_set_z_to_ground_level, Team0_Archers_Pos),
		(val_div, "$team0_level_archers", "$team0_num_archers"),
		(val_div, "$team0_weapon_length_archers", "$team0_num_archers"),
		(val_mul, "$team0_percent_ranged_throw", 100),
		(val_div, "$team0_percent_ranged_throw", "$team0_num_archers"),
	(try_end),
	(init_position, Team1_Archers_Pos),
	(try_begin),
		(gt, "$team1_num_archers", 0),
		(val_div, ":team1_x_archers", "$team1_num_archers"),
		(position_set_x, Team1_Archers_Pos, ":team1_x_archers"),
		(val_div, ":team1_y_archers", "$team1_num_archers"),
		(position_set_y, Team1_Archers_Pos, ":team1_y_archers"),
		(position_set_z_to_ground_level, Team1_Archers_Pos),
		(val_div, "$team1_level_archers", "$team1_num_archers"),
		(val_div, "$team1_weapon_length_archers", "$team1_num_archers"),
		(val_mul, "$team1_percent_ranged_throw", 100),
		(val_div, "$team1_percent_ranged_throw", "$team1_num_archers"),
	(try_end),
	(init_position, Team2_Archers_Pos),
	(try_begin),
		(gt, "$team2_num_archers", 0),
		(val_div, ":team2_x_archers", "$team2_num_archers"),
		(position_set_x, Team2_Archers_Pos, ":team2_x_archers"),
		(val_div, ":team2_y_archers", "$team2_num_archers"),
		(position_set_y, Team2_Archers_Pos, ":team2_y_archers"),
		(position_set_z_to_ground_level, Team2_Archers_Pos),
		(val_div, "$team2_level_archers", "$team2_num_archers"),
		(val_div, "$team2_weapon_length_archers", "$team2_num_archers"),
		(val_mul, "$team2_percent_ranged_throw", 100),
		(val_div, "$team2_percent_ranged_throw", "$team2_num_archers"),
	(try_end),
	(init_position, Team3_Archers_Pos),
	(try_begin),
		(gt, "$team3_num_archers", 0),
		(val_div, ":team3_x_archers", "$team3_num_archers"),
		(position_set_x, Team3_Archers_Pos, ":team3_x_archers"),
		(val_div, ":team3_y_archers", "$team3_num_archers"),
		(position_set_y, Team3_Archers_Pos, ":team3_y_archers"),
		(position_set_z_to_ground_level, Team3_Archers_Pos),
		(val_div, "$team3_level_archers", "$team3_num_archers"),
		(val_div, "$team3_weapon_length_archers", "$team3_num_archers"),
		(val_mul, "$team3_percent_ranged_throw", 100),
		(val_div, "$team3_percent_ranged_throw", "$team3_num_archers"),
	(try_end),
	(init_position, Team0_Cavalry_Pos),
	(try_begin),
		(gt, "$team0_num_cavalry", 0),
		(val_div, ":team0_x_cavalry", "$team0_num_cavalry"),
		(position_set_x, Team0_Cavalry_Pos, ":team0_x_cavalry"),
		(val_div, ":team0_y_cavalry", "$team0_num_cavalry"),
		(position_set_y, Team0_Cavalry_Pos, ":team0_y_cavalry"),
		(position_set_z_to_ground_level, Team0_Cavalry_Pos),
		(val_div, "$team0_level_cavalry", "$team0_num_cavalry"),
		(val_div, "$team0_weapon_length_cavalry", "$team0_num_cavalry"),
		(val_mul, "$team0_percent_cavalry_are_archers", 100),
		(val_div, "$team0_percent_cavalry_are_archers", "$team0_num_cavalry"),
	(try_end),
	(init_position, Team1_Cavalry_Pos),
	(try_begin),
		(gt, "$team1_num_cavalry", 0),
		(val_div, ":team1_x_cavalry", "$team1_num_cavalry"),
		(position_set_x, Team1_Cavalry_Pos, ":team1_x_cavalry"),
		(val_div, ":team1_y_cavalry", "$team1_num_cavalry"),
		(position_set_y, Team1_Cavalry_Pos, ":team1_y_cavalry"),
		(position_set_z_to_ground_level, Team1_Cavalry_Pos),
		(val_div, "$team1_level_cavalry", "$team1_num_cavalry"),
		(val_div, "$team1_weapon_length_cavalry", "$team1_num_cavalry"),
		(val_mul, "$team1_percent_cavalry_are_archers", 100),
		(val_div, "$team1_percent_cavalry_are_archers", "$team1_num_cavalry"),
	(try_end),
	(init_position, Team2_Cavalry_Pos),
	(try_begin),
		(gt, "$team2_num_cavalry", 0),
		(val_div, ":team2_x_cavalry", "$team2_num_cavalry"),
		(position_set_x, Team2_Cavalry_Pos, ":team2_x_cavalry"),
		(val_div, ":team2_y_cavalry", "$team2_num_cavalry"),
		(position_set_y, Team2_Cavalry_Pos, ":team2_y_cavalry"),
		(position_set_z_to_ground_level, Team2_Cavalry_Pos),
		(val_div, "$team2_level_cavalry", "$team2_num_cavalry"),
		(val_div, "$team2_weapon_length_cavalry", "$team2_num_cavalry"),
		(val_mul, "$team2_percent_cavalry_are_archers", 100),
		(val_div, "$team2_percent_cavalry_are_archers", "$team2_num_cavalry"),
	(try_end),
	(init_position, Team3_Cavalry_Pos),
	(try_begin),
		(gt, "$team3_num_cavalry", 0),
		(val_div, ":team3_x_cavalry", "$team3_num_cavalry"),
		(position_set_x, Team3_Cavalry_Pos, ":team3_x_cavalry"),
		(val_div, ":team3_y_cavalry", "$team3_num_cavalry"),
		(position_set_y, Team3_Cavalry_Pos, ":team3_y_cavalry"),
		(position_set_z_to_ground_level, Team3_Cavalry_Pos),
		(val_div, "$team3_level_cavalry", "$team3_num_cavalry"),
		(val_div, "$team3_weapon_length_cavalry", "$team3_num_cavalry"),
		(val_mul, "$team3_percent_cavalry_are_archers", 100),
		(val_div, "$team3_percent_cavalry_are_archers", "$team3_num_cavalry"),
	(try_end),

	(init_position, Player_Battle_Group3_Pos),
	(try_begin),
		(gt, "$teamp_num_group3", 0),
		(val_div, ":teamp_x_group3", "$teamp_num_group3"),
		(position_set_x, Player_Battle_Group3_Pos, ":teamp_x_group3"),
		(val_div, ":teamp_y_group3", "$teamp_num_group3"),
		(position_set_y, Player_Battle_Group3_Pos, ":teamp_y_group3"),
		(position_set_z_to_ground_level, Player_Battle_Group3_Pos),
		(val_div, "$teamp_level_group3", "$teamp_num_group3"),
		(val_div, "$teamp_weapon_length_group3", "$teamp_num_group3"),
	(try_end),
	(init_position, Player_Battle_Group4_Pos),
	(try_begin),
		(gt, "$teamp_num_group4", 0),
		(val_div, ":teamp_x_group4", "$teamp_num_group4"),
		(position_set_x, Player_Battle_Group4_Pos, ":teamp_x_group4"),
		(val_div, ":teamp_y_group4", "$teamp_num_group4"),
		(position_set_y, Player_Battle_Group4_Pos, ":teamp_y_group4"),
		(position_set_z_to_ground_level, Player_Battle_Group4_Pos),
		(val_div, "$teamp_level_group4", "$teamp_num_group4"),
		(val_div, "$teamp_weapon_length_group4", "$teamp_num_group4"),
	(try_end),
	(init_position, Player_Battle_Group5_Pos),
	(try_begin),
		(gt, "$teamp_num_group5", 0),
		(val_div, ":teamp_x_group5", "$teamp_num_group5"),
		(position_set_x, Player_Battle_Group5_Pos, ":teamp_x_group5"),
		(val_div, ":teamp_y_group5", "$teamp_num_group5"),
		(position_set_y, Player_Battle_Group5_Pos, ":teamp_y_group5"),
		(position_set_z_to_ground_level, Player_Battle_Group5_Pos),
		(val_div, "$teamp_level_group5", "$teamp_num_group5"),
		(val_div, "$teamp_weapon_length_group5", "$teamp_num_group5"),
	(try_end),
	(init_position, Player_Battle_Group6_Pos),
	(try_begin),
		(gt, "$teamp_num_group6", 0),
		(val_div, ":teamp_x_group6", "$teamp_num_group6"),
		(position_set_x, Player_Battle_Group6_Pos, ":teamp_x_group6"),
		(val_div, ":teamp_y_group6", "$teamp_num_group6"),
		(position_set_y, Player_Battle_Group6_Pos, ":teamp_y_group6"),
		(position_set_z_to_ground_level, Player_Battle_Group6_Pos),
		(val_div, "$teamp_level_group6", "$teamp_num_group6"),
		(val_div, "$teamp_weapon_length_group6", "$teamp_num_group6"),
	(try_end),
	(init_position, Player_Battle_Group7_Pos),
	(try_begin),
		(gt, "$teamp_num_group7", 0),
		(val_div, ":teamp_x_group7", "$teamp_num_group7"),
		(position_set_x, Player_Battle_Group7_Pos, ":teamp_x_group7"),
		(val_div, ":teamp_y_group7", "$teamp_num_group7"),
		(position_set_y, Player_Battle_Group7_Pos, ":teamp_y_group7"),
		(position_set_z_to_ground_level, Player_Battle_Group7_Pos),
		(val_div, "$teamp_level_group7", "$teamp_num_group7"),
		(val_div, "$teamp_weapon_length_group7", "$teamp_num_group7"),
	(try_end),
	(init_position, Player_Battle_Group8_Pos),
	(try_begin),
		(gt, "$teamp_num_group8", 0),
		(val_div, ":teamp_x_group8", "$teamp_num_group8"),
		(position_set_x, Player_Battle_Group8_Pos, ":teamp_x_group8"),
		(val_div, ":teamp_y_group8", "$teamp_num_group8"),
		(position_set_y, Player_Battle_Group8_Pos, ":teamp_y_group8"),
		(position_set_z_to_ground_level, Player_Battle_Group8_Pos),
		(val_div, "$teamp_level_group8", "$teamp_num_group8"),
		(val_div, "$teamp_weapon_length_group8", "$teamp_num_group8"),
	(try_end),
	]),


  # script_battlegroup_get_size by motomataru
  # Input: team, battle group (troop class)
  # Output:	reg0 contains size
  #			size of whole team if "troop class" input NOT set to 0-8
  # NB: Assumes that battle groups beyond 2 are PLAYER team
  ("battlegroup_get_size", [
	(store_script_param, ":bgteam", 1),
	(store_script_param, ":bgroup", 2),
	(try_begin),
		(eq, ":bgroup", grc_infantry),	#AKA battle group 0
		(try_begin),
			(eq, ":bgteam", 0),
			(assign, reg0, "$team0_num_infantry"),
		(else_try),
			(eq, ":bgteam", 1),
			(assign, reg0, "$team1_num_infantry"),
		(else_try),
			(eq, ":bgteam", 2),
			(assign, reg0, "$team2_num_infantry"),
		(else_try),
			(eq, ":bgteam", 3),
			(assign, reg0, "$team3_num_infantry"),
		(try_end),
	(else_try),
		(eq, ":bgroup", grc_archers),	#AKA battle group 1
		(try_begin),
			(eq, ":bgteam", 0),
			(assign, reg0, "$team0_num_archers"),
		(else_try),
			(eq, ":bgteam", 1),
			(assign, reg0, "$team1_num_archers"),
		(else_try),
			(eq, ":bgteam", 2),
			(assign, reg0, "$team2_num_archers"),
		(else_try),
			(eq, ":bgteam", 3),
			(assign, reg0, "$team3_num_archers"),
		(try_end),
	(else_try),
		(eq, ":bgroup", grc_cavalry),	#AKA battle group 2
		(try_begin),
			(eq, ":bgteam", 0),
			(assign, reg0, "$team0_num_cavalry"),
		(else_try),
			(eq, ":bgteam", 1),
			(assign, reg0, "$team1_num_cavalry"),
		(else_try),
			(eq, ":bgteam", 2),
			(assign, reg0, "$team2_num_cavalry"),
		(else_try),
			(eq, ":bgteam", 3),
			(assign, reg0, "$team3_num_cavalry"),
		(try_end),
	(else_try),
		(eq, ":bgroup", 3),
		(assign, reg0, "$teamp_num_group3"),
	(else_try),
		(eq, ":bgroup", 4),
		(assign, reg0, "$teamp_num_group4"),
	(else_try),
		(eq, ":bgroup", 5),
		(assign, reg0, "$teamp_num_group5"),
	(else_try),
		(eq, ":bgroup", 6),
		(assign, reg0, "$teamp_num_group6"),
	(else_try),
		(eq, ":bgroup", 7),
		(assign, reg0, "$teamp_num_group7"),
	(else_try),
		(eq, ":bgroup", 8),
		(assign, reg0, "$teamp_num_group8"),
	(else_try),	#undefined battle group from here on
		(eq, ":bgteam", 0),
		(assign, reg0, "$team0_size"),
	(else_try),
		(eq, ":bgteam", 1),
		(assign, reg0, "$team1_size"),
	(else_try),
		(eq, ":bgteam", 2),
		(assign, reg0, "$team2_size"),
	(else_try),
		(eq, ":bgteam", 3),
		(assign, reg0, "$team3_size"),
	(try_end),
  ]),	

 
  # 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_for_range, ":other_team", 0, 4),
		(teams_are_enemies, ":other_team", ":team_no"),
		(try_begin),
			(eq, ":troop_type", grc_everyone),
			(call_script, "script_battlegroup_get_size", ":other_team", grc_everyone),
			(assign, ":team_size", reg0),
			(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),
			(assign, ":team_size", 0),
			(try_begin),
				(eq, ":other_team", "$fplayer_team_no"),
				(assign, ":num_groups", 9),
			(else_try),
				(assign, ":num_groups", 3),
			(try_end),
			(try_for_range, ":enemy_battle_group", 0, ":num_groups"),
				(eq, ":enemy_battle_group", ":troop_type"),
				(call_script, "script_battlegroup_get_size", ":other_team", ":troop_type"),
				(gt, reg0, 0),
				(val_add, ":team_size", reg0),
				(call_script, "script_battlegroup_get_position", ":enemy_position", ":other_team", ":troop_type"),
				(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),
		(try_end),
		(val_add, ":total_size", ":team_size"),
	(try_end),
	
	(try_begin),
		(eq, ":total_size", 0),
		(init_position, ":enemy_position"),
	(else_try),
		(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"),
	(try_end),

	(assign, reg0, ":total_size"),
  ]),


# # M&B Standard AI with changes for formations
  # script_formation_battle_tactic_init_aux
  # Input: team_no, battle_tactic
  # Output: none
  ("formation_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),
# formations additions
	  (try_begin),
		(call_script, "script_cf_formation", ":team_no", grc_infantry, 0, formation_default),
	  (try_end),
	  (try_begin),
		(call_script, "script_cf_formation", ":team_no", grc_cavalry, 0, formation_wedge),
	  (try_end),
	  (try_begin),
		(call_script, "script_cf_formation", ":team_no", grc_archers, 2, formation_default),
	  (try_end),
	  (team_give_order, ":team_no", grc_archers, mordr_spread_out),
	  (team_give_order, ":team_no", grc_archers, mordr_spread_out),
# end formations additions
  ]),
  
  # script_formation_battle_tactic_apply_aux
  # Input: team_no, battle_tactic
  # Output: battle_tactic
  ("formation_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),
		  (call_script, "script_formation_end", ":team_no", grc_everyone),	#formations
          (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),
          (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),
          (ge, ":ai_leader", 0),
          (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),
#formations code
		  (call_script, "script_point_y_toward_position", pos63, pos60),
		  (agent_get_position, pos49, ":ai_leader"),
		  (agent_set_position, ":ai_leader", pos63),	#fake out script_cf_formation
		  (try_begin),
			(call_script, "script_cf_formation", ":team_no", grc_infantry, 0, formation_default),
		  (try_end),
		  (try_begin),
			(call_script, "script_cf_formation", ":team_no", grc_cavalry, 0, formation_wedge),
		  (try_end),
		  (try_begin),
			(call_script, "script_cf_formation", ":team_no", grc_archers, 2, formation_default),
		  (try_end),
		  (agent_set_position, ":ai_leader", pos49),
#end formations code
          (agent_get_position, pos1, ":ai_leader"),
          (try_begin),
            (lt, ":distance_to_enemy", 50),
            (ge, ":mission_time", 30),
            (assign, ":battle_tactic", 0),
			(call_script, "script_formation_end", ":team_no", grc_everyone),	#formations code
            (team_give_order, ":team_no", grc_everyone, mordr_charge),
            (agent_set_speed_limit, ":ai_leader", 60),
          (try_end),
        (else_try),
          (assign, ":battle_tactic", 0),
		  (call_script, "script_formation_end", ":team_no", grc_everyone),	#formations code
          (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),
		(call_script, "script_formation_end", ":team_no", grc_everyone),	#formations code
        (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"),
  ]),
  
  # Replacement script for battle_tactic_init_aux to switch between using
  # M&B Standard AI with changes for formations and original based on
  # NOTE: original script "battle_tactic_init_aux" should be renamed to "orig_battle_tactic_init_aux"
  # constant formation_native_ai_use_formation ( 0: original, 1: use formation )
  # 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),
	  (try_begin),
		(eq, formation_native_ai_use_formation, 1),
		(call_script, "script_formation_battle_tactic_init_aux", ":team_no", ":battle_tactic"),
	  (else_try),
		(call_script, "script_orig_battle_tactic_init_aux", ":team_no", ":battle_tactic"),
	  (try_end),
    ]),

  # Replacement script for battle_tactic_init_aux to switch between using
  # M&B Standard AI with changes for formations and original based on
  # NOTE: original script "battle_tactic_apply_aux" should be renamed to "orig_battle_tactic_apply_aux"
  # constant formation_native_ai_use_formation ( 0: original, 1: use formation )
  # 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),
	  (try_begin),
		(eq, formation_native_ai_use_formation, 1),
		(call_script, "script_formation_battle_tactic_apply_aux", ":team_no", ":battle_tactic"),
	  (else_try),
		(call_script, "script_orig_battle_tactic_apply_aux", ":team_no", ":battle_tactic"),
	  (try_end),
  ]),

] # scripts


from util_wrappers import *
from util_scripts import *

scripts_directives = [
	#rename scripts to "insert" switch scripts (see end of scripts[])
	[SD_RENAME, "battle_tactic_init_aux" , "orig_battle_tactic_init_aux"],
	[SD_RENAME, "battle_tactic_apply_aux" , "orig_battle_tactic_apply_aux"],
	#insert formations before last call in team_give_order_from_order_panel
	[SD_OP_BLOCK_INSERT, "team_give_order_from_order_panel", D_SEARCH_FROM_BOTTOM | D_SEARCH_LINENUMBER | D_INSERT_BEFORE, 0, 0, [
		(call_script, "script_player_order_formations", ":order"),	#for formations
	]],
] # scripts_rename

def modmerge_formations_scripts(orig_scripts):
	# process script directives first
	process_script_directives(orig_scripts, scripts_directives)
	# add remaining scripts
	add_scripts(orig_scripts, scripts, True)
# Formations for Warband by Motomataru # rel. 06/08/11 #################################################################################################################### # 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. #################################################################################################################### # #Formations Scripts # script_cf_formation v2 by motomataru # Input: team, troop class, spacing, formation type # Output: reg0 number of troops in formation, pos61 formation position # Formation(s) form near team leader (player) #any team members left? #more than 0 enemies still alive? #cavalry set up 5m RIGHT of leader #infantry set up 1m LEFT of leader #archers set up 10m FRONT of leader #unsupported division #	(team_set_order_position, ":fteam", ":fclass", pos61), # script_form_cavalry v2 by motomataru # Input: team, agent number of team leader, spacing # Output: reg0 troop count # Form in wedge, (now not) excluding horse archers # Creates formation starting at pos1 #larger y minimum distance to accommodate mounts #put troops with highest exp in front # script_form_archers v2 by motomataru # Input: team, agent number of team leader, spacing # Output: reg0 troop count # Form in staggered line both directions # Creates formation starting at pos1 #minimum distance between troops #staggering variable # script_form_infantry v2 by motomataru # Input: (pos1), team, agent number of team leader, spacing, formation # Output: reg0 troop count # If input "formation" is formation_default, will select a formation based on faction # Creates formation starting at pos1 #minimum distance between troops #put troops with highest exp in front #basic three ranks #put troops with highest exp in front #basic three ranks #shields #short weapons #pole weapons # script_get_default_formation by motomataru # Input: team id # Output: reg0 default formation #assign default formation #Swadians #Vaegirs #Khergit #Khergit have underdeveloped infantry #Nords #Rhodoks #Sarranid #independent player #riffraff don't use formations # script_formation_current_position by motomataru # Input: destination position, team, troop class, formation type, extra spacing # Output: in destination position # script_get_centering_amount by motomataru # Input: formation type, number of infantry, 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 #assume these are archers in a line # script_formation_end # Input: team, troop class # Output: none # script_formation_move_position v2 by motomataru # Input: team, troop class, formation current position, formation type, extra spacing, (1 to advance or -1 to withdraw or 0 to redirect) # Output: pos1 (offset for centering) #more than 0 enemies still alive? #restore current formation "position" #record angle from center to enemy #		(team_get_order_position, pos61, ":fteam", ":fclass"), #distance already moving from previous orders #moving forward? #less than a move away? #avoid rotating formation #		(team_set_order_position, ":fteam", ":fclass", pos1), # script_set_formation_position by motomataru # Input: team, troop class, position # Kluge around buggy *_order_position functions for teams 0-3 # script_get_formation_position by motomataru # Input: position, team, troop class # Output: input position (pos0 used) # Kluge around buggy *_order_position functions for teams 0-3 # "launder" team_get_order_position shutting down position_move_x # script_get_first_formation_member by motomataru # Input: team, troop class, formation # Output: reg0 leader agent number or -1 # Ought to follow script_form_infantry #must have polearm w/o shields... #must be a level sort # script_cf_valid_formation_member by motomataru # Input: team, troop class, agent number of team leader, test agent # Output: failure indicates agent is not member of formation # (store_script_param, ":fleader", 3), # script_classify_agent by motomataru # Input: agent # Output: reg0 (-1 if leader) # Assigns a "division" to the agent # #Player team formations functions # script_player_attempt_formation # Inputs:	arg1: troop class (grc_*) #			arg2: formation identifier (formation_*) # Output: none #spread out for ease of forming up #spread out for ease of forming up #must be archers #spread out for ease of forming up # script_player_order_formations by motomataru # Inputs:	arg1: order to formation (mordr_*) # Output: none #On hold, any formations reform in new location #Follow is hold	repeated frequently #charge or retreat ends formation #dismount ends formation #on change of orders cancel order position # (team_set_order_position, "$fplayer_team_no", grc_infantry, pos63), #on change of orders cancel order position # (team_set_order_position, "$fplayer_team_no", grc_cavalry, pos63), #on change of orders cancel order position # (team_set_order_position, "$fplayer_team_no", grc_archers, pos63), #on change of orders cancel order position # (team_set_order_position, "$fplayer_team_no", grc_infantry, pos63), #on change of orders cancel order position # (team_set_order_position, "$fplayer_team_no", grc_cavalry, pos63), #on change of orders cancel order position # (team_set_order_position, "$fplayer_team_no", grc_archers, pos63), #			(team_get_order_position, pos1, "$fplayer_team_no", grc_infantry), #			(team_get_order_position, pos1, "$fplayer_team_no", grc_cavalry), #			(team_get_order_position, pos1, "$fplayer_team_no", grc_archers), #			(team_get_order_position, pos1, "$fplayer_team_no", grc_infantry), #			(team_get_order_position, pos1, "$fplayer_team_no", grc_cavalry), #			(team_get_order_position, pos1, "$fplayer_team_no", grc_archers), # #Utilities used by formations # script_point_y_toward_position by motomataru # Input: from position, to position # Output: reg0 fixed point distance #avoid error on compile #precision 90/2exp6 (around 2 degrees) #point y-axis at destination # script_store_battlegroup_data by motomataru # Input: none # Output: sets positions and globals to track data on ALL groups in a battle # Globals used: pos1, reg0, reg1, positions 24-45 #AKA battle group 0 #AKA battle group 1 #AKA battle group 2 #leaders #calculate team sizes, sum positions #calculate team averages #calculate battle group averages # script_battlegroup_get_size by motomataru # Input: team, battle group (troop class) # Output:	reg0 contains size #			size of whole team if "troop class" input NOT set to 0-8 # NB: Assumes that battle groups beyond 2 are PLAYER team #AKA battle group 0 #AKA battle group 1 #AKA battle group 2 #undefined battle group from here on # 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! # # M&B Standard AI with changes for formations # script_formation_battle_tactic_init_aux # Input: team_no, battle_tactic # Output: none # call again just in case we are not at peak point. # call again just in case we are not at peak point. # formations additions # end formations additions # script_formation_battle_tactic_apply_aux # Input: team_no, battle_tactic # Output: battle_tactic #formations #pos62 = vector to enemy w.r.t leader #move 23 meters #pos63 is 23m away from leader in the direction of the enemy. #formations code #fake out script_cf_formation #end formations code #formations code #formations code # charge everyone after a while #formations code # Replacement script for battle_tactic_init_aux to switch between using # M&B Standard AI with changes for formations and original based on # NOTE: original script "battle_tactic_init_aux" should be renamed to "orig_battle_tactic_init_aux" # constant formation_native_ai_use_formation ( 0: original, 1: use formation ) # script_battle_tactic_init_aux # Input: team_no, battle_tactic # Output: none # Replacement script for battle_tactic_init_aux to switch between using # M&B Standard AI with changes for formations and original based on # NOTE: original script "battle_tactic_apply_aux" should be renamed to "orig_battle_tactic_apply_aux" # constant formation_native_ai_use_formation ( 0: original, 1: use formation ) # script_battle_tactic_apply_aux # Input: team_no, battle_tactic # Output: battle_tactic # scripts #rename scripts to "insert" switch scripts (see end of scripts[]) #insert formations before last call in team_give_order_from_order_panel #for formations # scripts_rename # process script directives first # add remaining scripts