OSP Code QoL Universal Inventory Sorting Script

Users who are viewing this thread

ConstantA

Knight
Here it is:
Code:
#script_rearrange_inventory
#input: arg1 - troop_id, arg2 - sorting order; 0 - simple (compact inventory, removing spaces), 1 - by cost, 2 - by type. 	  
#output : none
 ("rearrange_inventory",
  [(store_script_param, ":troop_id", 1),
   (store_script_param, ":type", 2),
   (troop_get_inventory_capacity, ":inv_cap", ":troop_id"),
   (try_begin),
     (eq, ":type", 0),
     (try_for_range, ":slot_no", 11, ":inv_cap"),
        (troop_get_inventory_slot, ":item", ":troop_id", ":slot_no"),
        (gt, ":item", 0),
        (troop_get_inventory_slot_modifier, ":mod", ":troop_id", ":slot_no"),
        (troop_inventory_slot_get_item_amount, ":amount", ":troop_id", ":slot_no"),
        (assign, ":target_slot", -1),
        (try_for_range_backwards, ":to_slot", 10, ":slot_no"),
          (troop_get_inventory_slot, ":is_full", ":troop_id", ":to_slot"),
          (le, ":is_full", 0),
          (assign, ":target_slot", ":to_slot"),
        (try_end),  
        (neq, ":target_slot", -1),
        (troop_set_inventory_slot, ":troop_id", ":target_slot", ":item"),
        (troop_set_inventory_slot_modifier, ":troop_id", ":target_slot", ":mod"),
        (try_begin),
         (gt, ":amount", 0),
         (troop_inventory_slot_set_item_amount, ":troop_id", ":target_slot", ":amount"),
        (try_end),
        (troop_set_inventory_slot, ":troop_id", ":slot_no", -1),
        (troop_inventory_slot_set_item_amount, ":troop_id", ":slot_no", 0),
     (try_end),
    (else_try),
       (eq, ":type", 1),
       (troop_sort_inventory, ":troop_id"),
    (else_try),       
       (eq, ":type", 2),
       (assign, ":temp_slot", 0),
#define sorting order
       (troop_set_slot, "trp_temp_array_a", 0, itp_type_one_handed_wpn),
       (troop_set_slot, "trp_temp_array_a", 1, itp_type_two_handed_wpn),
       (troop_set_slot, "trp_temp_array_a", 2, itp_type_polearm),
       (troop_set_slot, "trp_temp_array_a", 3, itp_type_bow),
       (troop_set_slot, "trp_temp_array_a", 4, itp_type_crossbow),
       (troop_set_slot, "trp_temp_array_a", 5, itp_type_thrown),
       (troop_set_slot, "trp_temp_array_a", 6, itp_type_arrows),
       (troop_set_slot, "trp_temp_array_a", 7, itp_type_bolts),
       (troop_set_slot, "trp_temp_array_a", 8, itp_type_shield),
       (troop_set_slot, "trp_temp_array_a", 9, itp_type_head_armor),
       (troop_set_slot, "trp_temp_array_a", 10, itp_type_body_armor),
       (troop_set_slot, "trp_temp_array_a", 11, itp_type_foot_armor),
       (troop_set_slot, "trp_temp_array_a", 12, itp_type_hand_armor),
       (troop_set_slot, "trp_temp_array_a", 13, itp_type_horse),
       (troop_set_slot, "trp_temp_array_a", 14, itp_type_goods),
       (troop_set_slot, "trp_temp_array_a", 15, itp_type_book),
       (troop_set_slot, "trp_temp_array_a", 16, itp_type_pistol),
       (troop_set_slot, "trp_temp_array_a", 17, itp_type_musket),
       (troop_set_slot, "trp_temp_array_a", 18, itp_type_bullets),
       (troop_set_slot, "trp_temp_array_a", 19, itp_type_animal),
#define sorting order    
       (call_script, "script_clear_inventory", "trp_temp_array_a"),
       (troop_sort_inventory, ":troop_id"),
       (try_for_range, ":array_pos", 0, 20),
         (try_for_range, ":slot_no", 10, ":inv_cap"),
           (troop_get_inventory_slot, ":item", ":troop_id", ":slot_no"),
           (gt, ":item", 0),           
           (item_get_type, ":item_type", ":item"),
           (troop_slot_eq, "trp_temp_array_a", ":array_pos", ":item_type"),
           (troop_inventory_slot_get_item_amount, ":amount", ":troop_id", ":slot_no"),
           (troop_get_inventory_slot_modifier, ":mod", ":troop_id", ":slot_no"),
           (troop_set_inventory_slot, "trp_temp_array_a", ":temp_slot", ":item"),
           (troop_set_inventory_slot_modifier, "trp_temp_array_a", ":temp_slot", ":mod"),
           (try_begin),
             (gt, ":amount", 0),
             (troop_inventory_slot_set_item_amount, "trp_temp_array_a", ":temp_slot", ":amount"),
           (try_end),
           (val_add, ":temp_slot", 1),
          (try_end),
       (try_end),
       (troop_clear_inventory, ":troop_id"),
       (try_for_range, ":slot_no", 0, ":inv_cap"),
            (troop_get_inventory_slot, ":item", "trp_temp_array_a", ":slot_no"),
            (troop_get_inventory_slot_modifier, ":mod", "trp_temp_array_a", ":slot_no"),
            (troop_inventory_slot_get_item_amount, ":amount", "trp_temp_array_a", ":slot_no"),
            (val_add, ":slot_no", 10),
            (troop_set_inventory_slot, ":troop_id", ":slot_no", ":item"),
            (troop_set_inventory_slot_modifier, ":troop_id", ":slot_no", ":mod"),
            (try_begin),
              (gt, ":amount", 0),
              (troop_inventory_slot_set_item_amount, ":troop_id", ":slot_no", ":amount"),
            (try_end),
            (try_begin),
              (le, ":item", 0),
              (assign, ":inv_cap", 0),
            (try_end),
         (try_end),
    (try_end),     
   ]),

It utilises one simple script - "script_clear_inventory" (which, unlike troop_clear_inventory operation, removes everything from inventory, including wielded weapons and such), here it is:
Code:
#script_clear_inventory
#INPUT: arg1 = troop_id
  ("clear_inventory",
   [(store_script_param_1, ":troop_id"),
    (troop_clear_inventory,":troop_id"),
    (try_for_range, ":item", 0, 10),
       (troop_set_inventory_slot, ":troop_id", ":item",  -1), 
    (try_end),]),

Explanations: First argument passed to script_rearrange_inventory must be troop_id, second - wanted sorting type. 0 - compacts inventory, preserving existing order (moves items to the top, removing blank spaces), 1 - sorts items by cost, 2 - sorts items by type.
Sorting by type order is set between #define sorting order comments. Each item group will also be sorted by price.

Not thoroughly tested, so feedback'll be much appreciated.

 
Im new to modding so please help me. how do i use this script?  already copied and pasted it on the module_script.py and rebuilt the module, but it doesnt work ingame. im kinda confused do i need to change the troop_id? does this work with warband?
 
Clgseed said:
Im new to modding so please help me. how do i use this script?  already copied and pasted it on the module_script.py and rebuilt the module, but it doesnt work ingame. im kinda confused do i need to change the troop_id? does this work with warband?

It's only a script, Clgseed, you need to call it to turn this on. An example line to call it as follow:
Code:
(call_script, "script_rearrange_inventory","trp_player",1),

You can assign this line to one menu option, every it's called your player's inventory will be sorted by cost... easy
 
Sorry to revive this zombie.... however I have been trying to get this to work, and having some odd results.

I was trying to sort inventory by price but it was behaving weird, so I decided to use your mod, not realizing it was just using the same command I was for the "sort by price" flag.  Anyway, the odd thing is, it seems to work for 90% of the items in my bags.  however food and a few other seemingly random items do not sort correctly.  any ideas?



running on 1.152 now of course.
 
Swyter said:
It's only a script, Clgseed, you need to call it to turn this on. An example line to call it as follow:
Code:
(call_script, "script_rearrange_inventory","trp_player",1),

You can assign this line to one menu option, every it's called your player's inventory will be sorted by cost... easy
I cant get this...
I found nothing about an inventory menu... I searched in these py.files;
module_game_menus
module_presentations
module_mission_templates
module_scripts

Where do I have to call the sorting script :?:
 
Back
Top Bottom