(but this bug has existed for years)
When using the Script AI Planner in a battle with more than one reinforcing army from the same faction, the Script AI Planner thinks some of the reinforcements are duplicates and so ignores those reinforcements.
- In lib_battle_script_ai_planner.lua, the function script_ai_planner:add_sunits expects script_unit.name to be unique. If two units share a name, only one gets added while the other is ignored.
- In lib_battle_script_unit.lua, a comment claims that script_unit.name is unique, but it doesn't seem to be if there is more than 1 reinforcing army from the same faction. It's not obvious from the script, because you might expect army_num to be different for each reinforcing army. However, for each reinforcing army from the same faction, the alliance_num and army_num will be the same (usually 1 if it's the player's armies), so if script units are created using numerical references, each reinforcing army's general will have a script_unit.name of "1:1:R_1", the second unit in each reinforcing army will have a script_unit.name of "1:1:R_2", etc.
To see that script_unit.name is not unique:
1. Enable console logging
2. I've attached a script to this post that just creates script_unit objects for all the player's units and outputs script_unit.name. Put that script wherever you like to test scripts for battle, such as battle_startup_test_scripts_here() in working_data/script/_lib/mod/test_script_here.lua
3. Start a battle using at least three friendly armies from the same faction (at least two reinforcing armies).
4. Once the battle loads, inspect the console log file. Notice that multiple units have script names of "1:1:R_1" even though they have different unique_ui_ids. Also notice that there's nothing special about their script_unit.unit:name that would cause them to be split up into different generated_armies, so there will be conflicts whenever a unique name is expected, as is the case in script_ai_planner:add_sunits.
Hambango's simple solution was to replace the line in script_unit:new that currently sets
new_ref = "R_"..reinforcing_ref and change it to
new_ref = "R_" .. tostring(i) .. "_" .. reinforcing_ref;.