-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharea_replace.lua
More file actions
139 lines (101 loc) · 3.85 KB
/
area_replace.lua
File metadata and controls
139 lines (101 loc) · 3.85 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
local utils = ...
local S = utils.S
local function replace_node (map, x, y, action, pos, radius, item, dir, player, ptdir, square)
if not map[x][y].match or utils.is_protected (map[x][y].pos, player) then
map[x][y].match = false
return true
end
local dist = vector.distance (pos, map[x][y].pos)
if dist <= radius or square then
local under_pos = vector.add (map[x][y].pos, utils.rotate_to_dir ({ x = 0, y = 0, z = 0 }, dir))
local above_pos = vector.subtract (under_pos, dir)
local above_node = utils.get_far_node (above_pos)
local above_def = (above_node and utils.find_item_def (above_node.name)) or nil
-- limit to open space before surface
if (above_node and above_node.name == "air") or
(above_def and (not above_def.walkable or above_def.liquidtype ~= "none")) then
local node = utils.get_far_node (under_pos)
local def = (node and utils.find_item_def (node.name)) or nil
if (node and node.name ~= "air") and (def and (def.walkable or def.liquidtype ~= "none")) then
local pt =
{
type = "node",
under = vector.new (map[x][y].pos),
above = vector.subtract (map[x][y].pos, ptdir)
}
if not action:place_node (map[x][y].pos, item, player, pt) then
return false
end
map[x][y].match = false
if (x + 1) <= map.max_x then
if not replace_node (map, x + 1, y, action, pos, radius, item, dir, player, ptdir, square) then
return false
end
end
if (x - 1) >= map.min_x then
if not replace_node (map, x - 1, y, action, pos, radius, item, dir, player, ptdir, square) then
return false
end
end
if (y + 1) <= map.max_y then
if not replace_node (map, x, y + 1, action, pos, radius, item, dir, player, ptdir, square) then
return false
end
end
if (y - 1) >= map.min_y then
if not replace_node (map, x, y - 1, action, pos, radius, item, dir, player, ptdir, square) then
return false
end
end
end
end
end
return true
end
local function replace (pos, item, radius, dir, player, pointed_thing, square)
local action = utils.new_action (player:get_player_name ())
if action then
local map = utils.map_nodes (pos, radius, dir, nil, true, false, square)
local ptdir = vector.subtract (pointed_thing.under, pointed_thing.above)
replace_node (map, 0, 0, action, pos, radius, item, dir, player, ptdir, square)
utils.commit_action (action)
end
end
local function on_place (itemstack, placer, pointed_thing)
if not utils.is_creative (placer) or
not utils.check_privs (placer) then
return itemstack
end
local stack, count = utils.get_item_stats (itemstack, placer)
local look_dir, point_dir, under = utils.get_place_stats (placer, pointed_thing)
if count and look_dir then
local on_rightclick = utils.get_on_rightclick (under, placer)
if on_rightclick then
return on_rightclick (under, utils.get_far_node (under), placer, itemstack, pointed_thing)
end
if not stack then
stack = "air"
end
replace (under, stack, count, point_dir, placer, pointed_thing, placer:get_player_control ().aux1)
minetest.log ("action", string.format ("lwcreative_tools area replace by %s with %s at %s, radius %d",
placer:get_player_name (),
(type (stack) == "string" and stack) or stack:get_name (),
minetest.pos_to_string (under, 0),
count + 1))
end
return itemstack
end
local function on_use (itemstack, user, pointed_thing)
return utils.on_use (itemstack, user, pointed_thing, utils.settings.max_block_radius)
end
minetest.register_craftitem ("lwcreative_tools:replace", {
description = S("Area Replace"),
short_description = S("Area Replace"),
groups = { },
inventory_image = "lwcreative_tools_replace.png",
wield_image = "lwcreative_tools_replace.png",
stack_max = utils.settings.max_block_radius,
liquids_pointable = true,
on_place = on_place,
on_use = on_use,
})