-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.lua
287 lines (271 loc) · 9.37 KB
/
init.lua
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
living_trees = {}
local modpath = minetest.get_modpath("living_trees")
dofile(modpath.."/breaking.lua")
dofile(modpath.."/branches.lua")
function opposite_dir(dir)
if dir % 2 == 0 then
return dir + 1
else
return dir - 1
end
end
function wallmounted_to_facedir(dir)
if dir == 0 then
return 0
elseif dir == 1 then
return 5
elseif dir == 2 then
return 3
elseif dir == 3 then
return 4
elseif dir == 4 then
return 1
elseif dir == 5 then
return 2
end
end
function add_dir_to_pos(pos, dir)
if dir == 0 then
pos.y = pos.y + 1
elseif dir == 1 then
pos.y = pos.y - 1
elseif dir == 2 then
pos.x = pos.x + 1
elseif dir == 3 then
pos.x = pos.x - 1
elseif dir == 4 then
pos.z = pos.z + 1
elseif dir == 5 then
pos.z = pos.z - 1
end
return pos
end
function living_trees.register_tree(tree)
for _, root in ipairs(tree.roots) do
minetest.override_item(root,
{
after_dig_node = function(pos, oldnode, oldmetadata, digger)
break_childs(pos, oldnode, true)
end
})
end
local trunk_def = table.copy(minetest.registered_nodes[tree.trunk])
trunk_def.description = tree.name.." trunk"
trunk_def.drop = tree.trunk
trunk_def.paramtype2 = "wallmounted"
trunk_def.on_place = nil
trunk_def.after_dig_node = function(pos, oldnode, oldmetadata, digger)
break_childs(pos, oldnode)
end
tree.name = tree.name:lower()
minetest.register_node("living_trees:branch_trunk_"..tree.name, trunk_def)
local branch_trunk = "living_trees:branch_trunk_"..tree.name
register_branches(tree.name, tree.texture)
local branch_3_4 = "living_trees:branch_3_4_"..tree.name
local branch_2_3 = "living_trees:branch_2_3_"..tree.name
local branch_1_2 = "living_trees:branch_1_2_"..tree.name
local branch_T_1 = "living_trees:branch_T_1_"..tree.name
local branch_4 = "living_trees:branch_4_"..tree.name
local branch_3 = "living_trees:branch_3_"..tree.name
local branch_2 = "living_trees:branch_2_"..tree.name
local branch_1 = "living_trees:branch_1_"..tree.name
minetest.register_abm({
nodenames = tree.roots,
interval = 50.0,
chance = 100,
action = function(pos, node, active_object_count, active_object_count_wider)
local curpos = pos
local currot = {dir = 0, up = 3, left = 4} -- In wallmounted numbers
local meta = minetest.get_meta(pos)
local lstr = meta:get_string("lstring")
local stack = {}
local skipbranch = 0
for i = 1, #lstr do
local c = lstr:sub(i,i)
if c == "]" then
if skipbranch > 1 then
skipbranch = skipbranch - 1
else
skipbranch = 0
currot = table.remove(stack)
curpos = table.remove(stack)
end
elseif c == "[" then
if skipbranch > 0 then
skipbranch = skipbranch + 1
else
local savedpos = {}
for k,v in pairs(curpos) do
savedpos[k] = v
end
local savedrot = {}
for k,v in pairs(currot) do
savedrot[k] = v
end
table.insert(stack, savedpos)
table.insert(stack, savedrot)
end
elseif skipbranch > 0 then
elseif c == "T" then
add_dir_to_pos(curpos, currot.dir)
local name = minetest.get_node(curpos).name
if name == "air" or name == tree.leaves then
minetest.set_node(curpos, {name=branch_3_4, param2 = opposite_dir(currot.dir)})
skipbranch = skipbranch + 1
elseif name == branch_3_4 then
minetest.set_node(curpos, {name=branch_2_3, param2 = opposite_dir(currot.dir)})
elseif name == branch_2_3 then
minetest.set_node(curpos, {name=branch_1_2, param2 = opposite_dir(currot.dir)})
elseif name == branch_1_2 then
minetest.set_node(curpos, {name=branch_T_1, param2 = opposite_dir(currot.dir)})
elseif name == branch_T_1 then
minetest.set_node(curpos, {name=branch_trunk, param2 = opposite_dir(currot.dir)}) --wallmounted_to_facedir(currot.dir) * 4})
elseif name ~= branch_trunk then
skipbranch = skipbranch + 1
end
elseif c == "Q" then
add_dir_to_pos(curpos, currot.dir)
local name = minetest.get_node(curpos).name
if name == "air" or name == tree.leaves then
minetest.set_node(curpos, {name=branch_3_4, param2 = opposite_dir(currot.dir)})
skipbranch = skipbranch + 1
elseif name == branch_3_4 then
minetest.set_node(curpos, {name=branch_2_3, param2 = opposite_dir(currot.dir)})
elseif name == branch_2_3 then
minetest.set_node(curpos, {name=branch_1_2, param2 = opposite_dir(currot.dir)})
elseif name == branch_1_2 then
minetest.set_node(curpos, {name=branch_T_1, param2 = opposite_dir(currot.dir)})
elseif name ~= branch_T_1 then
skipbranch = skipbranch + 1
end
elseif c == "W" then
add_dir_to_pos(curpos, currot.dir)
local name = minetest.get_node(curpos).name
if name == "air" or name == tree.leaves then
minetest.set_node(curpos, {name=branch_3_4, param2 = opposite_dir(currot.dir)})
skipbranch = skipbranch + 1
elseif name == branch_3_4 then
minetest.set_node(curpos, {name=branch_2_3, param2 = opposite_dir(currot.dir)})
elseif name == branch_2_3 then
minetest.set_node(curpos, {name=branch_1_2, param2 = opposite_dir(currot.dir)})
elseif name ~= branch_1_2 then
skipbranch = skipbranch + 1
end
elseif c == "E" then
add_dir_to_pos(curpos, currot.dir)
local name = minetest.get_node(curpos).name
if name == "air" or name == tree.leaves then
minetest.set_node(curpos, {name=branch_3_4, param2 = opposite_dir(currot.dir)})
skipbranch = skipbranch + 1
elseif name == branch_3_4 then
minetest.set_node(curpos, {name=branch_2_3, param2 = opposite_dir(currot.dir)})
elseif name ~= branch_2_3 then
skipbranch = skipbranch + 1
end
elseif c == "R" then
add_dir_to_pos(curpos, currot.dir)
local name = minetest.get_node(curpos).name
if name == "air" or name == tree.leaves then
minetest.set_node(curpos, {name=branch_3_4, param2 = opposite_dir(currot.dir)})
skipbranch = skipbranch + 1
elseif name ~= branch_3_4 then
skipbranch = skipbranch + 1
end
elseif c == "1" then
add_dir_to_pos(curpos, currot.dir)
local name = minetest.get_node(curpos).name
if name == "air" or name == tree.leaves then
minetest.set_node(curpos, {name=branch_4, param2 = opposite_dir(currot.dir)})
skipbranch = skipbranch + 1
elseif name == branch_4 then
minetest.set_node(curpos, {name=branch_3, param2 = opposite_dir(currot.dir)})
elseif name == branch_3 then
minetest.set_node(curpos, {name=branch_2, param2 = opposite_dir(currot.dir)})
elseif name == branch_2 then
minetest.set_node(curpos, {name=branch_1, param2 = opposite_dir(currot.dir)})
elseif name ~= branch_1 then
skipbranch = skipbranch + 1
end
elseif c == "2" then
add_dir_to_pos(curpos, currot.dir)
local name = minetest.get_node(curpos).name
if name == "air" or name == tree.leaves then
minetest.set_node(curpos, {name=branch_4, param2 = opposite_dir(currot.dir)})
skipbranch = skipbranch + 1
elseif name == branch_4 then
minetest.set_node(curpos, {name=branch_3, param2 = opposite_dir(currot.dir)})
elseif name == branch_3 then
minetest.set_node(curpos, {name=branch_2, param2 = opposite_dir(currot.dir)})
elseif name ~= branch_2 then
skipbranch = skipbranch + 1
end
elseif c == "3" then
add_dir_to_pos(curpos, currot.dir)
local name = minetest.get_node(curpos).name
if name == "air" or name == tree.leaves then
minetest.set_node(curpos, {name=branch_4, param2 = opposite_dir(currot.dir)})
skipbranch = skipbranch + 1
elseif name == branch_4 then
minetest.set_node(curpos, {name=branch_3, param2 = opposite_dir(currot.dir)})
elseif name ~= branch_3 then
skipbranch = skipbranch + 1
end
elseif c == "4" then
add_dir_to_pos(curpos, currot.dir)
local name = minetest.get_node(curpos).name
if name == "air" or name == tree.leaves then
minetest.set_node(curpos, {name=branch_4, param2 = opposite_dir(currot.dir)})
skipbranch = skipbranch + 1
elseif name ~= branch_4 then
skipbranch = skipbranch + 1
end
elseif c == "^" then
local temp = opposite_dir(currot.dir)
currot.dir = currot.up
currot.up = temp
elseif c == "&" then
local temp = opposite_dir(currot.up)
currot.up = currot.dir
currot.dir = temp
elseif c == "+" then
local temp = opposite_dir(currot.left)
currot.left = currot.dir
currot.dir = temp
elseif c == "-" then
local temp = opposite_dir(currot.dir)
currot.dir = currot.left
currot.left = temp
elseif c == "/" then
local temp = opposite_dir(currot.left)
currot.left = currot.up
currot.up = temp
elseif c == "*" then
local temp = opposite_dir(currot.up)
currot.up = currot.left
currot.left = temp
end
end
end
})
if tree.leaves then
minetest.register_abm({
nodenames = {branch_3_4, branch_4},
interval = 30.0,
chance = 50,
action = function(pos, node, active_object_count, active_object_count_wider)
for x = -1,1 do
for y = -1,1 do
for z = -1,1 do
local curpos = {x = x, y = y, z = z}
curpos = vector.add(pos, curpos)
if minetest.get_node(curpos).name == "air" and (y == 0 or x == 0 or z == 0) then
minetest.set_node(curpos, {name = tree.leaves})
end
end
end
end
end
})
end
end
dofile(modpath.."/default_trees.lua")