summaryrefslogtreecommitdiff
path: root/_main.cfg
blob: 28b789d756e9a6e64d5e583e6f50726c78acfd8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
[modification]
	id=Select_Trait
	name=_"Select Trait"
	description=_"Optionally select a new recruit's trait(s), at an increased cost"
	require_modification=yes

	[lua]
		code=<<
			Select_Trait = {}
			function Select_Trait.select_for_unit(unit, cost_per)
				local side = wesnoth.sides[unit.side]
				local type = wesnoth.unit_types[unit.type]
				local race = wesnoth.races[type.race]

				local available_traits = type.traits
				if available_traits == nil then
					if type.ignore_race_traits then return end
					available_traits = race.traits
				end
				if available_traits == nil then return end

				local num_traits = type.num_traits
				if num_traits == nil then
					num_traits = race.num_traits
				end
				if num_traits == nil then return end

				local gender = unit.gender

				local sorted_traits = {}
				for id, _ in pairs(available_traits) do
					table.insert(sorted_traits, id)
				end
				table.sort(sorted_traits)

				local options = {}
				local ids = {}
				for _,id in pairs(sorted_traits) do
					table.insert(options, {label=available_traits[id][gender.."_name"]})
					table.insert(ids, available_traits[id].id)
				end
				table.insert(options, {label="Random (no charge, ends loop)"})

				if num_traits >= #ids then return end

				local choice

				local traits = {}
				while num_traits > 0 and side.gold >= cost_per do
					choice = wesnoth.sync.evaluate_single(function()
						return {value = gui.show_narration(
							{
								title = "Trait Selection",
								message = "Select a trait ("..tostring(cost_per).." gold)",
							},
							options
						)}
					end).value

					if choice == #options then break end

					side.gold = side.gold - cost_per

					table.insert(traits, ids[choice])
					table.remove(options, choice)
					table.remove(ids, choice)
					num_traits = num_traits - 1
				end

				--while num_traits > 0 do
				--	choice = mathx.random(#ids)
				--	table.insert(traits, ids[choice])
				--	table.remove(ids, choice)
				--	num_traits = num_traits - 1
				--end

				local original_moves = unit.moves
				local original_attacks_left = unit.attacks_left
				local x = unit.x
				local y = unit.y
				local original_canrecruit = unit.canrecruit

				while true do -- lol
					unit:extract()
					wesnoth.units.to_map({
						side=unit.side,
						variation=unit.variation,
						type=unit.type,
						gender=unit.gender,
						race=unit.race,
						random_traits=true,
					}, x, y)
					unit = wesnoth.units.get(x, y)
					local correct = true
					for key, trait in pairs(traits) do
						if unit.traits[key] ~= trait then
							correct = false
							break
						end
					end
					if correct then break end
				end

				unit.moves = original_moves
				unit.attacks_left = original_attacks_left
				unit.canrecruit = original_canrecruit
			end
		>>
	[/lua]

	[options]
		[checkbox]
			id=Select_Trait_give_to_leaders
			default=true
			name=_"Select for leader"
			description=_"Allow the player to select the initial traits for their leader unit, if it didn't come with any"
		[/checkbox]

		[slider]
			id=Select_Trait_cost_per_trait
			default=1
			min=0
			max=10
			name=_"Cost"
			description=_"How much gold to charge for each trait the player selects, during recruiting"
		[/slider]
	[/options]

	[event]
		name=recruit
		first_time_only=no
		[lua]
			code=<<
				local args=...
				event_context = wesnoth.current.event_context

				local unit = wesnoth.units.get(event_context.x1, event_context.y1)
				if not unit then return end

				Select_Trait.select_for_unit(unit, args.cost)
			>>
			[args]
				cost=$Select_Trait_cost_per_trait
			[/args]
		[/lua]
	[/event]

	[event]
		name=side $side_number turn 1
		first_time_only=no
		[lua]
			code=<<
				args=...
				if not args.actually_run then return end

				for _,unit in ipairs(wesnoth.units.find_on_map({side=args.side_number, canrecruit=true})) do
					if #unit.traits == 0 then
						Select_Trait.select_for_unit(unit, 0)
					end
				end
			>>
			[args]
				side_number=$side_number
				actually_run=$Select_Trait_give_to_leaders
			[/args]
		[/lua]
	[/event]
[/modification]