-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathundo.lua
More file actions
460 lines (297 loc) · 8.01 KB
/
undo.lua
File metadata and controls
460 lines (297 loc) · 8.01 KB
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
local utils = ...
local function remove_node (pos)
local node_data = utils.get_node_data (pos)
if node_data then
local def = utils.find_item_def (node_data.node.name)
if not def then
return nil
end
if node_data.meta and node_data.drops and def and def.preserve_metadata then
def.preserve_metadata (pos, node_data.node, minetest.get_meta (pos), node_data.drops)
end
pcall (minetest.remove_node, pos)
return node_data
end
return nil
end
local function restore_node (pos, node_data)
if node_data.node.name ~= "air" then
local def = utils.find_item_def (node_data.node.name)
if not def then
return false
end
pcall (minetest.set_node, pos, node_data.node)
if node_data.meta then
local meta = minetest.get_meta (pos)
if not meta then
return false
end
meta:from_table (node_data.meta)
end
end
return true
end
local function get_place_param2 (itemname, look_dir)
local def = utils.find_item_def (itemname)
if def and def.paramtype2 then
if def.paramtype2 == "wallmounted" then
return minetest.dir_to_wallmounted (look_dir)
elseif def.paramtype2 == "facedir" then
return minetest.dir_to_facedir (look_dir, false)
end
end
return 0
end
local function place_node (pos, itemstack, placer, pointed_thing)
if itemstack then
local stack = ItemStack (itemstack)
if stack then
local def = utils.find_item_def (stack:get_name ())
if not def then
return nil
end
local placed = false
if def and def.on_place then
placed = pcall (def.on_place, stack, placer, pointed_thing)
if placed and def.after_place_node then
pcall (def.after_place_node, pos, placer, stack, pointed_thing)
end
end
if not placed then
local param2 = get_place_param2 (stack:get_name (),
vector.normalize (placer:get_look_dir ()))
pcall (minetest.set_node, pos, { name = stack:get_name (), param2 = param2 })
if def.after_place_node then
pcall (def.after_place_node, pos, placer, stack, pointed_thing)
end
end
end
end
return utils.get_node_data (pos)
end
local function place_node_from_data (pos, node_data, rotation, player)
if node_data and node_data.node and node_data.node.name and node_data.node.name ~= "air" then
local def = utils.find_item_def (node_data.node.name)
if not def then
if utils.settings.paste_unknown then
utils.player_error_message (player,
string.format ("Unknown node \"%s\" ignored",
node_data.node.name or ""))
return utils.get_node_data (pos)
else
utils.player_error_message (player,
string.format ("Unknown node \"%s\", operation stopped",
node_data.node.name or ""))
return false
end
end
local node =
{
name = node_data.node.name,
param1 = node_data.node.param1 or 0,
param2 = node_data.node.param2 or 0
}
-- adjust param2
if def.paramtype2 == "wallmounted" or
def.paramtype2 == "colorwallmounted" then
if (node.param2 % 8) > 1 then
local color = math.floor (node.param2 / 8) * 8
local wall_to_face =
{
[2] = 1,
[3] = 3,
[4] = 0,
[5] = 2
}
local face_to_wall =
{
[0] = 4,
[1] = 2,
[2] = 5,
[3] = 3
}
node.param2 = face_to_wall[(wall_to_face[node.param2 % 8] + rotation) % 4] + color
end
elseif def.paramtype2 == "facedir" or
def.paramtype2 == "colorfacedir" then
local color = math.floor (node.param2 / 32) * 32
local axis = math.floor (node.param2 / 4) % 8
if axis == 0 then
node.param2 = (((node.param2 % 4) + rotation) % 4) + (axis * 4) + color
elseif axis == 5 then
node.param2 = ((((node.param2 % 4) + 4) - rotation) % 4) + (axis * 4) + color
elseif axis >= 1 and axis <= 4 then
local axis_to_face =
{
[1] = 0,
[2] = 2,
[3] = 1,
[4] = 3
}
local face_to_axis =
{
[0] = 1,
[1] = 3,
[2] = 2,
[3] = 4
}
node.param2 = (face_to_axis[(axis_to_face[axis] + rotation) % 4] * 4) +
(node.param2 % 4) + color
end
end
pcall (minetest.set_node, pos, node)
if node_data.meta then
local meta = minetest.get_meta (pos)
if not meta then
return false
end
meta:from_table (node_data.meta)
end
end
return utils.get_node_data (pos)
end
local function destroy_buffer_drops (buffer)
for i = #buffer, 1, -1 do
local drops = buffer[i].drops
if drops then
for j = 1, #drops do
utils.on_destroy (drops[j])
end
end
end
end
local undo_entry = { }
-- constructor
function undo_entry:new (player_name)
if type (player_name) ~= "string" or player_name:len () < 1 then
return nil
end
local obj = { }
setmetatable(obj, self)
self.__index = self
obj.player_name = player_name
obj.buffer = { }
return obj
end
function undo_entry:add (pos, placed, removed)
self.buffer[#self.buffer + 1] =
{
placed = placed,
removed = removed,
pos = { x = pos.x, y = pos.y, z = pos.z }
}
end
local undo_action = { }
-- constructor
function undo_action:new (player_name)
if type (player_name) ~= "string" or player_name:len () < 1 then
return nil
end
local obj = { }
setmetatable(obj, self)
self.__index = self
obj.undo_entry = undo_entry:new (player_name)
return obj
end
function undo_action:place_node (pos, itemstack, placer, pointed_thing)
local stack = nil
if not (type (itemstack) == "string" and itemstack == "air") then
stack = ItemStack (itemstack)
if not itemstack then
-- unwind
return false
end
end
local removed = remove_node (vector.new (pos))
if removed then
local placed = place_node (vector.new (pos), stack, placer, pointed_thing)
if placed then
self.undo_entry:add (pos, placed, removed)
return true
end
restore_node (pos, removed)
end
return false
end
function undo_action:place_node_from_data (pos, node_data, rotation, player)
local removed = remove_node (vector.new (pos))
if removed then
local placed = place_node_from_data (vector.new (pos), node_data, rotation, player)
if placed then
self.undo_entry:add (pos, placed, removed)
return true
end
restore_node (pos, removed)
end
return false
end
local undo = { }
-- constructor
function undo:new ()
local obj = { }
setmetatable(obj, self)
self.__index = self
obj.list = { }
return obj
end
function undo:add_action (action)
if action and action.undo_entry then
-- check entries for player
if utils.settings.undo_limit > 0 then
local player_list = self.list[action.undo_entry.player_name]
if not player_list then
player_list = { }
self.list[action.undo_entry.player_name] = player_list
end
if #player_list > (utils.settings.undo_limit - 1) then
table.remove (player_list, #player_list)
end
table.insert (player_list, 1, action.undo_entry.buffer)
end
return true
end
end
function undo:undo (player_name)
local player_list = self.list[player_name]
if player_list and #player_list > 0 then
local buffer = player_list[1]
if buffer then
for i = #buffer, 1, -1 do
local node = utils.get_far_node (buffer[i].pos)
local def = (node and utils.find_item_def (node.name)) or nil
if (node and node.name == buffer[i].placed.node.name) or
(def and def.liquidtype ~= "none") then
remove_node (buffer[i].pos)
restore_node (buffer[i].pos, buffer[i].removed)
destroy_buffer_drops (buffer[i])
end
utils.long_process.expire (0.08)
end
table.remove (player_list, 1)
return true
end
end
return false
end
function undo:clear (player_name)
local player_list = self.list[player_name]
if player_list and #player_list > 0 then
table.remove (player_list, 1)
return true
end
return false
end
local undo_obj = undo:new ()
function utils.new_action (player_name)
return undo_action:new (player_name)
end
function utils.commit_action (action)
return undo_obj:add_action (action)
end
function utils.undo_action (player_name)
return undo_obj:undo (player_name)
end
function utils.clear_action (player_name)
return undo_obj:clear (player_name)
end
--