strafer
Recruit
Sorry for necroposting as well as for my english May be this script upgrade will be useful for somebody else.
I modding warband for last half-year and now i proceed to module system. For my mod i need tf_guarantee_polearm and tf_guarantee_two_handed so i had to implement they.
Thanks to TS for original script of course.
My main goal: if troop has polearm/two-handed in first inventory slot — he get polearm/two-handed with 100% probability. There is honest chance of needed weapon random choice (so far forth as ingame random function).
See in-code comments for more details:
It works for any troop with polearm or two-handed in first inventory position, you don't need to specify troops in code like in original script. If you don't want such guarantee for some troops — just set their first inventory items to non-polearm/non-two-handed and chance of weapon getting will be usual for MnB.
Now i get what i want for my epic mod: every specialized troop like spearman, partisan, halberdier, doppelsoldner has this primary weapon always, the same for horseman with lances too.
And i don't need to pad inventory with one-type weapons only for tiny additional chance.
I modding warband for last half-year and now i proceed to module system. For my mod i need tf_guarantee_polearm and tf_guarantee_two_handed so i had to implement they.
Thanks to TS for original script of course.
My main goal: if troop has polearm/two-handed in first inventory slot — he get polearm/two-handed with 100% probability. There is honest chance of needed weapon random choice (so far forth as ingame random function).
See in-code comments for more details:
Code:
("force_weapon",
[(store_script_param, ":agent", 1),
(try_begin),
(this_or_next|multiplayer_is_server),
(neg|game_in_multiplayer_mode),
(agent_is_alive, ":agent"),
(agent_is_non_player, ":agent"),
(agent_get_troop_id, ":troop", ":agent"),
# sift out heroes, especially merchants (usual town merchants as well as starting quest merchants)
(neg|troop_is_hero, ":troop"),
# take first item from inventory of troop template (from troops.txt/py)
# by unknown reason inventory of "troop" (template!) has items of specific "agent" (!), i don't know why
# real template inventory starts from record 11, i.e. 10 from 0
(troop_get_inventory_slot, ":first_inv_item", ":troop", 10),
# sift out troops with empty inventory
(neq, ":first_inv_item", -1),
# get and memorize type of first item and sift out everybody but with polearm or two-handed
(item_get_type, ":needed_item_type", ":first_inv_item"),
(this_or_next|eq, ":needed_item_type", itp_type_two_handed_wpn),
(eq, ":needed_item_type", itp_type_polearm),
(assign, ":have_polearm_or_twohanded", 0),
(assign, ":onehanded_slot", -1),
(assign, ":empty_slot", -1),
(try_begin),
# look over all 4 equipment slots for actual content
(try_for_range, ":i", 0, 4),
(agent_get_item_slot, ":item", ":agent", ":i"),
(try_begin),
(eq, ":item", -1),
# memorize empty slot for future use
(assign, ":empty_slot", ":i"),
(else_try),
(item_get_type, ":type", ":item"),
(eq, ":type", itp_type_one_handed_wpn),
# memorize slot with one-handed weapon for future use
(assign, ":onehanded_slot", ":i"),
(else_try),
(item_get_type, ":type", ":item"),
# if equipment has needed type weapon - set the flag
(eq, ":type", ":needed_item_type"),
(assign, ":have_polearm_or_twohanded", 1),
(try_end),
(try_end),
# if equipment has needed type weapon - exit
(neq, ":have_polearm_or_twohanded", 1),
# pick out equipment slot for replacement
(assign, ":replace_slot", -1),
(try_begin),
(ge, ":empty_slot", 0),
# pick out empty slot if it was
(assign, ":replace_slot", ":empty_slot"),
(else_try),
(ge, ":onehanded_slot", 0),
# otherwise pick out slot with one-handed if it was (and it was surely for my troop templates)
(assign, ":replace_slot", ":onehanded_slot"),
(agent_get_item_slot, ":item", ":agent", ":onehanded_slot"),
# Unequip item in this slot for free space for polearm/two-handed
(agent_unequip_item, ":agent", ":item", ":onehanded_slot"),
(else_try),
# paranoid case for dead certainty :-D·
# pick out last slot
(assign, ":replace_slot", 3),
(try_end),
# Enquire amount of needed weapons in troop inventory
# we seek from 10 record (see above) to record with nubmer returning by "troop_get_inventory_capacity"
# in my experiments i get 100
(troop_get_inventory_capacity, ":inventory_capacity", ":troop"),
(assign, ":needed_weapons_count", 0),
(try_for_range, ":i", 10, ":inventory_capacity"),
(try_begin),
(troop_get_inventory_slot, ":item", ":troop", ":i"),
(neq, ":item", -1),
(item_get_type, ":type", ":item"),
(eq, ":type", ":needed_item_type"),
(val_add, ":needed_weapons_count", 1),
(try_end),
(try_end),
# Get random index number of weapon
(store_random_in_range, ":selected_weapon_num", 0, ":needed_weapons_count"),
# And get weapon with this index number
(try_for_range, ":i", 10, ":inventory_capacity"),
(try_begin),
(troop_get_inventory_slot, ":item", ":troop", ":i"),
(neq, ":item", -1),
(item_get_type, ":type", ":item"),
(eq, ":type", ":needed_item_type"),
(val_sub, ":selected_weapon_num", 1),
(try_begin),
(eq, ":selected_weapon_num", -1),
# If we reach choosen index number - place this weapon to slot which we had choosen formerly
(agent_equip_item, ":agent", ":item", ":replace_slot"),
(try_end),
(try_end),
(try_end),
(try_end),
]),
It works for any troop with polearm or two-handed in first inventory position, you don't need to specify troops in code like in original script. If you don't want such guarantee for some troops — just set their first inventory items to non-polearm/non-two-handed and chance of weapon getting will be usual for MnB.
Now i get what i want for my epic mod: every specialized troop like spearman, partisan, halberdier, doppelsoldner has this primary weapon always, the same for horseman with lances too.
And i don't need to pad inventory with one-type weapons only for tiny additional chance.