-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstove.lua
More file actions
729 lines (629 loc) · 24.6 KB
/
Copy pathstove.lua
File metadata and controls
729 lines (629 loc) · 24.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
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
--[[
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 stove_fire_sounds = {}
local function get_grid_matrix_items(grid)
local grid_matrix = table.copy(grid)
local _items = {}
for row, items in ipairs(grid_matrix) do
for item_pos, item in pairs(items) do
if item.itemstring ~= '' then
-- add entity position to item table
item.ent_pos = item_pos
table.insert(_items, item)
end
end
end
return _items
end
local function add_item_to_grid_matrix(grid_matrix, item)
local result = {}
for row, items in ipairs(grid_matrix) do
local found = false
for item_pos, _item in pairs(items) do
if _item.itemstring == '' then
grid_matrix[row][item_pos] = item
result.added_to_row = row
result.added_to_pos = item_pos
found = true
break
end
end
if found then
break
end
end
return result
end
local function stop_stove_sound(pos, fadeout_step)
local hash = core.hash_node_position(pos)
local sound_ids = stove_fire_sounds[hash]
if sound_ids then
for _, sound_id in ipairs(sound_ids) do
core.sound_fade(sound_id, -1, 0)
end
stove_fire_sounds[hash] = nil
end
end
local function remove_item_from_grid_matrix(grid_matrix, item)
local result = {}
for row, items in ipairs(grid_matrix) do
local found = false
for item_pos, _item in pairs(items) do
if _item.itemstring == item.itemstring then
grid_matrix[row][item_pos] = {
itemstring = '',
output = {}
}
result.removed_from_row = row
result.removed_from_pos = item_pos
found = true
break
end
end
if found then
break
end
end
return result
end
local function add_item_smoke_particles(pos)
local particlespawner_def = {
amount = 5,
time = 1,
minpos = vector.new(pos.x - 0.1, pos.y, pos.z - 0.1),
maxpos = vector.new(pos.x + 0.1, pos.y, pos.z + 0.1),
minvel = vector.new(-0.1, 0.2, -0.1),
maxvel = vector.new(0.1, 0.4, 0.1),
minacc = vector.new(0, 0.1, 0),
maxacc = vector.new(0, 0.2, 0),
minexptime = 1,
maxexptime = 3,
minsize = 1,
maxsize = 1.5,
texture = 'x_farming_stove_item_smoke_particle.png',
collisiondetection = true
}
if core.has_feature({ dynamic_add_media_table = true, particlespawner_tweenable = true }) then
-- new syntax, after v5.6.0
particlespawner_def = {
amount = 5,
time = 1,
pos = {
min = vector.new(pos.x - 0.1, pos.y, pos.z - 0.1),
max = vector.new(pos.x + 0.1, pos.y, pos.z + 0.1)
},
size = {
min = 1,
max = 1.5,
},
vel = {
min = vector.new(-0.1, 0.2, -0.1),
max = vector.new(0.1, 0.4, 0.1)
},
acc = {
min = vector.new(0, 0.1, 0),
max = vector.new(0, 0.2, 0)
},
exptime = {
min = 1,
max = 3
},
texture = {
name = 'x_farming_stove_item_smoke_particle.png',
alpha_tween = {
1, 0.5,
style = 'fwd',
reps = 1
},
scale_tween = {
{ x = 1, y = 1 },
{ x = 0.5, y = 0.5 },
}
},
collisiondetection = true
}
end
core.add_particlespawner(particlespawner_def)
end
-- Entity
core.register_entity('x_farming:stove_food', {
initial_properties = {
visual = 'wielditem',
visual_size = { x = 0.2, y = 0.2, z = 0.2 },
physical = false,
collide_with_objects = false,
collisionbox = { 0, 0, 0, 0, 0, 0 },
-- collisionbox = { -0.15, -0.05, -0.15, 0.15, 0.05, 0.15 },
selectionbox = { 0, 0, 0, 0, 0, 0 },
-- selectionbox = { -0.15, -0.05, -0.15, 0.15, 0.05, 0.15 },
pointable = false,
makes_footstep_sound = false,
static_save = true,
shaded = true,
glow = 4
},
on_activate = function(self, staticdata, dtime_s)
if not self or not staticdata or staticdata == '' then
self.object:remove()
return
end
local _staticdata = core.deserialize(staticdata)
for key, value in pairs(_staticdata) do
self[key] = value
end
self._nodechecktimer = 2
self.object:set_armor_groups({ immortal = 1, stove_food = 1, fleshy = 100 })
self.object:set_properties({
wield_item = _staticdata.itemname,
infotext = _staticdata.itemname,
})
end,
on_step = function(self, dtime, moveresult)
self._nodechecktimer = self._nodechecktimer - dtime
if self._nodechecktimer <= 0 then
self._nodechecktimer = 2
local pos = self.object:get_pos()
local node_above = core.get_node(vector.new(pos.x, pos.y + 0.5, pos.z))
local node_under = core.get_node(vector.new(pos.x, pos.y - 0.5, pos.z))
-- drop items if above is obstructed or no heat_source below
if node_above.name ~= 'air'
or core.get_item_group(node_under.name, 'heat_source') < 1
then
local meta = core.get_meta(vector.new(pos.x, pos.y - 0.5, pos.z))
local grid_matrix = core.deserialize(meta:get_string('grid_matrix'))
if not grid_matrix then
return
end
local grid_items = get_grid_matrix_items(grid_matrix)
-- remove item from stove meta
for i, value in ipairs(grid_items) do
remove_item_from_grid_matrix(grid_matrix, value)
end
meta:set_string('grid_matrix', core.serialize(grid_matrix))
-- remove entity and drop item
core.add_item(pos, ItemStack({ name = self.itemname }))
self.object:remove()
return
end
end
end,
get_staticdata = function(self)
local staticdata = {
itemname = self.itemname
}
return core.serialize(staticdata)
end,
})
-- Nodes
core.register_node('x_farming:stove', {
description = S('Stove (inactive)'),
tiles = {
'x_farming_stove_top.png',
'x_farming_stove_side.png',
'x_farming_stove_side.png',
'x_farming_stove_side.png',
'x_farming_stove_side.png',
'x_farming_stove_front.png',
},
paramtype2 = '4dir',
is_ground_content = false,
groups = {
-- MTG
cracky = 2,
-- MCL
pickaxey = 1,
container = 4,
deco_block = 1,
material_stone = 1
},
_mcl_blast_resistance = 3.5,
_mcl_hardness = 3.5,
sounds = x_farming.node_sound_stone_defaults(),
on_construct = function(pos)
local meta = core.get_meta(pos)
local infotext = S('Stove inactive.') .. ' ' .. S('Activate by torch or flint and steel.')
local node = core.get_node(pos)
local x_shift = -0.6
local m_pos = vector.new(pos.x - 0.6, pos.y + 0.5, pos.z)
-- param2 = 0 or 2
local initial_grid_matrix = {
[1] = {
[vector.to_string(vector.new(m_pos.x + (1 * 0.3), m_pos.y, m_pos.z + 0.2))] = {
itemstring = '',
output = {},
cooked_time = 0
},
[vector.to_string(vector.new(m_pos.x + (2 * 0.3), m_pos.y, m_pos.z + 0.2))] = {
itemstring = '',
output = {},
cooked_time = 0
},
[vector.to_string(vector.new(m_pos.x + (3 * 0.3), m_pos.y, m_pos.z + 0.2))] = {
itemstring = '',
output = {},
cooked_time = 0
}
},
[2] = {
[vector.to_string(vector.new(m_pos.x + (1 * 0.3), m_pos.y, m_pos.z - 0.2))] = {
itemstring = '',
output = {},
cooked_time = 0
},
[vector.to_string(vector.new(m_pos.x + (2 * 0.3), m_pos.y, m_pos.z - 0.2))] = {
itemstring = '',
output = {},
cooked_time = 0
},
[vector.to_string(vector.new(m_pos.x + (3 * 0.3), m_pos.y, m_pos.z - 0.2))] = {
itemstring = '',
output = {},
cooked_time = 0
}
},
}
if node.param2 == 1 or node.param2 == 3 then
m_pos = vector.new(pos.x, pos.y + 0.5, pos.z + x_shift)
initial_grid_matrix = {
[1] = {
[vector.to_string(vector.new(m_pos.x + 0.2, m_pos.y, m_pos.z + (1 * 0.3)))] = {
itemstring = '',
output = {},
cooked_time = 0
},
[vector.to_string(vector.new(m_pos.x + 0.2, m_pos.y, m_pos.z + (2 * 0.3)))] = {
itemstring = '',
output = {},
cooked_time = 0
},
[vector.to_string(vector.new(m_pos.x + 0.2, m_pos.y, m_pos.z + (3 * 0.3)))] = {
itemstring = '',
output = {},
cooked_time = 0
}
},
[2] = {
[vector.to_string(vector.new(m_pos.x - 0.2, m_pos.y, m_pos.z + (1 * 0.3)))] = {
itemstring = '',
output = {},
cooked_time = 0
},
[vector.to_string(vector.new(m_pos.x - 0.2, m_pos.y, m_pos.z + (2 * 0.3)))] = {
itemstring = '',
output = {},
cooked_time = 0
},
[vector.to_string(vector.new(m_pos.x - 0.2, m_pos.y, m_pos.z + (3 * 0.3)))] = {
itemstring = '',
output = {},
cooked_time = 0
}
},
}
end
meta:set_string('grid_matrix', core.serialize(initial_grid_matrix))
meta:set_string('infotext', infotext)
end,
on_rightclick = function(pos, node, player, itemstack, pointed_thing)
local meta = core.get_meta(pos)
local stack = player:get_wielded_item()
local stack_name = stack:get_name()
if core.get_item_group(stack_name, 'torch') > 0
or stack_name == 'fire:flint_and_steel'
or stack_name == 'mcl_fire:flint_and_steel'
then
local infotext = S('Stove active.') .. ' ' .. S('De-activate with shovel. Stove will de-activate by its self after a while if there are no items to cook.')
meta:set_string('infotext', infotext)
core.swap_node(pos, { name = 'x_farming:stove_active', param2 = node.param2 })
core.get_node_timer(pos):start(1)
end
return itemstack
end,
on_rotate = function()
return false
end
})
-- Active stove
core.register_node('x_farming:stove_active', {
description = S('Stove active'),
tiles = {
{
name = 'x_farming_stove_top_animated.png',
animation = {
type = 'vertical_frames',
aspect_w = 16,
aspect_h = 16,
length = 5
},
},
'x_farming_stove_side.png',
'x_farming_stove_side.png',
'x_farming_stove_side.png',
'x_farming_stove_side.png',
{
name = 'x_farming_stove_front_animated.png',
animation = {
type = 'vertical_frames',
aspect_w = 16,
aspect_h = 16,
length = 2
},
},
},
paramtype2 = '4dir',
is_ground_content = false,
groups = {
-- MTG
cracky = 2,
heat_source = 1,
not_in_creative_inventory = 1,
-- MCL
pickaxey = 1,
container = 4,
deco_block = 1,
material_stone = 1
},
_mcl_blast_resistance = 3.5,
_mcl_hardness = 3.5,
sounds = x_farming.node_sound_stone_defaults(),
light_source = 8,
drop = 'x_farming:stove',
on_construct = function(pos)
local meta = core.get_meta(pos)
local infotext = S('Stove active.') .. ' ' .. S('De-activate with shovel. Stove will de-activate by its self after a while if there are no items to cook.')
meta:set_string('infotext', infotext)
end,
on_timer = function(pos, elapsed)
local meta = core.get_meta(pos)
local grid_matrix = core.deserialize(meta:get_string('grid_matrix'))
if not grid_matrix then
return
end
local timer_elapsed = meta:get_int('timer_elapsed') or 0
meta:set_int('timer_elapsed', timer_elapsed + 1)
-- total running time without items to cook
local total_running_time = meta:get_float('total_running_time') or 0
local grid_items = get_grid_matrix_items(grid_matrix)
if #grid_items == 0 then
total_running_time = total_running_time + elapsed
elseif total_running_time > 0 then
-- reset time when added another item to cook
total_running_time = 0
end
if total_running_time > 180 then
-- extinguish stove
total_running_time = 0
meta:set_float('total_running_time', total_running_time)
local node = core.get_node(pos)
core.swap_node(pos, { name = 'x_farming:stove', param2 = node.param2 })
meta:set_int('timer_elapsed', 0)
local infotext = S('Stove inactive.') .. ' ' .. S('Activate by torch or flint and steel.')
meta:set_string('infotext', infotext)
stop_stove_sound(pos)
return false
end
-- play sound
if timer_elapsed == 0 or (timer_elapsed + 1) % 5 == 0 then
local sound_id = core.sound_play('x_farming_stove_active', { pos = pos, max_hear_distance = 16, gain = 0.25 })
local hash = core.hash_node_position(pos)
stove_fire_sounds[hash] = stove_fire_sounds[hash] or {}
table.insert(stove_fire_sounds[hash], sound_id)
-- Only remember the 3 last sound handles
if #stove_fire_sounds[hash] > 3 then
table.remove(stove_fire_sounds[hash], 1)
end
-- Remove the sound ID automatically from table after 11 seconds
core.after(11, function()
if not stove_fire_sounds[hash] then
return
end
for f = #stove_fire_sounds[hash], 1, -1 do
if stove_fire_sounds[hash][f] == sound_id then
table.remove(stove_fire_sounds[hash], f)
end
end
if #stove_fire_sounds[hash] == 0 then
stove_fire_sounds[hash] = nil
end
end)
end
-- cook items, update cooked times
-- if cooked drop items and remove from meta
for row, items in ipairs(grid_matrix) do
for item_pos, item in pairs(items) do
if item.itemstring ~= '' then
grid_matrix[row][item_pos].cooked_time = (grid_matrix[row][item_pos].cooked_time or 0) + elapsed
-- drop cooked item and remove from meta
if grid_matrix[row][item_pos].cooked_time > item.output.time then
-- drop cooked item
core.add_item(vector.from_string(item_pos), ItemStack(item.output.item))
-- drop recipe replacements
for _, replacement in ipairs(item.output.replacements) do
core.add_item(vector.from_string(item_pos), ItemStack(replacement))
end
-- remove from metadata
local removed_items_result = remove_item_from_grid_matrix(grid_matrix, item)
if removed_items_result.removed_from_pos then
-- remove entity
for _, o in ipairs(core.get_objects_inside_radius(pos, 0.7)) do
local armor_groups = o:get_armor_groups() or {}
if armor_groups.stove_food and armor_groups.stove_food > 0 then
if o:get_pos() and vector.to_string(vector.new(o:get_pos())) == removed_items_result.removed_from_pos then
o:remove()
break
end
end
end
end
-- Play cooling sound
core.sound_play('x_farming_stove_sizzle', {
pos = vector.from_string(item_pos),
max_hear_distance = 16,
gain = 0.07
}, true)
else
add_item_smoke_particles(vector.from_string(item_pos))
end
-- restore entity items after `clearobjects`
local missing_items = table.copy(items)
for _, obj in ipairs(core.get_objects_inside_radius(pos, 0.7)) do
local armor_groups = obj:get_armor_groups() or {}
if armor_groups.stove_food and armor_groups.stove_food > 0 then
local lua_ent = obj:get_luaentity()
if lua_ent and obj:get_pos() then
-- match entity with metadata
-- removing the one we found and leaving
-- only the ones what needs to be restored
for ent_pos, value in pairs(missing_items) do
local obj_pos = vector.to_string(vector.new(obj:get_pos()))
if obj_pos == ent_pos then
missing_items[obj_pos] = nil
break
end
end
end
end
end
for ent_pos, value in pairs(missing_items) do
if value.itemstring ~= '' then
local staticdata = {
itemname = value.itemstring
}
local obj = core.add_entity(
vector.from_string(ent_pos),
'x_farming:stove_food',
core.serialize(staticdata)
)
if obj then
obj:set_rotation(vector.from_string(value.ent_rot))
end
end
end
end
end
end
-- set meta
meta:set_string('grid_matrix', core.serialize(grid_matrix))
meta:set_float('total_running_time', total_running_time)
return true
end,
on_rightclick = function(pos, node, player, itemstack, pointed_thing)
local meta = core.get_meta(pos)
local wield_stack = player:get_wielded_item()
local wield_stack_name = wield_stack:get_name()
-- de-activate stove
if core.get_item_group(wield_stack_name, 'shovel') > 0 then
core.swap_node(pos, { name = 'x_farming:stove', param2 = node.param2 })
meta:set_int('timer_elapsed', 0)
local infotext = S('Stove inactive.') .. ' ' .. S('Activate by torch or flint and steel.')
meta:set_string('infotext', infotext)
stop_stove_sound(pos)
end
-- check if item is cook-able
local output = core.get_craft_result({
method = 'cooking',
width = 1,
items = { itemstack }
})
if output.item:is_empty() then
-- item is not cook-able
return itemstack
end
-- check if space above
if core.get_node(vector.new(pos.x, pos.y + 1, pos.z)).name ~= 'air' then
return itemstack
end
local grid_matrix = core.deserialize(meta:get_string('grid_matrix'))
local grid_items = get_grid_matrix_items(grid_matrix)
if #grid_items >= 6 then
-- stove is full
return itemstack
end
local _output = {
time = output.time,
replacements = {},
item = output.item:to_table()
}
for _, value in ipairs(output.replacements) do
table.insert(_output.replacements, value:to_table())
end
-- degrees to radians
local pitch = -90 * (math.pi / 180)
local roll = math.pi * 2 + node.param2 * math.pi / 2
-- x = pitch (elevation), y = yaw (heading), z = roll (bank)
local ent_rot = vector.new(pitch, 0, roll)
local added_item_result = add_item_to_grid_matrix(grid_matrix, {
itemstring = wield_stack_name,
output = _output,
ent_rot = vector.to_string(ent_rot),
cooked_time = 0
})
if not (added_item_result.added_to_row and added_item_result.added_to_pos) then
return itemstack
end
local ent_pos = vector.from_string(added_item_result.added_to_pos)
if not ent_pos then
return itemstack
end
-- Add Entity
local staticdata = {
itemname = wield_stack_name
}
local obj = core.add_entity(
ent_pos,
'x_farming:stove_food',
core.serialize(staticdata)
)
if obj then
-- 90 degress to radians
obj:set_rotation(ent_rot)
end
-- Play cooling sound
core.sound_play('x_farming_stove_sizzle_put', {
pos = ent_pos,
max_hear_distance = 16,
gain = 0.1
}, true)
meta:set_string('grid_matrix', core.serialize(grid_matrix))
itemstack:take_item()
return itemstack
end,
on_destruct = function(pos)
stop_stove_sound(pos)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
if not oldmetadata.fields.grid_matrix then
return
end
local objs = core.get_objects_inside_radius(pos, 0.7)
local grid_matrix = core.deserialize(oldmetadata.fields.grid_matrix)
local grid_items = get_grid_matrix_items(grid_matrix)
-- remove entitites
for _, obj in ipairs(objs) do
local armor_groups = obj:get_armor_groups() or {}
if armor_groups.stove_food and armor_groups.stove_food > 0 then
obj:remove()
end
end
-- drop items
for _, item in ipairs(grid_items) do
core.add_item(vector.new(pos.x, pos.y + 0.7, pos.z), ItemStack(item.itemstring))
end
end,
on_rotate = function()
return false
end
})