Script for sorting a given troop's inventory by price. You need an additional troop: temp_array_c (just copy temp_array_a) in module_troops.py.
Usage example: (call_script, "script_sort_inventory", "trp_temp_troop"),
To get sorted loot for player: in module_game_menus.py, under the menu "total_victory". Look for:
(change_screen_loot, "trp_temp_troop"),
And put the call right before it:
(call_script, "script_sort_inventory", "trp_temp_troop"),
(change_screen_loot, "trp_temp_troop"),
Code (for module_scripts.py) is below:
#####################################################################
# BoW: gets an item's value
# Param1: item ID
# Param2: item modifier
#####################################################################
("get_item_value_with_imod", [ # returns the sell price based on the item's money value and its imod
(store_script_param, ":item", 1),
(store_script_param, ":imod", 2),
(store_item_value, ":score", ":item"),
(try_begin),
(eq, ":imod", imod_lame),
(val_mul, ":score", 30),
(else_try),
(this_or_next|eq, ":imod", imod_tattered),
(eq, ":imod", imod_cracked),
(val_mul, ":score", 40),
(else_try),
(this_or_next|eq, ":imod", imod_rusty),
(eq, ":imod", imod_swaybacked),
(val_mul, ":score", 50),
(else_try),
(this_or_next|eq, ":imod", imod_ragged),
(eq, ":imod", imod_bent),
(val_mul, ":score", 60),
(else_try),
(this_or_next|eq, ":imod", imod_chipped),
(eq, ":imod", imod_battered),
(val_mul, ":score", 65),
(else_try),
(this_or_next|eq, ":imod", imod_poor),
(eq, ":imod", imod_crude),
(val_mul, ":score", 80),
(else_try),
(this_or_next|eq, ":imod", imod_old),
(eq, ":imod", imod_stubborn),
(val_mul, ":score", 90),
(else_try),
(eq, ":imod", imod_plain),
(val_mul, ":score", 100),
(else_try),
(eq, ":imod", imod_heavy),
(val_mul, ":score", 135),
(else_try),
(eq, ":imod", imod_large_bag),
(val_mul, ":score", 190),
(else_try),
(eq, ":imod", imod_temperate),
(val_mul, ":score", 140),
(else_try),
(eq, ":imod", imod_sturdy),
(val_mul, ":score", 130),
(else_try),
(eq, ":imod", imod_thick),
(val_mul, ":score", 160),
(else_try),
(eq, ":imod", imod_spirited),
(val_mul, ":score", 240),
(else_try),
(eq, ":imod", imod_balanced),
(val_mul, ":score", 170),
(else_try),
(eq, ":imod", imod_hardened),
(val_mul, ":score", 180),
(else_try),
(eq, ":imod", imod_watered_steel),
(val_mul, ":score", 290),
(else_try),
(eq, ":imod", imod_reinforced),
(val_mul, ":score", 250),
(try_end),
(assign, reg0, ":score"),
]),
#####################################################################
# BoW: sorts a troops inventory by price
# Param1: source troop
#####################################################################
("sort_inventory", [
(store_script_param_1, ":source_troop"),
(troop_get_inventory_capacity, ":inv_cap", ":source_troop"),
(assign, ":num_items", 0),
# record the item IDs
(try_for_range, ":i_slot", 0, ":inv_cap"),
(troop_get_inventory_slot, ":item_id", ":source_troop", ":i_slot"),
(ge, ":item_id", 0),
(troop_set_slot, "trp_temp_array_a", ":num_items", ":item_id"),
(troop_get_inventory_slot_modifier, ":item_imod", ":source_troop", ":i_slot"),
(troop_set_slot, "trp_temp_array_b", ":num_items", ":item_imod"),
(troop_inventory_slot_get_item_amount, ":item_amount", ":source_troop", ":i_slot"),
(troop_set_slot, "trp_temp_array_c", ":num_items", ":item_amount"),
(val_add, ":num_items", 1),
(try_end),
# bubble-sort by cost
(try_for_range, ":i_slot", 1, ":num_items"),
(try_for_range_backwards, ":j_slot", ":i_slot",":num_items"),
(try_begin),
(store_sub, ":prev_slot", ":j_slot", 1),
(troop_get_slot, ":item_j", "trp_temp_array_a", ":j_slot"),
(troop_get_slot, ":item_prev", "trp_temp_array_a", ":prev_slot"),
(troop_get_slot, ":item_imod_j", "trp_temp_array_b", ":j_slot"),
(troop_get_slot, ":item_imod_prev", "trp_temp_array_b", ":prev_slot"),
(troop_get_slot, ":item_amt_j", "trp_temp_array_c", ":j_slot"),
(troop_get_slot, ":item_amt_prev", "trp_temp_array_c", ":prev_slot"),
(call_script, "script_get_item_value_with_imod", ":item_j", ":item_imod_j"),
(assign, ":item_cost_j", reg0),
(call_script, "script_get_item_value_with_imod", ":item_prev", ":item_imod_prev"),
(assign, ":item_cost_prev", reg0),
(lt, ":item_cost_prev", ":item_cost_j"),
# out of order; do the swap
(troop_set_slot, "trp_temp_array_a", ":j_slot", ":item_prev", ),
(troop_set_slot, "trp_temp_array_a", ":prev_slot", ":item_j"),
(troop_set_slot, "trp_temp_array_b", ":j_slot", ":item_imod_prev", ),
(troop_set_slot, "trp_temp_array_b", ":prev_slot", ":item_imod_j"),
(troop_set_slot, "trp_temp_array_c", ":j_slot", ":item_amt_prev", ),
(troop_set_slot, "trp_temp_array_c", ":prev_slot", ":item_amt_j"),
(try_end),
(try_end),
(try_end),
# remove items
(troop_clear_inventory, ":source_troop"),
(try_for_range, ":i_slot", 0, 10),
(troop_set_inventory_slot, ":source_troop", ":i_slot", -1),
(end_try),
# replace them on the troop, most expensive first
(try_for_range, ":i_slot", 0, ":num_items"),
(troop_get_slot, ":item_id", "trp_temp_array_a", ":i_slot"),
(troop_get_slot, ":item_imod", "trp_temp_array_b", ":i_slot"),
(troop_get_slot, ":item_amount", "trp_temp_array_c", ":i_slot"),
(troop_add_item, ":source_troop", ":item_id", ":item_imod"),
(try_begin),
(is_between, ":item_id", morale_boosters_begin, morale_boosters_end),
(store_add, ":inv_slot", ":i_slot", 10),
(troop_inventory_slot_set_item_amount, ":source_troop", ":inv_slot", ":item_amount"),
(try_end),
(try_end),
]),