I havent tested this out, so there are probably errors and you need to test it, but something like this:
("let_nearby_parties_join_current_battle",
[
(assign, ":enemy_party_no", "$g_encountered_party"),
(try_for_parties, ":party_no"),
(party_get_battle_opponent, "
pponent",":party_no"),
(lt, "
pponent", 0), #party is not itself involved in a battle
(party_get_attached_to, ":attached_to",":party_no"),
(lt, ":attached_to", 0), #party is not attached to another party
(get_party_ai_behavior, ":behavior", ":party_no"),
(neq, ":behavior", ai_bhvr_in_town),
(assign, ":join_distance", 5),
(try_begin),
(is_currently_night),
(assign, ":join_distance", 3),
(try_end),
(store_distance_to_party_from_party, ":distance", ":party_no", "p_main_party"),
(lt, ":distance", ":join_distance"),
(store_faction_of_party, ":faction_no", ":party_no"),
(store_faction_of_party, ":enemy_faction", ":enemy_party_no"),
(store_relation, ":reln_with_player", ":faction_no", "fac_player_faction"),
(store_relation, ":reln_with_enemy", ":faction_no", ":enemy_faction"),
(try_begin),
#TODO: Fix this
(lt, ":reln_with_player", 0),
(gt, ":reln_with_enemy", 0),
(party_get_slot, ":party_type", ":party_no"),
(eq, ":party_type", spt_kingdom_hero_party),
(get_party_ai_behavior, ":ai_bhvr", ":party_no"),
(neq, ":ai_bhvr", ai_bhvr_avoid_party),
(party_quick_attach_to_current_battle, ":party_no", 1), #attach as enemy
(str_store_party_name, s1, ":party_no"),
(display_message, "str_s1_joined_battle_enemy"),
(else_try), #add this red stuff in this exact spot in this script
(lt, ":reln_with_player", 0),
(gt, ":reln_with_enemy", 0), # I am not sure about this because I do not know what relationship bandits will have with each other. if this doesnt work change gt to ge
(party_get_template_id,":party_template",":party_no"),
(is_between,":party_template",bandits_begin,bandits_end),
(get_party_ai_behavior, ":ai_bhvr", ":party_no"),
(neq, ":ai_bhvr", ai_bhvr_avoid_party),
(party_get_slot,":protected_until_hours", ":party_no",slot_party_ignore_player_until),
(store_current_hours,":cur_hours"),
(store_sub, ":protection_remaining",":protected_until_hours",":cur_hours"),
(le, ":protection_remaining", 0)
(party_quick_attach_to_current_battle, ":party_no", 1), #attach as enemy
(str_store_party_name, s1, ":party_no"),
(display_message, "str_s1_joined_battle_enemy"),
(else_try),
#TODO: Fix this
(gt, ":reln_with_player", 0),
(lt, ":reln_with_enemy", 0),
(party_get_slot, ":party_type", ":party_no"),
(eq, ":party_type", spt_kingdom_hero_party),
(party_stack_get_troop_id, ":leader", ":party_no", 0),
(troop_get_slot, ":player_relation", ":leader", slot_troop_player_relation),
(ge, ":player_relation", 0),
(party_quick_attach_to_current_battle, ":party_no", 0), #attach as friend
(str_store_party_name, s1, ":party_no"),
(display_message, "str_s1_joined_battle_friend"),
(try_end),
(try_end),
]),
In module party templates, have all of your bandit parties grouped together, ex:
("forest_bandits", blah blah blah),
("steppe_bandits", blah blah blah),
("river_pirates", blah blah blah),
In module constants add this near village_elders_end
bandits_begin = "pt_" # after pt_ put the first bandit party template here.
bandits_end = "pt_" # after pt_ put the party template that is after the last bandit party
Or instead of messing with the stuff in module constants and party templates, and instead of using this line in the script: (is_between,":party_template",bandits_begin,bandits_end), you could store the party's faction and make a check to see if it is of the outlaw/bandit faction. Ex:
use (store_faction_of_party, ":party_faction", ":party_no"),
(eq, ":party_faction", "fac_"), # after fac_ put the faction