(store_script_param, ":agent", 1),
(store_script_param, ":force_shield", 2),
(store_script_param, ":force_length", 3),
(store_script_param, ":fire_order", 4),
#priority items
(assign, ":shield", itm_no_item),
(assign, ":weapon", itm_no_item),
(try_for_range, ":item_slot", ek_item_0, ek_head),
(agent_get_item_slot, ":item", ":agent", ":item_slot"),
(gt, ":item", itm_no_item),
(item_get_type, ":weapon_type", ":item"),
(try_begin),
(eq, ":weapon_type", itp_type_shield),
(assign, ":shield", ":item"),
(else_try),
(eq, ":weapon_type", itp_type_thrown),
(eq, ":fire_order", aordr_fire_at_will),
# (agent_get_ammo, ":ammo", ":agent", 0), #assume infantry would have no other kind of ranged weapon
# (gt, ":ammo", 0),
(assign, ":weapon", ":item"), #use thrown weapons first
(try_end),
(try_end),
#select weapon
(try_begin),
(eq, ":weapon", itm_no_item),
(assign, ":cur_score", 0),
(try_for_range, ":item_slot", ek_item_0, ek_head),
(agent_get_item_slot, ":item", ":agent", ":item_slot"),
(gt, ":item", itm_no_item),
(item_get_type, ":weapon_type", ":item"),
(neq, ":weapon_type", itp_type_shield),
(item_get_slot, reg0, ":item", slot_item_needs_two_hands),
(this_or_next|eq, reg0, 0),
(this_or_next|eq, ":force_shield", 0),
(eq, ":shield", itm_no_item),
(try_begin),#equipping longest item for the formation
(neq, ":force_length", 0),
(item_get_slot, ":item_length", ":item", slot_item_length),
(try_begin),
(lt, ":cur_score", ":item_length"),
(assign, ":cur_score", ":item_length"),
(assign, ":weapon", ":item"),
(try_end),
(else_try),
(assign, ":imod", imodbit_plain),
(agent_get_troop_id, ":troop_id", ":agent"),
(try_begin), #only heroes have item modifications
(troop_is_hero, ":troop_id"),
(try_for_range, ":troop_item_slot", ek_item_0, ek_head), # heroes have only 4 possible weapons (equipped)
(troop_get_inventory_slot, reg0, ":troop_id", ":troop_item_slot"), #Find Item Slot with same item ID as Equipped Weapon
(eq, reg0, ":item"),
(troop_get_inventory_slot_modifier, ":imod", ":troop_id", ":troop_item_slot"),
(try_end),
(try_end),
(call_script, "script_get_item_score_with_imod", ":item", ":imod"),
(lt, ":cur_score", reg0),
(assign, ":cur_score", reg0),
(assign, ":weapon", ":item"),
(try_end),
(try_end),
(try_end),
(agent_get_wielded_item, reg0, ":agent", 0),
(try_begin),
(neq, reg0, ":weapon"),
(try_begin),
(gt, ":shield", itm_no_item),
(agent_get_wielded_item, reg0, ":agent", 1),
(neq, reg0, ":shield"), #reequipping secondary will UNequip (from experience)
(agent_set_wielded_item, ":agent", ":shield"),
(try_end),
(gt, ":weapon", itm_no_item),
(agent_set_wielded_item, ":agent", ":weapon"),
(try_end),
]),