sorcery  Diff

Differences From Artifact [3dbeb921b1]:

To Artifact [483baaca72]:


1
2
3
4
5
6
7


8
9
10
11
12
13
14
...
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
local log = sorcery.logger('lib.node')
local ofs = {
	neighbors = {
		{x =  1, y =  0, z =  0};
		{x = -1, y =  0, z =  0};
		{x =  0, y =  1, z =  0};
		{x =  0, y = -1, z =  0};


		{x =  0, y =  0, z =  1};
		{x =  0, y =  0, z = -1};
	};
	planecorners = {
		{x =  1, y =  0, z =  1};
		{x = -1, y =  0, z =  1};
		{x = -1, y =  0, z = -1};
................................................................................
		-- various problems could be avoided by unconditionally inserted the meta key,
		-- or inserting it also when it comes into contact with another trunk node,
		-- but pepole use these things to build with and that is just way way too many
		-- meta keys for me to consider it an option.
		--
		-- verdict: not very good, but decent enough for most cases. mtg should have
		--          done better than this, but now we're all stuck with their bullshit



		local treetype = force(pos).name
		if minetest.get_item_group(treetype, 'tree') == 0 then -- sir this is not a tree
			return nil -- 無
		end
		local treedef = sorcery.lib.tbl.select(sorcery.data.trees, 'node', treetype)
		local leaftype = treedef and treedef.leaves or nil


		local uppermost, lowermost


		local treemap, treenodes = amass(pos,function(node, where)













































			if node.name == treetype then
				-- abuse predicate function to also track y minimum, so we can
				-- avoid iterating over it all later again -- this function is
				-- expensive enough already
				if (not lowermost) or where.y < lowermost then
					lowermost = where.y
				end
				if (not uppermost) or where.y > uppermost then
					uppermost = where.y
				end
				local m=minetest.get_meta(where)
				if m:get_int('sorcery:trunk_node_role') ~= 1 then
					return true
				else
					log.warn('found a log node!')
					return false
				end
			end
			if leaftype == nil then
				if minetest.get_item_group(node.name, 'leaves') ~= 0 and node.param2 == 0 then
					log.warn('guessing leaf node for tree',treetype,'is',node.name,'; please report this bug to the mod responsible for this tree and ask for appropriate Sorcery interop code to be added')
					leaftype = node.name
					return true
				end
			elseif leaftype == node.name and node.param2 == 0 then
				return true
			end
			return false
		end,ofs.adjoining)


		if leaftype == nil then return false end


		local trunkmap, trunknodes = amass(pos, {treetype}, ofs.adjoining)
		if treenodes[leaftype] == nil then return false end


		local cache = {}
		local uppermost_check_leaves = true
		local topnode
		for _, p in pairs(treenodes[treetype]) do
			-- if not sorcery.lib.tbl.select(trunknodes[treetype], function(v)
			-- 	return vector.equals(p, v)
			-- end, cache) then
			-- 	log.act('tree node', p, 'not accounted for in trunk!')
			-- 	return false
			-- end
			if p.y == uppermost and uppermost_check_leaves then
				topnode = p
				uppermost_check_leaves = false
			end
			if p.y == lowermost then
				-- this is the bottom of the tree, bail if it's in not full contact
				-- with soil or other eligible nodes as determined by the tree def's
				-- 'rooted' predicate
				local beneath = vector.offset(p, 0,-1,0);
				if treedef.rooted then
					if not treedef.rooted {
						trunk = p;
						groundpos = beneath;
						ground = force(beneath);
						treemap = treemap;
						treenodes = treenodes;
					} then return false end
				else
					if minetest.get_item_group(force(beneath).name, 'soil') == 0 then
						return false
					end
				end
			end
		end


		if uppermost_check_leaves then
			for _,p in pairs(treenodes[leaftype]) do
				if p.y == uppermost then
					topnode = p
					break
				end
			end
		end
		--
		--make sure the tree gets enough light
		if checklight and minetest.get_natural_light(vector.offset(topnode,0,1,0), 0.5) < 13 then return false end
		

		-- other possible checks: make sure all ground-touching nodes are directly
		-- adjacent


		return true, {map = treemap, nodes = treenodes, trunk = treetype, leaves = leaftype, topnode = topnode}
	end;

	get_arrival_point = function(pos)
		local try = function(p)
			local air = sorcery.lib.node.is_clear
			if air(p) then
				if air(vector.offset(p,0,1,0))  then return p end



<
<


>
>







 







>
>







>
>

>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
>
|
<
>
|
|
<
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
>
|
|
|
|
|
|
|
|
|
|
|
<
>
|
|
<
>
|







1
2
3


4
5
6
7
8
9
10
11
12
13
14
...
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
local log = sorcery.logger('lib.node')
local ofs = {
	neighbors = {


		{x =  0, y =  1, z =  0};
		{x =  0, y = -1, z =  0};
		{x =  1, y =  0, z =  0};
		{x = -1, y =  0, z =  0};
		{x =  0, y =  0, z =  1};
		{x =  0, y =  0, z = -1};
	};
	planecorners = {
		{x =  1, y =  0, z =  1};
		{x = -1, y =  0, z =  1};
		{x = -1, y =  0, z = -1};
................................................................................
		-- various problems could be avoided by unconditionally inserted the meta key,
		-- or inserting it also when it comes into contact with another trunk node,
		-- but pepole use these things to build with and that is just way way too many
		-- meta keys for me to consider it an option.
		--
		-- verdict: not very good, but decent enough for most cases. mtg should have
		--          done better than this, but now we're all stuck with their bullshit
		--
		--  UPDATE: in pratice this is way too expensive to be functional, and causes servers to hang. we're replacing it with a simpler version

		local treetype = force(pos).name
		if minetest.get_item_group(treetype, 'tree') == 0 then -- sir this is not a tree
			return nil -- 無
		end
		local treedef = sorcery.lib.tbl.select(sorcery.data.trees, 'node', treetype)
		local leaftype = treedef and treedef.leaves or nil
		if not leaftype then return false end

		local uppermost, lowermost
		local found_leaves = false

		local treemap, treenodes = amass(pos,function(node, where)
			print(where, 'treetype',treetype,'node',node.name,node.param1,node.param2)
			if node.name == treetype and node.param1 == 0 then
				-- abuse predicate so we can avoid iterating over it all later
				-- again -- this function is expensive enough already
				if (not lowermost) or where.y < lowermost.y then
					lowermost = where
				end

				if (not uppermost) or where.y > uppermost.y then
					uppermost = where
				end
				return true
			elseif not found_leaves and node.name == leaftype and node.param2 == 0 then
				found_leaves = true
			end
			return false
		end, ofs.adjoining)

		if not found_leaves then return false end

		-- do -- leaf search
		-- 	local pss, ct = minetest.find_nodes_in_area(uppermost:offset(-1,0,-1), uppermost:offset(1,1,1), leaftype)
		-- 	if ct[leaftype] >= 1 then
		-- 		for _, p in pairs(pss) do
		-- 			local ln = force(p)
		-- 			if ln.param2 == 0 then goto found_leaves end
		-- 		end
		-- 	end
		-- 	return false
		-- end

		::found_leaves:: do -- soil check
			local beneath = force(lowermost:offset(0,-1,0))
			if minetest.get_item_group(beneath.name, 'soil') == 0 then
				return false
			end
		end

		return true, {map = treemap, nodes = treenodes, trunk = treetype, leaves = leaftype, topnode = uppermost, roots = lowermost}

		-- if checklight then iterate to leaf top and check light

		-- local uppermost, lowermost
        --
		-- local treemap, treenodes = amass(pos,function(node, where)
		-- 	if node.name == treetype then
		-- 		-- abuse predicate function to also track y minimum, so we can
		-- 		-- avoid iterating over it all later again -- this function is
		-- 		-- expensive enough already
		-- 		if (not lowermost) or where.y < lowermost then
		-- 			lowermost = where.y
		-- 		end
		-- 		if (not uppermost) or where.y > uppermost then
		-- 			uppermost = where.y
		-- 		end
		-- 		local m=minetest.get_meta(where)
		-- 		if m:get_int('sorcery:trunk_node_role') ~= 1 then
		-- 			return true
		-- 		else
		-- 			log.warn('found a log node!')
		-- 			return false
		-- 		end
		-- 	end
		-- 	if leaftype == nil then
		-- 		if minetest.get_item_group(node.name, 'leaves') ~= 0 and node.param2 == 0 then
		-- 			log.warn('guessing leaf node for tree',treetype,'is',node.name,'; please report this bug to the mod responsible for this tree and ask for appropriate Sorcery interop code to be added')
		-- 			leaftype = node.name
		-- 			return true
		-- 		end
		-- 	elseif leaftype == node.name and node.param2 == 0 then
		-- 		return true
		-- 	end
		-- 	return false
		-- end,ofs.adjoining)

        --
		-- if leaftype == nil then return false end

        --
		-- local trunkmap, trunknodes = amass(pos, {treetype}, ofs.adjoining)
		-- if treenodes[leaftype] == nil then return false end

        --
		-- local cache = {}
		-- local uppermost_check_leaves = true
		-- local topnode
		-- for _, p in pairs(treenodes[treetype]) do
		-- 	-- if not sorcery.lib.tbl.select(trunknodes[treetype], function(v)
		-- 	-- 	return vector.equals(p, v)
		-- 	-- end, cache) then
		-- 	-- 	log.act('tree node', p, 'not accounted for in trunk!')
		-- 	-- 	return false
		-- 	-- end
		-- 	if p.y == uppermost and uppermost_check_leaves then
		-- 		topnode = p
		-- 		uppermost_check_leaves = false
		-- 	end
		-- 	if p.y == lowermost then
		-- 		-- this is the bottom of the tree, bail if it's in not full contact
		-- 		-- with soil or other eligible nodes as determined by the tree def's
		-- 		-- 'rooted' predicate
		-- 		local beneath = vector.offset(p, 0,-1,0);
		-- 		if treedef.rooted then
		-- 			if not treedef.rooted {
		-- 				trunk = p;
		-- 				groundpos = beneath;
		-- 				ground = force(beneath);
		-- 				treemap = treemap;
		-- 				treenodes = treenodes;
		-- 			} then return false end
		-- 		else
		-- 			if minetest.get_item_group(force(beneath).name, 'soil') == 0 then
		-- 				return false
		-- 			end
		-- 		end
		-- 	end
		-- end

        --
		-- if uppermost_check_leaves then
		-- 	for _,p in pairs(treenodes[leaftype]) do
		-- 		if p.y == uppermost then
		-- 			topnode = p
		-- 			break
		-- 		end
		-- 	end
		-- end
		-- --
		-- --make sure the tree gets enough light
		-- if checklight and minetest.get_natural_light(vector.offset(topnode,0,1,0), 0.5) < 13 then return false end

		--
		-- -- other possible checks: make sure all ground-touching nodes are directly
		-- -- adjacent

        --
		-- return true, {map = treemap, nodes = treenodes, trunk = treetype, leaves = leaftype, topnode = topnode}
	end;

	get_arrival_point = function(pos)
		local try = function(p)
			local air = sorcery.lib.node.is_clear
			if air(p) then
				if air(vector.offset(p,0,1,0))  then return p end