This is merely hatonastick's code adapted for battles and such. With it, cavalry units (any troop on a horse regardless of actual division) will use lances if they have them while on horseback, but will almost immediately switch to their sidearm (any non-lance weapon) if dehorsed or ordered to dismount and they are carrying such an item. No more dismounted Swadian knights stupidly getting killed by half-dead looters because they won't switch from their heavy lance on foot, etc.
Posted with hatonastick's permission, and the credit really goes to him as it's 98% his code. Also note that you may need to tweak the item checks or move stuff around in module_items to ensure all the weapons you want counted as "lances" are affected.
This goes into module_mission_templates.py. For battles, put it under lead_charge, for sieges, search for the words attack_castle and besiege (should be 4 places total for sieges, IIRC).
Please report it here if you find any glaring flaws, and of course suggestions for improvements are always welcome.
Posted with hatonastick's permission, and the credit really goes to him as it's 98% his code. Also note that you may need to tweak the item checks or move stuff around in module_items to ensure all the weapons you want counted as "lances" are affected.
This goes into module_mission_templates.py. For battles, put it under lead_charge, for sieges, search for the words attack_castle and besiege (should be 4 places total for sieges, IIRC).
Code:
# LANCE USAGE BEGIN
# Force mounted NPCs to switch to their lance. This is called once at the
# start of the battle. If you want lancers to ALWAYS use lances on horseback,
# replace ti_once with 1. Otherwise they may switch to sword if bogged down
(0, 1, ti_once, [],
[
# Run through all active NPCs on the battle field.
(try_for_agents, ":agent"),
# Isn't a player.
(agent_is_non_player, ":agent"),
# Isn't a horse.
(agent_is_human, ":agent"),
# Hasn't been defeated.
(agent_is_alive, ":agent"),
# They riding a horse?
(agent_get_horse, ":horse", ":agent"),
# Is riding a horse.
(gt, ":horse", 0),
# Get wielded item.
(agent_get_wielded_item, ":wielded", ":agent", 0),
# Is it a lance?
(neg|is_between, ":wielded", "itm_light_lance","itm_pike"), # adjust as needed
# Force the NPC to wield the lance, but this will only happen if they
# actually have a lance in their inventory. Otherwise this does
# nothing.
(try_for_range,":item","itm_light_lance","itm_pike"), # adjust as needed
(agent_set_wielded_item, ":agent", ":item"),
(try_end),
(try_end),
]),
# Check to make sure there are no lance users on foot, if so force them to
# switch to their sword. This should also affect troops that were NEVER mounted,
# but are still equipped with lances, such as Taiga Bandits.
(0, 0, 1, [],
[
# Run through all active NPCs on the battle field.
(try_for_agents, ":agent"),
# Isn't a player.
(agent_is_non_player, ":agent"),
# Isn't a horse.
(agent_is_human, ":agent"),
# Hasn't been defeated.
(agent_is_alive, ":agent"),
# They riding a horse?
(agent_get_horse, ":horse", ":agent"),
# Isn't riding a horse.
(le, ":horse", 0),
# Get wielded item.
(agent_get_wielded_item, ":wielded", ":agent", 0),
# Is it a lance?
(is_between, ":wielded", "itm_light_lance","itm_pike"),
# Find non-lance item in inventory
(agent_get_troop_id, ":troop",":agent"),
(troop_get_inventory_capacity,":cap",":troop"),
(assign,":has_choice",0),
(try_for_range, ":i", 0, ":cap"),
(troop_get_inventory_slot,":item",":troop",":i"),
(neg|is_between, ":item", "itm_light_lance","itm_pike"), # adjust as needed
(neg|is_between, ":item", "itm_wooden_shield","itm_darts"), # not a shield - adjust as needed
(neg|is_between, ":item", "itm_hunting_bow","itm_torch"), # not ranged - adjust as needed
(assign,":has_choice",1),
(assign,":cap",0),
(try_end),
# Equip their backup weapon.
(try_begin),
(eq, ":has_choice",1),
(agent_set_wielded_item, ":agent", ":item"),
(try_end),
(try_end),
]),
# LANCE USAGE END
Please report it here if you find any glaring flaws, and of course suggestions for improvements are always welcome.