Garrison scripts are a pain in the butt, face it . To avoid the units becoming 'free stacks' for the AI and bankrupting him further the garrison needs to be disbanded after a unsuccessful siege. We did that in 1648 for only a few cities of 'endangered' factions - thought I share the code.
Due to the nature of the disbanding it is necessary to create copies of suitable units. Simply add a suffix to the name ( we used G for garrison) and copy\edit the text entries in text\export_units. The only difference between the units will be an additional, custom attribute in the EDU, we chose garrison.
The spawning in the campaign_script file is a piece of cake (with a twist): the spawn happens immediately when the settlement gets attacked, no quick attack on an empty city with siege equipment here! We used the create_unit command asCode:attributes sea_faring, hide_forest, is_peasant, can_run_amok, garrison, no_customa) it pops the units right into the settlement andUnits exceeding the total of 20 in a settlement will be ignored\not created, eg 14 existing units plus 10 garrison spawn will still only be 20 total. Units will be added in the sequence of the listing in the monitor.
b) you need only one line per unit type.
The monitor terminates after a set number of rounds - if they still need garrisons then, then they might as well perish.Code:declare_counter Aschaffenburg_siege declare_counter Mainz_siege declare_counter Erfurt_siege declare_counter Heiligenstadt_siege ;--- Garnison einziehen --- monitor_event GeneralAssaultsResidence TargetFactionType egypt and I_IsFactionAIControlled egypt ; garrison only for AI and IsTargetRegionOneOf Erzbistum_Mainz and I_SettlementUnderSiege Aschaffenburg if I_TurnNumber < 100 create_unit Aschaffenburg, BuergerwehrG, num 4, exp 0, arm 0, wep 0 create_unit Aschaffenburg, GrenadiereG, num 1, exp 0, arm 0, wep 0 create_unit Aschaffenburg, MusketiereG, num 2, exp 0, arm 0, wep 0 create_unit Aschaffenburg, PikeniereG, num 3, exp 0, arm 0, wep 0 set_counter Aschaffenburg_siege 1 end_if if I_TurnNumber > 99 terminate_monitor end_if end_monitor monitor_event GeneralAssaultsResidence TargetFactionType egypt and I_IsFactionAIControlled egypt ; garrison only for AI and IsTargetRegionOneOf Stadt_Mainz and I_SettlementUnderSiege Mainz if I_TurnNumber < 50 create_unit Mainz, BuergerwehrG, num 4, exp 0, arm 0, wep 0 create_unit Mainz, GrenadiereG, num 1, exp 0, arm 0, wep 0 create_unit Mainz, MusketiereG, num 2, exp 0, arm 0, wep 0 create_unit Mainz, PikeniereG, num 3, exp 0, arm 0, wep 0 set_counter Mainz_siege 1 end_if if I_TurnNumber > 49 terminate_monitor end_if end_monitor monitor_event GeneralAssaultsResidence TargetFactionType egypt and IsTargetRegionOneOf Stadt_Erfurt and I_SettlementUnderSiege Erfurt if I_TurnNumber < 50 create_unit Erfurt, BuergerwehrG, num 4, exp 0, arm 0, wep 0 create_unit Erfurt, GrenadiereG, num 1, exp 0, arm 0, wep 0 create_unit Erfurt, MusketiereG, num 2, exp 0, arm 0, wep 0 create_unit Erfurt, PikeniereG, num 3, exp 0, arm 0, wep 0 set_counter Erfurt_siege 1 end_if if I_TurnNumber > 49 terminate_monitor end_if end_monitor monitor_event GeneralAssaultsResidence TargetFactionType egypt and IsTargetRegionOneOf Eichsfeld and I_SettlementUnderSiege Heiligenstadt if I_TurnNumber < 50 create_unit Heiligenstadt, BuergerwehrG, num 4, exp 0, arm 0, wep 0 create_unit Heiligenstadt, GrenadiereG, num 1, exp 0, arm 0, wep 0 create_unit Heiligenstadt, MusketiereG, num 2, exp 0, arm 0, wep 0 create_unit Heiligenstadt, PikeniereG, num 3, exp 0, arm 0, wep 0 set_counter Heiligenstadt_siege 1 end_if if I_TurnNumber > 49 terminate_monitor end_if end_monitor
Then you check if there is no siege for any of the cities that get a garrison (noticed all the set_counter lines?):
Last not least you remove the garison troops if none of the settlements of a faction are under siege anymore:Code:;--- Belagerung ist vorbei --- monitor_event FactionTurnEnd not FactionIsLocal if I_SettlementOwner Aschaffenburg = egypt and not I_SettlementUnderSiege Aschaffenburg set_counter Aschaffenburg_siege 0 end_if if I_SettlementOwner Mainz = egypt and not I_SettlementUnderSiege Mainz set_counter Mainz_siege 0 end_if if I_SettlementOwner Erfurt = egypt and not I_SettlementUnderSiege Erfurt set_counter Erfurt_siege 0 end_if if I_SettlementOwner Heiligenstadt = egypt and not I_SettlementUnderSiege Heiligenstadt set_counter Heiligenstadt_siege 0 end_if end_monitor
The units should not be available for recruitment, in the script or initially in the descr_strat as mentioned at the beginning (copies of regular units, suffix G = garrison) as they have a custom attribute that allows them to be disbanded in the last monitor, the no_custom entry in the EDU will prevent availability in custom battles.Code:;--- Garnison ausmustern --- monitor_event FactionTurnStart FactionType egypt and not FactionIsLocal if I_CompareCounter Mainz_siege < 1 and I_CompareCounter Aschaffenburg_siege < 1 and I_CompareCounter Erfurt_siege < 1 and I_CompareCounter Heiligenstadt_siege < 1 destroy_units egypt garrison end_if if I_TurnNumber > 100 terminate_monitor end_if end_monitor
This whole code is only for 4 settlements of one faction - you do the math how this will look for all settlements. There would be no problem with processing as this will be two basics monitors plus one (the last one) for each faction involved - nothing to be bothered about.