1local utils = wesnoth.require "wml-utils"
2local T = wml.tag
3
4local side_changes_needing_redraw = {
5	'shroud', 'fog', 'reset_map', 'reset_view', 'shroud_data',
6	'share_vision', 'share_maps', 'share_view',
7	'color', 'flag',
8}
9
10function wesnoth.wml_actions.modify_side(cfg)
11	local sides = utils.get_sides(cfg)
12	for i,side in ipairs(sides) do
13		if cfg.team_name then
14			side.team_name = cfg.team_name
15		end
16		if cfg.user_team_name then
17			side.user_team_name = cfg.user_team_name
18		end
19		if cfg.side_name then
20			side.side_name = cfg.side_name
21		end
22		if cfg.controller then
23			side.controller = cfg.controller
24		end
25		if cfg.defeat_condition then
26			side.defeat_condition = cfg.defeat_condition
27		end
28		if cfg.recruit then
29			local recruits = {}
30			for recruit in utils.split(cfg.recruit) do
31				table.insert(recruits, recruit)
32			end
33			side.recruit = recruits
34		end
35		if cfg.village_support then
36			side.village_support = cfg.village_support
37		end
38		if cfg.village_gold then
39			side.village_gold = cfg.village_gold
40		end
41		if cfg.income then
42			side.base_income = cfg.income + wesnoth.game_config.base_income
43		end
44		if cfg.gold then
45			side.gold = cfg.gold
46		end
47
48		if cfg.hidden ~= nil then
49			side.hidden = cfg.hidden
50		end
51		if cfg.color or cfg.flag then
52			wesnoth.set_side_id(side.side, cfg.flag, cfg.color)
53		end
54		if cfg.flag_icon then
55			side.flag_icon = cfg.flag_icon
56		end
57		if cfg.suppress_end_turn_confirmation ~= nil then
58			side.suppress_end_turn_confirmation = cfg.suppress_end_turn_confirmation
59		end
60		if cfg.scroll_to_leader ~= nil then
61			side.scroll_to_leader = cfg.scroll_to_leader
62		end
63
64		if cfg.shroud ~= nil then
65			side.shroud = cfg.shroud
66		end
67		if cfg.reset_maps then
68			wesnoth.place_shroud(side.side, "all")
69		end
70		if cfg.fog ~= nil then
71			side.fog = cfg.fog
72		end
73		if cfg.reset_view then
74			wesnoth.add_fog(side.side, {}, true)
75		end
76		if cfg.shroud_data then
77			wesnoth.remove_shroud(side.side, cfg.shroud_data)
78		end
79
80		if cfg.share_vision then
81			side.share_vision = cfg.share_vision
82		end
83		-- Legacy support
84		if cfg.share_view ~= nil or cfg.share_maps ~= nil then
85			if cfg.share_view then
86				side.share_vision = 'all'
87			elseif cfg.share_maps then
88				side.share_vision = 'shroud'
89			else
90				side.share_vision = 'none'
91			end
92		end
93
94		if cfg.switch_ai then
95			wesnoth.switch_ai(side.side, cfg.switch_ai)
96		end
97		local ai, replace_ai = {}, false
98		for next_ai in wml.child_range(cfg, "ai") do
99			table.insert(ai, T.ai(next_ai))
100			if next_ai.ai_algorithm then
101				replace_ai = true
102			end
103		end
104		if #ai > 0 then
105			if replace_ai then
106				wesnoth.switch_ai(side.side, ai)
107			else
108				wesnoth.append_ai(side.side, ai)
109			end
110		end
111	end
112	for i,key in ipairs(side_changes_needing_redraw) do
113		if cfg[key] ~= nil then
114			wesnoth.wml_actions.redraw{}
115			return
116		end
117	end
118end
119