summaryrefslogtreecommitdiff
path: root/worldedit_commands/area.lua
blob: 78b91d4f67c73e529a875777cd8c8177f2e09295 (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
minetest.register_chatcommand(
   "/outset",
   {
      params = "<amount> [h|v]",
      description = "expand the selection",
      privs = {worldedit=true},
      func = function(name, param)
	 local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$")
	 local message

	 if find == nil then
	    worldedit.player_notify(name, "invalid usage: " .. param)
	    return
	 end
	 
	 if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then
	    message = "Undefined region. Region must be defined beforehand."
	 else
	    amount = tonumber(amount)
	    local curpos1 = worldedit.pos1[name]
	    local curpos2 = worldedit.pos2[name]

	    local dirs = worldedit.get_outset_directions(curpos1, curpos2)

	    if dir == 'h' then
	       worldedit.pos1[name].x = curpos1.x + (amount * dirs.x1)
	       worldedit.pos1[name].z = curpos1.z + (amount * dirs.z1)

	       worldedit.pos2[name].x = curpos2.x + (amount * dirs.x2)
	       worldedit.pos2[name].z = curpos2.z + (amount * dirs.z2)

	       message = "area expanded by " .. amount .. " blocks horizontally"
	    elseif dir == 'v' then
	       worldedit.pos1[name].y = curpos1.y + (amount * dirs.y1)
	       worldedit.pos2[name].y = curpos2.y + (amount * dirs.y2)

	       message = "area expanded by " .. amount .. " blocks vertically"
	    else
	       worldedit.pos1[name].x = curpos1.x + (amount * dirs.x1)
	       worldedit.pos1[name].z = curpos1.z + (amount * dirs.z1)
	       worldedit.pos1[name].y = curpos1.y + (amount * dirs.y1)

	       worldedit.pos2[name].x = curpos2.x + (amount * dirs.x2)
	       worldedit.pos2[name].z = curpos2.z + (amount * dirs.z2)
	       worldedit.pos2[name].y = curpos2.y + (amount * dirs.y2)

	       message = "area expanded by " .. amount .. " blocks in all axes"
	    end

	    worldedit.mark_pos1(name)
	    worldedit.mark_pos2(name)
	 end

	 worldedit.player_notify(name, message)
      end,
   }
)

minetest.register_chatcommand(
   "/inset",
   {
      params = "<amount> [h|v]",
      description = "contract",
      privs = {worldedit=true},
      func = function(name, param)
	 local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$")
	 local message = ""

	 if find == nil then
	    worldedit.player_notify(name, "invalid usage: " .. param)
	    return
	 end
	 
	 if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then
	    message = "Undefined region. Region must be defined beforehand."
	 else
	    amount = tonumber(amount)
	    local curpos1 = worldedit.pos1[name]
	    local curpos2 = worldedit.pos2[name]

	    local dirs = worldedit.get_outset_directions(curpos1, curpos2)

	    if dir == 'h' then
	       worldedit.pos1[name].x = curpos1.x - (amount * dirs.x1)
	       worldedit.pos1[name].z = curpos1.z - (amount * dirs.z1)

	       worldedit.pos2[name].x = curpos2.x - (amount * dirs.x2)
	       worldedit.pos2[name].z = curpos2.z - (amount * dirs.z2)

	       message = "area contracted by " .. amount .. " blocks horizontally"
	    elseif dir == 'v' then
	       worldedit.pos1[name].y = curpos1.y - (amount * dirs.y1)
	       worldedit.pos2[name].y = curpos2.y - (amount * dirs.y2)

	       message = "area contracted by " .. amount .. " blocks vertically"
	    else
	       worldedit.pos1[name].x = curpos1.x - (amount * dirs.x1)
	       worldedit.pos1[name].z = curpos1.z - (amount * dirs.z1)
	       worldedit.pos1[name].y = curpos1.y - (amount * dirs.y1)

	       worldedit.pos2[name].x = curpos2.x - (amount * dirs.x2)
	       worldedit.pos2[name].z = curpos2.z - (amount * dirs.z2)
	       worldedit.pos2[name].y = curpos2.y - (amount * dirs.y2)

	       message = "area contracted by " .. amount .. " blocks in all axes"
	    end

	    worldedit.mark_pos1(name)
	    worldedit.mark_pos2(name)
	 end

	 worldedit.player_notify(name, message)
      end,
   }
)

worldedit.get_outset_directions = function(mark1, mark2)
   if mark1 == nil or mark2 == nil then return
   end
   
   local dirs =
      {
	 x1 = 0,
	 x2 = 0,
	 y1 = 0,
	 y2 = 0,
	 z1 = 0,
	 z2 = 0
      }
   
   if mark1.x < mark2.x then
      dirs.x1 = -1
      dirs.x2 = 1
   else
      dirs.x1 = 1
      dirs.x2 = -1
   end

   if mark1.y < mark2.y then
      dirs.y1 = -1
      dirs.y2 = 1
   else
      dirs.y1 = 1
      dirs.y2 = -1
   end

   if mark1.z < mark2.z then
      dirs.z1 = -1
      dirs.z2 = 1
   else
      dirs.z1 = 1
      dirs.z2 = -1
   end

   return dirs
end


minetest.register_chatcommand(
   "/shift",
   {
      params = "<amount> [up|down|left|right|front|back]",
      description = "Moves the selection region. Does not move contents.",
      privs = {worldedit=true},
      func = function(name, param)
	 local pos1 = worldedit.pos1[name]
	 local pos2 = worldedit.pos2[name]
	 local find, _, amount, direction = param:find("(%d+)%s*(%l*)")

	 if find == nil then
	    worldedit.player_notify(name, "invalid usage: " .. param)
	    return
	 end
	 
	 if pos1 == nil or pos2 == nil then
	    worldedit.player_notify(name, "Undefined region. Region must be defined beforehand.")
	    return
	 end

	 local axis, dirsign
	 
	 if direction ~= "" then
	    axis, dirsign = worldedit.translate_direction(name, direction)
	 else
	    axis, dirsign = worldedit.player_axis(name)
	 end

	 if axis == nil or dirsign == nil then
	    return false, "Invalid usage: " .. param
	 end
	 
	 worldedit.move_marker(name, 1, axis, amount * dirsign)
	 worldedit.move_marker(name, 2, axis, amount * dirsign)
	 worldedit.update_markers(name)

	 worldedit.player_notify(name, "Area shifted by " .. amount .. " in " .. direction .. " axis")
      end,
   }
)

minetest.register_chatcommand(
   "/expand",
   {
      params = "<amount> [reverse-amount] [up|down|left|right|front|back]",
      description = "expand the selection in one or two directions at once",
      privs = {worldedit=true},
      func = function(name, param)
	 local find, _, amount, arg2, arg3 = param:find("(%d+)%s*(%w*)%s*(%l*)")

	 if find == nil then
	    worldedit.player_notify(name, "invalid use: " .. param)
	    return
	 end

	 if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then
	    worldedit.player_notify(name, "Undefined region. Region must be defined beforehand.")
	    return
	 end

	 local axis, direction, mark

	 axis, direction = worldedit.player_axis(name)
	 mark = worldedit.get_marker_in_axis(name, axis, direction)

	 if arg3 ~= "" then
	    axis, direction = worldedit.translate_direction(name, arg3)
	    mark = worldedit.get_marker_in_axis(name, axis, direction)
	 end
	 
	 if arg2 ~= "" then
	    local tmp = tonumber(arg2)

	    if tmp == nil then
	       axis, direction = worldedit.translate_direction(name, arg2)
	       mark = worldedit.get_marker_in_axis(name, axis, direction)
	    else
	       local tmpmark
	       if mark == 1 then
		  tmpmark = 2
	       else
		  tmpmark = 1
	       end

	       if axis == nil or direction == nil then
		  return false, "Invalid use: " .. param
	       end

	       worldedit.move_marker(name, tmpmark, axis, tmp * direction * -1)
	    end
	 end

	 if axis == nil or direction == nil then
	    return false, "Invalid use: " .. param
	 end

	 worldedit.move_marker(name, mark, axis, amount * direction)	 
	 worldedit.update_markers(name)
	 worldedit.player_notify(name, "Area expanded by " .. amount)
      end,
   }
)

minetest.register_chatcommand(
   "/contract",
   {
      params = "<amount> [reverse-amount] [up|down|left|right|front|back]",
      description = "contract the selection in one or two directions at once",
      privs = {worldedit=true},
      func = function(name, param)
	 local find, _, amount, arg2, arg3 = param:find("(%d+)%s*(%w*)%s*(%l*)")

	 if find == nil then
	    worldedit.player_notify(name, "invalid use: " .. param)
	    return
	 end

	 if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then
	    worldedit.player_notify(name, "Undefined region. Region must be defined beforehand.")
	    return
	 end

	 local axis, direction, mark

	 axis, direction = worldedit.player_axis(name)
	 mark = worldedit.get_marker_in_axis(name, axis, direction)

	 if arg3 ~= "" then
	    axis, direction = worldedit.translate_direction(name, arg3)
	    mark = worldedit.get_marker_in_axis(name, axis, direction)
	 end
	 
	 if arg2 ~= "" then
	    local tmp = tonumber(arg2)

	    if tmp == nil then
	       axis, direction = worldedit.translate_direction(name, arg2)
	       mark = worldedit.get_marker_in_axis(name, axis, direction)
	    else
	       local tmpmark
	       if mark == 1 then
		  tmpmark = 2
	       else
		  tmpmark = 1
	       end

	       if axis == nil or direction == nil then
		  return false, "Invalid use: " .. param
	       end

	       worldedit.move_marker(name, tmpmark, axis, tmp * direction)
	    end
	 end

	 if axis == nil or direction == nil then
	    return false, "Invalid use: " .. param
	 end

	 worldedit.move_marker(name, mark, axis, amount * direction * -1)	 
	 worldedit.update_markers(name)
	 worldedit.player_notify(name, "Area contracted by " .. amount)
      end,
   }
)


-- Return the marker that is closest to the player
worldedit.get_closest_marker = function(name)
   local playerpos = minetest.get_player_by_name(name):getpos()

   local dist1 = vector.distance(playerpos, worldedit.pos1[name])
   local dist2 = vector.distance(playerpos, worldedit.pos2[name])

   if dist1 < dist2 then
      return 1
   else
      return 2
   end
end


-- returns which marker is closest to the specified axis and direction
worldedit.get_marker_in_axis = function(name, axis, direction)
   local pos1 = {x = 0, y = 0, z = 0}
   local pos2 = {x = 0, y = 0, z = 0}

   if direction ~= 1 and direction ~= -1 then
      return nil
   end

   if axis == 'x' then
      pos1.x = worldedit.pos1[name].x * direction
      pos2.x = worldedit.pos2[name].x * direction
      if pos1.x > pos2.x then
	 return 1
      else
	 return 2
      end
   elseif axis == 'y' then
      pos1.y = worldedit.pos1[name].y * direction
      pos2.y = worldedit.pos2[name].y * direction
      if pos1.y > pos2.y then
	 return 1
      else
	 return 2
      end
   elseif axis == 'z' then
      pos1.z = worldedit.pos1[name].z * direction
      pos2.z = worldedit.pos2[name].z * direction
      if pos1.z > pos2.z then
	 return 1
      else
	 return 2
      end
   else
      minetest.debug("worldedit.get_marker_in_axis: invalid axis.")
   end
end

-- Moves the selected marker in a single axis by amount nodes
worldedit.move_marker = function(name, marker, axis, amount)
   local pos1 = worldedit.pos1[name]
   local pos2 = worldedit.pos2[name]
   
   if marker == 1 then
      if axis == 'x' then
	 worldedit.pos1[name].x = pos1.x + amount
      elseif axis == 'y' then
	 worldedit.pos1[name].y = pos1.y + amount
      elseif axis == 'z' then
	 worldedit.pos1[name].z = pos1.z + amount
      else
	 minetest.debug("worldedit: Invalid axis in move_marker. Value was: " .. axis)
      end
   elseif marker == 2 then
      if axis == 'x' then
	 worldedit.pos2[name].x = pos2.x + amount
      elseif axis == 'y' then
	 worldedit.pos2[name].y = pos2.y + amount
      elseif axis == 'z' then
	 worldedit.pos2[name].z = pos2.z + amount
      else
	 minetest.debug("worldedit: Invalid axis in move_marker. Value was: " .. axis)
      end
   else
      minetest.debug("Bad marker id at worldedit.move_marker")
   end
end

-- Updates the location ingame of the markers
worldedit.update_markers = function(name, marker)
   if marker == nil then
      worldedit.mark_pos1(name)
      worldedit.mark_pos2(name)
   elseif marker == 1 then
      worldedit.mark_pos1(name)
   elseif marker == 2 then
      worldedit.mark_pos2(name)
   else
      minetest.debug("worldedit: Invalid execution of function update_markers")
   end
end


-- Translates up, down, left, right, front, back to their corresponding axes and directions according to faced direction
worldedit.translate_direction = function(name, direction)
   local axis, dir = worldedit.player_axis(name)
   local resaxis, resdir

   if direction == "up" then
      return 'y', 1
   end

   if direction == "down" then
      return 'y', -1
   end

   if direction == "front" then
      resaxis = axis
      resdir = dir
   end

   if direction == "back" then
      resaxis = axis
      resdir = -dir
   end

   if direction == "left" then
      if axis == 'x' then
	 resaxis = 'z'
	 resdir = dir
      elseif axis == 'z' then
	 resaxis = 'x'
	 resdir = -dir
      end
   end

   if direction == "right" then
      if axis == 'x' then
	 resaxis = 'z'
	 resdir = -dir
      elseif axis == 'z' then
	 resaxis = 'x'
	 resdir = dir
      end
   end   

   return resaxis, resdir
   
end