-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomposter.lua
More file actions
330 lines (295 loc) · 10.8 KB
/
composter.lua
File metadata and controls
330 lines (295 loc) · 10.8 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
local S = composting.translator;
local amount_limit = composting.settings.amount_limit;
local produced_per_clod = composting.settings.clod_cost;
local time_divider = composting.settings.composting_time_divider;
local base_production_time = ((365*24*3600)/amount_limit)/time_divider;
local function composter_update_timer(pos, C, N, amount, produced)
if (amount>0) then
local ratio = C/N;
local speed = 1/(((amount+produced)/amount_limit)^4);
if ratio>32.5 then
speed = speed*math.exp(ratio/500);
else
speed = speed*math.exp(ratio/25);
end
-- limit speed to keep some real times for composting proces
if speed>20 then
speed = 20;
end
-- check for dirt under, penalty if no dirt
local under = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z});
if (minetest.get_item_group(under.name, "soil")==0) then
speed = 2*speed;
end
local timer = minetest.get_node_timer(pos);
if timer:is_started() then
timer:set(base_production_time*speed, timer:get_elapsed());
else
timer:start(base_production_time*speed);
end
end
end
local function composter_update_node(pos, node)
local meta = minetest.get_meta(pos)
local amount = meta:get_int("amount");
local produced = meta:get_int("produced");
if (amount+produced)>0 then
local C = meta:get_int("C") or 0;
local N = meta:get_int("N") or 0;
local line = math.floor(3.75*math.log(C/N) - 5 + 0.5);
if (line<0) then
line = 0;
elseif (line>15) then
line = 15;
end
local done = 0;
if (produced>0) then
done = math.floor(15*produced/(produced+amount)+0.5);
end
node.name = "composting:composter_filled";
node.param2 = line*16+done;
minetest.swap_node(pos, node);
composter_update_timer(pos, C, N, amount, produced)
if (produced>=produced_per_clod) then
meta:set_string("infotext", S("Compost clod."));
elseif (amount==0) then
meta:set_string("infotext", S("More biomase needed."));
else
meta:set_string("infotext", S("Composting in progress."));
end
else
node.name = "composting:composter";
minetest.set_node(pos, node);
end
end
local function composter_on_punch(pos, node, puncher, pointed_thing)
if puncher then
local wield_item = puncher:get_wielded_item();
local item_name = wield_item:get_name();
local shovel = minetest.get_item_group(item_name, "shovel");
if (shovel==0) then
-- try to add biomase
local item_def = wield_item:get_definition();
if item_def and item_def._compost then
local meta = minetest.get_meta(pos);
local amount = meta:get_int("amount");
local produced = meta:get_int("produced");
amount = amount + item_def._compost.amount;
if ((amount+produced)<amount_limit) then
local C = meta:get_int("C");
local N = meta:get_int("N");
C = C + item_def._compost.C;
N = N + item_def._compost.N;
meta:set_int("amount", amount);
meta:set_int("C", C);
meta:set_int("N", N);
composter_update_node(pos, node, meta);
wield_item:take_item();
puncher:set_wielded_item(wield_item);
end
end
else
-- try to get compost clod
local meta = minetest.get_meta(pos);
local produced = meta:get_int("produced");
if (produced>=produced_per_clod) then
local amount = meta:get_int("amount");
local C = meta:get_int("C");
local N = meta:get_int("N");
local part = produced_per_clod/(amount+produced);
meta:set_int("C", math.floor(C*part+0.5));
meta:set_int("N", math.floor(N*part+0.5));
meta:set_int("produced", produced-produced_per_clod);
local inv = puncher:get_inventory();
local clod = ItemStack("composting:compost_clod");
if (inv:room_for_item("main", clod)) then
inv:add_item("main", clod);
else
minetest.add_item(pos, clod);
end
composter_update_node(pos, node, meta);
end
end
end
end
function composter_on_timer(pos, elapsed)
local node = minetest.get_node(pos);
local meta = minetest.get_meta(pos);
local amount = meta:get_int("amount");
local C = meta:get_int("C");
local N = meta:get_int("N");
local produced = meta:get_int("produced");
if (amount>0) then
amount = amount - 1;
produced = produced + 1;
meta:set_int("amount", amount);
meta:set_int("produced", produced);
end
if C==0 then
C = 1;
end
if N==0 then
N = 1;
end
local ratio = C/N;
local speed = 1/(((amount+produced)/amount_limit)^4);
composter_update_node(pos, node, meta);
return false; -- timer:start force timer to continue, return true will erase new timer setting
end
local short_desc = S("Composter");
local desc = short_desc;
local tt_help = S("Fill me by punching me with compostable item in hand.").."\n"..S("Use shovel to get compost clod.");
if (minetest.get_modpath("tt")==nil) then
desc = desc.."\n"..tt_help;
end
local node_sounds = nil
if minetest.get_modpath("default") then
node_sounds = default.node_sound_wood_defaults();
end
if minetest.get_modpath("hades_sounds") then
node_sounds = hades_sounds.node_sound_wood_defaults();
end
if minetest.get_modpath("sounds") then
node_sounds = sounds.node_wood();
end
-- node box {x=0, y=0, z=0}
local node_box = {
type = "fixed",
fixed = {
{-0.4375,-0.5,-0.5,-0.375,-0.4375,0.5},
{0.375,-0.5,-0.5,0.4375,-0.4375,0.5},
{-0.4375,-0.3125,-0.5,-0.375,-0.1875,0.5},
{0.375,-0.3125,-0.5,0.4375,-0.1875,0.5},
{-0.4375,-0.0625,-0.5,-0.375,0.0625,0.5},
{0.375,-0.0625,-0.5,0.4375,0.0625,0.5},
{-0.4375,0.1875,-0.5,-0.375,0.3125,0.5},
{0.375,0.1875,-0.5,0.4375,0.3125,0.5},
{-0.5,-0.4375,-0.4375,0.5,-0.3125,-0.375},
{-0.375,-0.3125,-0.4375,0.375,-0.25,-0.375},
{-0.5,-0.1875,-0.4375,0.5,-0.0625,-0.375},
{-0.375,-0.0625,-0.4375,0.375,0.0,-0.375},
{-0.5,0.0625,-0.4375,0.5,0.1875,-0.375},
{-0.375,0.1875,-0.4375,0.375,0.25,-0.375},
{-0.5,0.3125,-0.4375,0.5,0.4375,-0.375},
{-0.4375,0.4375,-0.4375,0.4375,0.5,-0.375},
{-0.4375,-0.4375,-0.375,-0.375,-0.375,0.4375},
{0.375,-0.4375,-0.375,0.4375,-0.375,0.4375},
{-0.4375,-0.1875,-0.375,-0.375,-0.125,0.4375},
{0.375,-0.1875,-0.375,0.4375,-0.125,0.4375},
{-0.4375,0.0625,-0.375,-0.375,0.125,0.4375},
{0.375,0.0625,-0.375,0.4375,0.125,0.4375},
{-0.4375,0.3125,-0.375,-0.375,0.375,0.4375},
{0.375,0.3125,-0.375,0.4375,0.375,0.4375},
{-0.5,-0.4375,0.375,-0.4375,-0.375,0.4375},
{-0.375,-0.4375,0.375,0.375,-0.25,0.4375},
{0.4375,-0.4375,0.375,0.5,-0.3125,0.4375},
{-0.4375,-0.375,0.375,-0.375,-0.3125,0.4375},
{0.375,-0.375,0.375,0.4375,-0.3125,0.4375},
{-0.5,-0.1875,0.375,-0.4375,-0.0625,0.4375},
{-0.375,-0.1875,0.375,0.375,0.0,0.4375},
{0.4375,-0.1875,0.375,0.5,-0.0625,0.4375},
{-0.4375,-0.125,0.375,-0.375,-0.0625,0.4375},
{0.375,-0.125,0.375,0.4375,-0.0625,0.4375},
{-0.5,0.0625,0.375,-0.4375,0.1875,0.4375},
{-0.375,0.0625,0.375,0.375,0.25,0.4375},
{0.4375,0.0625,0.375,0.5,0.1875,0.4375},
{-0.4375,0.125,0.375,-0.375,0.1875,0.4375},
{0.375,0.125,0.375,0.4375,0.1875,0.4375},
{-0.5,0.3125,0.375,-0.4375,0.4375,0.4375},
{-0.375,0.3125,0.375,0.375,0.5,0.4375},
{0.4375,0.3125,0.375,0.5,0.4375,0.4375},
{-0.4375,0.375,0.375,-0.375,0.5,0.4375},
{0.375,0.375,0.375,0.4375,0.5,0.4375},
},
}
minetest.register_node("composting:composter", {
short_description = short_desc,
description = desc,
_tt_help = tt_help,
paramtype = "light",
groups = {cracky = 2},
is_ground_content = false,
sounds = node_sounds,
drawtype = "mesh",
mesh = "composting_composter.obj",
tiles = {
"default_wood.png"
},
collision_box = node_box,
selection_box = node_box,
on_punch = composter_on_punch,
})
-- node box {x=0, y=0, z=0}
node_box_fill = {
type = "fixed",
fixed = {
{-0.4375,-0.5,-0.5,-0.375,-0.4375,0.5},
{0.375,-0.5,-0.5,0.4375,-0.4375,0.5},
{-0.4375,-0.3125,-0.5,-0.375,-0.1875,0.5},
{0.375,-0.3125,-0.5,0.4375,-0.1875,0.5},
{-0.4375,-0.0625,-0.5,-0.375,0.0625,0.5},
{0.375,-0.0625,-0.5,0.4375,0.0625,0.5},
{-0.4375,0.1875,-0.5,-0.375,0.3125,0.5},
{0.375,0.1875,-0.5,0.4375,0.3125,0.5},
{-0.5,-0.4375,-0.4375,0.5,-0.3125,-0.375},
{-0.375,-0.3125,-0.4375,0.375,-0.25,-0.375},
{-0.5,-0.1875,-0.4375,0.5,-0.0625,-0.375},
{-0.375,-0.0625,-0.4375,0.375,0.0,-0.375},
{-0.5,0.0625,-0.4375,0.5,0.1875,-0.375},
{-0.375,0.1875,-0.4375,0.375,0.25,-0.375},
{-0.5,0.3125,-0.4375,0.5,0.4375,-0.375},
{-0.4375,0.4375,-0.4375,0.4375,0.5,-0.375},
{-0.4375,-0.4375,-0.375,-0.375,-0.375,0.4375},
{0.375,-0.4375,-0.375,0.4375,-0.375,0.4375},
{-0.4375,-0.1875,-0.375,-0.375,-0.125,0.4375},
{0.375,-0.1875,-0.375,0.4375,-0.125,0.4375},
{-0.4375,0.0625,-0.375,-0.375,0.125,0.4375},
{0.375,0.0625,-0.375,0.4375,0.125,0.4375},
{-0.4375,0.3125,-0.375,-0.375,0.375,0.4375},
{0.375,0.3125,-0.375,0.4375,0.375,0.4375},
{-0.5,-0.4375,0.375,-0.4375,-0.375,0.4375},
{-0.375,-0.4375,0.375,0.375,-0.25,0.4375},
{0.4375,-0.4375,0.375,0.5,-0.3125,0.4375},
{-0.4375,-0.375,0.375,-0.375,-0.3125,0.4375},
{0.375,-0.375,0.375,0.4375,-0.3125,0.4375},
{-0.5,-0.1875,0.375,-0.4375,-0.0625,0.4375},
{-0.375,-0.1875,0.375,0.375,0.0,0.4375},
{0.4375,-0.1875,0.375,0.5,-0.0625,0.4375},
{-0.4375,-0.125,0.375,-0.375,-0.0625,0.4375},
{0.375,-0.125,0.375,0.4375,-0.0625,0.4375},
{-0.5,0.0625,0.375,-0.4375,0.1875,0.4375},
{-0.375,0.0625,0.375,0.375,0.25,0.4375},
{0.4375,0.0625,0.375,0.5,0.1875,0.4375},
{-0.4375,0.125,0.375,-0.375,0.1875,0.4375},
{0.375,0.125,0.375,0.4375,0.1875,0.4375},
{-0.5,0.3125,0.375,-0.4375,0.4375,0.4375},
{-0.375,0.3125,0.375,0.375,0.5,0.4375},
{0.4375,0.3125,0.375,0.5,0.4375,0.4375},
{-0.4375,0.375,0.375,-0.375,0.5,0.4375},
{0.375,0.375,0.375,0.4375,0.5,0.4375},
-- fill
{-0.375,-0.5,-0.375,0.375,0.4375,0.375},
},
}
minetest.register_node("composting:composter_filled", {
description = S("Filled Composter"),
paramtype = "light",
paramtype2 = "color",
groups = {cracky = 2, not_in_creative_inventory = 1},
legacy_facedir_simple = true,
is_ground_content = false,
sounds = node_sounds,
drawtype = "mesh",
mesh = "composting_composter_filled.obj",
use_texture_alpha = "blend",
tiles = {
{name="default_wood.png",color="white"},
"composting_composter_filled.png",
},
palette = "composting_composter_palette.png",
drop = "composting:composter",
collision_box = node_box_fill,
selection_box = node_box_fill,
on_punch = composter_on_punch,
on_timer = composter_on_timer,
})