summaryrefslogtreecommitdiff
path: root/_main.cfg
blob: c12f82a8813455258413f411e6afbf63e227e043 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
[modification]
	id=Unit_Displacement
	name=_"Unit Displacement"
	description=_"Allows you to displace your own units, so congested areas are more managable, and optionally also allies' units (with some restrictions)"
	require_modification=yes

	[lua]
		code=<<
			Unit_Displacement = {map={}, sides={}}
			function Unit_Displacement.lose_units(count, side)
				if count == 1 then
					wesnoth.interface.add_chat_message("RIP "..tostring(count).." unit from side "..tostring(side)..", who was improperly displaced.")
				else
					wesnoth.interface.add_chat_message("RIP "..tostring(count).." units from side "..tostring(side)..", who were improperly displaced.")
				end
			end
		>>
	[/lua]

	[options]
		[checkbox]
			id=Unit_Displacement_allow_displacing_allies
			default=true
			name=_"Allow displacing allies"
			description=_"Allows the player to displace allied units, if the ally moves after them, and no enemies are between"
		[/checkbox]
	[/options]

	[event]
		name=prestart

		[set_menu_item]
			id=Unit_Displacement_select
			description=_"Displace this unit"

			[show_if]
				[lua]
					code=<<
						if not wesnoth.current.user_can_invoke_commands then return false end

						local args=...

						local tmp = Unit_Displacement.map[args.x]
						if tmp and tmp[args.y] then return false end

						local unit = wesnoth.units.get(args.x, args.y)
						if not unit then return false end

						local side_number = wesnoth.current.side
						local side = wesnoth.sides[side_number]

						if unit.canrecruit then return false end
						if unit.side == side_number and unit.moves == 0 then return false end

						if unit.side == side_number then return true end

						if not args.allow_allied then return false end
						if side_number >= unit.side then return false end

						for i=side_number,unit.side do
							if side:is_enemy(wesnoth.sides[i]) then return false end
						end

						return true
					>>
					[args]
						allow_allied=$Unit_Displacement_allow_displacing_allies
						x=$x1
						y=$y1
					[/args]
				[/lua]
			[/show_if]

			[command]
				[lua]
					code=<<
						args=...
						local x = tonumber(args.x)
						local y = tonumber(args.y)

						local unit = wesnoth.units.get(x, y)
						unit:extract()

						Unit_Displacement.map[x] = Unit_Displacement.map[x] or {}
						Unit_Displacement.map[x][y] = unit

						Unit_Displacement.sides[unit.side] = Unit_Displacement.sides[unit.side] or {}
						Unit_Displacement.sides[unit.side][unit] = true
					>>
					[args]
						x=$x1
						y=$y1
					[/args]
				[/lua]
			[/command]
		[/set_menu_item]
	[/event]

	[event]
		name=moveto
		first_time_only=no

		[filter_condition]
			[lua]
				code=<<
					local args=...
					local x = tonumber(args.x)
					local y = tonumber(args.y)
					if not Unit_Displacement.map[x] or not Unit_Displacement.map[x][y] then return false end

					local old_unit = Unit_Displacement.map[x][y]
					local new_unit = wesnoth.units.get(x, y)
					if old_unit.side ~= new_unit.side then return false end
					if new_unit.canrecruit then return false end

					return true
				>>
				[args]
					x=$x1
					y=$y1
				[/args]
			[/lua]

			[or]
				[lua]
					code=<<
						local args=...
						local x = tonumber(args.x)
						local y = tonumber(args.y)
						if not Unit_Displacement.map[x] or not Unit_Displacement.map[x][y] then return false end

						return true
					>>
					[args]
						x=$x2
						y=$y2
					[/args]
				[/lua]
			[/or]
		[/filter_condition]

		[lua]
			code=<<
				-- same side, moving onto the tile

				local args=...
				local x = tonumber(args.x)
				local y = tonumber(args.y)
				if not Unit_Displacement.map[x] or not Unit_Displacement.map[x][y] then return end
				local old_unit = Unit_Displacement.map[x][y]
				local new_unit = wesnoth.units.get(x, y)
				if old_unit.side ~= new_unit.side then return end
				if new_unit.canrecruit then return end

				Unit_Displacement.sides[old_unit.side][old_unit] = nil

				new_unit:extract()
				old_unit:to_map()

				Unit_Displacement.map[x][y] = new_unit
				Unit_Displacement.sides[new_unit.side][new_unit] = true
			>>
			[args]
				x=$x1
				y=$y1
			[/args]
		[/lua]

		[lua]
			code=<<
				-- any side, moving out of the displaced tile

				local args=...
				local x = tonumber(args.x)
				local y = tonumber(args.y)
				if not Unit_Displacement.map[x] or not Unit_Displacement.map[x][y] then return end
				local unit = Unit_Displacement.map[x][y]
				Unit_Displacement.sides[unit.side][unit] = nil
				Unit_Displacement.map[x][y] = nil

				unit:to_map()
			>>
			[args]
				x=$x2
				y=$y2
			[/args]
		[/lua]
	[/event]

	[event]
		name=side turn end
		first_time_only=no

		[lua]
			code=<<
				-- attempt to place units on turn end, if unsuccessful... well, nowhere to put them...

				local args=...

				if not Unit_Displacement.sides[args.side] then return end

				local lost = 0
				for unit, _ in pairs(Unit_Displacement.sides[args.side]) do
					if wesnoth.units.get(unit.x, unit.y) then
						lost = lost + 1
					else
						unit:to_map()
					end

					Unit_Displacement.map[unit.x][unit.y] = nil
				end
				if lost > 0 then
					Unit_Displacement.lose_units(lost, args.side)
				end

				Unit_Displacement.sides[args.side] = {}

				for i=1,args.side do
					if Unit_Displacement.sides[i] then
						local lost = 0
						for unit, _ in pairs(Unit_Displacement.sides[i]) do
							lost = lost + 1
							Unit_Displacement.map[unit.x][unit.y] = nil
						end
						Unit_Displacement.sides[i] = {}
						if lost > 0 then
							Unit_Displacement.lose_units(lost, i)
						end
					end
				end
			>>
			[args]
				side=$side_number
			[/args]
		[/lua]
	[/event]

	[event]
		name=side turn
		first_time_only=no

		[lua]
			code=<<
				-- swap out units on turn start

				local args=...

				if not Unit_Displacement.sides[args.side] then return end

				local lost = 0
				for unit, _ in pairs(Unit_Displacement.sides[args.side]) do
					local new_unit = wesnoth.units.get(unit.x, unit.y)
					if new_unit then
						if new_unit.canrecruit then
							lost = lost + 1
							Unit_Displacement.map[unit.x][unit.y] = nil
						else
							new_unit:extract()

							Unit_Displacement.sides[new_unit.side] = Unit_Displacement.sides[new_unit.side] or {}
							Unit_Displacement.sides[new_unit.side][new_unit] = true
							Unit_Displacement.map[new_unit.x][new_unit.y] = new_unit

							unit:to_map()
						end
					else
						unit:to_map()
					end
				end
				if lost > 0 then
					Unit_Displacement.lose_units(lost, args.side)
				end

				Unit_Displacement.sides[args.side] = {}
			>>
			[args]
				side=$side_number
			[/args]
		[/lua]
	[/event]
[/modification]