-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbees.lua
More file actions
1086 lines (951 loc) · 34.9 KB
/
bees.lua
File metadata and controls
1086 lines (951 loc) · 34.9 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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
X Farming. Extends Luanti farming mod with new plants, crops and ice fishing.
Copyright (C) 2025 SaKeL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to juraj.vajda@gmail.com
--]]
local S = core.get_translator(core.get_current_modname())
local rand = PcgRandom(tonumber(tostring(os.time()):reverse():sub(1, 9)))
local function update_hive_infotext(pos)
local meta = core.get_meta(pos)
local data = core.deserialize(meta:get_string('x_farming'))
local tod = core.get_timeofday()
local is_day = false
if tod > 0.2 and tod < 0.78 then
is_day = true
end
if data then
local text = ""
if not is_day and data.occupancy > 0 then
text = "The Bees are sleeping." .. "\n"
elseif data.saturation >= 15 and data.occupancy > 0 then
text = "The Bees are teeming." .. "\n"
elseif data.saturation >= 7 and data.occupancy > 0 then
text = "The Bees are happy." .. "\n"
elseif data.occupancy > 3 then
text = "The Bees are cramped." .. "\n"
elseif data.occupancy >= 2 then
text = "The Bees are working." .. "\n"
elseif data.occupancy == 0 and data.recent_out > 0 then
text = "The Bees are out." .. "\n"
end
text = text .. 'Occupancy: ' .. data.occupancy .. ' / '.. 3 ..'\n'
.. 'Saturation: ' .. data.saturation .. ' / 16'
meta:set_string('infotext', text)
end
end
local function bee_particles(pos)
core.add_particlespawner({
amount = 20,
time = 0.1,
minpos = { x = pos.x - 0.25, y = pos.y, z = pos.z - 0.25 },
maxpos = { x = pos.x + 0.25, y = pos.y, z = pos.z + 0.25 },
minvel = { x = -0.5, y = -2, z = -0.5 },
maxvel = { x = 0.5, y = -2, z = 0.5 },
minacc = { x = -0.5, y = -2, z = -0.5 },
maxacc = { x = 0.5, y = -2, z = 0.5 },
minexptime = 0.5,
maxexptime = 1,
texture = 'x_farming_default_particle.png^[colorize:#FFD69C:255',
collisiondetection = true,
glow = 3
})
end
local function honey_particle(pos, params)
local move_up = true
local time = 0.1
local amount = 15
local minsize = 0.5
local maxsize = 1
local minexptime = 0.5
local maxexptime = 1.5
local glow = 8
if params then
if params.move_up ~= nil then
move_up = params.move_up
end
if params.time then
time = params.time
end
if params.amount then
amount = params.amount
end
if params.minsize then
minsize = params.minsize
end
if params.maxsize then
maxsize = params.maxsize
end
if params.minexptime then
minexptime = params.minexptime
end
if params.minexptime then
minexptime = params.minexptime
end
if params.glow ~= nil then
glow = params.glow
end
end
local y_vel = 1
if not move_up then
y_vel = -1
end
core.add_particlespawner({
amount = amount,
time = time,
minpos = { x = pos.x - 0.1, y = pos.y - 0.20, z = pos.z - 0.1 },
maxpos = { x = pos.x + 0.1, y = pos.y - 0.25, z = pos.z + 0.1 },
minvel = { x = -0.25, y = -0, z = -0.25 },
maxvel = { x = 0.25, y = -0, z = 0.25 },
minacc = { x = -0.45, y = y_vel, z = -0.45 },
maxacc = { x = 0.45, y = y_vel * 2, z = 0.45 },
minsize = minsize,
maxsize = maxsize,
minexptime = minexptime,
maxexptime = maxexptime,
texture = 'x_farming_default_particle.png^[colorize:#ffc137:160',
collisiondetection = true,
glow = glow
})
end
local function update_bee_infotext(pos)
local meta = core.get_meta(pos)
local data = core.deserialize(meta:get_string('x_farming'))
if data then
meta:set_string('infotext', 'Hive position: ' .. data.pos_hive)
end
end
-- how often node timers for plants will tick, +/- some random value
local function tick_hive(pos, params)
local min = 15
local max = 25
if params and params.min then
min = params.min
end
if params and params.max then
max = params.max
end
core.get_node_timer(pos):start(math.random(min, max))
end
-- how often a growth failure tick is retried (e.g. too dark)
local function tick_bee(pos)
local light_level = core.get_node_light(pos)
if light_level > 14 then
core.get_node_timer(pos):start(math.random(90, 150))
else
core.get_node_timer(pos):start(math.random(40, 90))
end
end
local function is_valid_hive_position(pos, params)
if not pos then
return false
end
local _params = params or {}
local ommit_node_group_check = _params.ommit_node_group_check or false
local ommit_hive_bee_check = _params.ommit_hive_bee_check or false
local hive_node = core.get_node(pos)
if not hive_node then
return false
end
if not ommit_node_group_check then
if core.get_item_group(hive_node.name, 'bee_hive') == 0 then
return false
end
end
local meta_hive = core.get_meta(pos)
local data_hive = core.deserialize(meta_hive:get_string('x_farming'))
if not data_hive then
return false
end
if not ommit_hive_bee_check then
if not data_hive.occupancy then
return false
end
if data_hive.occupancy >= 3 then
return false
end
end
return true
end
local shuffle = function(tbl)
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
return tbl
end
local function get_valid_hive_position(pos_hive, pos_bee)
local valid_pos = nil
if pos_hive then
valid_pos = vector.copy(pos_hive)
end
if is_valid_hive_position(valid_pos, { ommit_hive_bee_check = true }) then
return valid_pos
end
valid_pos = nil
-- Find neighboring bee hive position
local hive_positions = core.find_nodes_in_area(
vector.add(pos_bee, x_farming.beehive_distance + 3),
vector.subtract(pos_bee, x_farming.beehive_distance + 3),
{ 'group:bee_hive' }
)
local random_hives = shuffle(hive_positions)
for _, p in ipairs(random_hives) do
if is_valid_hive_position(p, { ommit_node_group_check = true }) then
valid_pos = p
break
end
end
return valid_pos
end
-- Hive give item
local hive_give_item = function(player, pos, stack)
core.after(0, function()
local inv = player:get_inventory()
if inv:room_for_item("main", stack) then
inv:add_item("main", stack)
else
core.add_item(pos, stack)
end
end)
end
-- Hive switcher
local hive_node_swap = function(pos, saturation, param2)
if saturation >= 15 then
core.swap_node(pos, { name = 'x_farming:bee_hive_saturated_2', param2 = param2 })
elseif saturation >= 10 then
core.swap_node(pos, { name = 'x_farming:bee_hive_saturated_1', param2 = param2 })
elseif saturation >= 5 then
core.swap_node(pos, { name = 'x_farming:bee_hive_saturated', param2 = param2 })
else
core.swap_node(pos, { name = 'x_farming:bee_hive', param2 = param2 })
end
end
-- Flower cooldown
local can_bee_use_flower = function(pos)
local flower_meta = core.get_meta(pos)
local has_timer = flower_meta:get_int("last_bee_time") > 0
local ttime = os.time() - (flower_meta:get_int("last_bee_time") or 0)
flower_meta:set_int("last_bee_tick", ttime)
if has_timer == false or ttime > 0 then
return true
end
return false
end
local bee_tick_flower = function(pos)
local cooldown = math.random(250, 600)
local flower_meta = core.get_meta(pos)
if can_bee_use_flower(pos) then
flower_meta:set_int("last_bee_time", os.time() + cooldown)
flower_meta:set_int("last_bee_tick", 0)
end
end
-- Hive timer
local hive_on_timer = function(pos, elapsed)
-- Hive data
local meta_hive = core.get_meta(pos)
local data_hive = core.deserialize(meta_hive:get_string('x_farming'))
local node = core.get_node(pos)
if not data_hive then
return
end
if data_hive.occupancy == 0 then
return
end
local _flower_positions = core.find_nodes_in_area(
vector.add(pos, x_farming.beehive_distance),
vector.subtract(pos, x_farming.beehive_distance),
{ 'group:flower', 'group:bees_pollinate_crop' }
)
local flower_positions = {}
for _, flwr in ipairs(_flower_positions) do
local light_level = core.get_node_light(flwr)
local above_pos = vector.new(flwr.x, flwr.y + 1, flwr.z)
local below_pos = vector.new(flwr.x, flwr.y - 1, flwr.z)
local node_above = core.get_node(above_pos)
local nname_above = core.get_node(above_pos).name
local nname_below = core.get_node(below_pos).name
local flower_open = true
if core.get_item_group(nname_below, 'group:flower') > 0 then
if not can_bee_use_flower(flwr) then
flower_open = false
end
end
if (flower_open and node_above.name == "air" or node_above.name == "technic:dummy_light_source" or
core.get_item_group(nname_above, 'atmosphere') > 1) and light_level > 11 and
core.get_item_group(nname_below, 'soil') > 0 then
table.insert(flower_positions, flwr)
end
end
if not flower_positions then
tick_hive(pos)
end
if flower_positions and #flower_positions > 0 then
local random_pos = flower_positions[rand:next(1, #flower_positions)]
local pos_bee = vector.new(random_pos.x, random_pos.y + 1, random_pos.z)
local tod = core.get_timeofday()
local is_day = false
if tod > 0.2 and tod < 0.760 then
is_day = true
end
if data_hive and data_hive.occupancy > 0 and is_day then
local pos_hive_front = vector.subtract(vector.new(pos.x, pos.y + 0.5, pos.z), core.facedir_to_dir(node.param2))
if not data_hive.recent_out then
data_hive.recent_out = 0
end
-- Send bee out
data_hive.occupancy = data_hive.occupancy - 1
data_hive.recent_out = data_hive.recent_out + 1
core.swap_node(pos_bee, { name = 'x_farming:bee', param2 = rand:next(0, 3) })
tick_bee(pos_bee)
core.sound_play('x_farming_bee', {
pos = pos_bee,
})
bee_particles(pos_bee)
bee_particles(pos_hive_front)
-- Bee data
local meta_bee = core.get_meta(pos_bee)
local data_bee = {
pos_hive = vector.new(pos):to_string()
}
-- Tick flower cooldown
bee_tick_flower(random_pos)
meta_bee:set_string('x_farming', core.serialize(data_bee))
meta_hive:set_string('x_farming', core.serialize(data_hive))
update_hive_infotext(pos)
update_bee_infotext(pos_bee)
else
update_hive_infotext(pos)
end
end
if data_hive and data_hive.occupancy > 0 then
tick_hive(pos)
update_hive_infotext(pos)
end
end
-- Saturated Hive timer
local hive_saturated_on_timer = function(pos, elapsed)
-- Hive data
local meta_hive = core.get_meta(pos)
local data_hive = core.deserialize(meta_hive:get_string('x_farming'))
if data_hive.occupancy == 0 then
return
end
update_hive_infotext(pos)
tick_hive(pos, {min = 20, max = 45})
end
-- Hive interact
local hive_on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
local stack_name = itemstack:get_name()
local stack = itemstack
local meta = core.get_meta(pos)
local data = core.deserialize(meta:get_string('x_farming'))
if not data then
return itemstack
end
if (stack_name == 'vessels:glass_bottle' or stack_name == 'x_farming:glass_bottle') and data.saturation >= 5 then
-- Fill bottle with honey and return it
itemstack:take_item()
local pos_hive_front = vector.subtract(vector.new(pos.x, pos.y + 0.5, pos.z), core.facedir_to_dir(node.param2))
hive_give_item(clicker, pos_hive_front, ItemStack({ name = 'x_farming:bottle_honey' }))
hive_node_swap(pos, data.saturation - 5, node.param2)
core.sound_play('x_farming_bee', {
pos = pos,
})
bee_particles(pos_hive_front)
data.saturation = data.saturation - 5
if data.saturation < 0 then
data.saturation = 0
end
meta:set_string('x_farming', core.serialize(data))
update_hive_infotext(pos)
tick_hive(pos)
elseif stack_name == 'x_farming:honeycomb_saw' and data.saturation >= 5 then
-- Add use to the tool and drop honeycomb
itemstack:add_wear(65535 / 50)
local pos_hive_front = vector.subtract(vector.new(pos.x, pos.y + 0.5, pos.z), core.facedir_to_dir(node.param2))
hive_give_item(clicker, pos_hive_front, ItemStack({ name = 'x_farming:honeycomb' }))
hive_node_swap(pos, data.saturation - 5, node.param2)
core.sound_play('x_farming_bee', {
pos = pos,
})
bee_particles(pos_hive_front)
data.saturation = data.saturation - 5
if data.saturation < 0 then
data.saturation = 0
end
meta:set_string('x_farming', core.serialize(data))
update_hive_infotext(pos)
tick_hive(pos)
end
if data.occupancy >= 3 then
return itemstack
end
if data.recent_out then
data.recent_out = 0
end
if core.get_item_group(itemstack:get_name(), 'bee') > 0 then
data.occupancy = data.occupancy + 1
meta:set_string('x_farming', core.serialize(data))
update_hive_infotext(pos)
itemstack:take_item()
local pos_hive_front = vector.subtract(vector.new(pos.x, pos.y + 0.5, pos.z), core.facedir_to_dir(node.param2))
hive_give_item(clicker, pos_hive_front, ItemStack({ name = 'x_farming:jar_empty' }))
core.sound_play('x_farming_bee', {
pos = pos,
})
end
if data.occupancy > 0 and not core.get_node_timer(pos):is_started() then
tick_hive(pos)
end
return stack
end
-- Preserve Metadata
local hive_preserve_metadata = function(pos, oldnode, oldmeta, drops)
local meta = core.get_meta(pos)
local inv = meta:get_inventory()
local stack = drops[1]
local meta_drop = stack:get_meta()
local data = core.deserialize(meta:get_string('x_farming'))
if data and data.saturation > 0 then
data.saturation = data.saturation - 6
if data.saturation < 0 then
data.saturation = 0
end
meta:set_string('x_farming', core.serialize(data))
end
if data and data.occupancy > 3 then
data.occupancy = 3
meta:set_string('x_farming', core.serialize(data))
end
local data_hive = core.deserialize(meta:get_string('x_farming'))
if data_hive and (data_hive.occupancy > 0 or data_hive.saturation > 0) then
meta_drop:set_string('x_farming', core.serialize(meta:get_string("x_farming")))
meta_drop:set_string('description', stack:get_description() .. '\n'
.. S('Occupancy') .. ': ' .. data_hive.occupancy .. '\n'
.. S('Saturation') .. ': ' .. data_hive.saturation)
end
end
-- Hive dig node
local hive_after_dig_node = function(pos, oldnode, oldmetadata, digger)
local data = core.deserialize(oldmetadata.fields.x_farming)
local positions = core.find_nodes_in_area_under_air(
vector.add(pos, x_farming.beehive_distance),
vector.subtract(pos, x_farming.beehive_distance),
{ 'group:flower', 'group:flora', 'group:bees_pollinate_crop' }
)
if positions and #positions > 0 and data and data.occupancy and data.occupancy > 3 then
for i = 1, data.occupancy - 3 do
local p = positions[i]
if p then
local pos_bee = vector.new(p.x, p.y + 1, p.z)
core.swap_node(pos_bee, { name = 'x_farming:bee', param2 = rand:next(0, 3) })
tick_bee(pos_bee)
bee_particles(pos_bee)
if i == 1 then
core.sound_play('x_farming_bee', {
pos = pos,
})
end
end
end
end
end
-- Hive
core.register_node('x_farming:bee_hive', {
description = S('Bee Hive'),
short_description = S('Bee Hive'),
_tt_help = S("Houses Bees to produce Honey"),
tiles = {
'x_farming_bee_hive_top.png',
'x_farming_bee_hive_bottom.png',
'x_farming_bee_hive_side.png',
'x_farming_bee_hive_side.png',
'x_farming_bee_hive_side.png',
'x_farming_bee_hive_front.png',
},
paramtype2 = 'facedir',
groups = {
-- MTG
choppy = 2,
oddly_breakable_by_hand = 1,
bee_hive = 1,
no_silktouch = 1,
-- MCL
handy = 1,
axey = 1,
building_block = 1,
material_wood = 1,
fire_encouragement = 5,
fire_flammability = 5,
-- ALL
tree = 1,
flammable = 2,
},
_mcl_blast_resistance = 2,
_mcl_hardness = 2,
sounds = x_farming.node_sound_wood_defaults(),
on_timer = hive_on_timer,
on_construct = function(pos)
local meta = core.get_meta(pos)
local data = {
occupancy = 0,
saturation = 0,
recent_out = 0
}
meta:set_string('x_farming', core.serialize(data))
update_hive_infotext(pos)
end,
after_place_node = function(pos, placer, itemstack, pointed_thing)
local node = core.get_node(pos)
local meta = core.get_meta(pos)
local meta_st = itemstack:get_meta()
local hive_meta = core.deserialize(meta_st:get_string('x_farming'))
if hive_meta then
meta:set_string("x_farming", hive_meta)
local meta_hive = core.get_meta(pos)
local data_hive = core.deserialize(meta_hive:get_string('x_farming'))
hive_node_swap(pos, data_hive.saturation or 0, node.param2)
update_hive_infotext(pos)
itemstack:take_item()
core.sound_play('x_farming_bee', {
pos = pos,
})
end
tick_hive(pos)
end,
on_rightclick = hive_on_rightclick,
after_dig_node = hive_after_dig_node,
preserve_metadata = hive_preserve_metadata,
})
-- Hive saturrated
core.register_node('x_farming:bee_hive_saturated', {
description = S('Bee Hive'),
short_description = S('Bee Hive'),
tiles = {
'x_farming_bee_hive_top.png',
'x_farming_bee_hive_bottom.png',
'x_farming_bee_hive_side.png^x_farming_bee_hive_saturated_overlay_1.png',
'x_farming_bee_hive_side.png^x_farming_bee_hive_saturated_overlay_1.png',
'x_farming_bee_hive_side.png^x_farming_bee_hive_saturated_overlay_1.png',
'x_farming_bee_hive_front.png^x_farming_bee_hive_saturated_overlay_1.png',
},
paramtype2 = 'facedir',
drop = 'x_farming:bee_hive',
groups = {
-- MTG
choppy = 2,
oddly_breakable_by_hand = 1,
bee_hive = 1,
no_silktouch = 1,
not_in_creative_inventory = 1,
-- MCL
handy = 1,
axey = 1,
building_block = 1,
material_wood = 1,
fire_encouragement = 5,
fire_flammability = 5,
-- ALL
tree = 1,
flammable = 2,
},
_mcl_blast_resistance = 2,
_mcl_hardness = 2,
sounds = x_farming.node_sound_wood_defaults(),
on_rightclick = hive_on_rightclick,
after_dig_node = hive_after_dig_node,
on_timer = hive_on_timer,
preserve_metadata = hive_preserve_metadata,
})
-- Hive saturrated 1
core.register_node('x_farming:bee_hive_saturated_1', {
description = S('Bee Hive'),
short_description = S('Bee Hive'),
tiles = {
'x_farming_bee_hive_top.png',
'x_farming_bee_hive_bottom.png',
'x_farming_bee_hive_side.png^x_farming_bee_hive_saturated_overlay_2.png',
'x_farming_bee_hive_side.png^x_farming_bee_hive_saturated_overlay_2.png',
'x_farming_bee_hive_side.png^x_farming_bee_hive_saturated_overlay_2.png',
'x_farming_bee_hive_front.png^x_farming_bee_hive_saturated_overlay_2.png',
},
paramtype2 = 'facedir',
drop = 'x_farming:bee_hive',
groups = {
-- MTG
choppy = 2,
oddly_breakable_by_hand = 1,
bee_hive = 1,
no_silktouch = 1,
not_in_creative_inventory = 1,
-- MCL
handy = 1,
axey = 1,
building_block = 1,
material_wood = 1,
fire_encouragement = 5,
fire_flammability = 5,
-- ALL
tree = 1,
flammable = 2,
},
_mcl_blast_resistance = 2,
_mcl_hardness = 2,
sounds = x_farming.node_sound_wood_defaults(),
on_rightclick = hive_on_rightclick,
after_dig_node = hive_after_dig_node,
on_timer = hive_on_timer,
preserve_metadata = hive_preserve_metadata,
})
-- Hive saturrated 2
core.register_node('x_farming:bee_hive_saturated_2', {
description = S('Bee Hive'),
short_description = S('Bee Hive'),
tiles = {
'x_farming_bee_hive_top.png',
'x_farming_bee_hive_bottom.png',
'x_farming_bee_hive_side.png^x_farming_bee_hive_saturated_overlay_3.png',
'x_farming_bee_hive_side.png^x_farming_bee_hive_saturated_overlay_3.png',
'x_farming_bee_hive_side.png^x_farming_bee_hive_saturated_overlay_3.png',
'x_farming_bee_hive_front.png^x_farming_bee_hive_saturated_overlay_3.png',
},
paramtype2 = 'facedir',
drop = 'x_farming:bee_hive',
groups = {
-- MTG
choppy = 2,
oddly_breakable_by_hand = 1,
bee_hive = 1,
no_silktouch = 1,
not_in_creative_inventory = 1,
-- MCL
handy = 1,
axey = 1,
building_block = 1,
material_wood = 1,
fire_encouragement = 5,
fire_flammability = 5,
-- ALL
tree = 1,
flammable = 2,
},
_mcl_blast_resistance = 2,
_mcl_hardness = 2,
sounds = x_farming.node_sound_wood_defaults(),
on_rightclick = hive_on_rightclick,
after_dig_node = hive_after_dig_node,
on_timer = hive_saturated_on_timer,
preserve_metadata = hive_preserve_metadata,
})
-- Bee
core.register_node('x_farming:bee', {
description = S('Bee'),
short_description = S('Bee'),
drawtype = 'mesh',
mesh = 'x_farming_bee.obj',
tiles = {
{
name = 'x_farming_bee_mesh_animated.png',
animation = {
type = 'vertical_frames',
aspect_w = 34,
aspect_h = 30,
length = 0.3
},
backface_culling = false
},
},
use_texture_alpha = 'clip',
paramtype = 'light',
sunlight_propagates = true,
paramtype2 = 'facedir',
walkable = false,
selection_box = {
type = 'fixed',
fixed = { -4 / 16, -4 / 16, -4 / 16, 4 / 16, 4 / 16, 4 / 16 }
},
groups = {
-- MTG
snappy = 2,
cracky = 2,
crumbly = 2,
choppy = 2,
fleshy = 10,
oddly_breakable_by_hand = 1,
disable_jump = 1,
bee = 1,
no_silktouch = 1,
not_in_creative_inventory = 1,
-- MCL
handy = 1,
},
damage_per_second = 2,
move_resistance = 7,
_mcl_blast_resistance = 1,
_mcl_hardness = 1,
sounds = x_farming.node_sound_wood_defaults(),
drop = {
max_items = 1,
items = {
{
-- 1/20 chance
items = { 'x_farming:honeycomb' },
rarity = 10,
}
}
},
on_timer = function(pos, elapsed)
-- Bee data
local meta_bee = core.get_meta(pos)
local data_bee = core.deserialize(meta_bee:get_string('x_farming')) or {}
local pos_hive = get_valid_hive_position(data_bee.pos_hive and vector.from_string(data_bee.pos_hive) or nil, pos)
if not pos_hive then
-- Bee data not found, remove bee
core.remove_node(pos)
core.sound_play('x_farming_bee', {
pos = pos,
})
bee_particles(pos)
honey_particle(pos, { amount = math.random(7, 12), minsize = 0.2, maxsize = 0.4 })
if data_bee and data_bee.pos_hive then
core.log("action", "[x_farming] Bee failed to locate Hive and was removed")
end
return
end
local meta_hive = core.get_meta(pos_hive)
local data_hive = core.deserialize(meta_hive:get_string('x_farming'))
local node_hive = core.get_node(pos_hive)
if data_hive == nil then
return
end
if (data_hive.saturation >= 15 and math.random(0, 3) >= 1) or (data_hive.occupancy >= 3) then
local pos_old_hive = vector.copy(pos_hive)
if pos_old_hive then
local pos_old_hive_front = vector.subtract(vector.new(pos_old_hive.x, pos_old_hive.y + 0.5, pos_old_hive.z), core.facedir_to_dir(node_hive.param2))
honey_particle(pos_old_hive_front, { amount = math.random(1, 5), minsize = 0.1, maxsize = 0.4, move_up = false})
end
-- get new hive position
pos_hive = get_valid_hive_position(nil, pos)
if pos_hive then
meta_hive = core.get_meta(pos_hive)
data_hive = core.deserialize(meta_hive:get_string('x_farming'))
node_hive = core.get_node(pos_hive)
if (pos_hive == pos_old_hive) then
core.log("action", "[x_farming] Bee located same Hive")
else
core.log("action", "[x_farming] Bee located new empty Hive")
end
elseif data_hive.occupancy > 5 then
core.remove_node(pos)
core.sound_play('x_farming_bee', {
pos = pos,
})
bee_particles(pos)
core.log("action", "[x_farming] Bee failed to locate Hive and was removed")
return
else
pos_hive = pos_old_hive;
core.log("action", "[x_farming] Bee failed to locate new Hive")
end
end
-- Bee go home
data_hive.occupancy = data_hive.occupancy + 1
data_hive.recent_out = 0
local flower_node = core.get_node(vector.new(pos.x, pos.y - 1, pos.z))
local flower_pollinated = false
if flower_node then
if core.get_item_group(flower_node.name, 'flower') > 0 and data_hive.saturation < 16 then
if math.random(0, 100) <= 50 then
data_hive.saturation = data_hive.saturation + 1
flower_pollinated = true
end
elseif core.get_item_group(flower_node.name, 'farmable') > 0 then
if data_hive.saturation < 16 then
if math.random(0, 100) <= 67 then
data_hive.saturation = data_hive.saturation + 1
flower_pollinated = true
end
end
local function grow_plants(p)
for x = -1, 1 do
for z = -1, 1 do
if math.random(100) < 33 then
local pp = vector.new(p.x + x, p.y, p.z + z)
local plant_node = core.get_node(pp)
if core.get_item_group(plant_node.name, 'farmable') > 0 then
x_farming.grow_plant(pp)
x_farming.x_bonemeal.particle_effect(pp)
end
end
end
end
end
local crop_pos = {x = pos.x, y = pos.y - 1, z = pos.z}
x_farming.grow_plant(crop_pos)
x_farming.x_bonemeal.particle_effect(crop_pos)
if flower_pollinated then
core.after(1, function()
grow_plants(crop_pos)
end)
core.after(2, function()
grow_plants(crop_pos)
end)
end
end
if data_hive.saturation >= 5 then
hive_node_swap(pos_hive, data_hive.saturation, node_hive.param2)
end
end
meta_hive:set_string('x_farming', core.serialize(data_hive))
core.remove_node(pos)
update_hive_infotext(pos_hive)
core.sound_play('x_farming_bee', {
pos = pos,
})
local pos_hive_front = vector.subtract(vector.new(pos_hive.x, pos_hive.y + 0.5, pos_hive.z), core.facedir_to_dir(node_hive.param2))
bee_particles(pos)
bee_particles(pos_hive_front)
if flower_pollinated then
honey_particle(pos_hive_front)
end
if not core.get_node_timer(pos_hive):is_started() then
tick_hive(pos_hive)
end
end,
on_punch = function(pos, node, puncher, pointed_thing)
if not puncher then
return
end
-- Hurt player when punching bee
local armor_groups = puncher:get_armor_groups()
local damage = 2
if armor_groups.fleshy then
damage = math.round((2 * 100) / armor_groups.fleshy)
end
puncher:punch(puncher, 1, {
full_punch_interval = 1,
damage_groups = { fleshy = damage },
}, nil)
core.sound_play('x_farming_bee', {
pos = pos,
})
bee_particles(pos)
end,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
local stack_name = itemstack:get_name()
if core.is_protected(pos, clicker:get_player_name()) then
-- Hurt player when unable to capture bee
local armor_groups = clicker:get_armor_groups()
local damage = 3
if armor_groups.fleshy then
damage = math.round((2 * 100) / armor_groups.fleshy)
end
clicker:punch(clicker, 1, {
full_punch_interval = 1,
damage_groups = { fleshy = damage },
}, nil)
core.sound_play('x_farming_bee', {
pos = pos,
})
return itemstack
end
if stack_name == 'x_farming:jar_empty' then
itemstack:take_item()
core.remove_node(pos)
core.add_item(vector.new(pos.x, pos.y + 0.3, pos.z), ItemStack({ name = 'x_farming:jar_with_bee' }))
core.sound_play('x_farming_bee', {
pos = pos,
})
bee_particles(pos)
end
return itemstack
end
})
-- Spawn bees on flowers
-- local bee_spawn_timer = 0
core.register_globalstep(function(dtime)
-- bee_spawn_timer = bee_spawn_timer + dtime