Repository navigation
Expand file tree
/
Copy pathdecay.lua
More file actions
661 lines (591 loc) · 20.6 KB
/
Copy pathdecay.lua
File metadata and controls
661 lines (591 loc) · 20.6 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
-- FastLeafDecay - fast leaf decay for the Cuberite Minecraft server
-- Copyright 2026 FastLeafDecay contributors
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Portions of this file are derived from Cuberite (https://cuberite.org),
-- Copyright 2011-2025 Cuberite Contributors, Apache-2.0 - see the NOTICE file.
-- decay.lua
-- Core of the FastLeafDecay plugin.
--
-- Cuberite already implements leaf decay: cBlockLeavesHandler::OnUpdate() runs a
-- breadth-first search over leaves (up to LEAVES_CHECK_DISTANCE = 6 steps) and drops the
-- block when no log is reachable. The cost of that implementation is that the search is
-- performed once per leaves block, each time reading a 13 x 13 x 13 area, and that the
-- checks are spread over several ticks through the block-update queue.
--
-- This file replaces that with a single pass: the connected leaves component is walked
-- once, and a multi-source BFS seeded from every leaves block that touches a log computes
-- the distance to the nearest log for all blocks of the component at the same time.
-- Leaves that cannot reach a log within MaxDistance steps are then dropped.
--
-- The traversal and the BFS follow exactly the same connectivity rules as the vanilla /
-- Cuberite check, so the set of leaves that is dropped is the same as with native decay,
-- only with far fewer block reads.
--
-- Two delivery modes for the drop itself:
-- Gradual = false everything is dropped inside the hook call (instant, can look abrupt)
-- Gradual = true the orphaned leaves are queued and dropped in batches of
-- LeavesPerBatch every DecayIntervalTicks, in the order the traversal
-- found them (roughly from the broken log outwards), which reads as the
-- canopy crumbling away.
--- Value stored in the block cache for a coordinate whose chunk is not loaded.
local FLD_UNKNOWN = -1
local FLD_LEAF_TYPES =
{
[E_BLOCK_LEAVES] = true,
[E_BLOCK_NEW_LEAVES] = true,
}
local FLD_LOG_TYPES =
{
[E_BLOCK_LOG] = true,
[E_BLOCK_NEW_LOG] = true,
}
local FLD_NEIGHBOURS =
{
{ 1, 0, 0},
{-1, 0, 0},
{ 0, 1, 0},
{ 0, -1, 0},
{ 0, 0, 1},
{ 0, 0, -1},
}
--- Returns true if a_BlockType is a leaves block type.
function FLD_IsLeaf(a_BlockType)
return FLD_LEAF_TYPES[a_BlockType] == true
end
--- Returns true if a_BlockType is a log block type.
function FLD_IsLog(a_BlockType)
return FLD_LOG_TYPES[a_BlockType] == true
end
-- Blocks a vine cannot attach to, mirroring cBlockVinesHandler::IsBlockAttachable().
local FLD_VINE_UNATTACHABLE =
{
[E_BLOCK_CHEST] = true,
[E_BLOCK_ENDER_CHEST] = true,
[E_BLOCK_GLASS] = true,
[E_BLOCK_PISTON] = true,
[E_BLOCK_PISTON_EXTENSION] = true,
[E_BLOCK_REDSTONE_REPEATER_OFF] = true,
[E_BLOCK_REDSTONE_REPEATER_ON] = true,
[E_BLOCK_STAINED_GLASS] = true,
[E_BLOCK_STICKY_PISTON] = true,
[E_BLOCK_TRAPPED_CHEST] = true,
}
-- The four horizontal sides a vine can attach to, with the meta bit that selects them.
local FLD_VINE_SIDES =
{
{ 0, 1, 1}, -- south (ZP)
{-1, 0, 2}, -- west (XM)
{ 0, -1, 4}, -- north (ZM)
{ 1, 0, 8}, -- east (XP)
}
--- Bit test that also works on Lua 5.1 (Cuberite has no bitwise operators).
local function FLD_HasMetaBit(a_Meta, a_Bit)
return (math.floor(a_Meta / a_Bit) % 2) == 1
end
--- Mirror of cBlockVinesHandler::IsBlockAttachable().
local function FLD_IsVineAttachable(a_BlockType)
if FLD_VINE_UNATTACHABLE[a_BlockType] then
return false
end
return cBlockInfo:IsSolid(a_BlockType) == true
end
--- Mirror of cBlockVinesHandler::GetMaxMeta(): can this vine still stay where it is?
-- Cuberite's own handler has a hole here: it returns VINE_UNCHANGED as soon as
-- "vineMeta & maxMeta == vineMeta", which is always true once the meta has been reduced to
-- 0. A vine that lost its side attachments but was still held by a solid block above
-- (leaves are solid!) therefore ends up with meta 0 and can never be destroyed by a later
-- neighbour change - this is what leaves floating vines behind after the canopy is gone.
local function FLD_VineSupported(a_World, a_X, a_Y, a_Z)
local IsValid, BlockType, Meta = a_World:GetBlockInfo(Vector3i(a_X, a_Y, a_Z))
if (not IsValid) or (BlockType ~= E_BLOCK_VINES) then
return false
end
-- Sides the vine still claims and that are attachable:
for Index = 1, 4 do
local Side = FLD_VINE_SIDES[Index]
if FLD_HasMetaBit(Meta, Side[3]) then
local Ok, OtherType = a_World:GetBlockInfo(Vector3i(a_X + Side[1], a_Y, a_Z + Side[2]))
if Ok and FLD_IsVineAttachable(OtherType) then
return true
end
end
end
if (a_Y >= 255) then
return true
end
local Ok, AboveType, AboveMeta = a_World:GetBlockInfo(Vector3i(a_X, a_Y + 1, a_Z))
if (not Ok) then
return true -- unknown chunk: better to leave the vine alone
end
if FLD_IsVineAttachable(AboveType) then
return true
end
if (AboveType == E_BLOCK_VINES) then
-- A vine hangs from the vine above wherever the two share a side bit:
for Index = 1, 4 do
local Side = FLD_VINE_SIDES[Index]
if FLD_HasMetaBit(Meta, Side[3]) and FLD_HasMetaBit(AboveMeta, Side[3]) then
return true
end
end
end
return false
end
--- Removes the vines around a block that just disappeared if they lost their support.
-- The search follows any vine it removes, so a whole hanging chain is cleaned up.
local function FLD_CleanVines(a_World, a_X, a_Y, a_Z)
local Todo = {{a_X, a_Y, a_Z}}
local Guard = 0
while (#Todo > 0) and (Guard < 512) do
Guard = Guard + 1
local Pos = table.remove(Todo)
for Index = 1, 6 do
local Offset = FLD_NEIGHBOURS[Index]
local X = Pos[1] + Offset[1]
local Y = Pos[2] + Offset[2]
local Z = Pos[3] + Offset[3]
if (Y >= 0) and (Y <= 255) then
local IsValid, BlockType = a_World:GetBlockInfo(Vector3i(X, Y, Z))
if IsValid and (BlockType == E_BLOCK_VINES) and (not FLD_VineSupported(a_World, X, Y, Z)) then
-- Matches the native handler: the vine is destroyed, not dropped.
a_World:SetBlock(Vector3i(X, Y, Z), E_BLOCK_AIR, 0)
Todo[#Todo + 1] = {X, Y, Z}
end
end
end
end
end
--- Drops a list of leaves (tables with X / Y / Z fields).
-- a_Verify re-reads every block first, for leaves that sat in the gradual-decay queue
-- while the world kept changing; a leaf that is already gone or replaced is skipped.
-- Returns the number of blocks actually dropped.
local function FLD_DropLeaves(a_World, a_Leaves, a_Verify)
local DropItems = FLD_Config.DropItems
local CleanVines = FLD_Config.CleanFloatingVines
local Dropped = 0
for Index = 1, #a_Leaves do
local Leaf = a_Leaves[Index]
local Ok = true
if a_Verify then
local IsValid, BlockType = a_World:GetBlockInfo(Vector3i(Leaf.X, Leaf.Y, Leaf.Z))
Ok = IsValid and FLD_IsLeaf(BlockType)
end
if Ok then
-- DropBlockAsPickups() runs the normal block handler, so the vanilla drops
-- (sapling / stick / apple) and HOOK_BLOCK_TO_PICKUPS still apply. It also
-- goes through cChunk::SetBlock, which queues the neighbour updates that make
-- vines, torches and the like re-check their support.
if DropItems then
a_World:DropBlockAsPickups(Vector3i(Leaf.X, Leaf.Y, Leaf.Z))
else
a_World:SetBlock(Vector3i(Leaf.X, Leaf.Y, Leaf.Z), E_BLOCK_AIR, 0)
end
Dropped = Dropped + 1
if CleanVines then
FLD_CleanVines(a_World, Leaf.X, Leaf.Y, Leaf.Z)
end
end
end
return Dropped
end
--- Clears the native "needs a decay check" bit (meta 0x08) on the leaves that survived a
-- pass. Removing their orphaned neighbours makes the block-update queue ask the native
-- handler for a (13 x 13 x 13) decay check on each of them; they are already known to be in
-- reach of a log, so the bit can be dropped instead.
local function FLD_ClearCheckBits(a_World, a_Leaves)
for Index = 1, #a_Leaves do
local Leaf = a_Leaves[Index]
local IsValid, BlockType, Meta = a_World:GetBlockInfo(Vector3i(Leaf.X, Leaf.Y, Leaf.Z))
if IsValid and FLD_IsLeaf(BlockType) and (Meta >= 8) then
a_World:SetBlockMeta(Vector3i(Leaf.X, Leaf.Y, Leaf.Z), Meta - 8)
end
end
end
--- Key used to find a queued leaf again (the queue stores plain coordinates, never block
-- references).
local function FLD_QueueKey(a_X, a_Y, a_Z)
return a_X .. "," .. a_Y .. "," .. a_Z
end
--- Takes the given leaves out of a world's gradual queue. Entries are marked as false
-- rather than deleted so that the FIFO head index stays valid.
-- Returns the number of leaves that were actually still queued.
local function FLD_CancelQueued(a_World, a_Leaves)
local Pending = FLD_Pending[a_World]
if (Pending == nil) then
return 0
end
local Cancelled = 0
for Index = 1, #a_Leaves do
local Leaf = a_Leaves[Index]
local Key = FLD_QueueKey(Leaf.X, Leaf.Y, Leaf.Z)
local Position = Pending.Index[Key]
if (Position ~= nil) then
Pending.Queue[Position] = false
Pending.Index[Key] = nil
Cancelled = Cancelled + 1
end
end
FLD_Stats.LeavesCancelled = FLD_Stats.LeavesCancelled + Cancelled
return Cancelled
end
--- Task run once per DecayIntervalTicks while leaves are queued for gradual decay.
function FLD_ProcessPending(a_World)
local Pending = (FLD_Pending ~= nil) and FLD_Pending[a_World] or nil
if (Pending == nil) or (FLD_Config == nil) then
return
end
local Config = FLD_Config
local Queue = Pending.Queue
-- Take the next batch, oldest first, so the canopy crumbles outwards from the log.
-- Entries marked as false were cancelled by a log placed back in the meantime.
local Batch = {}
local Count = 0
while (Count < Config.LeavesPerBatch) and (Pending.Head <= #Queue) do
local Leaf = Queue[Pending.Head]
Pending.Head = Pending.Head + 1
if (Leaf ~= false) then
Batch[#Batch + 1] = Leaf
Count = Count + 1
end
end
if (#Batch > 0) then
FLD_Stats.LeavesDropped = FLD_Stats.LeavesDropped + FLD_DropLeaves(a_World, Batch, true)
end
if (Pending.Head > #Queue) then
-- Everything is gone; the survivors can finally have their check bit cleared.
if (Config.ClearNativeCheckBit) and (#Pending.Survivors > 0) then
FLD_ClearCheckBits(a_World, Pending.Survivors)
end
Pending.Queue = {}
Pending.Survivors = {}
Pending.Index = {}
Pending.Head = 1
Pending.Scheduled = false
FLD_Pending[a_World] = nil
return
end
a_World:ScheduleTask(Config.DecayIntervalTicks, FLD_ProcessPending)
end
--- Drops every leaves block reachable from a_Seeds that cannot reach a log within
-- FLD_Config.MaxDistance leaves steps.
-- a_CenterX/Y/Z only bounds the search and indexes the visited set; the caller passes the
-- position of the removed log or the explosion center.
-- With a_Cancel the pass does not drop or queue anything: it only cancels the leaves it
-- found to be in reach of a log again from the world's gradual queue.
-- Returns the number of leaves that were dropped (instant mode) or queued (gradual mode)
-- and the number of scanned leaves, or nil plus a reason string when the pass was aborted
-- (the world was left untouched in that case).
function FLD_ProcessLeaves(a_World, a_CenterX, a_CenterY, a_CenterZ, a_Seeds, a_Cancel)
local Config = FLD_Config
local MaxDistance = Config.MaxDistance
local MaxScan = Config.MaxScan
local MaxRadius = Config.MaxRadius
-- Per-call block cache, keyed by absolute coordinates so that every block is read at
-- most once, no matter how often the traversal touches it.
local Cache = {}
local function ReadBlock(a_X, a_Y, a_Z)
local Column = Cache[a_X]
if (Column == nil) then
Column = {}
Cache[a_X] = Column
end
local Row = Column[a_Y]
if (Row == nil) then
Row = {}
Column[a_Y] = Row
end
local Value = Row[a_Z]
if (Value == nil) then
local IsValid, BlockType = a_World:GetBlockInfo(Vector3i(a_X, a_Y, a_Z))
if IsValid then
Value = BlockType
else
Value = FLD_UNKNOWN
end
Row[a_Z] = Value
end
return Value
end
local LeafList = {}
local LeafIndex = {}
local Queue = {}
local QueueHead = 1
local ScanCount = 0
-- Packs coordinates relative to the center into one integer. MaxRadius is capped at
-- 63 at configuration time, so the packed value stays well inside Lua's integer range.
local function KeyOf(a_X, a_Y, a_Z)
return ((a_X - a_CenterX + 64) * 128 + (a_Z - a_CenterZ + 64)) * 256 + a_Y
end
local function InRange(a_X, a_Y, a_Z)
return (a_Y >= 0) and (a_Y <= 255)
and (math.abs(a_X - a_CenterX) <= MaxRadius)
and (math.abs(a_Z - a_CenterZ) <= MaxRadius)
and (math.abs(a_Y - a_CenterY) <= MaxRadius)
end
local function AddLeaf(a_X, a_Y, a_Z)
local Key = KeyOf(a_X, a_Y, a_Z)
local Existing = LeafIndex[Key]
if (Existing ~= nil) then
return Existing
end
ScanCount = ScanCount + 1
if (ScanCount > MaxScan) then
return nil
end
local Index = #LeafList + 1
LeafList[Index] =
{
X = a_X,
Y = a_Y,
Z = a_Z,
NeighbourLeaves = {},
TouchesLog = false,
}
LeafIndex[Key] = Index
Queue[#Queue + 1] = Index
return Index
end
-- Seed the traversal with the leaves the caller already knows about.
for _, Seed in ipairs(a_Seeds) do
if (not InRange(Seed[1], Seed[2], Seed[3])) then
return nil, "leaves spread too far"
end
if (AddLeaf(Seed[1], Seed[2], Seed[3]) == nil) then
return nil, "too many leaves"
end
end
-- Phase 1: walk the connected leaves component and remember, per leaf, which of its
-- neighbours are leaves of this component and whether any neighbour is a log.
while (QueueHead <= #Queue) do
local Leaf = LeafList[Queue[QueueHead]]
QueueHead = QueueHead + 1
for Neighbour = 1, 6 do
local Offset = FLD_NEIGHBOURS[Neighbour]
local X = Leaf.X + Offset[1]
local Y = Leaf.Y + Offset[2]
local Z = Leaf.Z + Offset[3]
if (not InRange(X, Y, Z)) then
return nil, "leaves spread too far"
end
local BlockType = ReadBlock(X, Y, Z)
if (BlockType == FLD_UNKNOWN) then
return nil, "unloaded chunk"
end
if FLD_IsLeaf(BlockType) then
local Index = AddLeaf(X, Y, Z)
if (Index == nil) then
return nil, "too many leaves"
end
Leaf.NeighbourLeaves[Neighbour] = Index
elseif FLD_IsLog(BlockType) then
Leaf.TouchesLog = true
end
end
end
-- Phase 2: multi-source BFS. Leaves next to a log survive with distance 1; the
-- distance is then propagated through the component, which is exactly the search the
-- native handler performs for each individual block.
local Distance = {}
local DistQueue = {}
local DistHead = 1
for Index = 1, #LeafList do
if LeafList[Index].TouchesLog then
Distance[Index] = 1
DistQueue[#DistQueue + 1] = Index
end
end
while (DistHead <= #DistQueue) do
local Index = DistQueue[DistHead]
DistHead = DistHead + 1
local Current = Distance[Index]
if (Current < MaxDistance) then
local Leaf = LeafList[Index]
for Neighbour = 1, 6 do
local Other = Leaf.NeighbourLeaves[Neighbour]
if (Other ~= nil) and (Distance[Other] == nil) then
Distance[Other] = Current + 1
DistQueue[#DistQueue + 1] = Other
end
end
end
end
-- Collect the leaves that are out of reach of any log, and the ones that still reach one.
local Orphans = {}
local Survivors = {}
for Index = 1, #LeafList do
local Leaf = LeafList[Index]
if (Distance[Index] == nil) then
if (not a_Cancel) then
-- A revalidation pass only cares about what survived.
if Config.DecayPlacedLeaves then
Orphans[#Orphans + 1] = Leaf
else
-- Meta bit 0x04 marks player-placed (persistent) leaves.
local Meta = a_World:GetBlockMeta(Vector3i(Leaf.X, Leaf.Y, Leaf.Z))
if ((Meta % 8) < 4) then
Orphans[#Orphans + 1] = Leaf
end
end
end
elseif a_Cancel or Config.ClearNativeCheckBit then
Survivors[#Survivors + 1] = Leaf
end
end
local Scanned = #LeafList
if a_Cancel then
-- A log was placed back (HOOK_PLAYER_PLACED_BLOCK). Everything that is in reach of a
-- log again leaves the gradual queue, so rebuilding inside the decay window stops
-- the process instead of letting a stale snapshot run to completion.
FLD_CancelQueued(a_World, Survivors)
return 0, Scanned
end
if (#Orphans == 0) then
return 0, Scanned
end
if Config.Gradual then
local Pending = FLD_Pending[a_World]
if (Pending == nil) then
Pending = {Queue = {}, Head = 1, Survivors = {}, Index = {}, Scheduled = false}
FLD_Pending[a_World] = Pending
end
for _, Leaf in ipairs(Orphans) do
Pending.Queue[#Pending.Queue + 1] = {X = Leaf.X, Y = Leaf.Y, Z = Leaf.Z}
Pending.Index[FLD_QueueKey(Leaf.X, Leaf.Y, Leaf.Z)] = #Pending.Queue
end
for _, Leaf in ipairs(Survivors) do
Pending.Survivors[#Pending.Survivors + 1] = Leaf
end
if (not Pending.Scheduled) then
Pending.Scheduled = true
a_World:ScheduleTask(Config.DecayIntervalTicks, FLD_ProcessPending)
end
else
FLD_Stats.LeavesDropped = FLD_Stats.LeavesDropped + FLD_DropLeaves(a_World, Orphans, false)
if (#Survivors > 0) then
FLD_ClearCheckBits(a_World, Survivors)
end
end
return #Orphans, Scanned
end
--- Collects the leaves adjacent to a block; they seed a decay or revalidation pass.
-- Returns the seed list, or nil plus a reason when a neighbouring chunk is not loaded.
local function FLD_CollectLeafSeeds(a_World, a_X, a_Y, a_Z)
local Seeds = {}
for Neighbour = 1, 6 do
local Offset = FLD_NEIGHBOURS[Neighbour]
local X = a_X + Offset[1]
local Y = a_Y + Offset[2]
local Z = a_Z + Offset[3]
if (Y >= 0) and (Y <= 255) then
local IsValid, BlockType = a_World:GetBlockInfo(Vector3i(X, Y, Z))
if (not IsValid) then
return nil, "unloaded chunk"
end
if FLD_IsLeaf(BlockType) then
Seeds[#Seeds + 1] = {X, Y, Z}
end
end
end
return Seeds
end
--- Handles a log that has just been removed at the given position. Only leaves that are
-- adjacent to the removed log can be orphaned by it, so those are used as the seeds.
function FLD_ProcessRemovedLog(a_World, a_X, a_Y, a_Z)
if (a_World == nil) or (FLD_Config == nil) or (not FLD_Config.Enabled) then
return nil, "disabled"
end
if FLD_Busy[a_World] then
return nil, "already processing this world"
end
local Seeds, Reason = FLD_CollectLeafSeeds(a_World, a_X, a_Y, a_Z)
if (Seeds == nil) then
return nil, Reason
end
if (#Seeds == 0) then
return 0, 0
end
FLD_Busy[a_World] = true
local Dropped, Scanned = FLD_ProcessLeaves(a_World, a_X, a_Y, a_Z, Seeds)
FLD_Busy[a_World] = nil
return Dropped, Scanned
end
--- Handles a log that has just been placed back at the given position. Leaves that are
-- waiting in the gradual queue and are in reach of a log again are cancelled, so a log
-- placed inside the decay window stops the process.
function FLD_ProcessPlacedLog(a_World, a_X, a_Y, a_Z)
if (a_World == nil) or (FLD_Config == nil) or (not FLD_Config.Enabled) then
return 0
end
if (FLD_Pending[a_World] == nil) then
return 0 -- nothing queued for this world
end
if FLD_Busy[a_World] then
return 0
end
local Seeds, Reason = FLD_CollectLeafSeeds(a_World, a_X, a_Y, a_Z)
if (Seeds == nil) then
if FLD_Config.Debug then
LOG("[FastLeafDecay] cannot revalidate the placed log: " .. tostring(Reason))
end
return 0
end
if (#Seeds == 0) then
return 0
end
FLD_Busy[a_World] = true
FLD_ProcessLeaves(a_World, a_X, a_Y, a_Z, Seeds, true)
FLD_Busy[a_World] = nil
return 1
end
--- Handles an explosion that may have removed logs, by seeding the decay pass with the
-- leaves found in a cube around the explosion center.
function FLD_ProcessExplosion(a_World, a_X, a_Y, a_Z, a_Size)
if (a_World == nil) or (FLD_Config == nil) or (not FLD_Config.Enabled) then
return nil, "disabled"
end
local Radius = math.floor(a_Size + FLD_Config.ExplosionRadiusBoost)
if (Radius > FLD_Config.ExplosionMaxRadius) then
Radius = FLD_Config.ExplosionMaxRadius
end
if (Radius < 1) then
Radius = 1
end
local Seeds = {}
local MinY = math.max(a_Y - Radius, 0)
local MaxY = math.min(a_Y + Radius, 255)
for Y = MinY, MaxY do
for Z = a_Z - Radius, a_Z + Radius do
for X = a_X - Radius, a_X + Radius do
local IsValid, BlockType = a_World:GetBlockInfo(Vector3i(X, Y, Z))
if (not IsValid) then
return nil, "unloaded chunk"
end
if FLD_IsLeaf(BlockType) then
Seeds[#Seeds + 1] = {X, Y, Z}
end
end
end
end
if (#Seeds == 0) then
return 0, 0
end
if FLD_Busy[a_World] then
return nil, "already processing this world"
end
FLD_Busy[a_World] = true
local Dropped, Scanned = FLD_ProcessLeaves(a_World, a_X, a_Y, a_Z, Seeds)
FLD_Busy[a_World] = nil
return Dropped, Scanned
end